-
-
Notifications
You must be signed in to change notification settings - Fork 773
🏷️ Adjust type annotation for Field.sa_type to support instantiated SQLAlchemy types
#1345
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?
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.
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.
LGTM
Simple code example to check (in the 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
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.
LGTM
See #1345 (review)
|
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 |
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.Column