python / python/mypy

"Invalid base class" when inheriting from tuple of classes

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

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

feature
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

Mô tả

I appreciate there are some complexities around handling inheritance from dynamic sources, so it's possible that this is a duplicate of one of those (#4284 seems most likely related), though I think this is a slightly different case.

I would like the following to pass type checking, yet I get an "Invalid base class" error even after adding an explicit type annotation.

Because in this case the tuple is of a fixed size (and the members of the tuple are all valid types) it feels like it should be possible to determine that it's valid to inherit from an expansion of that tuple.

# foo.py
from typing import Tuple, Type

class A: pass
class B: pass

PlainBases = (A, B)
reveal_type(PlainBases)
# ^ Revealed type is 'Tuple[def () -> foo.A, def () -> foo.B]'

class PlainC(*PlainBases):
# ^ Invalid base class
    pass

AnotatedBases = (A, B)  # type: Tuple[Type[A], Type[B]]
reveal_type(AnotatedBases)
# ^ Revealed type is 'Tuple[Type[foo.A], Type[foo.B]]'

class AnotatedC(*AnotatedBases):
# ^ Invalid base class
    pass

I'm using Python 3.5 and mypy 0.620. Having just updated to 0.641 I see that there are now also errors that "Can use starred expression only as assignment target" in the above, which goes some way to explaining why this is currently unsupported, though leads to another one (apologies if this is documented somewhere):
Why is it that mypy is unable to expand the types of this starred expression?

Is this a result of the issue relating to multiple passes which is mentioned in https://github.com/python/mypy/issues/4284#issuecomment-346908509 or something else?

Is this something which is possible/likely to be supported?

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 bằng cách tái hiện các ví dụ về base có dấu sao trong issue bằng các phiên bản mypy được tham chiếu, sau đó đọc phần thảo luận trong issue #4284 về nhiều lượt xử lý. Công việc chỉ hoàn tất khi cách xử lý dự kiến đối với các tuple cố định trong base của class được quyết định và triển khai, hoặc hành vi không được hỗ trợ được ghi rõ trong tài liệu cùng các kiểm tra thích hợp.

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