python / python/mypy

mypy not flagging subclasses with incompatible constructors

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

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

feature needs discussion priority-1-normal topic-inheritance
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() calls user_class() this implies that all subclasses of User must 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

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 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

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.