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: Incompatible types in capture pattern (pattern captures type "Exception", variable has type <deleted>) #18752

Open
Krismix1 opened this issue Mar 3, 2025 · 1 comment
Labels
bug mypy got something wrong topic-error-reporting How we report errors topic-match-statement Python 3.10's match statement

Comments

@Krismix1
Copy link

Krismix1 commented Mar 3, 2025

Bug Report
When trying to use a match pattern and bind to a variable with the same name as an except ... as bind, mypy starts returning an error.
If I remove the try/except block or don't try to attempt to reassign the variable, the issue goes away.

I've tried to search for other similar issues, but couldn't find much. Hope it's not a duplicate.

To Reproduce

def main() -> None:
    try:
        pass
    except Exception as exc:
        print(exc)

    result = Exception()  # this could for example be the result of `asyncio.gather(..., return_exceptions=True)`
    match result:
        case Exception() as exc:
            print("got exception", exc)
        case _:
            print("got something else", result)


main()

Gist URL: https://gist.github.com/mypy-play/4d9bc9f42141eb6093823efcbc0dafd7
Playground URL: https://mypy-play.net/?mypy=master&python=3.13&gist=4d9bc9f42141eb6093823efcbc0dafd7&flags=allow-redefinition
Tried with various versions of mypy (1.8.0, 1.15.0, master) and Python (3.10, 3.11, 3.13).
Tried with --allow-redefinition and without.

Expected Behavior

No error from mypy, at least when using --allow-redefinition.

Actual Behavior

main.py:9: error: Incompatible types in capture pattern (pattern captures type "Exception", variable has type <deleted>)  [misc]
main.py:10: error: Trying to read deleted variable "exc"  [misc]
Found 2 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.8.0, 1.15.0, master
  • Mypy command-line flags: --allow-redefinition
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.10, 3.11, 3.13
@Krismix1 Krismix1 added the bug mypy got something wrong label Mar 3, 2025
@A5rocks
Copy link
Collaborator

A5rocks commented Mar 4, 2025

I think this is just a bad error message compared to:

def main() -> None:
    try:
        pass
    except Exception as exc:
        print(exc)

    exc = Exception()  # E: Assignment to variable "exc" outside except: block

@A5rocks A5rocks added topic-match-statement Python 3.10's match statement topic-error-reporting How we report errors labels Mar 4, 2025
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-error-reporting How we report errors topic-match-statement Python 3.10's match statement
Projects
None yet
Development

No branches or pull requests

2 participants