Skip to content

Commit aa9c0e9

Browse files
committed
Improve _get_return_nodes_skip_functions algorithm
Return statements can only appear in multiline bodies (like function definitions), so there is no need to check other nodes for them.
1 parent 956d5fc commit aa9c0e9

File tree

2 files changed

+77
-11
lines changed

2 files changed

+77
-11
lines changed

astroid/node_classes.py

Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -651,11 +651,7 @@ def _get_name_nodes(self):
651651
yield matching
652652

653653
def _get_return_nodes_skip_functions(self):
654-
for child_node in self.get_children():
655-
if child_node.is_function:
656-
continue
657-
for matching in child_node._get_return_nodes_skip_functions():
658-
yield matching
654+
yield from ()
659655

660656
def _get_yield_nodes_skip_lambdas(self):
661657
for child_node in self.get_children():
@@ -2832,6 +2828,12 @@ def catch(self, exceptions): # pylint: disable=redefined-outer-name
28322828
return True
28332829
return False
28342830

2831+
def _get_return_nodes_skip_functions(self):
2832+
for child_node in self.body:
2833+
if child_node.is_function:
2834+
continue
2835+
yield from child_node._get_return_nodes_skip_functions()
2836+
28352837

28362838
class Exec(Statement):
28372839
"""Class representing the ``exec`` statement.
@@ -2982,6 +2984,17 @@ def _get_assign_nodes(self):
29822984
for child_node in self.orelse:
29832985
yield from child_node._get_assign_nodes()
29842986

2987+
def _get_return_nodes_skip_functions(self):
2988+
for child_node in self.body:
2989+
if child_node.is_function:
2990+
continue
2991+
yield from child_node._get_return_nodes_skip_functions()
2992+
2993+
for child_node in self.orelse:
2994+
if child_node.is_function:
2995+
continue
2996+
yield from child_node._get_return_nodes_skip_functions()
2997+
29852998

29862999
class AsyncFor(For):
29873000
"""Class representing an :class:`ast.AsyncFor` node.
@@ -3262,6 +3275,17 @@ def _get_assign_nodes(self):
32623275
for child_node in self.orelse:
32633276
yield from child_node._get_assign_nodes()
32643277

3278+
def _get_return_nodes_skip_functions(self):
3279+
for child_node in self.body:
3280+
if child_node.is_function:
3281+
continue
3282+
yield from child_node._get_return_nodes_skip_functions()
3283+
3284+
for child_node in self.orelse:
3285+
if child_node.is_function:
3286+
continue
3287+
yield from child_node._get_return_nodes_skip_functions()
3288+
32653289

32663290
class IfExp(NodeNG):
32673291
"""Class representing an :class:`ast.IfExp` node.
@@ -3673,12 +3697,6 @@ def get_children(self):
36733697
def _get_return_nodes_skip_functions(self):
36743698
yield self
36753699

3676-
for child_node in self.get_children():
3677-
if child_node.is_function:
3678-
continue
3679-
for matching in child_node._get_return_nodes_skip_functions():
3680-
yield matching
3681-
36823700

36833701
class Set(_BaseContainer):
36843702
"""Class representing an :class:`ast.Set` node.
@@ -3987,6 +4005,18 @@ def _get_assign_nodes(self):
39874005
for child_node in self.orelse:
39884006
yield from child_node._get_assign_nodes()
39894007

4008+
def _get_return_nodes_skip_functions(self):
4009+
for child_node in self.body:
4010+
if child_node.is_function:
4011+
continue
4012+
yield from child_node._get_return_nodes_skip_functions()
4013+
4014+
for child_node in self.orelse or ():
4015+
if child_node.is_function:
4016+
continue
4017+
for matching in child_node._get_return_nodes_skip_functions():
4018+
yield matching
4019+
39904020

39914021
class TryFinally(mixins.BlockRangeMixIn, Statement):
39924022
"""Class representing an :class:`ast.TryFinally` node.
@@ -4054,6 +4084,18 @@ def _get_assign_nodes(self):
40544084
for child_node in self.finalbody:
40554085
yield from child_node._get_assign_nodes()
40564086

4087+
def _get_return_nodes_skip_functions(self):
4088+
for child_node in self.body:
4089+
if child_node.is_function:
4090+
continue
4091+
yield from child_node._get_return_nodes_skip_functions()
4092+
4093+
for child_node in self.finalbody:
4094+
if child_node.is_function:
4095+
continue
4096+
for matching in child_node._get_return_nodes_skip_functions():
4097+
yield matching
4098+
40574099

40584100
class Tuple(_BaseContainer):
40594101
"""Class representing an :class:`ast.Tuple` node.
@@ -4252,6 +4294,18 @@ def _get_assign_nodes(self):
42524294
for child_node in self.orelse:
42534295
yield from child_node._get_assign_nodes()
42544296

4297+
def _get_return_nodes_skip_functions(self):
4298+
for child_node in self.body:
4299+
if child_node.is_function:
4300+
continue
4301+
yield from child_node._get_return_nodes_skip_functions()
4302+
4303+
for child_node in self.orelse:
4304+
if child_node.is_function:
4305+
continue
4306+
for matching in child_node._get_return_nodes_skip_functions():
4307+
yield matching
4308+
42554309

42564310
class With(mixins.BlockRangeMixIn, mixins.AssignTypeMixin, Statement):
42574311
"""Class representing an :class:`ast.With` node.
@@ -4313,6 +4367,12 @@ def _get_assign_nodes(self):
43134367
for child_node in self.body:
43144368
yield from child_node._get_assign_nodes()
43154369

4370+
def _get_return_nodes_skip_functions(self):
4371+
for child_node in self.body:
4372+
if child_node.is_function:
4373+
continue
4374+
yield from child_node._get_return_nodes_skip_functions()
4375+
43164376

43174377
class AsyncWith(With):
43184378
"""Asynchronous ``with`` built with the ``async`` keyword."""

astroid/scoped_nodes.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,6 +1598,12 @@ def _get_assign_nodes(self):
15981598
for child_node in self.body:
15991599
yield from child_node._get_assign_nodes()
16001600

1601+
def _get_return_nodes_skip_functions(self):
1602+
for child_node in self.body:
1603+
if child_node.is_function:
1604+
continue
1605+
yield from child_node._get_return_nodes_skip_functions()
1606+
16011607

16021608
class AsyncFunctionDef(FunctionDef):
16031609
"""Class representing an :class:`ast.FunctionDef` node.

0 commit comments

Comments
 (0)