Inferring query results #10273
Replies: 3 comments 20 replies
-
Hi,
That raises an error, since that model definition is not correct.
Assuming you have a correct model, you can use
There is currently a limitation in the typing side that prevents Row to behave like a tuple. Tbe situation should hopefully be fixed at some point |
Beta Was this translation helpful? Give feedback.
-
that's a non-starter (edit: you can be absolutely sure this is the first place I went when trying to solve this problem. adding
iterating a the solution here is that Python typing has to improve upon https://peps.python.org/pep-0646/ so that we can actually make |
Beta Was this translation helpful? Give feedback.
-
On second thought @zzzeek @CaselIT I think it would be better to make Row inherit from tuple and benefit from PEP646 TypeVarTuple. _TP = TypeVarTuple("_TP")
if TYPE_CHECKING:
class _Row(BaseRow, tuple[Unpack[_TP]]):
...
else:
class _Row(BaseRow, Generic[Unpack[_TP]]):
...
class Row(_Row[Unpack[_TP]]):
... # code goes here And we can now benefit from both PEP646 features as well as tuple unpacking magic, without having to ever call specific methods such as .t With the POC above, pyright now infers
What do you think ? |
Beta Was this translation helpful? Give feedback.
-
Hello,
Currently migrating a large code base which is largely type annotated to SQLAlchemy 2.0, we have a LOT of code such as this
model_name and model_names will be inferred as Any, which defeats the point of having type checking.
I understand that to benefit from better inference we'd have to rewrite that as
However, such usages are very hard to track down, and it's very hard to ensure across a large team that the first syntax is no longer used.
IMO, there are 2 options:
What do you think ?
Beta Was this translation helpful? Give feedback.
All reactions