Skip to content

GenericAlias from TypeAliasType subscription lacks dunder attributes #469

@Daraan

Description

@Daraan

Depending on the python version some attributes for subcripted TypeAliasType are not present after the __getitem__ call.

__name__ is present in Python 3.11+; "__name__", "__type_params__", "__value__" are missing in earlier versions.

from typing_extensions import TypeAliasType, List, TypeVar

T = TypeVar("T")
X = TypeAliasType("X", List[T], type_params=(T,)

XT = X[T]

for attr in ("__name__", "__type_params__", "__value__", "__module__"):
    print("XT has", attr, hasattr(XT, attr)

Tests to pass:

The created aliases should pass the following tests:

    def test_alias_attributes(self):
        T = TypeVar('T')
        T2 = TypeVar('T2')
        ListOrSetT = TypeAliasType("ListOrSetT", Union[List[T], Set[T]], type_params=(T,))
        subscripted = ListOrSetT[int]
        still_generic = ListOrSetT[Iterable[T2]]
        fully_subscripted = still_generic[float]
        
        self.assertEqual(subscripted.__name__, "ListOrSetT")
        self.assertEqual(subscripted.__value__, Union[List[T], Set[T]])
        self.assertEqual(subscripted.__type_params__, (T,))
        self.assertEqual(subscripted.__module__, ListOrSetT.__module__)
        self.assertEqual(still_generic.__name__, "ListOrSetT")
        self.assertEqual(still_generic.__value__, Union[List[T], Set[T]])
        self.assertEqual(still_generic.__type_params__, (T,))
        self.assertEqual(still_generic.__module__, ListOrSetT.__module__)
        self.assertEqual(fully_subscripted.__name__, "ListOrSetT")
        self.assertEqual(fully_subscripted.__value__, Union[List[T], Set[T]])
        self.assertEqual(fully_subscripted.__type_params__, (T,))
        self.assertEqual(fully_subscripted.__module__, ListOrSetT.__module__)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions