python / python/mypy

False positive: Incompatible types in capture pattern (pattern captures type "Exception", variable has type <deleted>)

Open
#18,752 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-error-reporting topic-match-statement
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the main.py reproduction and run mypy with and without --allow-redefinition. Trace the interaction between the except ... as binding and the later match capture pattern for exc. Done means the reproduction no longer reports the incompatible capture or deleted-variable errors, with regression coverage for the case.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.