Skip to content

Commit 54b7a11

Browse files
committed
FunctionDef.is_generator properly handles yield nodes in While tests
Close pylint-dev/pylint#3519
1 parent 27ebf13 commit 54b7a11

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ What's New in astroid 2.4.2?
66
============================
77
Release Date: TBA
88

9+
* `FunctionDef.is_generator` properly handles `yield` nodes in `While` tests
10+
11+
Close PyCQA/pylint#3519
12+
913

1014
What's New in astroid 2.4.1?
1115
============================

astroid/node_classes.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4500,6 +4500,11 @@ def get_children(self):
45004500
yield from self.body
45014501
yield from self.orelse
45024502

4503+
def _get_yield_nodes_skip_lambdas(self):
4504+
"""A While node can contain a Yield node in the test"""
4505+
yield from self.test._get_yield_nodes_skip_lambdas()
4506+
yield from super()._get_yield_nodes_skip_lambdas()
4507+
45034508

45044509
class With(
45054510
mixins.MultiLineBlockMixin,

astroid/scoped_nodes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1661,7 +1661,7 @@ def is_generator(self):
16611661
:returns: True is this is a generator function, False otherwise.
16621662
:rtype: bool
16631663
"""
1664-
return next(self._get_yield_nodes_skip_lambdas(), False)
1664+
return bool(next(self._get_yield_nodes_skip_lambdas(), False))
16651665

16661666
def infer_call_result(self, caller=None, context=None):
16671667
"""Infer what the function returns when called.

tests/unittest_nodes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,5 +1335,17 @@ def test_const_itered():
13351335
assert [elem.value for elem in itered] == list("string")
13361336

13371337

1338+
def test_is_generator_for_yield_in_while():
1339+
code = """
1340+
def paused_iter(iterable):
1341+
while True:
1342+
# Continue to yield the same item until `next(i)` or `i.send(False)`
1343+
while (yield value):
1344+
pass
1345+
"""
1346+
node = astroid.extract_node(code)
1347+
assert bool(node.is_generator())
1348+
1349+
13381350
if __name__ == "__main__":
13391351
unittest.main()

0 commit comments

Comments
 (0)