How should we annotate cooperative inheritance?
Chưa có ai nhận issue này.
- 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ả
Consider annotating this example of cooperative multiple inheritance:
from typing import Any
class Hidden:
def __init__(self, z: float, **kwargs: Any):
super().__init__(**kwargs)
self.z = z
class Base:
def __init__(self, x: int, **kwargs: Any):
super().__init__(**kwargs)
self.x = x
class Inherited(Base):
def __init__(self, y: int, **kwargs: Any):
super().__init__(**kwargs)
self.y = y
class Final(Inherited, Hidden):
pass
MyPy gives errors because of the super-calls, which is addressed by https://github.com/python/mypy/issues/4001.
It would be nice for Inherited to ensure in its super-call that x has the right type, which is the topic of https://github.com/python/mypy/issues/4441 although it would also be nice for Final's constructor to only accept x, y, and z. I can't find any issue for that.
This example is done with __init__, but the problem can occur in any such “augmenting” method pattern. It is not restricted to __init__. This augmenting pattern always needs a top level method in some class. In __init__'s case, it's in object.
One idea is to have a special annotation on **kwargs, for example, typing.Collector? MyPy could then figure out the necessary arguments and their types in all derived classes. An inherited class would only be allowed to explicitly forward variables accepted by parents, but they should be able to forward anything. MyPy would make a best effort to ensure that all required arguments get in somehow.
I recognize that not everyone is a fan of cooperative MI, but I personally think it's quite beautiful. I would love to see MyPy handle it elegantly.
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu bằng cách đọc ví dụ về đa kế thừa hợp tác và các issue 4001 và 4441 được liên kết của mypy. Công việc này sẽ cần thống nhất hành vi chú thích và kiểm tra kiểu cho việc bổ sung phương thức, bao gồm các đối số hàm khởi tạo được Final chấp nhận; việc hoàn thành cần được chứng minh bằng các kiểm tra mypy cho ví dụ.
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
- compilers, devtools
- Loại issue
- Tính năng
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Cần làm rõ
- Mức phù hợp với người mới
- 25/100