66
66
),
67
67
key = str ,
68
68
)
69
+ _Type = getattr (typing , "Type" , None )
70
+ _List = getattr (typing , "List" , None )
71
+ _Dict = getattr (typing , "Dict" , None )
72
+ _Set = getattr (typing , "Set" , None )
73
+ _FrozenSet = getattr (typing , "FrozenSet" , None )
74
+ _Tuple = getattr (typing , "Tuple" , None )
69
75
70
76
71
77
@pytest .mark .parametrize ("typ" , generics , ids = repr )
@@ -104,10 +110,13 @@ def test_specialised_scalar_types(data, typ, instance_of):
104
110
105
111
106
112
def test_typing_Type_int ():
107
- assert_simple_property (from_type (typing .Type [int ]), lambda x : x is int )
113
+ for t in (type [int ], type ["int" ], _Type [int ], _Type ["int" ]):
114
+ assert_simple_property (from_type (t ), lambda x : x is int )
108
115
109
116
110
- @given (from_type (typing .Type [typing .Union [str , list ]]))
117
+ @given (
118
+ from_type (type [typing .Union [str , list ]]) | from_type (_Type [typing .Union [str , list ]])
119
+ )
111
120
def test_typing_Type_Union (ex ):
112
121
assert ex in (str , list )
113
122
@@ -143,15 +152,21 @@ class Elem:
143
152
@pytest .mark .parametrize (
144
153
"typ,coll_type" ,
145
154
[
146
- (typing .Set [Elem ], set ),
147
- (typing .FrozenSet [Elem ], frozenset ),
148
- (typing .Dict [Elem , None ], dict ),
155
+ (_Set [Elem ], set ),
156
+ (_FrozenSet [Elem ], frozenset ),
157
+ (_Dict [Elem , None ], dict ),
158
+ (set [Elem ], set ),
159
+ (frozenset [Elem ], frozenset ),
160
+ # (dict[Elem, None], dict), # FIXME this should work
149
161
(typing .DefaultDict [Elem , None ], collections .defaultdict ),
150
162
(typing .KeysView [Elem ], type ({}.keys ())),
151
163
(typing .ValuesView [Elem ], type ({}.values ())),
152
- (typing .List [Elem ], list ),
153
- (typing .Tuple [Elem ], tuple ),
154
- (typing .Tuple [Elem , ...], tuple ),
164
+ (_List [Elem ], list ),
165
+ (_Tuple [Elem ], tuple ),
166
+ (_Tuple [Elem , ...], tuple ),
167
+ (list [Elem ], list ),
168
+ (tuple [Elem ], tuple ),
169
+ (tuple [Elem , ...], tuple ),
155
170
(typing .Iterator [Elem ], typing .Iterator ),
156
171
(typing .Sequence [Elem ], typing .Sequence ),
157
172
(typing .Iterable [Elem ], typing .Iterable ),
@@ -226,23 +241,24 @@ def test_Optional_minimises_to_None():
226
241
assert minimal (from_type (typing .Optional [int ]), lambda ex : True ) is None
227
242
228
243
229
- @pytest .mark .parametrize ("n" , range (10 ))
230
- def test_variable_length_tuples (n ):
231
- type_ = typing .Tuple [int , ...]
244
+ @pytest .mark .parametrize ("n" , [0 , 1 , 5 ])
245
+ @pytest .mark .parametrize ("t" , [tuple , _Tuple ])
246
+ def test_variable_length_tuples (t , n ):
247
+ type_ = t [int , ...]
232
248
check_can_generate_examples (from_type (type_ ).filter (lambda ex : len (ex ) == n ))
233
249
234
250
235
251
def test_lookup_overrides_defaults ():
236
252
sentinel = object ()
237
253
with temp_registered (int , st .just (sentinel )):
238
254
239
- @given (from_type (typing . List [int ]))
255
+ @given (from_type (list [int ]))
240
256
def inner_1 (ex ):
241
257
assert all (elem is sentinel for elem in ex )
242
258
243
259
inner_1 ()
244
260
245
- @given (from_type (typing . List [int ]))
261
+ @given (from_type (list [int ]))
246
262
def inner_2 (ex ):
247
263
assert all (isinstance (elem , int ) for elem in ex )
248
264
@@ -253,7 +269,7 @@ def test_register_generic_typing_strats():
253
269
# I don't expect anyone to do this, but good to check it works as expected
254
270
with temp_registered (
255
271
typing .Sequence ,
256
- types ._global_type_lookup [typing . get_origin ( typing . Set ) or typing . Set ],
272
+ types ._global_type_lookup [set ],
257
273
):
258
274
# We register sets for the abstract sequence type, which masks subtypes
259
275
# from supertype resolution but not direct resolution
@@ -264,9 +280,7 @@ def test_register_generic_typing_strats():
264
280
from_type (typing .Container [int ]),
265
281
lambda ex : not isinstance (ex , typing .Sequence ),
266
282
)
267
- assert_all_examples (
268
- from_type (typing .List [int ]), lambda ex : isinstance (ex , list )
269
- )
283
+ assert_all_examples (from_type (list [int ]), lambda ex : isinstance (ex , list ))
270
284
271
285
272
286
def if_available (name ):
@@ -587,7 +601,7 @@ def test_override_args_for_namedtuple(thing):
587
601
assert thing .a is None
588
602
589
603
590
- @pytest .mark .parametrize ("thing" , [typing .Optional , typing . List , typing . Type ])
604
+ @pytest .mark .parametrize ("thing" , [typing .Optional , list , type , _List , _Type ])
591
605
def test_cannot_resolve_bare_forward_reference (thing ):
592
606
t = thing ["ConcreteFoo" ]
593
607
with pytest .raises (InvalidArgument ):
@@ -740,7 +754,7 @@ def test_resolving_recursive_type_with_registered_constraint_not_none():
740
754
find_any (s , lambda s : s .next_node is not None )
741
755
742
756
743
- @given (from_type (typing . Tuple [()]))
757
+ @given (from_type (tuple [()]) | from_type ( _Tuple [()]))
744
758
def test_resolves_empty_Tuple_issue_1583_regression (ex ):
745
759
# See e.g. https://github.com/python/mypy/commit/71332d58
746
760
assert ex == ()
@@ -805,11 +819,17 @@ def test_cannot_resolve_abstract_class_with_no_concrete_subclass(instance):
805
819
806
820
807
821
@fails_with (ResolutionFailed )
808
- @given (st .from_type (typing . Type ["ConcreteFoo" ]))
822
+ @given (st .from_type (type ["ConcreteFoo" ]))
809
823
def test_cannot_resolve_type_with_forwardref (instance ):
810
824
raise AssertionError ("test body unreachable as strategy cannot resolve" )
811
825
812
826
827
+ @fails_with (ResolutionFailed )
828
+ @given (st .from_type (_Type ["ConcreteFoo" ]))
829
+ def test_cannot_resolve_type_with_forwardref_old (instance ):
830
+ raise AssertionError ("test body unreachable as strategy cannot resolve" )
831
+
832
+
813
833
@pytest .mark .parametrize ("typ" , [typing .Hashable , typing .Sized ])
814
834
@given (data = st .data ())
815
835
def test_inference_on_generic_collections_abc_aliases (typ , data ):
@@ -938,9 +958,12 @@ def test_timezone_lookup(type_):
938
958
@pytest .mark .parametrize (
939
959
"typ" ,
940
960
[
941
- typing .Set [typing .Hashable ],
942
- typing .FrozenSet [typing .Hashable ],
943
- typing .Dict [typing .Hashable , int ],
961
+ _Set [typing .Hashable ],
962
+ _FrozenSet [typing .Hashable ],
963
+ _Dict [typing .Hashable , int ],
964
+ set [typing .Hashable ],
965
+ frozenset [typing .Hashable ],
966
+ dict [typing .Hashable , int ],
944
967
],
945
968
)
946
969
@settings (suppress_health_check = [HealthCheck .data_too_large ])
@@ -973,7 +996,8 @@ def __init__(self, value=-1) -> None:
973
996
"typ,repr_" ,
974
997
[
975
998
(int , "integers()" ),
976
- (typing .List [str ], "lists(text())" ),
999
+ (list [str ], "lists(text())" ),
1000
+ (_List [str ], "lists(text())" ),
977
1001
("not a type" , "from_type('not a type')" ),
978
1002
(random .Random , "randoms()" ),
979
1003
(_EmptyClass , "from_type(tests.cover.test_lookup._EmptyClass)" ),
@@ -1123,15 +1147,22 @@ def test_resolves_forwardrefs_to_builtin_types(t, data):
1123
1147
1124
1148
@pytest .mark .parametrize ("t" , BUILTIN_TYPES , ids = lambda t : t .__name__ )
1125
1149
def test_resolves_type_of_builtin_types (t ):
1126
- assert_simple_property (st .from_type (typing . Type [t .__name__ ]), lambda v : v is t )
1150
+ assert_simple_property (st .from_type (type [t .__name__ ]), lambda v : v is t )
1127
1151
1128
1152
1129
- @given (st .from_type (typing .Type [typing .Union ["str" , "int" ]]))
1153
+ @given (
1154
+ st .from_type (type [typing .Union ["str" , "int" ]])
1155
+ | st .from_type (_Type [typing .Union ["str" , "int" ]])
1156
+ )
1130
1157
def test_resolves_type_of_union_of_forwardrefs_to_builtins (x ):
1131
1158
assert x in (str , int )
1132
1159
1133
1160
1134
- @pytest .mark .parametrize ("type_" , [typing .List [int ], typing .Optional [int ]])
1161
+ @pytest .mark .parametrize (
1162
+ # Old-style `List` because `list[int]() == list()`, so no need for the hint.
1163
+ "type_" ,
1164
+ [getattr (typing , "List" , None )[int ], typing .Optional [int ]],
1165
+ )
1135
1166
def test_builds_suggests_from_type (type_ ):
1136
1167
with pytest .raises (
1137
1168
InvalidArgument , match = re .escape (f"try using from_type({ type_ !r} )" )
0 commit comments