Skip to content

Commit 52b032c

Browse files
author
exarkun
committed
Merge pyflakes-class-decorator-2863
Author: exarkun Reviewer: mithrandi Fixes: #2863 Add support for recognizing names used with the class decorator syntax, both so that a name used in such a way is recognized as used (ie, not unused) and so that undefined names used in such a way are reported as such.
1 parent 659e40f commit 52b032c

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

pyflakes/checker.py

+9
Original file line numberDiff line numberDiff line change
@@ -328,14 +328,23 @@ def addArgs(arglist):
328328

329329
self.defer(runFunction)
330330

331+
331332
def CLASS(self, node):
333+
"""
334+
Check names used in a class definition, including its decorators, base
335+
classes, and the body of its definition. Additionally, add its name to
336+
the current scope.
337+
"""
338+
if getattr(node, "decorators", None) is not None:
339+
self.handleChildren(node.decorators)
332340
self.addBinding(node.lineno, Assignment(node.name, node))
333341
for baseNode in node.bases:
334342
self.handleNode(baseNode)
335343
self.pushClassScope()
336344
self.handleChildren(node.code)
337345
self.popScope()
338346

347+
339348
def ASSNAME(self, node):
340349
if node.flags == 'OP_DELETE':
341350
if isinstance(self.scope, FunctionScope) and node.name in self.scope.globals:

pyflakes/test/test_imports.py

+35
Original file line numberDiff line numberDiff line change
@@ -510,3 +510,38 @@ def f():
510510
def f():
511511
return "hello"
512512
''', m.UndefinedName)
513+
514+
515+
class Python26Tests(harness.Test):
516+
"""
517+
Tests for checking of syntax which is valid in PYthon 2.6 and newer.
518+
"""
519+
if version_info < (2, 6):
520+
skip = "Python 2.6 required for class decorator tests."
521+
522+
523+
def test_usedAsClassDecorator(self):
524+
"""
525+
Using an imported name as a class decorator results in no warnings,
526+
but using an undefined name as a class decorator results in an
527+
undefined name warning.
528+
"""
529+
self.flakes('''
530+
from interior import decorate
531+
@decorate
532+
class foo:
533+
pass
534+
''')
535+
536+
self.flakes('''
537+
from interior import decorate
538+
@decorate("foo")
539+
class bar:
540+
pass
541+
''')
542+
543+
self.flakes('''
544+
@decorate
545+
class foo:
546+
pass
547+
''', m.UndefinedName)

0 commit comments

Comments
 (0)