@@ -1164,27 +1164,33 @@ class Inner[U](T):
1164
1164
1165
1165
class DefaultsTest (unittest .TestCase ):
1166
1166
def test_defaults_on_func (self ):
1167
- def func [T = int , * U = float , ** V = None ]():
1168
- pass
1167
+ ns = run_code ("""
1168
+ def func[T=int, **U=float, *V=None]():
1169
+ pass
1170
+ """ )
1169
1171
1170
- T , U , V = func .__type_params__
1172
+ T , U , V = ns [ " func" ] .__type_params__
1171
1173
self .assertIs (T .__default__ , int )
1172
1174
self .assertIs (U .__default__ , float )
1173
1175
self .assertIs (V .__default__ , type (None ))
1174
1176
1175
1177
def test_defaults_on_class (self ):
1176
- class C [T = int , * U = float , ** V = None ]:
1177
- pass
1178
+ ns = run_code ("""
1179
+ class C[T=int, **U=float, *V=None]:
1180
+ pass
1181
+ """ )
1178
1182
1179
- T , U , V = C .__type_params__
1183
+ T , U , V = ns [ "C" ] .__type_params__
1180
1184
self .assertIs (T .__default__ , int )
1181
1185
self .assertIs (U .__default__ , float )
1182
1186
self .assertIs (V .__default__ , type (None ))
1183
1187
1184
1188
def test_defaults_on_type_alias (self ):
1185
- type Alias [T = int , * U = float , ** V = None ] = int
1189
+ ns = run_code ("""
1190
+ type Alias[T = int, **U = float, *V = None] = int
1191
+ """ )
1186
1192
1187
- T , U , V = Alias .__type_params__
1193
+ T , U , V = ns [ " Alias" ] .__type_params__
1188
1194
self .assertIs (T .__default__ , int )
1189
1195
self .assertIs (U .__default__ , float )
1190
1196
self .assertIs (V .__default__ , type (None ))
@@ -1194,20 +1200,25 @@ def test_starred_invalid(self):
1194
1200
check_syntax_error (self , "type Alias[**P = *int] = int" )
1195
1201
1196
1202
def test_starred_typevartuple (self ):
1197
- default = tuple [int , str ]
1198
- type Alias [* Ts = * default ] = Ts
1199
- Ts , = Alias .__type_params__
1200
- self .assertEqual (Ts .__default__ , next (iter (default )))
1203
+ ns = run_code ("""
1204
+ default = tuple[int, str]
1205
+ type Alias[*Ts = *default] = Ts
1206
+ """ )
1207
+
1208
+ Ts , = ns ["Alias" ].__type_params__
1209
+ self .assertEqual (Ts .__default__ , next (iter (ns ["default" ])))
1201
1210
1202
1211
def test_nondefault_after_default (self ):
1203
1212
check_syntax_error (self , "def func[T=int, U](): pass" , "non-default type parameter 'U' follows default type parameter" )
1204
1213
check_syntax_error (self , "class C[T=int, U]: pass" , "non-default type parameter 'U' follows default type parameter" )
1205
1214
check_syntax_error (self , "type A[T=int, U] = int" , "non-default type parameter 'U' follows default type parameter" )
1206
1215
1207
1216
def test_lazy_evaluation (self ):
1208
- type Alias [T = Undefined , * U = Undefined , ** V = Undefined ] = int
1217
+ ns = run_code ("""
1218
+ type Alias[T = Undefined, *U = Undefined, **V = Undefined] = int
1219
+ """ )
1209
1220
1210
- T , U , V = Alias .__type_params__
1221
+ T , U , V = ns [ " Alias" ] .__type_params__
1211
1222
1212
1223
with self .assertRaises (NameError ):
1213
1224
T .__default__
@@ -1216,30 +1227,36 @@ def test_lazy_evaluation(self):
1216
1227
with self .assertRaises (NameError ):
1217
1228
V .__default__
1218
1229
1219
- Undefined = "defined"
1230
+ ns [ " Undefined" ] = "defined"
1220
1231
self .assertEqual (T .__default__ , "defined" )
1221
1232
self .assertEqual (U .__default__ , "defined" )
1222
1233
self .assertEqual (V .__default__ , "defined" )
1223
1234
1224
1235
# Now it is cached
1225
- Undefined = "redefined"
1236
+ ns [ " Undefined" ] = "redefined"
1226
1237
self .assertEqual (T .__default__ , "defined" )
1227
1238
self .assertEqual (U .__default__ , "defined" )
1228
1239
self .assertEqual (V .__default__ , "defined" )
1229
1240
1230
1241
def test_symtable_key_regression_default (self ):
1231
1242
# Test against the bugs that would happen if we used .default_
1232
1243
# as the key in the symtable.
1233
- type X [T = [T for T in [T ]]] = T
1234
- T , = X .__type_params__
1244
+ ns = run_code ("""
1245
+ type X[T = [T for T in [T]]] = T
1246
+ """ )
1247
+
1248
+ T , = ns ["X" ].__type_params__
1235
1249
self .assertEqual (T .__default__ , [T ])
1236
1250
1237
1251
def test_symtable_key_regression_name (self ):
1238
1252
# Test against the bugs that would happen if we used .name
1239
1253
# as the key in the symtable.
1240
- type X1 [T = A ] = T
1241
- type X2 [T = B ] = T
1242
- A = "A"
1243
- B = "B"
1244
- self .assertEq
57AE
ual (X1 .__type_params__ [0 ].__default__ , "A" )
1245
- self .assertEqual (X2 .__type_params__ [0 ].__default__ , "B" )
1254
+ ns = run_code ("""
1255
+ type X1[T = A] = T
1256
+ type X2[T = B] = T
1257
+ A = "A"
1258
+ B = "B"
1259
+ """ )
1260
+
1261
+ self .assertEqual (ns ["X1" ].__type_params__ [0 ].__default__ , "A" )
1262
+ self .assertEqual (ns ["X2" ].__type_params__ [0 ].__default__ , "B" )
0 commit comments