Skip to content
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

Closed
wants to merge 10 commits into from
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/5327.false_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Don't emit ``used-before-assignment`` in pattern matching with a guard.

Closes #5327
5 changes: 5 additions & 0 deletions pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
)
from pylint.constants import (
PY39_PLUS,
PY310_PLUS,
TYPING_NEVER,
TYPING_NORETURN,
TYPING_TYPE_CHECKS_GUARDS,
Expand Down Expand Up @@ -1641,6 +1642,10 @@ def _check_consumer(
if not (
self._postponed_evaluation_enabled
and isinstance(stmt, (nodes.AnnAssign, nodes.FunctionDef))
) and not (
PY310_PLUS
and isinstance(stmt, nodes.Match)
and isinstance(node.parent, nodes.Compare)
Comment on lines +1698 to +1701
Copy link
Member

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...")

):
self.add_message(
"used-before-assignment",
Expand Down
1 change: 1 addition & 0 deletions pylint/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

PY38_PLUS = sys.version_info[:2] >= (3, 8)
PY39_PLUS = sys.version_info[:2] >= (3, 9)
PY310_PLUS = sys.version_info[:2] >= (3, 10)

IS_PYPY = platform.python_implementation() == "PyPy"

Expand Down