Skip to content

Commit a0e4eea

Browse files
committed
Ignore decorated functions and classes
1 parent 8b92003 commit a0e4eea

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

pyflakes/checker.py

+8-2
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

+28
Original file line numberDiff line numberDiff line change
@@ -1793,6 +1793,20 @@ def _():
17931793
pass
17941794
''')
17951795

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

17971811
class TestUnusedClass(TestCase):
17981812
"""
@@ -1820,6 +1834,20 @@ class _:
18201834
pass
18211835
''')
18221836

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

18241852
class TestStringFormatting(TestCase):
18251853

0 commit comments

Comments
 (0)