Replies: 2 comments
-
Some relevant discussion in this past PR #6497 (comment) |
Beta Was this translation helpful? Give feedback.
0 replies
-
For future reference: once SQLA2.1 is released and we add it as dependency, we should review all related instances of:
(by related, I mean "in the context of functions returning |
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
-
I am opening this thread mainly for future reference, without changing the documentation for now.
Current approach
My current approach to this issue is:
TypedDict
hints to annotate the return type of these functions. Ultimately, in this context, this is what we mainly care about (we can assume that the types returned by the query are correct, since we perform validation when entering the data in the DB - e.g. we can assume that theHeartbeats.readable
data coming from the query is definitelystr | None
).Result
andRow
objects, as explained here: sqlalchemy/sqlalchemy@0007200#diff-d54af7d55637bc92aefa7c48b51e08b36fa6cd7ae0adc5461d06638e438d08cbR41-R44Motivation
In short, SQLAlchemy 2.0 cannot expose the types of individual columns in the Result object.
This is due to the fact that it doesn't support PEP-646:
SQLAlchemy 2.1 adds PEP-646 support:
However, SQLA 2.1 is not out yet.
There is a workaround in SQLA 2.0 by using the
Row._tuple()
method (this method is specifically created for this purpose - it returnsself
as runtime, so it's only used for type-checking: https://docs.sqlalchemy.org/en/20/core/connections.html#sqlalchemy.engine.Row._tuple).Example:
I am not in favour of using this workaround, as it might be prone to bugs in case the query changes (e.g. let's say we swap
readable
andthread_name
in the query - they're bothstr | None
, so the type checker would not notice any issues)Resources
There are some key SQLAlchemy discussions about this:
And a relevant SO question: https://stackoverflow.com/questions/63070515/get-class-hint-after-query-data-by-sqlalchemy
Beta Was this translation helpful? Give feedback.
All reactions