diff options
| author | Claudiu Popa <pcmanticore@gmail.com> | 2020-05-12 08:31:49 +0200 |
|---|---|---|
| committer | Claudiu Popa <pcmanticore@gmail.com> | 2020-05-12 08:47:41 +0200 |
| commit | 54b7a1118d76a645ef8efc48f9f082b53390fb46 (patch) | |
| tree | 7d2e83a51961c0642defa52dd9d577c4e4c7f154 | |
| parent | 27ebf1358546735dae188494627e026152825739 (diff) | |
| download | astroid-git-54b7a1118d76a645ef8efc48f9f082b53390fb46.tar.gz | |
`FunctionDef.is_generator` properly handles `yield` nodes in `While` tests
Close PyCQA/pylint#3519
| -rw-r--r-- | ChangeLog | 4 | ||||
| -rw-r--r-- | astroid/node_classes.py | 5 | ||||
| -rw-r--r-- | astroid/scoped_nodes.py | 2 | ||||
| -rw-r--r-- | tests/unittest_nodes.py | 12 |
4 files changed, 22 insertions, 1 deletions
@@ -6,6 +6,10 @@ What's New in astroid 2.4.2? ============================ Release Date: TBA +* `FunctionDef.is_generator` properly handles `yield` nodes in `While` tests + + Close PyCQA/pylint#3519 + What's New in astroid 2.4.1? ============================ diff --git a/astroid/node_classes.py b/astroid/node_classes.py index 647272be..f064944f 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -4500,6 +4500,11 @@ class While(mixins.MultiLineBlockMixin, mixins.BlockRangeMixIn, Statement): yield from self.body yield from self.orelse + def _get_yield_nodes_skip_lambdas(self): + """A While 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 With( mixins.MultiLineBlockMixin, diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index de2d151e..3cd7e723 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -1661,7 +1661,7 @@ class FunctionDef(mixins.MultiLineBlockMixin, node_classes.Statement, Lambda): :returns: True is this is a generator function, False otherwise. :rtype: bool """ - return next(self._get_yield_nodes_skip_lambdas(), False) + return bool(next(self._get_yield_nodes_skip_lambdas(), False)) def infer_call_result(self, caller=None, context=None): """Infer what the function returns when called. diff --git a/tests/unittest_nodes.py b/tests/unittest_nodes.py index 0e8863b8..07733e5f 100644 --- a/tests/unittest_nodes.py +++ b/tests/unittest_nodes.py @@ -1335,5 +1335,17 @@ def test_const_itered(): assert [elem.value for elem in itered] == list("string") +def test_is_generator_for_yield_in_while(): + code = """ + def paused_iter(iterable): + while True: + # Continue to yield the same item until `next(i)` or `i.send(False)` + while (yield value): + pass + """ + node = astroid.extract_node(code) + assert bool(node.is_generator()) + + if __name__ == "__main__": unittest.main() |
