-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix false positive used-before-assignment
in pattern matching with a guard
#7793
Conversation
for more information, see https://pre-commit.ci
This comment has been minimized.
This comment has been minimized.
Any guidance on what would be a proper way of testing this? |
Pull Request Test Coverage Report for Build 3521229696
π - Coveralls |
This comment has been minimized.
This comment has been minimized.
Hey @i-likecheese , thank you for contributing. You can create a functional test using a configuration file like this so it's executed only for python 3.10. See the functional tests documentation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for tackling this false positive!
I'd prefer to solve it closer to the source instead of at the very last minute, so that the variables checker, which is already pretty hard to reason about, stays somewhat workable.
I suggest handling this case in _maybe_used_and_assigned_at_once
, perhaps like:
diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py
index 627e82ef0..7ad0c0913 100644
--- a/pylint/checkers/variables.py
+++ b/pylint/checkers/variables.py
@@ -1963,6 +1963,7 @@ class VariablesChecker(BaseChecker):
nodes.AugAssign,
nodes.Expr,
nodes.Return,
+ nodes.Match,
),
)
and VariablesChecker._maybe_used_and_assigned_at_once(defstmt)
@@ -2053,6 +2054,8 @@ class VariablesChecker(BaseChecker):
"""Check if `defstmt` has the potential to use and assign a name in the
same statement.
"""
+ 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"
) and not ( | ||
PY310_PLUS | ||
and isinstance(stmt, nodes.Match) | ||
and isinstance(node.parent, nodes.Compare) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't solve this false positive, because the .parent
is nodes.Name
:
match ("example", "one"):
case (x, y) if x: # <-- pylint complains about 'x' here
print("woo!")
case _:
print("boo...")
π€ According to the primer, this change has no effect on the checked open source code. π€π This comment was generated for commit be2bffe |
used-before-assignment
in pattern matching with a guard #5327used-before-assignment
in pattern matching with a guard
Superseded by #7922. |
Type of Changes
Description
Fix false positive
used-before-assignment
in pattern matching with a guard #5327Closes #5327