Skip to content

Commit a0b28f9

Browse files
Fix FP for used-before-assignment with assignment expressions in containers (#8253)
1 parent bd22f28 commit a0b28f9

File tree

4 files changed

+32
-24
lines changed

4 files changed

+32
-24
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix false positive for ``used-before-assignment`` for named expressions
2+
appearing after the first element in a list, tuple, or set.
3+
4+
Closes #8252

pylint/checkers/variables.py

+19-18
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,15 @@
115115
astroid.nodes.node_classes.Dict,
116116
)
117117

118+
NODES_WITH_VALUE_ATTR = (
119+
nodes.Assign,
120+
nodes.AnnAssign,
121+
nodes.AugAssign,
122+
nodes.Expr,
123+
nodes.Return,
124+
nodes.Match,
125+
)
126+
118127

119128
class VariableVisitConsumerAction(Enum):
120129
"""Reported by _check_consumer() and its sub-methods to determine the
@@ -2139,17 +2148,7 @@ def _is_variable_violation(
21392148
# same line as the function definition
21402149
maybe_before_assign = False
21412150
elif (
2142-
isinstance(
2143-
defstmt,
2144-
(
2145-
nodes.Assign,
2146-
nodes.AnnAssign,
2147-
nodes.AugAssign,
2148-
nodes.Expr,
2149-
nodes.Return,
2150-
nodes.Match,
2151-
),
2152-
)
2151+
isinstance(defstmt, NODES_WITH_VALUE_ATTR)
21532152
and VariablesChecker._maybe_used_and_assigned_at_once(defstmt)
21542153
and frame is defframe
21552154
and defframe.parent_of(node)
@@ -2245,13 +2244,15 @@ def _maybe_used_and_assigned_at_once(defstmt: nodes.Statement) -> bool:
22452244
"""
22462245
if isinstance(defstmt, nodes.Match):
22472246
return any(case.guard for case in defstmt.cases)
2248-
if isinstance(defstmt.value, nodes.BaseContainer) and defstmt.value.elts:
2249-
# The assignment must happen as part of the first element
2250-
# e.g. "assert (x:= True), x"
2251-
# NOT "assert x, (x:= True)"
2252-
value = defstmt.value.elts[0]
2253-
else:
2254-
value = defstmt.value
2247+
if isinstance(defstmt, nodes.IfExp):
2248+
return True
2249+
if isinstance(defstmt.value, nodes.BaseContainer):
2250+
return any(
2251+
VariablesChecker._maybe_used_and_assigned_at_once(elt)
2252+
for elt in defstmt.value.elts
2253+
if isinstance(elt, NODES_WITH_VALUE_ATTR + (nodes.IfExp, nodes.Match))
2254+
)
2255+
value = defstmt.value
22552256
if isinstance(value, nodes.IfExp):
22562257
return True
22572258
if isinstance(value, nodes.Lambda) and isinstance(value.body, nodes.IfExp):

tests/functional/u/undefined/undefined_variable_py38.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,13 @@ def expression_in_ternary_operator_inside_container_tuple():
173173
return [(val3, val3) if (val3 := 'something') else 'anything']
174174

175175

176-
def expression_in_ternary_operator_inside_container_wrong_position():
177-
"""2-element list where named expression comes too late"""
178-
return [val3, val3 if (val3 := 'something') else 'anything'] # [used-before-assignment]
176+
def expression_in_ternary_operator_inside_container_later_position():
177+
"""
178+
Named expression follows unrelated item in container.
179+
180+
If 23 is replaced with `val3`, there is currently a false negative,
181+
but the false positive here is more important and likely to occur."""
182+
return [23, val3 if (val3 := 'something') else 'anything']
179183

180184

181185
# Self-referencing

tests/functional/u/undefined/undefined_variable_py38.txt

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@ undefined-variable:83:6:83:19::Undefined variable 'else_assign_1':INFERENCE
66
undefined-variable:106:6:106:19::Undefined variable 'else_assign_2':INFERENCE
77
used-before-assignment:141:10:141:16:type_annotation_used_improperly_after_comprehension:Using variable 'my_int' before assignment:HIGH
88
used-before-assignment:148:10:148:16:type_annotation_used_improperly_after_comprehension_2:Using variable 'my_int' before assignment:HIGH
9-
used-before-assignment:178:12:178:16:expression_in_ternary_operator_inside_container_wrong_position:Using variable 'val3' before assignment:HIGH
10-
used-before-assignment:182:9:182:10::Using variable 'z' before assignment:HIGH
11-
used-before-assignment:189:6:189:19::Using variable 'NEVER_DEFINED' before assignment:CONTROL_FLOW
9+
used-before-assignment:186:9:186:10::Using variable 'z' before assignment:HIGH
10+
used-before-assignment:193:6:193:19::Using variable 'NEVER_DEFINED' before assignment:CONTROL_FLOW

0 commit comments

Comments
 (0)