Skip to content

Commit

Permalink
Recognize __qualname__ when used in class scope (#588)
Browse files Browse the repository at this point in the history
Relates #259.
  • Loading branch information
pilou- committed Oct 12, 2020
1 parent dbb8fc4 commit 26cf063
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pyflakes/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,10 @@ def __init__(self, lineno, col_offset):
self.col_offset = col_offset


class DetectClassScopedMagic:
names = dir()


# Globally defined names which are not attributes of the builtins module, or
# are only present on some platforms.
_MAGIC_GLOBALS = ['__file__', '__builtins__', 'WindowsError']
Expand Down Expand Up @@ -1231,7 +1235,7 @@ def handleNodeLoad(self, node):
# the special name __path__ is valid only in packages
return

if name == '__module__' and isinstance(self.scope, ClassScope):
if name in DetectClassScopedMagic.names and isinstance(self.scope, ClassScope):
return

# protected with a NameError handler?
Expand Down
17 changes: 17 additions & 0 deletions pyflakes/test/test_undefined_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,23 @@ def bar(self):
__module__
''', m.UndefinedName)

@skipIf(version_info < (3, 3), "Python >= 3.3 only")
def test_magicQualnameInClassScope(self):
"""
Use of the C{__qualname__} magic builtin should not emit an undefined
name warning if used in class scope.
"""
self.flakes('__qualname__', m.UndefinedName)
self.flakes('''
class Foo:
__qualname__
''')
self.flakes('''
class Foo:
def bar(self):
__qualname__
''', m.UndefinedName)

def test_globalImportStar(self):
"""Can't find undefined names with import *."""
self.flakes('from fu import *; bar',
Expand Down

0 comments on commit 26cf063

Please sign in to comment.