Disallow literal 0
step in slice expressions
#18065
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Follow up to #18063
Using a step of
0
when slicing a sequence is often a runtime error:This is true for standard library sequence types (tuple, list, str, memoryview, array.array, etc.), as well as for numpy arrays, pandas series, and many other third-party sequence types (including all that use
slice.indices
internally).Strictly speaking, creating a slice with a step of zero is possible, similar to how creating a slice with non-integer-like start/stop/step values is possible. However, just like non-integer start/stop/step values, this is usually a mistake (and one that will cause a runtime error) as opposed to an intended API. Mypy currently warns about non-integer start/stop/step values, so also warning about using a step of 0 seems consistent.
This PR is fairly liberal in what it will warn about: it disallows any expressions with a literal-
0
type (or unions including a literal0
) in the step position of a slice expression. Depending on how significant the mypy_primer hit is, I may relax this a bit.