Skip to content

Commit 0d2a954

Browse files
authored
Correctly process nested definitions in astmerge (#14104)
Fixes #12744 The fix is straightforward. Current logic can produce a random mix of old and new nodes if there are functions/methods nested in other statements.
1 parent 6cdee7b commit 0d2a954

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

mypy/server/astmerge.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ def visit_mypy_file(self, node: MypyFile) -> None:
213213
super().visit_mypy_file(node)
214214

215215
def visit_block(self, node: Block) -> None:
216-
super().visit_block(node)
217216
node.body = self.replace_statements(node.body)
217+
super().visit_block(node)
218218

219219
def visit_func_def(self, node: FuncDef) -> None:
220220
node = self.fixup(node)

test-data/unit/fine-grained.test

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10155,3 +10155,53 @@ def test() -> None:
1015510155
[out]
1015610156
==
1015710157
m.py:4: error: Argument 1 to "meth" of "C" has incompatible type "int"; expected "D"
10158+
10159+
[case testNoNestedDefinitionCrash]
10160+
import m
10161+
[file m.py]
10162+
from typing import Any, TYPE_CHECKING
10163+
10164+
class C:
10165+
if TYPE_CHECKING:
10166+
def __init__(self, **kw: Any): ...
10167+
10168+
C
10169+
[file m.py.2]
10170+
from typing import Any, TYPE_CHECKING
10171+
10172+
class C:
10173+
if TYPE_CHECKING:
10174+
def __init__(self, **kw: Any): ...
10175+
10176+
C
10177+
# change
10178+
[builtins fixtures/dict.pyi]
10179+
[out]
10180+
==
10181+
10182+
[case testNoNestedDefinitionCrash2]
10183+
import m
10184+
[file m.py]
10185+
from typing import Any
10186+
10187+
class C:
10188+
try:
10189+
def __init__(self, **kw: Any): ...
10190+
except:
10191+
pass
10192+
10193+
C
10194+
[file m.py.2]
10195+
from typing import Any
10196+
10197+
class C:
10198+
try:
10199+
def __init__(self, **kw: Any): ...
10200+
except:
10201+
pass
10202+
10203+
C
10204+
# change
10205+
[builtins fixtures/dict.pyi]
10206+
[out]
10207+
==

0 commit comments

Comments
 (0)