python / python/cpython

Support None as start/stop index?

Abierto
#138,134 4 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

interpreter-core type-feature
Lenguaje dominante
Python
Estrellas
77.2k
Forks
35.9k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza revisando el comportamiento actual descrito para los métodos de slice y de secuencia; después, lee las issues #57549, #74121 y #67275 junto con las implicaciones para inspect.signature() y pydoc. El trabajo estará completo cuando el proyecto tenga una política decidida y coherente para los índices None, y sus firmas y comportamiento documentados coincidan con esa decisión.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
backend
Tipo de issue
Nueva funcionalidad
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Necesita aclaración
Aptitud para principiantes
30/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.