Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6d70615

Browse files
authored
New analyzer: test that class properties are calculated for classes in functions (#7147)
Fixes #6494 The issue seems to be already fixed because of how `local_definitions()` works, I just add a test case to avoid regressions.
1 parent 496bb64 commit 6d70615

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

mypy/newsemanal/semanal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ def visit_func_def(self, defn: FuncDef) -> None:
517517
self.statement = defn
518518
defn.is_conditional = self.block_depth[-1] > 0
519519

520-
# Set full names even for those definitionss that aren't added
520+
# Set full names even for those definitions that aren't added
521521
# to a symbol table. For example, for overload items.
522522
defn._fullname = self.qualified_name(defn.name())
523523

mypy/newsemanal/semanal_main.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,6 @@ def calculate_class_properties(graph: 'Graph', scc: List[str], errors: Errors) -
367367
for module in scc:
368368
tree = graph[module].tree
369369
assert tree
370-
# TODO: calculate properties also for classes nested in functions.
371370
for _, node, _ in tree.local_definitions():
372371
if isinstance(node.node, TypeInfo):
373372
saved = (module, node.node, None) # module, class, function

test-data/unit/check-newsemanal.test

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2968,3 +2968,27 @@ reveal_type(C) # N: Revealed type is 'Any' \
29682968
from typing import Final
29692969
x: Final = 0
29702970
x = x # E: Cannot assign to final name "x"
2971+
2972+
[case testNewAnalyzerClassPropertiesInAllScopes]
2973+
from abc import abstractmethod, ABCMeta
2974+
2975+
class TopLevel(metaclass=ABCMeta):
2976+
@abstractmethod
2977+
def f(self) -> None: pass
2978+
2979+
TopLevel() # E: Cannot instantiate abstract class 'TopLevel' with abstract attribute 'f'
2980+
2981+
def func() -> None:
2982+
class Function(metaclass=ABCMeta):
2983+
@abstractmethod
2984+
def f(self) -> None: pass
2985+
2986+
Function() # E: Cannot instantiate abstract class 'Function' with abstract attribute 'f'
2987+
2988+
class C:
2989+
def meth(self) -> None:
2990+
class Method(metaclass=ABCMeta):
2991+
@abstractmethod
2992+
def f(self) -> None: pass
2993+
2994+
Method() # E: Cannot instantiate abstract class 'Method' with abstract attribute 'f'

0 commit comments

Comments
 (0)