Skip to content

Commit 52576b7

Browse files
authored
Fix used-before-assignment for assignment expressions in lambda (#5530)
1 parent 2a69387 commit 52576b7

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ Release date: TBA
5050

5151
* ``used-before-assignment`` now checks names in try blocks.
5252

53+
* Fixed false positive with ``used-before-assignment`` for assignment expressions
54+
in lambda statements.
55+
56+
Closes #5360, #3877
57+
5358
* Some files in ``pylint.testutils`` were deprecated. In the future imports should be done from the
5459
``pylint.testutils.functional`` namespace directly.
5560

doc/whatsnew/2.13.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ Other Changes
8686

8787
* ``used-before-assignment`` now checks names in try blocks.
8888

89+
* Fixed false positive with ``used-before-assignment`` for assignment expressions
90+
in lambda statements.
91+
92+
Closes #5360, #3877
93+
8994
* Require Python ``3.6.2`` to run pylint.
9095

9196
Closes #5065

pylint/checkers/variables.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,10 +1402,6 @@ def _check_consumer(
14021402
"used-before-assignment", args=node.name, node=node
14031403
)
14041404

1405-
elif current_consumer.scope_type == "lambda":
1406-
self.add_message("undefined-variable", args=node.name, node=node)
1407-
return (VariableVisitConsumerAction.CONSUME, found_nodes)
1408-
14091405
elif self._is_only_type_assignment(node, defstmt):
14101406
self.add_message("undefined-variable", args=node.name, node=node)
14111407
return (VariableVisitConsumerAction.CONSUME, found_nodes)

tests/functional/u/undefined/undefined_variable_py38.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,14 @@ def no_parameters_in_function_default() -> None:
9797
COMPREHENSION_FIVE = {i: (else_assign_2 := i) if False else 0 for i in range(10)}
9898

9999
print(else_assign_2) # [undefined-variable]
100+
101+
102+
# Tests for assignment expressions in lambda statements
103+
104+
things = []
105+
sorted_things = sorted(
106+
things,
107+
key=lambda thing: x_0
108+
if (x_0 := thing.this_value) < (x_1 := thing.that_value)
109+
else x_1,
110+
)

0 commit comments

Comments
 (0)