@@ -1605,7 +1605,10 @@ def func1(*args: *Ts): pass
16051605 self .assertEqual (gth (func1 ), {'args' : Unpack [Ts ]})
16061606
16071607 def func2 (* args : * tuple [int , str ]): pass
1608- self .assertEqual (gth (func2 ), {'args' : Unpack [tuple [int , str ]]})
1608+ hint = gth (func2 )['args' ]
1609+ self .assertIsInstance (hint , types .GenericAlias )
1610+ self .assertEqual (hint .__args__ [0 ], int )
1611+ self .assertIs (hint .__unpacked__ , True )
16091612
16101613 class CustomVariadic (Generic [* Ts ]): pass
16111614
@@ -1620,7 +1623,10 @@ def func1(*args: '*Ts'): pass
16201623 {'args' : Unpack [Ts ]})
16211624
16221625 def func2 (* args : '*tuple[int, str]' ): pass
1623- self .assertEqual (gth (func2 ), {'args' : Unpack [tuple [int , str ]]})
1626+ hint = gth (func2 )['args' ]
1627+ self .assertIsInstance (hint , types .GenericAlias )
1628+ self .assertEqual (hint .__args__ [0 ], int )
1629+ self .assertIs (hint .__unpacked__ , True )
16241630
16251631 class CustomVariadic (Generic [* Ts ]): pass
16261632
@@ -7114,6 +7120,24 @@ def add_right(self, node: 'Node[T]' = None):
71147120 right_hints = get_type_hints (t .add_right , globals (), locals ())
71157121 self .assertEqual (right_hints ['node' ], Node [T ])
71167122
7123+ def test_get_type_hints_preserve_generic_alias_subclasses (self ):
7124+ # https://github.com/python/cpython/issues/130870
7125+ # A real world example of this is `collections.abc.Callable`. When parameterized,
7126+ # the result is a subclass of `types.GenericAlias`.
7127+ class MyAlias (types .GenericAlias ):
7128+ pass
7129+
7130+ class MyClass :
7131+ def __class_getitem__ (cls , args ):
7132+ return MyAlias (cls , args )
7133+
7134+ # Using a forward reference is important, otherwise it works as expected.
7135+ # `y` tests that the `GenericAlias` subclass is preserved when stripping `Annotated`.
7136+ def func (x : MyClass ['int' ], y : MyClass [Annotated [int , ...]]): ...
7137+
7138+ assert isinstance (get_type_hints (func )['x' ], MyAlias )
7139+ assert isinstance (get_type_hints (func )['y' ], MyAlias )
7140+
71177141
71187142class GetUtilitiesTestCase (TestCase ):
71197143 def test_get_origin (self ):
0 commit comments