Skip to content

Commit 57916d5

Browse files
Remove unnecessary if statement in variable consumption checker (#5531)
Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent ffede32 commit 57916d5

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

pylint/checkers/variables.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,23 +1389,19 @@ def _check_consumer(
13891389
# class A:
13901390
# x = lambda attr: f + attr
13911391
# f = 42
1392-
if isinstance(frame, nodes.ClassDef) and node.name in frame.locals:
1393-
if isinstance(node.parent, nodes.Arguments):
1394-
if stmt.fromlineno <= defstmt.fromlineno:
1395-
# Doing the following is fine:
1396-
# class A:
1397-
# x = 42
1398-
# y = lambda attr=x: attr
1399-
self.add_message(
1400-
"used-before-assignment",
1401-
args=node.name,
1402-
node=node,
1403-
)
1404-
else:
1405-
self.add_message(
1406-
"undefined-variable", args=node.name, node=node
1407-
)
1408-
return (VariableVisitConsumerAction.CONSUME, found_nodes)
1392+
# We check lineno because doing the following is fine:
1393+
# class A:
1394+
# x = 42
1395+
# y = lambda attr: x + attr
1396+
if (
1397+
isinstance(frame, nodes.ClassDef)
1398+
and node.name in frame.locals
1399+
and stmt.fromlineno <= defstmt.fromlineno
1400+
):
1401+
self.add_message(
1402+
"used-before-assignment", args=node.name, node=node
1403+
)
1404+
14091405
elif current_consumer.scope_type == "lambda":
14101406
self.add_message("undefined-variable", args=node.name, node=node)
14111407
return (VariableVisitConsumerAction.CONSUME, found_nodes)

0 commit comments

Comments
 (0)