@@ -3935,6 +3935,9 @@ def check_and_set_up_type_alias(self, s: AssignmentStmt) -> bool:
39353935 # When this type alias gets "inlined", the Any is not explicit anymore,
39363936 # so we need to replace it with non-explicit Anys.
39373937 res = make_any_non_explicit (res )
3938+ if self .options .disallow_any_unimported and has_any_from_unimported_type (res ):
3939+ self .msg .unimported_type_becomes_any ("Type alias target" , res , s )
3940+ res = make_any_non_unimported (res )
39383941 # Note: with the new (lazy) type alias representation we only need to set no_args to True
39393942 # if the expected number of arguments is non-zero, so that aliases like `A = List` work
39403943 # but not aliases like `A = TypeAliasType("A", List)` as these need explicit type params.
@@ -5407,6 +5410,9 @@ def visit_type_alias_stmt(self, s: TypeAliasStmt) -> None:
54075410 # When this type alias gets "inlined", the Any is not explicit anymore,
54085411 # so we need to replace it with non-explicit Anys.
54095412 res = make_any_non_explicit (res )
5413+ if self .options .disallow_any_unimported and has_any_from_unimported_type (res ):
5414+ self .msg .unimported_type_becomes_any ("Type alias target" , res , s )
5415+ res = make_any_non_unimported (res )
54105416 eager = self .is_func_scope ()
54115417 if isinstance (res , ProperType ) and isinstance (res , Instance ) and not res .args :
54125418 fix_instance (res , self .fail , self .note , disallow_any = False , options = self .options )
@@ -7433,6 +7439,21 @@ def visit_type_alias_type(self, t: TypeAliasType) -> Type:
74337439 return t .copy_modified (args = [a .accept (self ) for a in t .args ])
74347440
74357441
7442+ def make_any_non_unimported (t : Type ) -> Type :
7443+ """Replace all Any types that come from unimported types with special form Any."""
7444+ return t .accept (MakeAnyNonUnimported ())
7445+
7446+
7447+ class MakeAnyNonUnimported (TrivialSyntheticTypeTranslator ):
7448+ def visit_any (self , t : AnyType ) -> Type :
7449+ if t .type_of_any == TypeOfAny .from_unimported_type :
7450+ return t .copy_modified (TypeOfAny .special_form , missing_import_name = None )
7451+ return t
7452+
7453+ def visit_type_alias_type (self , t : TypeAliasType ) -> Type :
7454+ return t .copy_modified (args = [a .accept (self ) for a in t .args ])
7455+
7456+
74367457def apply_semantic_analyzer_patches (patches : list [tuple [int , Callable [[], None ]]]) -> None :
74377458 """Call patch callbacks in the right order.
74387459
0 commit comments