Skip to content

Commit bb3ddf6

Browse files
committed
Add test cases to handle nested classes
1 parent 3a2fce5 commit bb3ddf6

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

mypy/checkexpr.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,17 @@ def extract_refexpr_names(expr: RefExpr) -> Set[str]:
5555
while expr.kind == MODULE_REF or expr.fullname is not None:
5656
if expr.kind == MODULE_REF:
5757
output.add(expr.fullname)
58-
elif expr.fullname is not None and '.' in expr.fullname:
59-
if not (isinstance(expr.node, Var) and expr.node.is_import):
60-
output.add(expr.fullname.rsplit('.', 1)[0])
6158

6259
if isinstance(expr, NameExpr):
60+
is_silenced_import = isinstance(expr.node, Var) and expr.node.is_import
6361
if expr.info is not None:
62+
# Reference to regular type
6463
output.update(split_module_names(expr.info.module_name))
64+
elif isinstance(expr.node, TypeInfo):
65+
# Nested class
66+
output.update(split_module_names(expr.node.module_name))
67+
elif expr.fullname is not None and '.' in expr.fullname and not is_silenced_import:
68+
output.add(expr.fullname.rsplit('.', 1)[0])
6569
break
6670
elif isinstance(expr, MemberExpr):
6771
if isinstance(expr.expr, RefExpr):

test-data/unit/check-incremental.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,3 +1178,17 @@ def test() -> str: return "foo"
11781178
[stale foo, unrelated]
11791179
[out]
11801180
tmp/main.py:3: error: Revealed type is 'builtins.str'
1181+
1182+
[case testIncrementalWorksWithNestedClasses]
1183+
import foo
1184+
1185+
[file foo.py]
1186+
class MyClass:
1187+
class NestedClass:
1188+
pass
1189+
1190+
class_attr = NestedClass()
1191+
1192+
[rechecked]
1193+
[stale]
1194+
[out]

0 commit comments

Comments
 (0)