Skip to content

[typing: PEP 646]: *tuple[int, int] is improperly evaluated by get_type_hints #101015

Closed
@hjalmarlucius

Description

@hjalmarlucius

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

if isinstance(t, GenericAlias):
return GenericAlias(t.__origin__, ev_args)

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

from typing import ForwardRef from typing import TypeVarTuple Ts = TypeVarTuple("Ts") typ = ForwardRef("*Ts")._evaluate({}, {"Ts": Ts}, frozenset()) assert typ == next(iter(Ts)) # <-- PASSES AS EXPECTED typ = ForwardRef("*tuple[int]")._evaluate({}, {}, frozenset()) assert typ == tuple[int] # <-- PASSES BUT SHOULDN'T assert typ == next(iter(tuple[int])) # <-- SHOULD PASS BUT DOESN'T

Your environment

Python 3.11

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic-typingtriagedThe issue has been accepted as valid by a triager.type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions