Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 791
✨ 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
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Conversation
nsaccente commented Jul 28, 2025
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. |
TypeError for fields annotated with Literal This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
YuriiMotov commented Aug 5, 2025
@nsaccente, thanks for working on this! |
nsaccente commented Aug 6, 2025
Sure. I'll try to get that done sometime this week. |
…del into bugfix-57/literal-satype
2aaef2d to 3c024e2Comparensaccente commented Aug 16, 2025
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 commented Aug 26, 2025
@nsaccente: there were some tricky linting errors that I've now fixed. @YuriiMotov: do you have time to give this another review? |
YuriiMotov left a comment
There was a problem hiding this 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):?
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as resolved.
This comment was marked as resolved.
svlandeg commented Aug 27, 2025 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
@YuriiMotov: you're right - it's more clean overall. I'm happy to defer the |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
This comment was marked as spam.
TypeError for fields annotated with LiteralLiteral typestiangolo commented Sep 20, 2025
I'm seeing that this assumes all classHero(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 commented Oct 1, 2025 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
Things are getting complex.. We can easily add special cases for The question is, do we expect any kind of validation for these values? |
Need to decide about data validation first
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 becauseLiteralis not a class, but a typing._SpecialForm, so static funcs likeisinstanceandissubclassdon't work with it.The fix was pretty straightforward, I just added a check before an
isinstanceorissubclasswould have been called that checksif type_ is Literal.