Bug report
Bug description:
Problem
inspect.get_annotations(eval_str=True) raises a SyntaxError when evaluating unpacked tuple annotations (*tuple[...]):
from __future__ import annotations
from inspect import get_annotations
def f(*args: *tuple[int, ...]): ...
print(get_annotations(f, eval_str=True)) # Raises SyntaxError
The error occurs because *tuple[int, ...] is not a valid standalone expression for eval().
Expected
According to typing spec, *tuple[...] is valid in this context.
typing.get_type_hints already normalizes such annotations:
from __future__ import annotations
from typing import get_type_hints
def f(*args: *tuple[int, ...]): ...
print(get_type_hints(f)) # {'args': typing.Unpack[tuple[int, ...]]}
inspect.get_annotations should behave consistently and not raise SyntaxError.
Possible Resolution
Make inspect.get_annotations(eval_str=True) aware of this special case and transform annotation strings of the form *tuple[...] into typing.Unpack[tuple[...]] before evaluation, similar to what typing.get_type_hints does.
CPython versions tested on:
3.13
Operating systems tested on:
Linux
Linked PRs
Bug report
Bug description:
Problem
inspect.get_annotations(eval_str=True)raises aSyntaxErrorwhen evaluating unpacked tuple annotations (*tuple[...]):The error occurs because
*tuple[int, ...]is not a valid standalone expression foreval().Expected
According to typing spec,
*tuple[...]is valid in this context.typing.get_type_hintsalready normalizes such annotations:inspect.get_annotationsshould behave consistently and not raiseSyntaxError.Possible Resolution
Make
inspect.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_hintsdoes.CPython versions tested on:
3.13
Operating systems tested on:
Linux
Linked PRs