summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2020-06-09 22:02:48 +0200
committerGitHub <noreply@github.com>2020-06-09 22:02:48 +0200
commit1cf413ff986cf464202a07df60e7bd54695ed0e0 (patch)
tree65ecd70f5d6057047403122e9f01158038f22637
parentca821aacd3d27b118781de77463238aa58f1f3ca (diff)
downloadastroid-git-1cf413ff986cf464202a07df60e7bd54695ed0e0.tar.gz
`FunctionDef.is_generator` properly handles `yield` nodes in `If` tests (#799)
Close PyCQA/pylint#3583
-rw-r--r--ChangeLog4
-rw-r--r--astroid/node_classes.py5
-rw-r--r--tests/unittest_nodes.py13
3 files changed, 22 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index b407e6ed..5e6a8a03 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -19,6 +19,10 @@ Release Date: TBA
Close PyCQA/pylint#3639
+* `FunctionDef.is_generator` properly handles `yield` nodes in `If` tests
+
+ Close PyCQA/pylint#3583
+
What's New in astroid 2.4.2?
============================
diff --git a/astroid/node_classes.py b/astroid/node_classes.py
index 621dc5f2..8cc8585d 100644
--- a/astroid/node_classes.py
+++ b/astroid/node_classes.py
@@ -3508,6 +3508,11 @@ class If(mixins.MultiLineBlockMixin, mixins.BlockRangeMixIn, Statement):
def has_elif_block(self):
return len(self.orelse) == 1 and isinstance(self.orelse[0], If)
+ def _get_yield_nodes_skip_lambdas(self):
+ """An If node can contain a Yield node in the test"""
+ yield from self.test._get_yield_nodes_skip_lambdas()
+ yield from super()._get_yield_nodes_skip_lambdas()
+
class IfExp(NodeNG):
"""Class representing an :class:`ast.IfExp` node.
diff --git a/tests/unittest_nodes.py b/tests/unittest_nodes.py
index 07733e5f..5b6a39e3 100644
--- a/tests/unittest_nodes.py
+++ b/tests/unittest_nodes.py
@@ -1347,5 +1347,18 @@ def test_is_generator_for_yield_in_while():
assert bool(node.is_generator())
+def test_is_generator_for_yield_in_if():
+ code = """
+ import asyncio
+
+ def paused_iter(iterable):
+ if (yield from asyncio.sleep(0.01)):
+ pass
+ return
+ """
+ node = astroid.extract_node(code)
+ assert bool(node.is_generator())
+
+
if __name__ == "__main__":
unittest.main()