Skip to content

Commit 17b68c6

Browse files
authored
New analyser: Quick and dirty fix for nested classes in fine grained mode (#7085)
Fixes #7073 This is ugly, but really quick.
1 parent 9152182 commit 17b68c6

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

mypy/server/aststripnew.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ def strip_file_top_level(self, file_node: MypyFile) -> None:
6262
self.recurse_into_functions = False
6363
file_node.plugin_deps.clear()
6464
file_node.accept(self)
65-
file_node.names.clear()
65+
for name in file_node.names.copy():
66+
# TODO: this is a hot fix, we should delete all names,
67+
# see https://github.com/python/mypy/issues/6422.
68+
if '@' not in name:
69+
del file_node.names[name]
6670

6771
def visit_block(self, b: Block) -> None:
6872
if b.is_unreachable:

test-data/unit/fine-grained.test

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8992,3 +8992,61 @@ def bar() -> str: return '0'
89928992
main:9: error: Argument 1 to "foo" has incompatible type "int"; expected "str"
89938993
==
89948994
main:9: error: Argument 1 to "foo" has incompatible type "int"; expected "str"
8995+
8996+
[case testInfiniteLoop]
8997+
# flags: --new-semantic-analyzer
8998+
[file a.py]
8999+
from b import f
9000+
from typing import Callable, TypeVar
9001+
9002+
F = TypeVar('F', bound=Callable)
9003+
def dec(x: F) -> F: return x
9004+
9005+
@dec
9006+
def foo(self):
9007+
class A:
9008+
@classmethod
9009+
def asdf(cls, x: 'A') -> None: pass
9010+
9011+
@dec
9012+
def bar(self):
9013+
class B:
9014+
@classmethod
9015+
def asdf(cls, x: 'B') -> None: pass
9016+
f()
9017+
9018+
[file b.py]
9019+
def f() -> int: pass
9020+
[file b.py.2]
9021+
def f() -> str: pass
9022+
[builtins fixtures/classmethod.pyi]
9023+
[out]
9024+
==
9025+
9026+
[case testInfiniteLoop2]
9027+
# flags: --new-semantic-analyzer
9028+
[file a.py]
9029+
from b import f
9030+
from typing import Callable, TypeVar, NamedTuple
9031+
9032+
F = TypeVar('F', bound=Callable)
9033+
def dec(x: F) -> F: return x
9034+
9035+
@dec
9036+
def foo(self):
9037+
N = NamedTuple('N', [('x', int)])
9038+
def g(x: N) -> None: pass
9039+
9040+
@dec
9041+
def bar(self):
9042+
N = NamedTuple('N', [('x', int)])
9043+
def g(x: N) -> None: pass
9044+
f()
9045+
9046+
[file b.py]
9047+
def f() -> int: pass
9048+
[file b.py.2]
9049+
def f() -> str: pass
9050+
[builtins fixtures/classmethod.pyi]
9051+
[out]
9052+
==

0 commit comments

Comments
 (0)