🏷️ Adjust type annotation for Field.sa_type to support instantiated SQLAlchemy types#1345
Open
diachkow wants to merge 10 commits intofastapi:mainfrom
Open
🏷️ Adjust type annotation for Field.sa_type to support instantiated SQLAlchemy types#1345diachkow wants to merge 10 commits intofastapi:mainfrom
Field.sa_type to support instantiated SQLAlchemy types#1345diachkow wants to merge 10 commits intofastapi:mainfrom
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
YuriiMotov
approved these changes
Aug 21, 2025
Member
YuriiMotov
left a comment
There was a problem hiding this comment.
LGTM
Simple code example to check (in the details)
Details
from datetime import datetime
from sqlmodel import DateTime, Field, SQLModel, create_engine
class A(SQLModel):
created_at: datetime = Field(sa_type=DateTime(timezone=False))
engine = create_engine("sqlite:///")
SQLModel.metadata.create_all(engine)Running mypy gives
error: No overload variant of "Field" matches argument type "DateTime" [call-overload]
on master and
Success: no issues found in 1 source file
after applying this fix
Field.sa_type to support instantiated SQLAlchemy types
Author
|
Thanks for approval! Who is responsible for merging this, what are the rules in this repo? |
Member
Only Sebastian can merge it. I already forwarded it to him. We should just wait |
This comment was marked as resolved.
This comment was marked as resolved.
Author
|
I have resolved conflicts here. I see you guys made some patch releases recently, hope this change can make it into one of them 🙃 |
This comment was marked as resolved.
This comment was marked as resolved.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note
When I was writing description for this PR, I found another discussion started for just the same issue I was experiencing with
mypy, so this changes are basically fixing the issue described hereUsing
sa_typeandsa_column_kwargsinstead of justsa_columncan benefit when using inheritance for classes derived fromSQLModelas it was suggested here.If
sa_columnis not specified andsa_typeis provided, it will be passed as a second, type argumnet tosqlalchemy.Columninstance. If you would checksqlalchemy.Columnconstruction definition, it looks as following:Note the
__type_posargument withUnion[_TypeEngineArgument[_T], SchemaEventTarget]whereSo, from technical perspective you can pass not only the subclass of
TypeEngine, e.g. SQLAlchemy's sqltype such asString,Integer,DateTime,JSONetc, but also an instance of this type.I was trying for
JSONB(none_as_null=True)andString(50)and it worked just fine,alembicmigrations were generated correctly, onlymypywas arguing for type mismatch withcall-overloadissue.To fix
mypyerror, we can update type annotation forsqlmodel.main.Field.sa_typeto support also an instantiated SQLAlchemy's sqltype to match those ofsqlalchemy.ColumnRelated discussions: