@@ -207,12 +207,14 @@ def visit_unbound_type_nonoptional(self, t: UnboundType, defining_literal: bool)
207
207
all_vars = node .alias_tvars
208
208
target = node .target
209
209
an_args = self .anal_array (t .args )
210
- res = expand_type_alias (target , all_vars , an_args , self .fail , node .no_args , t )
210
+ disallow_any = self .options .disallow_any_generics and not self .is_typeshed_stub
211
+ res = expand_type_alias (target , all_vars , an_args , self .fail , node .no_args , t ,
212
+ disallow_any = disallow_any )
211
213
# The only case where expand_type_alias() can return an incorrect instance is
212
214
# when it is top-level instance, so no need to recurse.
213
215
if (isinstance (res , Instance ) and len (res .args ) != len (res .type .type_vars ) and
214
216
not self .defining_alias ):
215
- fix_instance (res , self .fail )
217
+ fix_instance (res , self .fail , disallow_any = disallow_any , use_generic_error = True )
216
218
return res
217
219
elif isinstance (node , TypeInfo ):
218
220
return self .analyze_type_with_type_info (node , t .args , t )
@@ -254,9 +256,9 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Opt
254
256
return AnyType (TypeOfAny .special_form )
255
257
if len (t .args ) == 0 and not t .empty_tuple_index :
256
258
# Bare 'Tuple' is same as 'tuple'
257
- if self . options . disallow_any_generics and not self .is_typeshed_stub :
258
- self .fail ( message_registry . BARE_GENERIC , t )
259
- return self . named_type ( 'builtins.tuple' , line = t .line , column = t .column )
259
+ any_type = self .get_omitted_any ( t )
260
+ return self .named_type ( 'builtins.tuple' , [ any_type ],
261
+ line = t .line , column = t .column )
260
262
if len (t .args ) == 2 and isinstance (t .args [1 ], EllipsisType ):
261
263
# Tuple[T, ...] (uniform, variable-length tuple)
262
264
instance = self .named_type ('builtins.tuple' , [self .anal_type (t .args [0 ])])
@@ -276,8 +278,7 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Opt
276
278
return self .analyze_callable_type (t )
277
279
elif fullname == 'typing.Type' :
278
280
if len (t .args ) == 0 :
279
- any_type = AnyType (TypeOfAny .from_omitted_generics ,
280
- line = t .line , column = t .column )
281
+ any_type = self .get_omitted_any (t )
281
282
return TypeType (any_type , line = t .line , column = t .column )
282
283
if len (t .args ) != 1 :
283
284
self .fail ('Type[...] must have exactly one type argument' , t )
@@ -302,6 +303,10 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Opt
302
303
return self .analyze_literal_type (t )
303
304
return None
304
305
306
+ def get_omitted_any (self , ctx : Context , fullname : Optional [str ] = None ) -> AnyType :
307
+ disallow_any = not self .is_typeshed_stub and self .options .disallow_any_generics
308
+ return get_omitted_any (disallow_any , self .fail , ctx , fullname )
309
+
305
310
def analyze_type_with_type_info (self , info : TypeInfo , args : List [Type ], ctx : Context ) -> Type :
306
311
"""Bind unbound type when were able to find target TypeInfo.
307
312
@@ -318,19 +323,9 @@ def analyze_type_with_type_info(self, info: TypeInfo, args: List[Type], ctx: Con
318
323
instance = Instance (info , self .anal_array (args ), ctx .line , ctx .column )
319
324
# Check type argument count.
320
325
if len (instance .args ) != len (info .type_vars ) and not self .defining_alias :
321
- fix_instance (instance , self .fail )
322
- if not args and self .options .disallow_any_generics and not self .defining_alias :
323
- # We report/patch invalid built-in instances already during second pass.
324
- # This is done to avoid storing additional state on instances.
325
- # All other (including user defined) generics will be patched/reported
326
- # in the third pass.
327
- if not self .is_typeshed_stub and info .fullname () in nongen_builtins :
328
- alternative = nongen_builtins [info .fullname ()]
329
- self .fail (message_registry .IMPLICIT_GENERIC_ANY_BUILTIN .format (alternative ), ctx )
330
- any_type = AnyType (TypeOfAny .from_error , line = ctx .line )
331
- else :
332
- any_type = AnyType (TypeOfAny .from_omitted_generics , line = ctx .line )
333
- instance .args = [any_type ] * len (info .type_vars )
326
+ fix_instance (instance , self .fail ,
327
+ disallow_any = self .options .disallow_any_generics and
328
+ not self .is_typeshed_stub )
334
329
335
330
tup = info .tuple_type
336
331
if tup is not None :
@@ -557,8 +552,7 @@ def analyze_callable_type(self, t: UnboundType) -> Type:
557
552
fallback = self .named_type ('builtins.function' )
558
553
if len (t .args ) == 0 :
559
554
# Callable (bare). Treat as Callable[..., Any].
560
- any_type = AnyType (TypeOfAny .from_omitted_generics ,
561
- line = t .line , column = t .column )
555
+ any_type = self .get_omitted_any (t )
562
556
ret = CallableType ([any_type , any_type ],
563
557
[nodes .ARG_STAR , nodes .ARG_STAR2 ],
564
558
[None , None ],
@@ -846,14 +840,33 @@ def tuple_type(self, items: List[Type]) -> TupleType:
846
840
TypeVarList = List [Tuple [str , TypeVarExpr ]]
847
841
848
842
849
- def fix_instance (t : Instance , fail : Callable [[str , Context ], None ]) -> None :
843
+ def get_omitted_any (disallow_any : bool , fail : Callable [[str , Context ], None ],
844
+ ctx : Context , fullname : Optional [str ] = None ) -> AnyType :
845
+ if disallow_any :
846
+ if fullname in nongen_builtins :
847
+ # We use a dedicated error message for builtin generics (as the most common case).
848
+ alternative = nongen_builtins [fullname ]
849
+ fail (message_registry .IMPLICIT_GENERIC_ANY_BUILTIN .format (alternative ), ctx )
850
+ else :
851
+ fail (message_registry .BARE_GENERIC , ctx )
852
+ any_type = AnyType (TypeOfAny .from_error , line = ctx .line , column = ctx .column )
853
+ else :
854
+ any_type = AnyType (TypeOfAny .from_omitted_generics , line = ctx .line , column = ctx .column )
855
+ return any_type
856
+
857
+
858
+ def fix_instance (t : Instance , fail : Callable [[str , Context ], None ],
859
+ disallow_any : bool , use_generic_error : bool = False ) -> None :
850
860
"""Fix a malformed instance by replacing all type arguments with Any.
851
861
852
862
Also emit a suitable error if this is not due to implicit Any's.
853
863
"""
854
864
if len (t .args ) == 0 :
855
- any_type = AnyType (TypeOfAny .from_omitted_generics ,
856
- line = t .line , column = t .column )
865
+ if use_generic_error :
866
+ fullname = None # type: Optional[str]
867
+ else :
868
+ fullname = t .type .fullname ()
869
+ any_type = get_omitted_any (disallow_any , fail , t , fullname )
857
870
t .args = [any_type ] * len (t .type .type_vars )
858
871
return
859
872
# Invalid number of type parameters.
@@ -876,7 +889,8 @@ def fix_instance(t: Instance, fail: Callable[[str, Context], None]) -> None:
876
889
877
890
878
891
def expand_type_alias (target : Type , alias_tvars : List [str ], args : List [Type ],
879
- fail : Callable [[str , Context ], None ], no_args : bool , ctx : Context ) -> Type :
892
+ fail : Callable [[str , Context ], None ], no_args : bool , ctx : Context , * ,
893
+ disallow_any : bool = False ) -> Type :
880
894
"""Expand a (generic) type alias target following the rules outlined in TypeAlias docstring.
881
895
882
896
Here:
@@ -892,7 +906,8 @@ def expand_type_alias(target: Type, alias_tvars: List[str], args: List[Type],
892
906
if exp_len > 0 and act_len == 0 :
893
907
# Interpret bare Alias same as normal generic, i.e., Alias[Any, Any, ...]
894
908
assert alias_tvars is not None
895
- return set_any_tvars (target , alias_tvars , ctx .line , ctx .column )
909
+ return set_any_tvars (target , alias_tvars , ctx .line , ctx .column ,
910
+ disallow_any = disallow_any , fail = fail )
896
911
if exp_len == 0 and act_len == 0 :
897
912
if no_args :
898
913
assert isinstance (target , Instance )
@@ -907,7 +922,7 @@ def expand_type_alias(target: Type, alias_tvars: List[str], args: List[Type],
907
922
fail ('Bad number of arguments for type alias, expected: %s, given: %s'
908
923
% (exp_len , act_len ), ctx )
909
924
return set_any_tvars (target , alias_tvars or [],
910
- ctx .line , ctx .column , implicit = False )
925
+ ctx .line , ctx .column , from_error = True )
911
926
typ = replace_alias_tvars (target , alias_tvars , args , ctx .line , ctx .column )
912
927
# HACK: Implement FlexibleAlias[T, typ] by expanding it to typ here.
913
928
if (isinstance (typ , Instance )
@@ -938,11 +953,17 @@ def replace_alias_tvars(tp: Type, vars: List[str], subs: List[Type],
938
953
939
954
940
955
def set_any_tvars (tp : Type , vars : List [str ],
941
- newline : int , newcolumn : int , implicit : bool = True ) -> Type :
942
- if implicit :
943
- type_of_any = TypeOfAny .from_omitted_generics
956
+ newline : int , newcolumn : int , * ,
957
+ from_error : bool = False ,
958
+ disallow_any : bool = False ,
959
+ fail : Optional [Callable [[str , Context ], None ]] = None ) -> Type :
960
+ if from_error or disallow_any :
961
+ type_of_any = TypeOfAny .from_error
944
962
else :
945
- type_of_any = TypeOfAny .special_form
963
+ type_of_any = TypeOfAny .from_omitted_generics
964
+ if disallow_any :
965
+ assert fail is not None
966
+ fail (message_registry .BARE_GENERIC , Context (newline , newcolumn ))
946
967
any_type = AnyType (type_of_any , line = newline , column = newcolumn )
947
968
return replace_alias_tvars (tp , vars , [any_type ] * len (vars ), newline , newcolumn )
948
969
@@ -1112,20 +1133,22 @@ def make_optional_type(t: Type) -> Type:
1112
1133
return UnionType ([t , NoneType ()], t .line , t .column )
1113
1134
1114
1135
1115
- def fix_instance_types (t : Type , fail : Callable [[str , Context ], None ]) -> None :
1136
+ def fix_instance_types (t : Type , fail : Callable [[str , Context ], None ],
1137
+ disallow_any : bool ) -> None :
1116
1138
"""Recursively fix all instance types (type argument count) in a given type.
1117
1139
1118
1140
For example 'Union[Dict, List[str, int]]' will be transformed into
1119
1141
'Union[Dict[Any, Any], List[Any]]' in place.
1120
1142
"""
1121
- t .accept (InstanceFixer (fail ))
1143
+ t .accept (InstanceFixer (fail , disallow_any ))
1122
1144
1123
1145
1124
1146
class InstanceFixer (TypeTraverserVisitor ):
1125
- def __init__ (self , fail : Callable [[str , Context ], None ]) -> None :
1147
+ def __init__ (self , fail : Callable [[str , Context ], None ], disallow_any : bool ) -> None :
1126
1148
self .fail = fail
1149
+ self .disallow_any = disallow_any
1127
1150
1128
1151
def visit_instance (self , typ : Instance ) -> None :
1129
1152
super ().visit_instance (typ )
1130
1153
if len (typ .args ) != len (typ .type .type_vars ):
1131
- fix_instance (typ , self .fail )
1154
+ fix_instance (typ , self .fail , self . disallow_any , use_generic_error = True )
0 commit comments