Skip to content

Commit 45dede4

Browse files
committed
Improve _get_yield_nodes_skip_lambdas algorithm
Yield statements can only appear in multiline bodies (like function definitions) and in lambdas, so there is no need to check other nodes for them.
1 parent aa9c0e9 commit 45dede4

File tree

2 files changed

+84
-11
lines changed

2 files changed

+84
-11
lines changed

astroid/node_classes.py

Lines changed: 72 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -654,11 +654,7 @@ def _get_return_nodes_skip_functions(self):
654654
yield from ()
655655

656656
def _get_yield_nodes_skip_lambdas(self):
657-
for child_node in self.get_children():
658-
if child_node.is_lambda:
659-
continue
660-
for matching in child_node._get_yield_nodes_skip_lambdas():
661-
yield matching
657+
yield from ()
662658

663659
def _infer_name(self, frame, name):
664660
# overridden for ImportFrom, Import, Global, TryExcept and Arguments
@@ -2708,6 +2704,10 @@ def postinit(self, value=None):
27082704
def get_children(self):
27092705
yield self.value
27102706

2707+
def _get_yield_nodes_skip_lambdas(self):
2708+
if not self.value.is_lambda:
2709+
yield from self.value._get_yield_nodes_skip_lambdas()
2710+
27112711

27122712
class Ellipsis(NodeNG): # pylint: disable=redefined-builtin
27132713
"""Class representing an :class:`ast.Ellipsis` node.
@@ -2834,6 +2834,12 @@ def _get_return_nodes_skip_functions(self):
28342834
continue
28352835
yield from child_node._get_return_nodes_skip_functions()
28362836

2837+
def _get_yield_nodes_skip_lambdas(self):
2838+
for child_node in self.body:
2839+
if child_node.is_lambda:
2840+
continue
2841+
yield from child_node._get_yield_nodes_skip_lambdas()
2842+
28372843

28382844
class Exec(Statement):
28392845
"""Class representing the ``exec`` statement.
@@ -2995,6 +3001,17 @@ def _get_return_nodes_skip_functions(self):
29953001
continue
29963002
yield from child_node._get_return_nodes_skip_functions()
29973003

3004+
def _get_yield_nodes_skip_lambdas(self):
3005+
for child_node in self.body:
3006+
if child_node.is_lambda:
3007+
continue
3008+
yield from child_node._get_yield_nodes_skip_lambdas()
3009+
3010+
for child_node in self.orelse:
3011+
if child_node.is_lambda:
3012+
continue
3013+
yield from child_node._get_yield_nodes_skip_lambdas()
3014+
29983015

29993016
class AsyncFor(For):
30003017
"""Class representing an :class:`ast.AsyncFor` node.
@@ -3286,6 +3303,17 @@ def _get_return_nodes_skip_functions(self):
32863303
continue
32873304
yield from child_node._get_return_nodes_skip_functions()
32883305

3306+
def _get_yield_nodes_skip_lambdas(self):
3307+
for child_node in self.body:
3308+
if child_node.is_lambda:
3309+
continue
3310+
yield from child_node._get_yield_nodes_skip_lambdas()
3311+
3312+
for child_node in self.orelse:
3313+
if child_node.is_lambda:
3314+
continue
3315+
yield from child_node._get_yield_nodes_skip_lambdas()
3316+
32893317

32903318
class IfExp(NodeNG):
32913319
"""Class representing an :class:`ast.IfExp` node.
@@ -4017,6 +4045,17 @@ def _get_return_nodes_skip_functions(self):
40174045
for matching in child_node._get_return_nodes_skip_functions():
40184046
yield matching
40194047

4048+
def _get_yield_nodes_skip_lambdas(self):
4049+
for child_node in self.body:
4050+
if child_node.is_lambda:
4051+
continue
4052+
yield from child_node._get_yield_nodes_skip_lambdas()
4053+
4054+
for child_node in self.orelse:
4055+
if child_node.is_lambda:
4056+
continue
4057+
yield from child_node._get_yield_nodes_skip_lambdas()
4058+
40204059

40214060
class TryFinally(mixins.BlockRangeMixIn, Statement):
40224061
"""Class representing an :class:`ast.TryFinally` node.
@@ -4096,6 +4135,17 @@ def _get_return_nodes_skip_functions(self):
40964135
for matching in child_node._get_return_nodes_skip_functions():
40974136
yield matching
40984137

4138+
def _get_yield_nodes_skip_lambdas(self):
4139+
for child_node in self.body:
4140+
if child_node.is_lambda:
4141+
continue
4142+
yield from child_node._get_yield_nodes_skip_lambdas()
4143+
4144+
for child_node in self.finalbody:
4145+
if child_node.is_lambda:
4146+
continue
4147+
yield from child_node._get_yield_nodes_skip_lambdas()
4148+
40994149

41004150
class Tuple(_BaseContainer):
41014151
"""Class representing an :class:`ast.Tuple` node.
@@ -4306,6 +4356,17 @@ def _get_return_nodes_skip_functions(self):
43064356
for matching in child_node._get_return_nodes_skip_functions():
43074357
yield matching
43084358

4359+
def _get_yield_nodes_skip_lambdas(self):
4360+
for child_node in self.body:
4361+
if child_node.is_lambda:
4362+
continue
4363+
yield from child_node._get_yield_nodes_skip_lambdas()
4364+
4365+
for child_node in self.orelse:
4366+
if child_node.is_lambda:
4367+
continue
4368+
yield from child_node._get_yield_nodes_skip_lambdas()
4369+
43094370

43104371
class With(mixins.BlockRangeMixIn, mixins.AssignTypeMixin, Statement):
43114372
"""Class representing an :class:`ast.With` node.
@@ -4373,6 +4434,12 @@ def _get_return_nodes_skip_functions(self):
43734434
continue
43744435
yield from child_node._get_return_nodes_skip_functions()
43754436

4437+
def _get_yield_nodes_skip_lambdas(self):
4438+
for child_node in self.body:
4439+
if child_node.is_lambda:
4440+
continue
4441+
yield from child_node._get_yield_nodes_skip_lambdas()
4442+
43764443

43774444
class AsyncWith(With):
43784445
"""Asynchronous ``with`` built with the ``async`` keyword."""
@@ -4407,12 +4474,6 @@ def get_children(self):
44074474
def _get_yield_nodes_skip_lambdas(self):
44084475
yield self
44094476

4410-
for child_node in self.get_children():
4411-
if child_node.is_function_or_lambda:
4412-
continue
4413-
for matching in child_node._get_yield_nodes_skip_lambdas():
4414-
yield matching
4415-
44164477

44174478
class YieldFrom(Yield):
44184479
"""Class representing an :class:`ast.YieldFrom` node."""

astroid/scoped_nodes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,6 +1604,12 @@ def _get_return_nodes_skip_functions(self):
16041604
continue
16051605
yield from child_node._get_return_nodes_skip_functions()
16061606

1607+
def _get_yield_nodes_skip_lambdas(self):
1608+
for child_node in self.body:
1609+
if child_node.is_lambda:
1610+
continue
1611+
yield from child_node._get_yield_nodes_skip_lambdas()
1612+
16071613

16081614
class AsyncFunctionDef(FunctionDef):
16091615
"""Class representing an :class:`ast.FunctionDef` node.
@@ -2714,6 +2720,12 @@ def _get_assign_nodes(self):
27142720
for child_node in self.body:
27152721
yield from child_node._get_assign_nodes()
27162722

2723+
def _get_yield_nodes_skip_lambdas(self):
2724+
for child_node in self.body:
2725+
if child_node.is_lambda:
2726+
continue
2727+
yield from child_node._get_yield_nodes_skip_lambdas()
2728+
27172729

27182730
# Backwards-compatibility aliases
27192731
Class = util.proxy_alias('Class', ClassDef)

0 commit comments

Comments
 (0)