From a0e4eea8119f690c0f62219fd23e949e8bcbfdf6 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Sat, 26 Oct 2019 09:39:16 +0100 Subject: [PATCH] Ignore decorated functions and classes --- pyflakes/checker.py | 10 ++++++++-- pyflakes/test/test_other.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/pyflakes/checker.py b/pyflakes/checker.py index d79dad2c..28334d59 100644 --- a/pyflakes/checker.py +++ b/pyflakes/checker.py @@ -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) diff --git a/pyflakes/test/test_other.py b/pyflakes/test/test_other.py index 0063622d..3300880f 100644 --- a/pyflakes/test/test_other.py +++ b/pyflakes/test/test_other.py @@ -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): """ @@ -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):