Skip to content

Commit fbae432

Browse files
authored
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 10f3ce5 commit fbae432

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

mypy/semanal.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -4014,8 +4014,10 @@ def check_and_set_up_type_alias(self, s: AssignmentStmt) -> bool:
40144014
# so we need to replace it with non-explicit Anys.
40154015
res = make_any_non_explicit(res)
40164016
if self.options.disallow_any_unimported and has_any_from_unimported_type(res):
4017-
self.msg.unimported_type_becomes_any("Type alias target", res, s)
4018-
res = make_any_non_unimported(res)
4017+
# Only show error message once, when the type is fully analyzed.
4018+
if not has_placeholder(res):
4019+
self.msg.unimported_type_becomes_any("Type alias target", res, s)
4020+
res = make_any_non_unimported(res)
40194021
# Note: with the new (lazy) type alias representation we only need to set no_args to True
40204022
# if the expected number of arguments is non-zero, so that aliases like `A = List` work
40214023
# but not aliases like `A = TypeAliasType("A", List)` as these need explicit type params.
@@ -4069,6 +4071,8 @@ def check_and_set_up_type_alias(self, s: AssignmentStmt) -> bool:
40694071
existing.node.alias_tvars = alias_tvars
40704072
existing.node.no_args = no_args
40714073
updated = True
4074+
# Invalidate recursive status cache in case it was previously set.
4075+
existing.node._is_recursive = None
40724076
else:
40734077
# Otherwise just replace existing placeholder with type alias.
40744078
existing.node = alias_node

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

+8
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)