Skip to content

inspect.get_annotations(eval_str=True) raises SyntaxError for PEP-646 unpacked tuple annotation *tuple[...] #138891

Closed

Description

@artemyefimov

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

Activity

  1. brianschubert commented on Sep 14, 2025

    @brianschubert
    Contributor
  2. added 2 commits that reference this issue on Sep 15, 2025
  3. added a commit that references this issue on Oct 20, 2025
  4. added a commit that references this issue on Oct 20, 2025
  5. added a commit that references this issue on Oct 20, 2025
  6. added a commit that references this issue on Dec 6, 2025
  7. added 2 commits that reference this issue on Mar 26, 2026
  8. added a commit that references this issue on Mar 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-typingtype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions