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: Additional changes are required before this can be merged.
Discussion: sqlalchemy#11093
Description
Summary
These are some efforts to get better typing of
mapped_column
. I believe the return type ofmapped_column
cannot be expressed correctly as it is now. It would be required to change the default value ofnullable
to get types correct.Context
Right now,
mapped_column
returnsMappedColumn[Any]
so there is no guarantee that the type annotation matches the SQLA type engine passed to mapped_column (if any).sqlalchemy#11093
When I first started the discussion I was hopeful that this could be fully solved with overloads. But now I think it is just not possible to type
mapped_column
correctly as long as the default value fornullable
isTrue
. Because thenthere is no way to type something like
mapped_column(Integer)
correctly.mapped_column(Integer)
can either be of typeMappedColumn[int]
orMappedColumn[Optional[int]]
, and the default should be the more specific type of those two, namelyMappedColumn[int]
since both of the following assignments should be valid:In this PR:
mapped_column
toMappedColumn
type matching the type engine, thenullable
and thedefault
parameters.TypeEngine
covariant. This was necessary for assignments such asto be valid for the typechecker.