-
-
Notifications
You must be signed in to change notification settings - Fork 767
🏷️ 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_type
andsa_column_kwargs
instead of justsa_column
can benefit when using inheritance for classes derived fromSQLModel
as it was suggested here.If
sa_column
is not specified andsa_type
is provided, it will be passed as a second, type argumnet tosqlalchemy.Column
instance. If you would checksqlalchemy.Column
construction definition, it looks as following:Note the
__type_pos
argument 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
,JSON
etc, but also an instance of this type.I was trying for
JSONB(none_as_null=True)
andString(50)
and it worked just fine,alembic
migrations were generated correctly, onlymypy
was arguing for type mismatch withcall-overload
issue.To fix
mypy
error, we can update type annotation forsqlmodel.main.Field.sa_type
to support also an instantiated SQLAlchemy's sqltype to match those ofsqlalchemy.Column