python / python/cpython

Support None as start/stop index?

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

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

interpreter-core type-feature
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ả

None is accepted as start and stop index in slices. a[None:i] is equivalent to a[:i] and a[i:None] is equivalent to a[i:], independently from the type of the sequence. It is also accepted as the start or end index in the str, bytes and bytesrray methods index(), count(), startswith() and endswith(). This is not documented and officially supported feature. The documentation contains something like index(value[, start[, end]]), pydoc shows this as well, and inspect.signature() fails for these methods

The start and end indices in the tuple, list, memoryview, array and deque method index() only accept integers (including negative values). It was previously discussed in

  • #57549
  • #74121

and support of None was even merged, but then reverted.

Sequence.index() supports None since #67275 (although it seems there is a bug for start=None).

This inconsistency is just an annoying nuisance, except that it is difficult to express the default value for the stop index. For in-memory collections we can use sys.maxsize, but currently inspect and pydoc show it as a huge integer value:

>>> inspect.signature(tuple.index)
<Signature (self, value, start=0, stop=9223372036854775807, /)>

I think this is an argument to support None as the stop index (and as the start index for consistency). The possible drawback is that when None was the result of error, this error will no longer be detected.

For now, there were two open ways:

  1. Declare official support of None in methods which support it de-facto. inspect.signature() will support this. Also, add support of None in the index() methods of other classes (this is very easy now).
  2. Deprecate support of None in methods which support it. This was perhaps an unintentional effect of using a particular private C API. We need support of multisignatures in the inspect and pydoc modules to get and show adequate signatures, but we need this for tuple.index() etc:
    (value, start=0, /)
    (value, start, stop, /)
    

cc @rhettinger

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 việc xem xét hành vi hiện tại được mô tả cho các phương thức slice và sequence, sau đó đọc các issue #57549, #74121 và #67275 cùng với những ảnh hưởng đối với inspect.signature() và pydoc. Công việc hoàn tất khi dự án đã có một chính sách được quyết định và nhất quán đối với các chỉ mục None, đồng thời các signature và hành vi được ghi chép khớp với quyết định đó.

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
backend
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
Cần làm rõ
Mức phù hợp với người mới
30/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.