summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPilou <pierre-louis@libregerbil.fr>2020-10-13 00:34:19 +0200
committerGitHub <noreply@github.com>2020-10-12 15:34:19 -0700
commit26cf0631fe89f61d5b0ef8d6949676f051e35796 (patch)
treee08db578dad9e4602709e609236f840c9436071d
parentdbb8fc4152c7d8dc4f7d666df4171860679f4b47 (diff)
downloadpyflakes-26cf0631fe89f61d5b0ef8d6949676f051e35796.tar.gz
Recognize __qualname__ when used in class scope (#588)
Relates #259.
-rw-r--r--pyflakes/checker.py6
-rw-r--r--pyflakes/test/test_undefined_names.py17
2 files changed, 22 insertions, 1 deletions
diff --git a/pyflakes/checker.py b/pyflakes/checker.py
index d6d057e..0d65506 100644
--- a/pyflakes/checker.py
+++ b/pyflakes/checker.py
@@ -669,6 +669,10 @@ class DummyNode(object):
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']
@@ -1231,7 +1235,7 @@ class Checker(object):
# 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?
diff --git a/pyflakes/test/test_undefined_names.py b/pyflakes/test/test_undefined_names.py
index c952cbb..e0e628d 100644
--- a/pyflakes/test/test_undefined_names.py
+++ b/pyflakes/test/test_undefined_names.py
@@ -279,6 +279,23 @@ class Test(TestCase):
__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',