diff --git a/doc/whatsnew/fragments/8252.false_positive b/doc/whatsnew/fragments/8252.false_positive new file mode 100644 index 0000000000..2c449ac36d --- /dev/null +++ b/doc/whatsnew/fragments/8252.false_positive @@ -0,0 +1,4 @@ +Fix false positive for ``used-before-assignment`` for named expressions +appearing after the first element in a list, tuple, or set. + +Closes #8252 diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 6df00d01aa..76faf5cc8c 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -115,6 +115,15 @@ astroid.nodes.node_classes.Dict, ) +NODES_WITH_VALUE_ATTR = ( + nodes.Assign, + nodes.AnnAssign, + nodes.AugAssign, + nodes.Expr, + nodes.Return, + nodes.Match, +) + class VariableVisitConsumerAction(Enum): """Reported by _check_consumer() and its sub-methods to determine the @@ -2128,17 +2137,7 @@ def _is_variable_violation( # same line as the function definition maybe_before_assign = False elif ( - isinstance( - defstmt, - ( - nodes.Assign, - nodes.AnnAssign, - nodes.AugAssign, - nodes.Expr, - nodes.Return, - nodes.Match, - ), - ) + isinstance(defstmt, NODES_WITH_VALUE_ATTR) and VariablesChecker._maybe_used_and_assigned_at_once(defstmt) and frame is defframe and defframe.parent_of(node) @@ -2234,13 +2233,15 @@ def _maybe_used_and_assigned_at_once(defstmt: nodes.Statement) -> bool: """ if isinstance(defstmt, nodes.Match): return any(case.guard for case in defstmt.cases) - if isinstance(defstmt.value, nodes.BaseContainer) and defstmt.value.elts: - # The assignment must happen as part of the first element - # e.g. "assert (x:= True), x" - # NOT "assert x, (x:= True)" - value = defstmt.value.elts[0] - else: - value = defstmt.value + if isinstance(defstmt, nodes.IfExp): + return True + if isinstance(defstmt.value, nodes.BaseContainer): + return any( + VariablesChecker._maybe_used_and_assigned_at_once(elt) + for elt in defstmt.value.elts + if isinstance(elt, NODES_WITH_VALUE_ATTR + (nodes.IfExp, nodes.Match)) + ) + value = defstmt.value if isinstance(value, nodes.IfExp): return True if isinstance(value, nodes.Lambda) and isinstance(value.body, nodes.IfExp): diff --git a/tests/functional/u/undefined/undefined_variable_py38.py b/tests/functional/u/undefined/undefined_variable_py38.py index ef774e53a6..6fb543e800 100644 --- a/tests/functional/u/undefined/undefined_variable_py38.py +++ b/tests/functional/u/undefined/undefined_variable_py38.py @@ -173,9 +173,13 @@ def expression_in_ternary_operator_inside_container_tuple(): return [(val3, val3) if (val3 := 'something') else 'anything'] -def expression_in_ternary_operator_inside_container_wrong_position(): - """2-element list where named expression comes too late""" - return [val3, val3 if (val3 := 'something') else 'anything'] # [used-before-assignment] +def expression_in_ternary_operator_inside_container_later_position(): + """ + Named expression follows unrelated item in container. + + If 23 is replaced with `val3`, there is currently a false negative, + but the false positive here is more important and likely to occur.""" + return [23, val3 if (val3 := 'something') else 'anything'] # Self-referencing diff --git a/tests/functional/u/undefined/undefined_variable_py38.txt b/tests/functional/u/undefined/undefined_variable_py38.txt index 832d8dd11e..1674707a57 100644 --- a/tests/functional/u/undefined/undefined_variable_py38.txt +++ b/tests/functional/u/undefined/undefined_variable_py38.txt @@ -6,6 +6,5 @@ undefined-variable:83:6:83:19::Undefined variable 'else_assign_1':INFERENCE undefined-variable:106:6:106:19::Undefined variable 'else_assign_2':INFERENCE used-before-assignment:141:10:141:16:type_annotation_used_improperly_after_comprehension:Using variable 'my_int' before assignment:HIGH used-before-assignment:148:10:148:16:type_annotation_used_improperly_after_comprehension_2:Using variable 'my_int' before assignment:HIGH -used-before-assignment:178:12:178:16:expression_in_ternary_operator_inside_container_wrong_position:Using variable 'val3' before assignment:HIGH -used-before-assignment:182:9:182:10::Using variable 'z' before assignment:HIGH -used-before-assignment:189:6:189:19::Using variable 'NEVER_DEFINED' before assignment:CONTROL_FLOW +used-before-assignment:186:9:186:10::Using variable 'z' before assignment:HIGH +used-before-assignment:193:6:193:19::Using variable 'NEVER_DEFINED' before assignment:CONTROL_FLOW