🏷️ Adjust type annotation for Field.sa_type to support instantiated SQLAlchemy types#1345
🏷️ Adjust type annotation for Field.sa_type to support instantiated SQLAlchemy types#1345diachkow wants to merge 14 commits into
Field.sa_type to support instantiated SQLAlchemy types#1345Conversation
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
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
|
Thanks for approval! Who is responsible for merging this, what are the rules in this repo? |
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.
|
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 comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
|
was hitting this error when using sa_type, thanks for keeping it updated |
|
It would be nice to get it merged though 😅 |
|
Hey @tiangolo, gently bumping this. It's been open about a year now, has an LGTM from @YuriiMotov, and @svlandeg mentioned back in September she'd forwarded it to you for merging. To recap the issue:
Worth noting this is really a bug fix, not a feature. It's currently labeled as feature, but the code already does the right thing, this PR just corrects the type signature to match. Might be worth relabeling! Totally understand if it's just buried under everything else, I didn't want it to get lost. Thanks for all the incredible work you do on FastAPI and SQLModel (and friends) |
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: