GeezerVison is showing github.com — Support None as start/stop index? · Issue #138134 · python/cpython
Support None as start/stop index? #138134DescriptionNone 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
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
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).
- 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 Activitypicnixz commented on Aug 25, 2025 To understand the issue:
Sequence.index is meant to support None.
- Some sequence implementations don't support
None in their index() method.
- Some sequence implementations support
None in their index() method.
We have two plans:
- Make all
Sequence.index implementations support None.
- Remove existing support for None.
If we do 2), shouldn't we also revert Sequence.index supporting None? AFAIK, the examples in https://docs.python.org/3/library/collections.abc.html actually don't even show that Sequence.index must have 3 arguments despite https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence indicating that start and stop are now supported in index.
The source code however says Supporting start and stop arguments is optional, but recommended:
|
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 start and stop (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 for stop). 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 stop it doesn't matter because it's the last parameter, so whether we write x.index(i, None) or x.index(i, sys.maxsize), both can simply be x.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 being sys.maxsize for instance). serhiy-storchaka commented on Aug 25, 2025 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 None is 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. AA-Turner commented on Aug 25, 2025 I think I prefer option 2 (do not support None). It is supported in the [...] methods I believe as a result of slice(...), but for the various methods None has unclear intent, and as you say could hide an error with an unexpected None value.
I think it would be better to advance multisignature support than use None as a 'nice' default. We could also revisit inspect.UNREPRESENTABLE as a stop-gap for help/pydoc output, instead of changing the runtime behaviour.
A serhiy-storchaka commented on Feb 20, 2026
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|