From 04f1ffd196a579e9af4acc277d49aff4f33be9f1 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Thu, 25 Jun 2015 17:49:23 +0300 Subject: yield-outside-func is also emitted for `yield from`. --- ChangeLog | 2 ++ pylint/checkers/base.py | 11 +++++++++-- pylint/test/functional/yield_from_outside_func.py | 4 ++++ pylint/test/functional/yield_from_outside_func.rc | 2 ++ pylint/test/functional/yield_from_outside_func.txt | 1 + 5 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 pylint/test/functional/yield_from_outside_func.py create mode 100644 pylint/test/functional/yield_from_outside_func.rc create mode 100644 pylint/test/functional/yield_from_outside_func.txt diff --git a/ChangeLog b/ChangeLog index b122328..ca0d9fe 100644 --- a/ChangeLog +++ b/ChangeLog @@ -174,6 +174,8 @@ ChangeLog for Pylint * Don't emit undefined-all-variables for nodes which can't be inferred (YES nodes). + + * yield-outside-func is also emitted for `yield from`. diff --git a/pylint/checkers/base.py b/pylint/checkers/base.py index e787965..1ae2f53 100644 --- a/pylint/checkers/base.py +++ b/pylint/checkers/base.py @@ -358,8 +358,11 @@ class BasicErrorChecker(_BasicChecker): @check_messages('yield-outside-function') def visit_yield(self, node): - if not isinstance(node.frame(), (astroid.Function, astroid.Lambda)): - self.add_message('yield-outside-function', node=node) + self._check_yield_outside_func(node) + + @check_messages('yield-outside-function') + def visit_yieldfrom(self, node): + self._check_yield_outside_func(node) @check_messages('not-in-loop') def visit_continue(self, node): @@ -414,6 +417,10 @@ class BasicErrorChecker(_BasicChecker): args=(infered.name, ), node=node) + def _check_yield_outside_func(self, node): + if not isinstance(node.frame(), (astroid.Function, astroid.Lambda)): + self.add_message('yield-outside-function', node=node) + def _check_else_on_loop(self, node): """Check that any loop with an else clause has a break statement.""" if node.orelse and not _loop_exits_early(node): diff --git a/pylint/test/functional/yield_from_outside_func.py b/pylint/test/functional/yield_from_outside_func.py new file mode 100644 index 0000000..e2db90c --- /dev/null +++ b/pylint/test/functional/yield_from_outside_func.py @@ -0,0 +1,4 @@ +"""This is gramatically correct, but it's still a SyntaxError""" +yield from [1, 2] # [yield-outside-function] + +LAMBDA_WITH_YIELD = lambda: (yield from [1, 2]) diff --git a/pylint/test/functional/yield_from_outside_func.rc b/pylint/test/functional/yield_from_outside_func.rc new file mode 100644 index 0000000..28c99bc --- /dev/null +++ b/pylint/test/functional/yield_from_outside_func.rc @@ -0,0 +1,2 @@ +[testoptions] +min_pyver=3.3 diff --git a/pylint/test/functional/yield_from_outside_func.txt b/pylint/test/functional/yield_from_outside_func.txt new file mode 100644 index 0000000..fa3a7fc --- /dev/null +++ b/pylint/test/functional/yield_from_outside_func.txt @@ -0,0 +1 @@ +yield-outside-function:2::Yield outside function -- cgit v1.2.1