python / python/cpython

Support None as start/stop index?

オープン
#138,134 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

interpreter-core type-feature
主要言語
Python
スター
77.2k
フォーク
36k
PR マージ指標
PR 指標を取得中

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、slice メソッドとシーケンスメソッドについて記述されている現在の動作を確認し、次に issue #57549、#74121、#67275 を inspect.signature() と pydoc への影響と併せて読んでください。プロジェクトが None インデックスに関する明確で一貫した方針を決定し、文書化されたシグネチャと動作がその決定と一致した時点で、作業は完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
backend
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
30/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。