22
22
from typing import NamedTuple , TypedDict
23
23
from typing import IO , TextIO , BinaryIO
24
24
from typing import Pattern , Match
25
- from typing import Annotated
25
+ from typing import Annotated , ForwardRef
26
26
import abc
27
27
import typing
28
28
import weakref
@@ -1756,11 +1756,17 @@ def test_extended_generic_rules_repr(self):
1756
1756
1757
1757
def test_generic_forward_ref (self ):
1758
1758
def foobar (x : List [List ['CC' ]]): ...
1759
+ def foobar2 (x : list [list [ForwardRef ('CC' )]]): ...
1759
1760
class CC : ...
1760
1761
self .assertEqual (
1761
1762
get_type_hints (foobar , globals (), locals ()),
1762
1763
{'x' : List [List [CC ]]}
1763
1764
)
1765
+ self .assertEqual (
1766
+ get_type_hints (foobar2 , globals (), locals ()),
1767
+ {'x' : list [list [CC ]]}
1768
+ )
1769
+
1764
1770
T = TypeVar ('T' )
1765
1771
AT = Tuple [T , ...]
1766
1772
def barfoo (x : AT ): ...
@@ -2446,6 +2452,12 @@ def foo(a: Tuple['T']):
2446
2452
self .assertEqual (get_type_hints (foo , globals (), locals ()),
2447
2453
{'a' : Tuple [T ]})
2448
2454
2455
+ def foo (a : tuple [ForwardRef ('T' )]):
2456
+ pass
2457
+
2458
+ self .assertEqual (get_type_hints (foo , globals (), locals ()),
2459
+ {'a' : tuple [T ]})
2460
+
2449
2461
def test_forward_recursion_actually (self ):
2450
2462
def namespace1 ():
2451
2463
a = typing .ForwardRef ('A' )
@@ -2909,19 +2921,41 @@ def foobar(x: List['X']): ...
2909
2921
get_type_hints (foobar , globals (), locals (), include_extras = True ),
2910
2922
{'x' : List [Annotated [int , (1 , 10 )]]}
2911
2923
)
2924
+
2925
+ def foobar (x : list [ForwardRef ('X' )]): ...
2926
+ X = Annotated [int , (1 , 10 )]
2927
+ self .assertEqual (
2928
+ get_type_hints (foobar , globals (), locals ()),
2929
+ {'x' : list [int ]}
2930
+ )
2931
+ self .assertEqual (
2932
+ get_type_hints (foobar , globals (), locals (), include_extras = True ),
2933
+ {'x' : list [Annotated [int , (1 , 10 )]]}
2934
+ )
2935
+
2912
2936
BA = Tuple [Annotated [T , (1 , 0 )], ...]
2913
2937
def barfoo (x : BA ): ...
2914
2938
self .assertEqual (get_type_hints (barfoo , globals (), locals ())['x' ], Tuple [T , ...])
2915
2939
self .assertIs (
2916
2940
get_type_hints (barfoo , globals (), locals (), include_extras = True )['x' ],
2917
2941
BA
2918
2942
)
2943
+
2944
+ BA = tuple [Annotated [T , (1 , 0 )], ...]
2945
+ def barfoo (x : BA ): ...
2946
+ self .assertEqual (get_type_hints (barfoo , globals (), locals ())['x' ], tuple [T , ...])
2947
+ self .assertIs (
2948
+ get_type_hints (barfoo , globals (), locals (), include_extras = True )['x' ],
2949
+ BA
2950
+ )
2951
+
2919
2952
def barfoo2 (x : typing .Callable [..., Annotated [List [T ], "const" ]],
2920
2953
y : typing .Union [int , Annotated [T , "mutable" ]]): ...
2921
2954
self .assertEqual (
2922
2955
get_type_hints (barfoo2 , globals (), locals ()),
2923
2956
{'x' : typing .Callable [..., List [T ]], 'y' : typing .Union [int , T ]}
2924
2957
)
2958
+
2925
2959
BA2 = typing .Callable [..., List [T ]]
2926
2960
def barfoo3 (x : BA2 ): ...
2927
2961
self .assertIs (
@@ -2972,6 +3006,9 @@ class C(Generic[T]): pass
2972
3006
self .assertIs (get_origin (Generic [T ]), Generic )
2973
3007
self .assertIs (get_origin (List [Tuple [T , T ]][int ]), list )
2974
3008
self .assertIs (get_origin (Annotated [T , 'thing' ]), Annotated )
3009
+ self .assertIs (get_origin (List ), list )
3010
+ self .assertIs (get_origin (list [int ]), list )
3011
+ self .assertIs (get_origin (list ), None )
2975
3012
2976
3013
def test_get_args (self ):
2977
3014
T = TypeVar ('T' )
@@ -2993,6 +3030,9 @@ class C(Generic[T]): pass
2993
3030
self .assertEqual (get_args (Tuple [int , ...]), (int , ...))
2994
3031
self .assertEqual (get_args (Tuple [()]), ((),))
2995
3032
self .assertEqual (get_args (Annotated [T , 'one' , 2 , ['three' ]]), (T , 'one' , 2 , ['three' ]))
3033
+ self .assertEqual (get_args (List ), (typing .T ,))
3034
+ self .assertEqual (get_args (list [int ]), (int ,))
3035
+ self .assertEqual (get_args (list ), ())
2996
3036
2997
3037
2998
3038
class CollectionsAbcTests (BaseTestCase ):
0 commit comments