Make abstract/dunder methods positional-only.
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 35.9k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
Feature or enhancement
It would be nice if certain abstract methods / dunder methods would be defined with positional-only arguments, so that when one subclasses them an appropriate name can be chosen, or they can be defined positional-only in the subclass.[^1]
Code sample in pyright playground[^1]
from typing import overload
from collections.abc import Sequence
class MySequence[T](Sequence[T]):
def __len__(self): return 0
@overload
def __getitem__(self, idx: int, /) -> T: ...
@overload
def __getitem__(self, idx: slice, /) -> Sequence[T]: ...
def __getitem__(self, idx: int | slice, /) -> T | Sequence[T]: # ❌ override
return MySequence[T]()
In some cases, the stubs already fake that the argument is positional-only, such as for Container.__contains__:
def __contains__(self, x: object, /) -> bool: ...intypeshed/stdlib/typing.pyidef __contains__(self, x):incpython/Lib/_collections_abc.py
Personally, I think only changing the stubs would be a pragmatic choice, but I was told by the maintainers that they believe this change should be done in Cpython first.
Backward Compatibility
-
Changing non-abstract methods can potentially break user code. For example, this is currently legal code at runtime
from collections.abc import Container. Container.__subclasshook__(C=list) # True -
Changing the signature of abstract methods can introduce type-checker errors Code sample in pyright playground[^1]
from collections.abc import Sequence def get(x: Sequence[float], index: int) -> float: return x.__getitem__(index=index)
With this in mind, I believe changing abstrac dunder methods to use positional only arguments should be relatively safe, as calling them directly with keyword is very uncommon, and at worst it will introduce type errors.
Personally, I'd also like so see methods like Container.__subclasshook__ use positional-only arguments, but I also understand if this is not feasible due to bc-conerns.
[^1]: Note that mypy special cases certain dunder methods, and yields different results than pyright on these examples
Has this already been discussed elsewhere?
https://github.com/python/typeshed/issues/14071
Linked PRs
- gh-136324
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 các định nghĩa được trích dẫn trong Lib/_collections_abc.py và typeshed/stdlib/typing.pyi, sau đó xem xét PR được liên kết gh-136324 và typeshed issue liên quan. Công việc được xem là hoàn tất khi các chữ ký đã thống nhất của các phương thức abstract và dunder được cập nhật trong CPython, đồng thời các mối lo ngại về khả năng tương thích ngược đã được nêu trong tài liệu được giải quyết.
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ó
- 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
- 25/100