Skip to content

Rely on typeshed/stubs for slice typing#21401

Open
ilevkivskyi wants to merge 2 commits intopython:masterfrom
ilevkivskyi:enable-slice
Open

Rely on typeshed/stubs for slice typing#21401
ilevkivskyi wants to merge 2 commits intopython:masterfrom
ilevkivskyi:enable-slice

Conversation

@ilevkivskyi
Copy link
Copy Markdown
Member

Fixes #2410

slice has been generic in typeshed for more than a year now. Also all builtin collections have precise slice[SupportsIndex | None] types for __getitem__(). So, I don't think we need to be "policing" slice anymore, it should be really responsibility of the stub maintainers now.

After some searching, I see there are still many plain slice annotations in the wild, so this will be a trade-off of false positives to false negatives, but I think this is a right trade-off, since status quo causes issues for pandas dataframes and similar APIs.

I also considered some "intermediate" solutions, where we would still enforce SupportsIndex in some situations, but can't find anything that wouldn't be overly complicated.

@ilevkivskyi
Copy link
Copy Markdown
Member Author

cc @Dr-Irv

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 2, 2026

Diff from mypy_primer, showing the effect of this PR on open source code:

sympy (https://github.com/sympy/sympy)
+ sympy/stats/stochastic_process_types.py:1255: error: Unused "type: ignore" comment  [unused-ignore]

pandas (https://github.com/pandas-dev/pandas)
+ pandas/tests/indexes/string/test_indexing.py:152: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:153: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:154: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:155: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:156: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:157: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:159: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:160: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:161: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:162: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:163: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:164: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:165: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:166: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:167: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/copy_view/test_indexing.py:410: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/copy_view/test_indexing.py:478: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/copy_view/test_indexing.py:480: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/copy_view/test_indexing.py:482: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/copy_view/test_indexing.py:485: error: Unused "type: ignore" comment  [unused-ignore]

xarray (https://github.com/pydata/xarray)
+ xarray/tests/test_dataarray.py:1327: error: Unused "type: ignore" comment  [unused-ignore]
+ xarray/tests/test_dataarray.py:1346: error: Unused "type: ignore" comment  [unused-ignore]

freqtrade (https://github.com/freqtrade/freqtrade)
- freqtrade/strategy/strategy_helper.py:107: error: Slice index must be an integer, SupportsIndex or None  [misc]
- freqtrade/strategy/strategy_helper.py:108: error: Slice index must be an integer, SupportsIndex or None  [misc]
- freqtrade/data/btanalysis/trade_parallelism.py:119: error: Slice index must be an integer, SupportsIndex or None  [misc]
- freqtrade/data/btanalysis/trade_parallelism.py:120: error: Slice index must be an integer, SupportsIndex or None  [misc]
- freqtrade/data/btanalysis/trade_parallelism.py:135: error: Slice index must be an integer, SupportsIndex or None  [misc]
- freqtrade/data/btanalysis/trade_parallelism.py:136: error: Invalid index type "tuple[slice[Timestamp, str | bytes | date | timedelta | datetime64[date | int | None] | <7 more items> | None, None], str]" for "_LocIndexerFrame[DataFrame]"; expected type "tuple[int | str | str_ | Timestamp | tuple[str | bytes | date | datetime | timedelta | <7 more items> | complex | integer[Any] | floating[Any] | complexfloating[Any, Any], ...] | Callable[[DataFrame], Never], int | str | str_ | tuple[str | bytes | date | datetime | timedelta | <7 more items> | complex | integer[Any] | floating[Any] | complexfloating[Any, Any], ...]]"  [index]
- freqtrade/data/btanalysis/trade_parallelism.py:136: error: Invalid index type "tuple[slice[Timestamp, str | bytes | date | timedelta | datetime64[date | int | None] | <7 more items> | None, None], str]" for "_LocIndexerFrame[DataFrame]"; expected type "tuple[tuple[IndexOpsMixin[Any, Any] | Series[builtins.bool] | ndarray[tuple[Any, ...], dtype[numpy.bool[builtins.bool]]] | list[builtins.bool] | str | bytes | date | datetime | timedelta | <7 more items> | complex | integer[Any] | floating[Any] | complexfloating[Any, Any] | Sequence[str | bytes | date | datetime | timedelta | <7 more items> | complex | integer[Any] | floating[Any] | complexfloating[Any, Any]] | slice[Any, Any, Any], ...], Hashable]"  [index]
- freqtrade/data/btanalysis/trade_parallelism.py:136: error: Slice index must be an integer, SupportsIndex or None  [misc]
- freqtrade/data/btanalysis/trade_parallelism.py:151: error: Slice index must be an integer, SupportsIndex or None  [misc]
- freqtrade/data/btanalysis/trade_parallelism.py:152: error: Invalid index type "tuple[slice[Timestamp, str | bytes | date | timedelta | datetime64[date | int | None] | <7 more items> | None, None], str]" for "_LocIndexerFrame[DataFrame]"; expected type "tuple[int | str | str_ | Timestamp | tuple[str | bytes | date | datetime | timedelta | <7 more items> | complex | integer[Any] | floating[Any] | complexfloating[Any, Any], ...] | Callable[[DataFrame], Never], int | str | str_ | tuple[str | bytes | date | datetime | timedelta | <7 more items> | complex | integer[Any] | floating[Any] | complexfloating[Any, Any], ...]]"  [index]
- freqtrade/data/btanalysis/trade_parallelism.py:152: error: Invalid index type "tuple[slice[Timestamp, str | bytes | date | timedelta | datetime64[date | int | None] | <7 more items> | None, None], str]" for "_LocIndexerFrame[DataFrame]"; expected type "tuple[tuple[IndexOpsMixin[Any, Any] | Series[builtins.bool] | ndarray[tuple[Any, ...], dtype[numpy.bool[builtins.bool]]] | list[builtins.bool] | str | bytes | date | datetime | timedelta | <7 more items> | complex | integer[Any] | floating[Any] | complexfloating[Any, Any] | Sequence[str | bytes | date | datetime | timedelta | <7 more items> | complex | integer[Any] | floating[Any] | complexfloating[Any, Any]] | slice[Any, Any, Any], ...], Hashable]"  [index]
- freqtrade/data/btanalysis/trade_parallelism.py:152: error: Slice index must be an integer, SupportsIndex or None  [misc]

@ilevkivskyi
Copy link
Copy Markdown
Member Author

mypy_primer is as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unexpected "error: Slice index must be an integer or None"

2 participants