Repository navigation
Support None as start/stop index? #138134
Description
Activity
- addedtype-featureA feature request or enhancementA feature request or enhancementinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
on Aug 25, 2025 To understand the issue:
Sequence.indexis meant to support None.- Some sequence implementations don't support
Nonein their index() method. - Some sequence implementations support
Nonein their index() method.
We have two plans:
- Make all
Sequence.indeximplementations supportNone. - Remove existing support for None.
If we do 2), shouldn't we also revert
Sequence.indexsupporting None? AFAIK, the examples in https://docs.python.org/3/library/collections.abc.html actually don't even show thatSequence.indexmust have 3 arguments despite https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence indicating thatstartandstopare now supported inindex.The source code however says
Supporting start and stop arguments is optional, but recommended:cpython/Lib/_collections_abc.py
Lines 1047 to 1052 in 7e4afe0
def index(self, value, start=0, stop=None): '''S.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present. Supporting start and stop arguments is optional, but recommended. AFAICT, there is no strict guidelines on the meaning of
startandstop(though we're full of incompatible signature violations, e.g., tuple is not a subtype of Sequence in the strict sense as it doesn't accept None forstop). Should we support them to be a sequence or not?
Personally, I'm in favor of supporting None because it could simplify the rendered signature and makes it simpler to use. However, one could argue that for
stopit doesn't matter because it's the last parameter, so whether we writex.index(i, None)orx.index(i, sys.maxsize), both can simply bex.index(i). What's interesting however is that it allows consumers to be simplified as they won't need to handle a None end themselves (and they could avoid using a default beingsys.maxsizefor instance).Note that support of start/stop arguments in
Sequence.index()is recent change and it was breaking. Not all sequences support them, and very few of them support them as keyword arguments.Adding support of
Noneis very easy in CPython code. It is a little more in user code, but I do not propose to force this, and it is dwarfed by the need to support negative and out of range values.I think I prefer option 2 (do not support None). It is supported in the
[...]methods I believe as a result ofslice(...), but for the various methodsNonehas unclear intent, and as you say could hide an error with an unexpectedNonevalue.I think it would be better to advance multisignature support than use
Noneas a 'nice' default. We could also revisitinspect.UNREPRESENTABLEas a stop-gap forhelp/pydocoutput, instead of changing the runtime behaviour.A
Other discussion (opened 1.5 years before this issue): https://discuss.python.org/t/none-as-index/50825/
Noneis accepted as start and stop index in slices.a[None:i]is equivalent toa[:i]anda[i:None]is equivalent toa[i:], independently from the type of the sequence. It is also accepted as the start or end index in thestr,bytesandbytesrraymethodsindex(),count(),startswith()andendswith(). This is not documented and officially supported feature. The documentation contains something likeindex(value[, start[, end]]),pydocshows this as well, andinspect.signature()fails for these methodsThe start and end indices in the
tuple,list,memoryview,arrayanddequemethodindex()only accept integers (including negative values). It was previously discussed inand support of
Nonewas even merged, but then reverted.Sequence.index()supportsNonesince #67275 (although it seems there is a bug forstart=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 currentlyinspectandpydocshow 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
Noneas the stop index (and as thestartindex for consistency). The possible drawback is that whenNonewas the result of error, this error will no longer be detected.For now, there were two open ways:
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).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:cc @rhettinger