Skip to content

Commit 26cf063

Browse files
authored
Recognize __qualname__ when used in class scope (#588)
Relates #259.
1 parent dbb8fc4 commit 26cf063

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

pyflakes/checker.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,10 @@ def __init__(self, lineno, col_offset):
669669
self.col_offset = col_offset
670670

671671

672+
class DetectClassScopedMagic:
673+
names = dir()
674+
675+
672676
# Globally defined names which are not attributes of the builtins module, or
673677
# are only present on some platforms.
674678
_MAGIC_GLOBALS = ['__file__', '__builtins__', 'WindowsError']
@@ -1231,7 +1235,7 @@ def handleNodeLoad(self, node):
12311235
# the special name __path__ is valid only in packages
12321236
return
12331237

1234-
if name == '__module__' and isinstance(self.scope, ClassScope):
1238+
if name in DetectClassScopedMagic.names and isinstance(self.scope, ClassScope):
12351239
return
12361240

12371241
# protected with a NameError handler?

pyflakes/test/test_undefined_names.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,23 @@ def bar(self):
279279
__module__
280280
''', m.UndefinedName)
281281

282+
@skipIf(version_info < (3, 3), "Python >= 3.3 only")
283+
def test_magicQualnameInClassScope(self):
284+
"""
285+
Use of the C{__qualname__} magic builtin should not emit an undefined
286+
name warning if used in class scope.
287+
"""
288+
self.flakes('__qualname__', m.UndefinedName)
289+
self.flakes('''
290+
class Foo:
291+
__qualname__
292+
''')
293+
self.flakes('''
294+
class Foo:
295+
def bar(self):
296+
__qualname__
297+
''', m.UndefinedName)
298+
282299
def test_globalImportStar(self):
283300
"""Can't find undefined names with import *."""
284301
self.flakes('from fu import *; bar',

0 commit comments

Comments
 (0)