python / python/mypy

`super().__new__` inherited from Tuple doesn't match element type

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

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

needs discussion priority-1-normal
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ả

Hi, when I inherit from Tuple[float, float, float], and pass a tuple of floats as an argument to super().__new__, I get an error saying that an Iterable[_T_co] was expected instead. As per the typeshed stub, _T_co is supposed to be the type of the tuple elements, so I guess it's not getting deduced correctly. Further, this works with tuple.__new__, so the involvement of super() must be to blame and the type signatures indeed aren't identical.

from typing import Tuple, Type, TypeVar

CoordsT = TypeVar('CoordsT', bound='Coords')
class Coords(Tuple[float, float, float]):
    def __new__(cls: Type[CoordsT]) -> CoordsT:

        reveal_type(super().__new__)
        # Revealed type is 'def [_T] (cls: Type[_T`-1], iterable: typing.Iterable[_T_co`1] =) -> _T`-1'
        reveal_type(tuple.__new__)
        # Revealed type is 'def [_T_co, _T] (cls: Type[_T`-1], iterable: typing.Iterable[_T_co`1] =) -> _T`-1'

        value = (0.0, 0.0, 0.0)
        result = tuple.__new__(cls, value)
        # fine
        super().__new__(cls, value)
        # error: Argument 2 to "__new__" of "tuple" has incompatible type "Tuple[float, float, float]"; expected "Iterable[_T_co]"

        return result

I'm not sure if it's mypy or typeshed that is responsible for the function signature of super's __new__, but I didn't find an overload for it in typeshed, so I suspect this is mypy's remit

Edit: mypy 0.770, Python 3.7.3

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 reproducer và so sánh các kiểu được hiển thị của super().__new__tuple.__new__. Kiểm tra chữ ký trong builtins.pyi của typeshed được tham chiếu, sau đó theo dõi cách mypy xử lý lời gọi __new__ được kế thừa. Hoàn tất khi lời gọi super().__new__(cls, value) được hiển thị chấp nhận tuple các số thực mà không có lỗi kiểu đã báo cáo.

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
Khá rõ ràng
Mức phù hợp với người mới
35/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.