summaryrefslogtreecommitdiff
path: root/pylint/checkers/variables.py
diff options
context:
space:
mode:
authorJacob Walls <jacobtylerwalls@gmail.com>2023-02-11 11:02:27 -0500
committerGitHub <noreply@github.com>2023-02-11 17:02:27 +0100
commita0b28f9019fabddd8ef428be75659082377abb4c (patch)
treef720a642977c85443b67888f76b27945557ef76e /pylint/checkers/variables.py
parentbd22f2822f9344487357c90e18a8505705c60a29 (diff)
downloadpylint-git-a0b28f9019fabddd8ef428be75659082377abb4c.tar.gz
Fix FP for used-before-assignment with assignment expressions in containers (#8253)
Diffstat (limited to 'pylint/checkers/variables.py')
-rw-r--r--pylint/checkers/variables.py37
1 files changed, 19 insertions, 18 deletions
diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py
index adb0faa15..bc4f887dc 100644
--- a/pylint/checkers/variables.py
+++ b/pylint/checkers/variables.py
@@ -115,6 +115,15 @@ DICT_TYPES = (
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
@@ -2139,17 +2148,7 @@ class VariablesChecker(BaseChecker):
# 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)
@@ -2245,13 +2244,15 @@ class VariablesChecker(BaseChecker):
"""
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):