Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.3k
Open
Description
Bug report
Bug description:
Problem
inspect.get_annotations(eval_str=True) raises aSyntaxError when evaluating unpacked tuple annotations (*tuple[...]):
from __future__importannotationsfrominspectimportget_annotationsdeff(*args:*tuple[int, ...]): ...print(get_annotations(f,eval_str=True))# Raises SyntaxError
The error occurs because*tuple[int, ...] is not a valid standalone expression foreval().
Expected
According totyping spec,*tuple[...] is valid in this context.
typing.get_type_hints already normalizes such annotations:
from __future__importannotationsfromtypingimportget_type_hintsdeff(*args:*tuple[int, ...]): ...print(get_type_hints(f))# {'args': typing.Unpack[tuple[int, ...]]}
inspect.get_annotations should behave consistently and not raiseSyntaxError.
Possible Resolution
Makeinspect.get_annotations(eval_str=True) aware of this special case and transform annotation strings of the form*tuple[...] intotyping.Unpack[tuple[...]] before evaluation, similar to whattyping.get_type_hints does.
CPython versions tested on:
3.13
Operating systems tested on:
Linux