Disallow literal 0 step in slice expressions#18065
Draft
brianschubert wants to merge 3 commits intopython:masterfrom
Draft
Disallow literal 0 step in slice expressions#18065brianschubert wants to merge 3 commits intopython:masterfrom
0 step in slice expressions#18065brianschubert wants to merge 3 commits intopython:masterfrom
Conversation
This comment has been minimized.
This comment has been minimized.
Collaborator
Author
|
mypy_primer hits look good. All hits are from tests that assert that using a step of |
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: pandas (https://github.com/pandas-dev/pandas)
+ pandas/tests/series/accessors/test_list_accessor.py:100: error: Slice step cannot be 0 [misc]
+ pandas/tests/series/accessors/test_list_accessor.py:103: error: Slice step cannot be 0 [misc]
+ pandas/tests/series/accessors/test_list_accessor.py:118: error: Slice step cannot be 0 [misc]
+ pandas/tests/indexing/test_indexing.py:716: error: Slice step cannot be 0 [misc]
sympy (https://github.com/sympy/sympy)
+ sympy/sets/tests/test_fancysets.py:273: error: Slice step cannot be 0 [misc]
+ sympy/sets/tests/test_fancysets.py:279: error: Slice step cannot be 0 [misc]
|
Collaborator
Author
|
Thinking about this more, we probably want to move mypy away from this sort of hardcoded (and lint-y) checks. Now that slices are generic, I imagine they'll be better ways we can handle this soon. Marking this as a draft for now |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
0when 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.indicesinternally).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-
0type (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.