-
-
Notifications
You must be signed in to change notification settings - Fork 32k
[typing: PEP 646]: *tuple[int, int]
is improperly evaluated by get_type_hints
#101015
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
It happens because of this line: Lines 827 to 831 in ef633e5
Moreover, it is not ever tested 😒 Moreover, it has different semantic for Lines 1056 to 1057 in ef633e5
I've sent a proposal with the fix, but I am not 100% happy with it. Please, share your feedback! |
Can somebody provide an example of the bug that doesn't use private APIs such as |
See my PR: def test_get_type_hints_on_unpack_args(self):
Ts = TypeVarTuple('Ts')
def func2(*args: '*tuple[int, str]'): pass
self.assertEqual(gth(func2), {'args': Unpack[tuple[int, str]]}) It was (.venv) ~/Desktop/cpython main ✔
» ./python.exe
Python 3.12.0a4+ (heads/issue-100942-dirty:a2babb4f58, Jan 12 2023, 01:08:31) [Clang 11.0.0 (clang-1100.0.33.16)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from typing import *
>>> Ts = TypeVarTuple('Ts')
>>> def func2(*args: '*tuple[int, str]'): pass
...
>>> get_type_hints(func2)
{'args': tuple[int, str]} |
typing._eval_type
removes unpack information of types.GenericAlias
*tuple[int, int]
is improperly evaluated by get_type_hints
*tuple[int, int]
is improperly evaluated by get_type_hints
*tuple[int, int]
is improperly evaluated by get_type_hints
The bug is not unique to forward references. Here, obtaining the annotations directly via >>> import typing
>>> def foo(*args: *tuple[int, int]): ...
>>> foo.__annotations__
{'args': *tuple[int, int]}
>>> typing.get_type_hints(foo)
{'args': tuple[int, int]} |
Yes, that's a good catch. My PR does not solve the second problem. Fixing it! |
Now it does. With even more hacks 😒 |
I don't have a setup to test changes so going by intuition to simplify the change: If you try only the fix I proposed in |
@hjalmarlucius I've managed to remove new I prefer to keep my code with |
…PEP 646) (pythonGH-101031) (cherry picked from commit 807d6b5) Co-authored-by: Nikita Sobolev <[email protected]>
…6) (GH-101031) (cherry picked from commit 807d6b5) Co-authored-by: Nikita Sobolev <[email protected]>
Thanks @hjalmarlucius for the report, and @sobolevn for the fix! |
Uh oh!
There was an error while loading. Please reload this page.
Bug report
Unpack information of
typing.GenericAlias
is not transferred from string annotations to interpreted annotations.typing.Unpacked
works as expected.A fix could be to add
if getattr(t, "__unpacked__", False): return next(iter(GenericAlias(t.__origin__, ev_args)))
clause here:cpython/Lib/typing.py
Lines 373 to 374 in 6492492
NOTE: This bug is in typing's internal API and I don't think there's issues in the public API (but haven't looked hard). However, this issue pops up quickly if you try to do anything wrt PEP 646 typing at runtime.
Minimal example
Your environment
Python 3.11
Linked PRs
typing.get_type_hints
with unpacked*tuple
(PEP 646) #101031typing.get_type_hints
with unpacked*tuple
(PEP 646) (GH-101031) #101254The text was updated successfully, but these errors were encountered: