`mypy` does not infer correctly with a `__class__` override

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

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

Đánh giá

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

Hướng nghiên cứu

Chạy bản tái hiện mypy-play được liên kết trong issue và so sánh kết quả của reveal_type(foo([1])) với int như mong đợi. Theo dõi cách quá trình phân giải overload xử lý ReducedCovariantList khi Protocol của nó định nghĩa một thuộc tính __class__. Được xem là hoàn tất khi ví dụ suy luận ra int và một regression test bao quát trường hợp override.

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

Mô tả

bug

Bug Report

mypy does not infer correctly with a __class__ override.

To Reproduce

https://mypy-play.net/?gist=9e3ffebda54d02c5e68f45fe7cd33eb0

from typing import Any, Protocol, overload, reveal_type
from collections.abc import Iterator, Sequence
from typing_extensions import TypeVar

_T_co = TypeVar("_T_co", covariant=True)

class ReducedCovariantList(Protocol[_T_co]):
    @property
    def __class__(self) -> type[list[Any]]: ...
    def __iter__(self) -> Iterator[_T_co]: ...

@overload
def foo(x: ReducedCovariantList[str]) -> str: ...
@overload
def foo(x: Sequence[int]) -> int: ...

# ty gives Unknown; mypy gives str; pyright and pyrefly give int
reveal_type(foo([1]))
# if we remove the __class__ override, both ty and mypy give int

Expected Behaviour

reveal_type gives int

Actual Behavior

reveal_type gives str

Your Environment

  • Mypy version used: 2.3.0
  • Mypy command-line flags: -
  • Mypy configuration options from mypy.ini (and other config files): -
  • Python version used: 3.12
Ngôn ngữ chính
Python
Star
20.6k
Fork
3.3k
Merge trung bình
1 ngày 9 giờ
Pull request đã merge (30 ngày)
56

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.

Issue khác của python/mypy

Tất cả issue của python/mypy

Issue tương tự

Thêm issue về Python

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.