Typechecking Column Comparisons with select
#1713
Unanswered
travis-cook-sfdc
asked this question in
Questions
Replies: 1 comment
-
|
Ugh - I was searching in ways that was specific to Looks like this can be resolved by wrapping the column with Would be helpful if this was documented |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
When using a type checker (tested with
ty), column comparisons on SQLModel table models are inferred as bool instead of ColumnElement. This causes false positive type errors when passing comparisons to SQLAlchemy functions like func.cast().Expected Behavior
Issue.status == 1should be typed as ColumnElement[bool], not bool.Root Cause
SQLModel uses plain Python annotations (status: int) and replaces them with Column objects at runtime via the metaclass:
Type checkers see the annotation int and correctly infer int.eq() returns bool. They can't see the runtime transformation.
Comparison with SQLAlchemy 2.0
SQLAlchemy 2.0 solves this with Mapped[T] annotations:
This passes type checking because SQLAlchemy's stubs properly type Mapped descriptors.
Workaround
For now, I can workaround with a
type: ignore, but it would be cool if sqlmodel could type this correctlyOperating System
macOS
Operating System Details
No response
SQLModel Version
0.0.31
Python Version
3.13.7
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions