Skip to content

Commit

Permalink
Correctly process nested definitions in astmerge (#14104)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ilevkivskyi authored Nov 15, 2022
1 parent 6cdee7b commit 0d2a954
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mypy/server/astmerge.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ def visit_mypy_file(self, node: MypyFile) -> None:
super().visit_mypy_file(node)

def visit_block(self, node: Block) -> None:
super().visit_block(node)
node.body = self.replace_statements(node.body)
super().visit_block(node)

def visit_func_def(self, node: FuncDef) -> None:
node = self.fixup(node)
Expand Down
50 changes: 50 additions & 0 deletions test-data/unit/fine-grained.test
Original file line number Diff line number Diff line change
Expand Up @@ -10155,3 +10155,53 @@ def test() -> None:
[out]
==
m.py:4: error: Argument 1 to "meth" of "C" has incompatible type "int"; expected "D"

[case testNoNestedDefinitionCrash]
import m
[file m.py]
from typing import Any, TYPE_CHECKING

class C:
if TYPE_CHECKING:
def __init__(self, **kw: Any): ...

C
[file m.py.2]
from typing import Any, TYPE_CHECKING

class C:
if TYPE_CHECKING:
def __init__(self, **kw: Any): ...

C
# change
[builtins fixtures/dict.pyi]
[out]
==

[case testNoNestedDefinitionCrash2]
import m
[file m.py]
from typing import Any

class C:
try:
def __init__(self, **kw: Any): ...
except:
pass

C
[file m.py.2]
from typing import Any

class C:
try:
def __init__(self, **kw: Any): ...
except:
pass

C
# change
[builtins fixtures/dict.pyi]
[out]
==

0 comments on commit 0d2a954

Please sign in to comment.