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

False positive used-before-assignment in pattern matching with a guard. #5327

Closed
pgoy-dod opened this issue Nov 16, 2021 · 5 comments · Fixed by #7922
Closed

False positive used-before-assignment in pattern matching with a guard. #5327

pgoy-dod opened this issue Nov 16, 2021 · 5 comments · Fixed by #7922
Labels
C: used-before-assignment Issues related to 'used-before-assignment' check False Positive 🦟 A message is emitted but nothing is wrong with the code Help wanted 🙏 Outside help would be appreciated, good for new contributors Match case Needs PR This issue is accepted, sufficiently specified and now needs an implementation python 3.10
Milestone

Comments

@pgoy-dod
Copy link

Bug description

When using pattern matching with a guard, pylint claims that the variable the guard uses is used before assignment.

Example:

match ("example", "one"):
    case (x, y) if x == "example":  # <-- pylint complains about 'x' here
        print("woo!")
    case _:
        print("boo...")

Configuration

No response

Command used

pylint a.py

Pylint output

************* Module a
a.py:2:19: E0601: Using variable 'x' before assignment (used-before-assignment)

-------------------------------------
Your code has been rated at -10.00/10

Expected behavior

Pattern matching with guards is correctly parsed and supported.

Pylint version

pylint 2.11.1
astroid 2.8.4
Python 3.10.0 (default, Oct 13 2021, 06:45:00) [Clang 13.0.0 (clang-1300.0.29.3)]

OS / Environment

Mac OSX Big Sur 11.6 (20G165)

Additional dependencies

No response

@pgoy-dod pgoy-dod added Bug 🪲 Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Nov 16, 2021
@cdce8p cdce8p added C: used-before-assignment Issues related to 'used-before-assignment' check False Positive 🦟 A message is emitted but nothing is wrong with the code python 3.10 and removed Bug 🪲 Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Nov 17, 2021
@pilattebe
Copy link

This bug is still present in:

pylint 2.12.2
astroid 2.9.3
Python 3.10.2 (main, Feb  1 2022, 16:44:51) [Clang 12.0.5 (clang-1205.0.22.9)]

Here my example code

import sys
match sys.argv[1:]:         # vvvvvvvvv Using variable 'direction' before assignment
    case ["go", direction] if direction in ["up", "down", "left", "right"]:
        print(f"going {direction}")
    case command:
        print(f"unknown command: {' '.join(command)}")

@jborman-exos
Copy link

Also hitting this false-positive, is there a plan to include this in an upcoming release?

@spagh-eddie
Copy link
Contributor

I too suffer from this issue

@mthuurne
Copy link
Contributor

mthuurne commented Jul 1, 2022

I'm running into the same issue using:

pylint 2.14.4
astroid 2.11.6
Python 3.10.4 (main, Mar 26 2022, 21:00:27) [GCC]

My testcase:

def describe(i: int) -> str:
    match i:
        case n if n % 2 == 0:
            return "even"
        case _:
            return "odd"

@Pierre-Sassoulas Pierre-Sassoulas added the Needs PR This issue is accepted, sufficiently specified and now needs an implementation label Jul 1, 2022
@maujim
Copy link

maujim commented Aug 30, 2022

If the same variable is used as a match guard twice, pylint won't complain the second time.

l = [7, 7]
match l:
    case [_, n] if n >= 4: 		# E0601: Using variable 'n' before assignment
        print("hi")

t = (4, "abc")
match t:
    case (n, _) if n < 12:		# not triggered here
        print("hi")

If the line case (n, _) if n < 12: is changed to use x instead of n, the false positive triggers again.

version:

pylint 2.14.5
astroid 2.12.5
Python 3.10.6 (main, Aug  3 2022, 17:39:45) [GCC 12.1.1 20220730]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: used-before-assignment Issues related to 'used-before-assignment' check False Positive 🦟 A message is emitted but nothing is wrong with the code Help wanted 🙏 Outside help would be appreciated, good for new contributors Match case Needs PR This issue is accepted, sufficiently specified and now needs an implementation python 3.10
Projects
None yet
8 participants