Check keyword argument compatibility across class hierarchies

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

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

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
25/100
Loại issue
Tính năng
Độ rõ ràng
Cần làm rõ
Mức độ hoạt động
Đình trệ
Công nghệ
python
Lĩnh vực
compilers

Hướng nghiên cứu

Start by investigating mypy's class-hierarchy analysis and the separate type-checking pass proposed in the issue. Determine how keyword calls and all subclasses could be connected without producing errors for positional-only usage. Done means a considered implementation or design that reports incompatible overrides only when a keyword argument is used, with false-positive behavior addressed.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

feature needs discussion priority-2-low topic-inheritance

Currently mypy doesn't check that a method override has keyword argument names that are compatible with the overridden method. The reason is that a lot of code doesn't define these names consistently, and mypy would generate a ton of useless errors if it insisted on compatibility here. So mypy doesn't complain about this:

class A:
    def f(self, x): ...
class B(A):
    def f(self, xx): ...

However, this means that calls via keyword arguments can fail at runtime:

def f(a: A) -> None:
    a.f(x=1)  # Failure if a is an instance of B

f(B())

We could do better than this: if we call a method m of A using a keyword argument x, we could verify that all methods m in the class hierarchy below A define the keyword argument x. So we'd only enforce this if some code actually depends on the keyword argument name. Mypy would then give a list of all classes that have an incompatible definition of m and explain why the code could go wrong. If a method is always called using positional arguments only, no errors would be reported.

We might want to give a warning instead of an error for this, as it could generate false positives.

Implementing this would be fairly complicated as it would require access to all subclasses of a given class. In practice, this could happen in a separate type checking pass that happens after the entire program has been type checked, but I'm not really sure what's the best way to implement this and whether this is useful enough to implement at all.

Ngôn ngữ chính
Python
Star
20.6k
Fork
3.3k
Merge trung bình
1 ngày 18 giờ
Pull request đã merge (30 ngày)
54

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.

Issue khác của python/mypy

Tất cả issue của python/mypy

Issue tương tự

Thêm issue về Python

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.