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

used-before-def incorrectly flagged when using match/case with a walrus operator #14659

Closed
euresti opened this issue Feb 9, 2023 · 1 comment · Fixed by #14665
Closed

used-before-def incorrectly flagged when using match/case with a walrus operator #14659

euresti opened this issue Feb 9, 2023 · 1 comment · Fixed by #14665
Assignees
Labels
bug mypy got something wrong topic-match-statement Python 3.10's match statement topic-possibly-undefined possibly-undefined error code

Comments

@euresti
Copy link
Contributor

euresti commented Feb 9, 2023

Bug Report

When you use a walrus operator in a match statement mypy will complain about an used-before-def for any use of the variable after the first case. (It doesn't complain for the first case)

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.11&gist=f0900514987ceef340db8076b6319cd8

Expected Behavior

I'm pretty sure the code is fine and that there's no way the variable is actually undefined. I also realize that I can rewrite the code to be case 5 as value: to work around the issue. Also, I realize the example is trivial but imagine if the 5 was int().

Actual Behavior

main.py:6: error: Name "result" is used before definition [used-before-def]

Your Environment

  • Mypy version used: mypy 1.0.0 (compiled: yes)
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.11
@euresti euresti added the bug mypy got something wrong label Feb 9, 2023
@ilinum ilinum self-assigned this Feb 9, 2023
@ilinum ilinum added the topic-possibly-undefined possibly-undefined error code label Feb 9, 2023
@ilinum
Copy link
Collaborator

ilinum commented Feb 9, 2023

Thanks for reporting and providing a small example! You should expect the fix in the 1.0.1 release. You can follow along in #13685.

@AlexWaygood AlexWaygood added the topic-match-statement Python 3.10's match statement label Feb 9, 2023
JukkaL pushed a commit that referenced this issue Feb 10, 2023
#14665)

The subject needs to be processed outside of match statement and not as
part of the first branch.

Fixes #14659.
ilinum added a commit to ilinum/mypy that referenced this issue Feb 10, 2023
python#14665)

The subject needs to be processed outside of match statement and not as
part of the first branch.

Fixes python#14659.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-match-statement Python 3.10's match statement topic-possibly-undefined possibly-undefined error code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants