Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

✨ Add support for Literal types #1439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
nsaccente wants to merge 24 commits into fastapi:main
base: main
Choose a base branch
Loading
from nsaccente:bugfix-57/literal-satype

Conversation

@nsaccente
Copy link

@nsaccente nsaccente commented Jul 24, 2025

This PR addresses issue 57. The original issue explains that attempting to use a Literal type annotation in a SQLModel raises a type error from issubclass. This is because Literal is not a class, but a typing._SpecialForm, so static funcs like isinstance and issubclass don't work with it.

The fix was pretty straightforward, I just added a check before an isinstance or issubclass would have been called that checks if type_ is Literal.

svlandeg, iloveitaly, and patrick91 reacted with rocket emoji
@nsaccente nsaccente changed the title (削除) Add check for Literal type annotation in get_sqlalchemy_type (削除ここまで) (追記) 🐛Add check for Literal type annotation in get_sqlalchemy_type (追記ここまで) Jul 24, 2025
Copy link
Author

I have no idea how to add the proper labels to this PR, it looks like the permissions aren't set up to allow me to.

@YuriiMotov YuriiMotov changed the title (削除) 🐛Add check for Literal type annotation in get_sqlalchemy_type (削除ここまで) (追記) 🐛 Fix TypeError for fields annotated with Literal (追記ここまで) Aug 5, 2025
@YuriiMotov YuriiMotov added the bug Something isn't working label Aug 5, 2025
YuriiMotov

This comment was marked as outdated.

Copy link
Member

@nsaccente, thanks for working on this!
This works, but could you please add a test?

Copy link
Author

@nsaccente, thanks for working on this! This works, but could you please add a test?

Sure. I'll try to get that done sometime this week.

YuriiMotov reacted with thumbs up emoji

@nsaccente nsaccente force-pushed the bugfix-57/literal-satype branch from 2aaef2d to 3c024e2 Compare August 16, 2025 01:32
Copy link
Author

I've been fighting with getting the tests to work. I've been able to get things to pass in some versions of python, but not others. Any guidance on how to run these CI tests locally?

@svlandeg svlandeg marked this pull request as draft August 22, 2025 12:12
@svlandeg svlandeg self-assigned this Aug 22, 2025
@svlandeg svlandeg marked this pull request as ready for review August 26, 2025 14:18
Copy link
Member

@nsaccente: there were some tricky linting errors that I've now fixed.

@YuriiMotov: do you have time to give this another review?

@svlandeg svlandeg removed their assignment Aug 26, 2025
Copy link
Member

@YuriiMotov YuriiMotov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me that we can simplify things by just importing Literal from typing_extensions directly. Why do we need that trick with if sys.version_info >= (3, 9):?

This comment was marked as resolved.

Copy link
Member

svlandeg commented Aug 27, 2025
edited
Loading

@YuriiMotov: you're right - it's more clean overall. I'm happy to defer the Literal import to either another PR or to just wait for 3.8 deprecation, at which point we shouldn't forget to clean this up. Let me clean up this PR, addressing your comments.

@svlandeg svlandeg marked this pull request as draft August 27, 2025 10:14
@svlandeg svlandeg self-assigned this Aug 27, 2025
@svlandeg svlandeg marked this pull request as ready for review August 27, 2025 10:27
@svlandeg svlandeg mentioned this pull request Aug 27, 2025
@svlandeg svlandeg removed their assignment Aug 27, 2025
YuriiMotov

This comment was marked as outdated.

This comment was marked as spam.

@tiangolo tiangolo changed the title (削除) 🐛 Fix TypeError for fields annotated with Literal (削除ここまで) (追記) ✨ Add support for Literal types (追記ここまで) Sep 20, 2025
@tiangolo tiangolo added feature New feature or request and removed bug Something isn't working labels Sep 20, 2025
Copy link
Member

I'm seeing that this assumes all Literal values are strings, so a model like this:

class Hero(SQLModel, table=True):
 id: Optional[int] = Field(default=None, primary_key=True)
 name: str = Field(unique=True)
 weakness: Literal[1, 2, 3]

...would still create the column in the DB as a string, not as an integer. 🤔

@YuriiMotov YuriiMotov self-assigned this Sep 30, 2025
Copy link
Member

YuriiMotov commented Oct 1, 2025
edited
Loading

Things are getting complex.. We can easily add special cases for Literal[1, 2, 3] or Literal[True, False] to create column with proper type.

The question is, do we expect any kind of validation for these values?
Our database column type is just CHAR or INTEGER, so, database will not enforce the value to be correct against Literal[1, 2, 3] type. If we have the value 42 in database, it can break user's code in a very tricky way one day

@YuriiMotov YuriiMotov dismissed their stale review October 1, 2025 15:16

Need to decide about data validation first

@YuriiMotov YuriiMotov removed their assignment Oct 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@svlandeg svlandeg svlandeg left review comments

@YuriiMotov YuriiMotov YuriiMotov left review comments

Assignees

No one assigned

Labels

feature New feature or request

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

[BUG] Variables with annotation of 'typing.Literal' causes a panic

AltStyle によって変換されたページ (->オリジナル) /