python / python/mypy

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

Đang mở
#18,752 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

bug topic-error-reporting topic-match-statement
Ngôn ngữ chính
Python
Star
20.6k
Fork
3.3k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với bản tái hiện trong main.py và chạy mypy có và không có --allow-redefinition. Theo dõi tương tác giữa việc liên kết của except ... as và mẫu bắt giữ match xuất hiện sau đó cho exc. Hoàn thành khi bản tái hiện không còn báo lỗi về bắt giữ không tương thích hoặc biến đã bị xóa, kèm theo độ bao phủ hồi quy cho trường hợp này.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
tooling
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
45/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.