diff options
author | Claudiu Popa <cpopa@cloudbasesolutions.com> | 2015-06-25 17:49:23 +0300 |
---|---|---|
committer | Claudiu Popa <cpopa@cloudbasesolutions.com> | 2015-06-25 17:49:23 +0300 |
commit | 04f1ffd196a579e9af4acc277d49aff4f33be9f1 (patch) | |
tree | 25bd31b48c63f14c6e172d75a68ad8b83db4fc37 | |
parent | a0c8e5cfb2834040f49deaef3dea2f52e46f067b (diff) | |
download | pylint-04f1ffd196a579e9af4acc277d49aff4f33be9f1.tar.gz |
yield-outside-func is also emitted for `yield from`.
-rw-r--r-- | ChangeLog | 2 | ||||
-rw-r--r-- | pylint/checkers/base.py | 11 | ||||
-rw-r--r-- | pylint/test/functional/yield_from_outside_func.py | 4 | ||||
-rw-r--r-- | pylint/test/functional/yield_from_outside_func.rc | 2 | ||||
-rw-r--r-- | pylint/test/functional/yield_from_outside_func.txt | 1 |
5 files changed, 18 insertions, 2 deletions
@@ -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 |