Skip to content

Commit d6f54c3

Browse files
committed
Recognize __module__ when used in class scope
Fixes #259.
1 parent 15a35c1 commit d6f54c3

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

pyflakes/checker.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,9 @@ def handleNodeLoad(self, node):
756756
# the special name __path__ is valid only in packages
757757
return
758758

759+
if name == '__module__' and isinstance(self.scope, ClassScope):
760+
return
761+
759762
# protected with a NameError handler?
760763
if 'NameError' not in self.exceptHandlers[-1]:
761764
self.report(messages.UndefinedName, node, name)

pyflakes/test/test_undefined_names.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,22 @@ def test_magicGlobalsPath(self):
256256
self.flakes('__path__', m.UndefinedName)
257257
self.flakes('__path__', filename='package/__init__.py')
258258

259+
def test_magicModuleInClassScope(self):
260+
"""
261+
Use of the C{__module__} magic builtin should not emit an undefined
262+
name warning if used in class scope.
263+
"""
264+
self.flakes('__module__', m.UndefinedName)
265+
self.flakes('''
266+
class Foo:
267+
__module__
268+
''')
269+
self.flakes('''
270+
class Foo:
271+
def bar(self):
272+
__module__
273+
''', m.UndefinedName)
274+
259275
def test_globalImportStar(self):
260276
"""Can't find undefined names with import *."""
261277
self.flakes('from fu import *; bar',

0 commit comments

Comments
 (0)