@@ -3935,6 +3935,9 @@ def check_and_set_up_type_alias(self, s: AssignmentStmt) -> bool:
3935
3935
# When this type alias gets "inlined", the Any is not explicit anymore,
3936
3936
# so we need to replace it with non-explicit Anys.
3937
3937
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 )
3938
3941
# Note: with the new (lazy) type alias representation we only need to set no_args to True
3939
3942
# if the expected number of arguments is non-zero, so that aliases like `A = List` work
3940
3943
# 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:
5407
5410
# When this type alias gets "inlined", the Any is not explicit anymore,
5408
5411
# so we need to replace it with non-explicit Anys.
5409
5412
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 )
5410
5416
eager = self .is_func_scope ()
5411
5417
if isinstance (res , ProperType ) and isinstance (res , Instance ) and not res .args :
5412
5418
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:
7433
7439
return t .copy_modified (args = [a .accept (self ) for a in t .args ])
7434
7440
7435
7441
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
+
7436
7457
def apply_semantic_analyzer_patches (patches : list [tuple [int , Callable [[], None ]]]) -> None :
7437
7458
"""Call patch callbacks in the right order.
7438
7459
0 commit comments