Typechecking Column Comparisons with select
#1713
-
First Check
Commit to Help
Example Code"""Minimal repro for ty invalid-argument-type with SQLModel column comparisons."""
from sqlmodel import Field, Integer, SQLModel, func
class Issue(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True)
status: int
# ty reports: Expected `SQLCoreOperations[Any] | ...`, found `bool`
expr = func.cast(Issue.status == 1, Integer)DescriptionWhen using a type checker (tested with Expected Behavior
Root CauseSQLModel 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.0SQLAlchemy 2.0 solves this with Mapped[T] annotations: This passes type checking because SQLAlchemy's stubs properly type Mapped descriptors. WorkaroundFor now, I can workaround with a Operating SystemmacOS Operating System DetailsNo response SQLModel Version0.0.31 Python Version3.13.7 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
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.
Ugh - I was searching in ways that was specific to
__eq__and didn't see this: #1419 (comment)Looks like this can be resolved by wrapping the column with
col.Would be helpful if this was documented