Support None as start/stop index?
Personne n'a encore pris cette issue.
- Langage dominant
- Python
- Étoiles
- 77.2k
- Forks
- 35.9k
- Métriques de merge des PR
- Métriques de PR en attente
Description
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:
- Declare official support of
Nonein methods which support it de-facto.inspect.signature()will support this. Also, add support ofNonein theindex()methods of other classes (this is very easy now). - Deprecate support of
Nonein methods which support it. This was perhaps an unintentional effect of using a particular private C API. We need support of multisignatures in theinspectandpydocmodules to get and show adequate signatures, but we need this fortuple.index()etc:(value, start=0, /) (value, start, stop, /)
cc @rhettinger
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par examiner le comportement actuel décrit pour les méthodes de slice et de séquence, puis lisez les issues #57549, #74121 et #67275 ainsi que les implications pour inspect.signature() et pydoc. Le travail sera terminé lorsque le projet disposera d’une politique définie et cohérente pour les indices None, et que ses signatures et son comportement documentés correspondront à cette décision.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- python
- Domaine
- backend
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- À clarifier
- Accessibilité débutants
- 30/100