Skip to content

Backport of "_collect_type_vars" behaves wrong if Unpack is used on generic non-TypeVarTuple types #471

@Daraan

Description

@Daraan

Consider the following two cases, which only should yield a single type_var T.

# Python < 3.11
from typing_extensions import Unpack, TypeVarTuple, _collect_type_vars, Tuple, TypeVar, TypedDict, Generic
Ts = TypeVarTuple("Ts")
T= TypeVar("T")

class Movie(Generic[T], TypedDict):; 
    genre : T

print(_collect_type_vars([Unpack[Movie[T]]]))
# (typing_extensions.Unpack[typing_extensions.Movie[~T]], ~T)

print(_collect_type_vars([Unpack[Tuple[T]]]))
# (typing_extensions.Unpack[typing.Tuple[~T]], ~T)

The problem is that the second condition isinstance(Unpack_instance, TypeVar) is True, which appends the unpack variable itself which makes no sense.

typing_extensions monkey patches this function in the typing module

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions