Skip to content

Commit a5e9b0b

Browse files
ilevkivskyihauntsaninja
authored andcommitted
Fix crash when showing partially analyzed type in error message (#17961)
Fixes #17954 People say something about cache invalidation being one of the hardest problems...
1 parent 4775da1 commit a5e9b0b

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

mypy/semanal.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3958,8 +3958,10 @@ def check_and_set_up_type_alias(self, s: AssignmentStmt) -> bool:
39583958
# so we need to replace it with non-explicit Anys.
39593959
res = make_any_non_explicit(res)
39603960
if self.options.disallow_any_unimported and has_any_from_unimported_type(res):
3961-
self.msg.unimported_type_becomes_any("Type alias target", res, s)
3962-
res = make_any_non_unimported(res)
3961+
# Only show error message once, when the type is fully analyzed.
3962+
if not has_placeholder(res):
3963+
self.msg.unimported_type_becomes_any("Type alias target", res, s)
3964+
res = make_any_non_unimported(res)
39633965
# Note: with the new (lazy) type alias representation we only need to set no_args to True
39643966
# if the expected number of arguments is non-zero, so that aliases like `A = List` work
39653967
# but not aliases like `A = TypeAliasType("A", List)` as these need explicit type params.
@@ -4013,6 +4015,8 @@ def check_and_set_up_type_alias(self, s: AssignmentStmt) -> bool:
40134015
existing.node.alias_tvars = alias_tvars
40144016
existing.node.no_args = no_args
40154017
updated = True
4018+
# Invalidate recursive status cache in case it was previously set.
4019+
existing.node._is_recursive = None
40164020
else:
40174021
# Otherwise just replace existing placeholder with type alias.
40184022
existing.node = alias_node

test-data/unit/check-recursive-types.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,3 +1006,11 @@ ta: Tuple[A]
10061006
p: Proto
10071007
p = ta
10081008
[builtins fixtures/tuple.pyi]
1009+
1010+
[case testRecursiveAliasesWithAnyUnimported]
1011+
# flags: --disallow-any-unimported
1012+
from typing import Callable
1013+
from bogus import Foo # type: ignore
1014+
1015+
A = Callable[[Foo, "B"], Foo] # E: Type alias target becomes "Callable[[Any, B], Any]" due to an unfollowed import
1016+
B = Callable[[Foo, A], Foo] # E: Type alias target becomes "Callable[[Any, A], Any]" due to an unfollowed import

0 commit comments

Comments
 (0)