summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteven Myint <git@stevenmyint.com>2018-01-19 14:23:13 -0800
committerSteven Myint <git@stevenmyint.com>2018-01-19 14:23:13 -0800
commitf5f7dcc316bfee8547689ca137446ce23d10dec8 (patch)
tree836640d1f2a8907e731abc369cee7c76c2ca4cac
parent8a1feac08dae2478e3f67ab4018af86ff4ec56f0 (diff)
downloadpyflakes-class-without-self.tar.gz
Support `__class__` without `self` in Python 3class-without-self
This fixes #223. I created this patch previously in: https://github.com/PyCQA/pyflakes/commit/83ab002d6fab2b717df91e854624c6fdfd9b9213 But I deleted that branch for some reason. I can't recall why I disliked it enough to delete it. But since I can't remember, I'll commit it to branch again for others to look at.
-rw-r--r--pyflakes/checker.py12
-rw-r--r--pyflakes/test/test_undefined_names.py18
2 files changed, 26 insertions, 4 deletions
diff --git a/pyflakes/checker.py b/pyflakes/checker.py
index baef833..9b39e6c 100644
--- a/pyflakes/checker.py
+++ b/pyflakes/checker.py
@@ -710,10 +710,14 @@ class Checker(object):
# try enclosing function scopes and global scope
for scope in self.scopeStack[-1::-1]:
- # only generators used in a class scope can access the names
- # of the class. this is skipped during the first iteration
- if in_generators is False and isinstance(scope, ClassScope):
- continue
+ if isinstance(scope, ClassScope):
+ if not PY2 and name == '__class__':
+ return
+ elif in_generators is False:
+ # only generators used in a class scope can access the
+ # names of the class. this is skipped during the first
+ # iteration
+ continue
try:
scope[name].used = (self.scope, node)
diff --git a/pyflakes/test/test_undefined_names.py b/pyflakes/test/test_undefined_names.py
index 3d19210..d53e529 100644
--- a/pyflakes/test/test_undefined_names.py
+++ b/pyflakes/test/test_undefined_names.py
@@ -799,6 +799,24 @@ class Test(TestCase):
any(lambda: id(y) for x in range(10))
''', m.UndefinedName)
+ def test_dunderClass(self):
+ """
+ `__class__` is defined in class scope under Python 3, but is not
+ in Python 2.
+ """
+ code = '''
+ class Test(object):
+ def __init__(self):
+ print(__class__.__name__)
+ self.x = 1
+
+ t = Test()
+ '''
+ if version_info < (3,):
+ self.flakes(code, m.UndefinedName)
+ else:
+ self.flakes(code)
+
class NameTests(TestCase):
"""