mypy not flagging subclasses with incompatible constructors
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ả
reproducer
from typing import Type
class Foo:
def __init__(self, a):
self.a = a
class Bar(Foo):
def __init__(self):
super().__init__("Bar")
def takes_foo(foo: Type[Foo]):
x = foo("Foo")
takes_foo(Foo)
takes_foo(Bar)
observed behavior
mypy raises no errors, but the code raises a TypeError at runtime.
expected behavior
pep 484 states:
when
new_user()callsuser_class()this implies that all subclasses ofUsermust support this in their constructor signature...A type checker ought to flag violations of such assumptions, but by default constructor calls that match the constructor signature in the indicated base class (User in the example above) should be allowed.
My understanding of the above is that mypy should be raising an error on the Bar definition because its signature is incompatible with that of its superclass. More generally, I tend to be surprised when code that passes mypy raises a TypeError at runtime.
environment
master as of 2019-06-10
$ pipenv graph
mypy==0.710+dev.e2f31ed71bd1edd60bffc86d3fda9da15ba63b3d
- mypy-extensions [required: >=0.4.0,<0.5.0, installed: 0.4.1]
- typed-ast [required: >=1.4.0,<1.5.0, installed: 1.4.0]
$ pipenv run python --version
Python 3.7.3
Thank you all for an amazing tool!
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 với reproducer trong issue và so sánh hành vi của nó với hướng dẫn về constructor và class-object trong PEP 484. Điều tra cách mypy xử lý các chữ ký constructor của lớp con và xác định liệu kết quả mong đợi là lỗi trên Bar hay tại takes_foo(Bar). Được xem là hoàn tất khi hành vi này được kiểm thử bằng một regression test và checker báo cáo đúng incompatibility dự kiến.
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
- 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