Skip to content

Commit

Permalink
typing.TypeAliasType compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Daraan committed Oct 25, 2024
1 parent 139ac68 commit 217e93c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/test_typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7439,6 +7439,15 @@ def test_type_var_tuple_compatibility(self):
T6 = TypeAliasType("TypingTypeVarTuple", ..., type_params=(typingTs,))
self.assertEqual(T6.__type_params__, (typingTs,))

@skipUnless(TYPING_3_12_0, "typing.TypeAliasType is not available before 3.12")
def test_isinstance_compatibility(self):
typing_TA = typing.TypeAliasType("typing_TA", int)
ns = {}
exec("type Int = int", None, ns)
self.assertTrue(isinstance(typing_TA, TypeAliasType))
self.assertTrue(isinstance(ns["Int"], TypeAliasType))


def test_type_params_possibilities(self):
T = TypeVar('T')
# Test not a tuple
Expand Down
11 changes: 10 additions & 1 deletion src/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3572,7 +3572,16 @@ def __getitem__(self, item):
result.__class__ = type(self)
return result

class TypeAliasType:
class _TypeAliasTypeMeta(type):
# 3.12+
if hasattr(typing, "TypeAliasType"):
def __instancecheck__(cls, instance):
return (
cls.__class__ == TypeAliasType
or isinstance(instance, typing.TypeAliasType)
)

class TypeAliasType(metaclass=_TypeAliasTypeMeta):
"""Create named, parameterized type aliases.
This provides a backport of the new `type` statement in Python 3.12:
Expand Down

0 comments on commit 217e93c

Please sign in to comment.