python / python/mypy

Overloaded generic constructors can cause spurious errors in class methods

Đang mở
#7,935 2 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 false-positive priority-1-normal topic-overloads topic-type-variables
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ả

This fails, while it shouldn't:

from typing import TypeVar, Generic, overload

T = TypeVar('T')

class C(Generic[T]):
    @overload
    def __new__(cls) -> C[None]: ...
    @overload
    def __new__(cls, item: T) -> C[T]: ...
    def __new__(cls, item=None):
        ...
    @classmethod
    def f(cls, x: T) -> T:
        return x

C.f(42)  # Argument 1 to "f" of "C" has incompatible type "int"; expected "None"

I think the same also will happen with __init__(). The problem is because the return type of the first overload is always passed as an instance type to analyze_class_attribute_access().

Note: enabling test case testGenericClassInGenericFunctionOverloadedConstructor is blocked on this.

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 tại analyze_class_attribute_access() và tái hiện trường hợp hàm khởi tạo generic bị overload được nêu trong issue. Điều tra lý do kiểu trả về của overload đầu tiên được sử dụng làm kiểu của instance, sau đó kiểm tra hành vi init liên quan. Bật testGenericClassInGenericFunctionOverloadedConstructor và xác nhận rằng lỗi class-method không chính đáng không còn xảy ra.

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
devtools
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
Đặc tả 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.