diff --git a/src/test_typing_extensions.py b/src/test_typing_extensions.py index ec629b40..dd28f7a0 100644 --- a/src/test_typing_extensions.py +++ b/src/test_typing_extensions.py @@ -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 diff --git a/src/typing_extensions.py b/src/typing_extensions.py index f9f93d7c..f7313dff 100644 --- a/src/typing_extensions.py +++ b/src/typing_extensions.py @@ -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: