Skip to content

Commit

Permalink
Ignore decorated functions and classes
Browse files Browse the repository at this point in the history
  • Loading branch information
adamchainz committed Nov 16, 2019
1 parent 8b92003 commit a0e4eea
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pyflakes/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1845,9 +1845,15 @@ def checkUnusedBindings():
for name, binding in self.scope.unusedBindings():
if isinstance(binding, Assignment):
self.report(messages.UnusedVariable, binding.source, name)
elif isinstance(binding, ClassDefinition):
elif (
isinstance(binding, ClassDefinition)
and not binding.source.decorator_list
):
self.report(messages.UnusedClass, binding.source, name)
elif isinstance(binding, FunctionDefinition):
elif (
isinstance(binding, FunctionDefinition)
and not binding.source.decorator_list
):
self.report(messages.UnusedFunction, binding.source, name)
self.deferAssignment(checkUnusedBindings)

Expand Down
28 changes: 28 additions & 0 deletions pyflakes/test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -1793,6 +1793,20 @@ def _():
pass
''')

def test_usedDecoratedFunction(self):
"""
Don't warn when the function is decorated because decorators can do
anything, like copy it into global state.
"""
self.flakes('''
from somewhere import decorator
def a():
@decorator
def b():
pass
''')


class TestUnusedClass(TestCase):
"""
Expand Down Expand Up @@ -1820,6 +1834,20 @@ class _:
pass
''')

def test_usedDecoratedClass(self):
"""
Don't warn when the class is decorated because decorators can do
anything, like copy it into global state.
"""
self.flakes('''
from somewhere import decorator
def a():
@decorator
class B:
pass
''')


class TestStringFormatting(TestCase):

Expand Down

0 comments on commit a0e4eea

Please sign in to comment.