8000 Always re-process annotations refering to an alias on update (#13759) · python/mypy@0f4e0fb · GitHub
[go: up one dir, main page]

Skip to content

Commit 0f4e0fb

Browse files
authored
Always re-process annotations refering to an alias on update (#13759)
This should fix a crash discovered by `mypy_primer` in #13516 The logic here is that when type aliases have placeholders they are updated _in place_ (the node gets a new resolved target type). But this means that some variables annotated with type aliases may not get stored types, unless we defer targets where they are defined. This didn't cause troubles before, because we waited for type alias to be complete before putting it into symbol table. Now it is not possible, we need to put something into symbol table for partially complete aliases to support recursive aliases (similar to recursive classes). Also, it was tricky to come up with a repro for this issue, because when it happens, the variable gets a "silent" `Any` type, and only when it appears in a dataclass, it causes a crash.
1 parent ef22444 commit 0f4e0fb

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

mypy/semanal.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3032,6 +3032,7 @@ def process_type_annotation(self, s: AssignmentStmt) -> None:
30323032
analyzed = self.anal_type(s.type, allow_tuple_literal=allow_tuple_literal)
30333033
# Don't store not ready types (including placeholders).
30343034
if analyzed is None or has_placeholder(analyzed):
3035+
self.defer(s)
30353036
return
30363037
s.type = analyzed
30373038
if (

test-data/unit/check-dataclasses.test

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,3 +1913,23 @@ class MyDataclass:
19131913

19141914
takes_cp(MyDataclass)
19151915
[builtins fixtures/dataclasses.pyi]
1916+
1917+
[case testDataclassTypeAnnotationAliasUpdated]
1918+
# flags: --enable-recursive-aliases
1919+
import a
1920+
[file a.py]
1921+
from dataclasses import dataclass
1922+
from b import B
1923+
1924+
@dataclass
1925+
class D:
1926+
x: B
1927+
1928+
reveal_type(D) # N: Revealed type is "def (x: builtins.list[b.C]) -> a.D"
1929+
[file b.py]
1930+
from typing import List
1931+
import a
1932+
B = List[C]
1933+
class C(CC): ...
1934+
class CC: ...
1935+
[builtins fixtures/dataclasses.pyi]

0 commit comments

Comments
 (0)
0