8585from mypy .nodes import implicit_module_attrs
8686from mypy .typeanal import (
8787 TypeAnalyser , TypeAnalyserPass3 , analyze_type_alias , no_subscript_builtin_alias ,
88- TypeVariableQuery , TypeVarList , remove_dups ,
88+ TypeVariableQuery , TypeVarList , remove_dups , has_any_from_silent_import
8989)
9090from mypy .exprtotype import expr_to_unanalyzed_type , TypeTranslationError
9191from mypy .sametypes import is_same_type
@@ -964,7 +964,9 @@ def analyze_base_classes(self, defn: ClassDef) -> None:
964964 self .fail ("Cannot subclass NewType" , defn )
965965 base_types .append (base )
966966 elif isinstance (base , AnyType ):
967- if self .options .disallow_subclassing_any :
967+ # if --disallow-implicit-any-types is set, the issue is reported later
968+ if (self .options .disallow_subclassing_any and
969+ not self .options .disallow_implicit_any_types ):
968970 if isinstance (base_expr , (NameExpr , MemberExpr )):
969971 msg = "Class cannot subclass '{}' (has type 'Any')" .format (base_expr .name )
970972 else :
@@ -974,6 +976,15 @@ def analyze_base_classes(self, defn: ClassDef) -> None:
974976 else :
975977 self .fail ('Invalid base class' , base_expr )
976978 info .fallback_to_any = True
979+ if (self .options .disallow_implicit_any_types and
980+ has_any_from_silent_import (base )):
981+ if isinstance (base_expr , (NameExpr , MemberExpr )):
982+ msg = ("Subclassing type '{}' that is implicitly converted to '{}' due to "
983+ "import from unanalyzed module" .format (base_expr .name , base ))
984+ else :
985+ msg = ("Subclassing a type that is implicitly converted to '{}' "
986+ "due to import from unanalyzed module" .format (base ))
987+ self .fail (msg , base_expr )
977988
978989 # Add 'object' as implicit base if there is no other base class.
979990 if (not base_types and defn .fullname != 'builtins.object' ):
@@ -1428,7 +1439,12 @@ def add_unknown_symbol(self, name: str, context: Context, is_import: bool = Fals
14281439 else :
14291440 var ._fullname = self .qualified_name (name )
14301441 var .is_ready = True
1431- var .type = AnyType ()
1442+ any_type = AnyType ()
1443+ if self .options .silent_mode ():
1444+ # if silent mode is not enabled, no need to report implicit conversion to Any,
1445+ # let mypy report an import error.
1446+ any_type .is_from_silent_import = is_import
1447+ var .type = any_type
14321448 var .is_suppressed_import = is_import
14331449 self .add_symbol (name , SymbolTableNode (GDEF , var , self .cur_mod_id ), context )
14341450
0 commit comments