21
21
22
22
from mypy .nodes import (
23
23
TypeInfo , Context , SymbolTableNode , Var , Expression ,
24
- nongen_builtins , check_arg_names , check_arg_kinds , ARG_POS , ARG_NAMED ,
24
+ get_nongen_builtins , check_arg_names , check_arg_kinds , ARG_POS , ARG_NAMED ,
25
25
ARG_OPT , ARG_NAMED_OPT , ARG_STAR , ARG_STAR2 , TypeVarExpr , TypeVarLikeExpr , ParamSpecExpr ,
26
26
TypeAlias , PlaceholderNode , SYMBOL_FUNCBASE_TYPES , Decorator , MypyFile
27
27
)
@@ -94,6 +94,8 @@ def analyze_type_alias(node: Expression,
94
94
95
95
def no_subscript_builtin_alias (name : str , propose_alt : bool = True ) -> str :
96
96
msg = '"{}" is not subscriptable' .format (name .split ('.' )[- 1 ])
97
+ # This should never be called if the python_version is 3.9 or newer
98
+ nongen_builtins = get_nongen_builtins ((3 , 8 ))
97
99
replacement = nongen_builtins [name ]
98
100
if replacement and propose_alt :
99
101
msg += ', use "{}" instead' .format (replacement )
@@ -194,7 +196,7 @@ def visit_unbound_type_nonoptional(self, t: UnboundType, defining_literal: bool)
194
196
hook = self .plugin .get_type_analyze_hook (fullname )
195
197
if hook is not None :
196
198
return hook (AnalyzeTypeContext (t , t , self ))
197
- if (fullname in nongen_builtins
199
+ if (fullname in get_nongen_builtins ( self . options . python_version )
198
200
and t .args and
199
201
not self .allow_unnormalized and
200
202
not self .api .is_future_flag_set ("annotations" )):
@@ -241,6 +243,7 @@ def visit_unbound_type_nonoptional(self, t: UnboundType, defining_literal: bool)
241
243
self .fail ,
242
244
self .note ,
243
245
disallow_any = disallow_any ,
246
+ python_version = self .options .python_version ,
244
247
use_generic_error = True ,
245
248
unexpanded_type = t )
246
249
return res
@@ -272,7 +275,9 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Opt
272
275
self .fail ("Final can be only used as an outermost qualifier"
273
276
" in a variable annotation" , t )
274
277
return AnyType (TypeOfAny .from_error )
275
- elif fullname == 'typing.Tuple' :
278
+ elif (fullname == 'typing.Tuple' or
279
+ (fullname == 'builtins.tuple' and (self .options .python_version >= (3 , 9 ) or
280
+ self .api .is_future_flag_set ('annotations' )))):
276
281
# Tuple is special because it is involved in builtin import cycle
277
282
# and may be not ready when used.
278
283
sym = self .api .lookup_fully_qualified_or_none ('builtins.tuple' )
@@ -305,7 +310,8 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Opt
305
310
elif fullname == 'typing.Callable' :
306
311
return self .analyze_callable_type (t )
307
312
elif (fullname == 'typing.Type' or
308
- (fullname == 'builtins.type' and self .api .is_future_flag_set ('annotations' ))):
313
+ (fullname == 'builtins.type' and (self .options .python_version >= (3 , 9 ) or
314
+ self .api .is_future_flag_set ('annotations' )))):
309
315
if len (t .args ) == 0 :
310
316
if fullname == 'typing.Type' :
311
317
any_type = self .get_omitted_any (t )
@@ -342,7 +348,8 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Opt
342
348
343
349
def get_omitted_any (self , typ : Type , fullname : Optional [str ] = None ) -> AnyType :
344
350
disallow_any = not self .is_typeshed_stub and self .options .disallow_any_generics
345
- return get_omitted_any (disallow_any , self .fail , self .note , typ , fullname )
351
+ return get_omitted_any (disallow_any , self .fail , self .note , typ ,
352
+ self .options .python_version , fullname )
346
353
347
354
def analyze_type_with_type_info (
348
355
self , info : TypeInfo , args : Sequence [Type ], ctx : Context ) -> Type :
@@ -364,7 +371,8 @@ def analyze_type_with_type_info(
364
371
if len (instance .args ) != len (info .type_vars ) and not self .defining_alias :
365
372
fix_instance (instance , self .fail , self .note ,
366
373
disallow_any = self .options .disallow_any_generics and
367
- not self .is_typeshed_stub )
374
+ not self .is_typeshed_stub ,
375
+ python_version = self .options .python_version )
368
376
369
377
tup = info .tuple_type
370
378
if tup is not None :
@@ -979,9 +987,11 @@ def tuple_type(self, items: List[Type]) -> TupleType:
979
987
980
988
981
989
def get_omitted_any (disallow_any : bool , fail : MsgCallback , note : MsgCallback ,
982
- orig_type : Type , fullname : Optional [str ] = None ,
990
+ orig_type : Type , python_version : Tuple [int , int ],
991
+ fullname : Optional [str ] = None ,
983
992
unexpanded_type : Optional [Type ] = None ) -> AnyType :
984
993
if disallow_any :
994
+ nongen_builtins = get_nongen_builtins (python_version )
985
995
if fullname in nongen_builtins :
986
996
typ = orig_type
987
997
# We use a dedicated error message for builtin generics (as the most common case).
@@ -1019,7 +1029,8 @@ def get_omitted_any(disallow_any: bool, fail: MsgCallback, note: MsgCallback,
1019
1029
1020
1030
1021
1031
def fix_instance (t : Instance , fail : MsgCallback , note : MsgCallback ,
1022
- disallow_any : bool , use_generic_error : bool = False ,
1032
+ disallow_any : bool , python_version : Tuple [int , int ],
1033
+ use_generic_error : bool = False ,
1023
1034
unexpanded_type : Optional [Type ] = None ,) -> None :
1024
1035
"""Fix a malformed instance by replacing all type arguments with Any.
1025
1036
@@ -1030,7 +1041,8 @@ def fix_instance(t: Instance, fail: MsgCallback, note: MsgCallback,
1030
1041
fullname = None # type: Optional[str]
1031
1042
else :
1032
1043
fullname = t .type .fullname
1033
- any_type = get_omitted_any (disallow_any , fail , note , t , fullname , unexpanded_type )
1044
+ any_type = get_omitted_any (disallow_any , fail , note , t , python_version , fullname ,
1045
+ unexpanded_type )
1034
1046
t .args = (any_type ,) * len (t .type .type_vars )
1035
1047
return
1036
1048
# Invalid number of type parameters.
@@ -1289,21 +1301,26 @@ def make_optional_type(t: Type) -> Type:
1289
1301
return UnionType ([t , NoneType ()], t .line , t .column )
1290
1302
1291
1303
1292
- def fix_instance_types (t : Type , fail : MsgCallback , note : MsgCallback ) -> None :
1304
+ def fix_instance_types (t : Type , fail : MsgCallback , note : MsgCallback ,
1305
+ python_version : Tuple [int , int ]) -> None :
1293
1306
"""Recursively fix all instance types (type argument count) in a given type.
1294
1307
1295
1308
For example 'Union[Dict, List[str, int]]' will be transformed into
1296
1309
'Union[Dict[Any, Any], List[Any]]' in place.
1297
1310
"""
1298
- t .accept (InstanceFixer (fail , note ))
1311
+ t .accept (InstanceFixer (fail , note , python_version ))
1299
1312
1300
1313
1301
1314
class InstanceFixer (TypeTraverserVisitor ):
1302
- def __init__ (self , fail : MsgCallback , note : MsgCallback ) -> None :
1315
+ def __init__ (
1316
+ self , fail : MsgCallback , note : MsgCallback , python_version : Tuple [int , int ]
1317
+ ) -> None :
1303
1318
self .fail = fail
1304
1319
self .note = note
1320
+ self .python_version = python_version
1305
1321
1306
1322
def visit_instance (self , typ : Instance ) -> None :
1307
1323
super ().visit_instance (typ )
1308
1324
if len (typ .args ) != len (typ .type .type_vars ):
1309
- fix_instance (typ , self .fail , self .note , disallow_any = False , use_generic_error = True )
1325
+ fix_instance (typ , self .fail , self .note , disallow_any = False ,
1326
+ python_version = self .python_version , use_generic_error = True )
0 commit comments