joinedload() fails static type checking (Pyright/Pylance) when using SQLModel relationships
#1853
Unanswered
Raymondo97
asked this question in
Questions
Replies: 0 comments
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 SQLAlchemy's eager loading options like
joinedload(), passing a SQLModel relationship directly (e.g.,joinedload(Model.relationship)) results in static type checking errors in Pyright/Pylance.The static analyzer reads the relationship as the associated Model type (or
Model | None), butjoinedload()expects aQueryableAttribute. While this works perfectly at runtime due to SQLModel's metaclass swapping the attributes, the developer experience suffers due to the static analyzer throwing errors.Current Workaround:
Currently, the only way to satisfy the static type checker without using
# pyright: ignoreis to cast the relationship usingtyping.cast:Expected Behavior:
Ideally,
SQLModelcould provide a built-in typing utility (similar to thecol()function used for.where()clauses, but compatible with loader options) or type theRelationshipin a way that static analyzers recognize it as a valid argument for SQLAlchemy loader options.Operating System
Linux, Windows, macOS
Operating System Details
No response
SQLModel Version
0.0.38
Python Version
3.14
Additional Context
Pyright Version: 1.1.408
FastAPI Version: 0.135.1
Pydantic Version: 2.12.5
SQLAlchemy Version: 2.0.48
Beta Was this translation helpful? Give feedback.
All reactions