You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If we use a TypedDict like below as a type hint, type conversion is done for key x but not for y.
fromtypingimportTypedDictclassExample(TypedDict):
x: inty: 'int'
This is different compared to functions and methods where argument conversion is done both for x and y if we have def example(x: int, y: 'int'). The reason is that with TypedDict the annotation assigned to y by Python is ForwardRef, while with functions and methods we get the original string. Our conversion logic handles strings as aliases but doesn't know that to do with ForwardRefs.
A simple fix for this issue is using typing.get_type_hints for getting types instead of accessing __annotations__ like we currently do. That has a problem that using something like x: 'int | float' will cause an error with Python < 3.10. We just added support for such stringified types in arguments (#4711) and supporting them with TypedDict would be good as well. I guess we need to use get_type_hints first and then access __annotations__ as a fall-back. Then we may get ForwardRefs and need to handle them as well.
The text was updated successfully, but these errors were encountered:
This issue was initially reported by @aaltat on our Slack. He had used from __future__ import annotations instead of using stringified types, but that special import basically turns all annotations to strings and thus has the same effect.
If we use a
TypedDict
like below as a type hint, type conversion is done for keyx
but not fory
.This is different compared to functions and methods where argument conversion is done both for
x
andy
if we havedef example(x: int, y: 'int')
. The reason is that withTypedDict
the annotation assigned toy
by Python isForwardRef
, while with functions and methods we get the original string. Our conversion logic handles strings as aliases but doesn't know that to do withForwardRef
s.A simple fix for this issue is using
typing.get_type_hints
for getting types instead of accessing__annotations__
like we currently do. That has a problem that using something likex: 'int | float'
will cause an error with Python < 3.10. We just added support for such stringified types in arguments (#4711) and supporting them withTypedDict
would be good as well. I guess we need to useget_type_hints
first and then access__annotations__
as a fall-back. Then we may getForwardRef
s and need to handle them as well.The text was updated successfully, but these errors were encountered: