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

Two possibly-undefined bugs with match statements #15958

Closed
AlexWaygood opened this issue Aug 25, 2023 · 3 comments
Closed

Two possibly-undefined bugs with match statements #15958

AlexWaygood opened this issue Aug 25, 2023 · 3 comments
Labels
bug mypy got something wrong topic-match-statement Python 3.10's match statement topic-possibly-undefined possibly-undefined error code topic-reachability Detecting unreachable code

Comments

@AlexWaygood
Copy link
Member

AlexWaygood commented Aug 25, 2023

Bug Report

We're seeing two bugs relating to the possibly-undefined error code over at python/cpython#108454

To Reproduce

  1. Save the following code to a file repro.py
  2. Run mypy --enable-error-code=possibly-undefined repro.py
  3. Run mypy --enable-error-code=possibly-undefined repro.py --warn-unreachable
from typing_extensions import assert_never

x: int | str
match x:
    case str():
        f = 'foo'
    case int():
        f = 'bar'
    case _:
        assert_never(x)

print(f)

Expected Behavior

Neither invocation of mypy should result in any errors being emitted. The match statement is an exhaustive match, so there's no possibility that f could be undefined by the end of the match x statement.

Actual Behavior

mypy --enable-error-code=possibly-undefined repro.py produces this output, which seems like a false positive:

> mypy --enable-error-code=possibly-undefined repro.py
repro.py:12: error: Name "f" may be undefined  [possibly-undefined]
Found 1 error in 1 file (checked 1 source file)

What's particularly strange, however, is that adding --warn-unreachable makes the error go away:

> mypy --enable-error-code=possibly-undefined --warn-unreachable repro.py
Success: no issues found in 1 source file

It seems like there's two bugs here:

  1. The possibly-undefined error is a false positive
  2. Adding --warn-unreachable to the config reduces the number of possibly-undefined errors emitted by mypy, and it seems like it definitely shouldn't be doing that.

Cc. @ilinum for possibly-undefined expertise :)

Your Environment

  • Mypy version used: 1.5.1
  • Python version used: 3.11
@AlexWaygood AlexWaygood added bug mypy got something wrong topic-match-statement Python 3.10's match statement topic-reachability Detecting unreachable code topic-possibly-undefined possibly-undefined error code labels Aug 25, 2023
@AlexWaygood
Copy link
Member Author

Hmm, I guess bug (1) that I listed above is a duplicate of #13926. Bug (2) might be new, though :)

@AlexWaygood AlexWaygood changed the title Two possibly-undefined bugs with match statements Two possibly-undefined bugs with match statements Aug 25, 2023
@hauntsaninja
Copy link
Collaborator

Link for myself to pick back up with pdb: python/cpython#108454 (comment)

@AlexWaygood
Copy link
Member Author

Fixed in #15995

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 topic-reachability Detecting unreachable code
Projects
None yet
Development

No branches or pull requests

2 participants