Skip to content

Commit 8d3dbb8

Browse files
committed
Ignore decorated functions and classes
1 parent 8556e82 commit 8d3dbb8

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

pyflakes/checker.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1845,9 +1845,15 @@ def checkUnusedBindings():
18451845
for name, binding in self.scope.unusedBindings():
18461846
if isinstance(binding, Assignment):
18471847
self.report(messages.UnusedVariable, binding.source, name)
1848-
elif isinstance(binding, ClassDefinition):
1848+
elif (
1849+
isinstance(binding, ClassDefinition)
1850+
and not binding.source.decorator_list
1851+
):
18491852
self.report(messages.UnusedClass, binding.source, name)
1850-
elif isinstance(binding, FunctionDefinition):
1853+
elif (
1854+
isinstance(binding, FunctionDefinition)
1855+
and not binding.source.decorator_list
1856+
):
18511857
self.report(messages.UnusedFunction, binding.source, name)
18521858
self.deferAssignment(checkUnusedBindings)
18531859

pyflakes/test/test_other.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,6 +1791,20 @@ def _():
17911791
pass
17921792
''')
17931793

1794+
def test_usedDecoratedFunction(self):
1795+
"""
1796+
Don't warn when the function is decorated because decorators can do
1797+
anything, like copy it into global state.
1798+
"""
1799+
self.flakes('''
1800+
from somewhere import decorator
1801+
1802+
def a():
1803+
@decorator
1804+
def b():
1805+
pass
1806+
''')
1807+
17941808

17951809
class TestUnusedClass(TestCase):
17961810
"""
@@ -1818,6 +1832,20 @@ class _:
18181832
pass
18191833
''')
18201834

1835+
def test_usedDecoratedClass(self):
1836+
"""
1837+
Don't warn when the class is decorated because decorators can do
1838+
anything, like copy it into global state.
1839+
"""
1840+
self.flakes('''
1841+
from somewhere import decorator
1842+
1843+
def a():
1844+
@decorator
1845+
class B:
1846+
pass
1847+
''')
1848+
18211849

18221850
class TestStringFormatting(TestCase):
18231851

0 commit comments

Comments
 (0)