Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace plain slice type hints with IndexSlice. #13007

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

randolf-scholz
Copy link
Contributor

Trying to see what happens when we replace slice type hints with

IndexSlice: TypeAlias = slice[SupportsIndex | None, SupportsIndex | None, SupportsIndex | None]

annotations.

Copy link
Contributor

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

prefect (https://github.com/PrefectHQ/prefect)
- src/prefect/utilities/annotations.py:41: note:          def __getitem__(self, slice[Any, Any, Any], /) -> tuple[Never, ...]
+ src/prefect/utilities/annotations.py:41: note:          def __getitem__(self, slice[SupportsIndex | None, SupportsIndex | None, SupportsIndex | None], /) -> tuple[Never, ...]
- src/prefect/utilities/annotations.py:41: note:          def __getitem__(self, slice[Any, Any, Any], /) -> Sequence[Never]
+ src/prefect/utilities/annotations.py:41: note:          def __getitem__(self, slice[SupportsIndex | None, SupportsIndex | None, SupportsIndex | None], /) -> Sequence[Never]
- src/prefect/cli/cloud/__init__.py:326: note:     def __getitem__(self, SupportsIndex | slice[Any, Any, Any], /) -> str
+ src/prefect/cli/cloud/__init__.py:326: note:     def __getitem__(self, SupportsIndex | slice[SupportsIndex | None, SupportsIndex | None, SupportsIndex | None], /) -> str
- src/prefect/cli/deploy.py:1127: note:     def __setitem__(self, slice[Any, Any, Any], Iterable[dict[Any, Any]], /) -> None
+ src/prefect/cli/deploy.py:1127: note:     def __setitem__(self, slice[SupportsIndex | None, SupportsIndex | None, SupportsIndex | None], Iterable[dict[Any, Any]], /) -> None
- src/prefect/cli/deploy.py:1153: note:     def __setitem__(self, slice[Any, Any, Any], Iterable[dict[Any, Any]], /) -> None
+ src/prefect/cli/deploy.py:1153: note:     def __setitem__(self, slice[SupportsIndex | None, SupportsIndex | None, SupportsIndex | None], Iterable[dict[Any, Any]], /) -> None
- src/prefect/cli/deploy.py:1161: note:     def __setitem__(self, slice[Any, Any, Any], Iterable[dict[Any, Any]], /) -> None
+ src/prefect/cli/deploy.py:1161: note:     def __setitem__(self, slice[SupportsIndex | None, SupportsIndex | None, SupportsIndex | None], Iterable[dict[Any, Any]], /) -> None
- src/prefect/cli/deploy.py:1178: note:     def __getitem__(self, slice[Any, Any, Any], /) -> list[dict[Any, Any]]
+ src/prefect/cli/deploy.py:1178: note:     def __getitem__(self, slice[SupportsIndex | None, SupportsIndex | None, SupportsIndex | None], /) -> list[dict[Any, Any]]

speedrun.com_global_scoreboard_webapp (https://github.com/Avasam/speedrun.com_global_scoreboard_webapp)
- backend/api/tournament_scheduler_api.py:327: note:     def __getitem__(self, slice[Any, Any, Any], /) -> list[Any]
+ backend/api/tournament_scheduler_api.py:327: note:     def __getitem__(self, slice[SupportsIndex | None, SupportsIndex | None, SupportsIndex | None], /) -> list[Any]
- backend/api/tournament_scheduler_api.py:331: note:     def __getitem__(self, slice[Any, Any, Any], /) -> list[Any]
+ backend/api/tournament_scheduler_api.py:331: note:     def __getitem__(self, slice[SupportsIndex | None, SupportsIndex | None, SupportsIndex | None], /) -> list[Any]
- backend/api/tournament_scheduler_api.py:335: note:     def __getitem__(self, slice[Any, Any, Any], /) -> list[Any]
+ backend/api/tournament_scheduler_api.py:335: note:     def __getitem__(self, slice[SupportsIndex | None, SupportsIndex | None, SupportsIndex | None], /) -> list[Any]

streamlit (https://github.com/streamlit/streamlit)
- lib/tests/streamlit/elements/lib/column_config_utils_test.py:385:13: note:     def __getitem__(self, Union[SupportsIndex, slice[Any, Any, Any]], /) -> str
+ lib/tests/streamlit/elements/lib/column_config_utils_test.py:385:13: note:     def __getitem__(self, Union[SupportsIndex, slice[Optional[SupportsIndex], Optional[SupportsIndex], Optional[SupportsIndex]]], /) -> str
- lib/tests/streamlit/runtime/state/query_params_test.py:264:37: note:         def __getitem__(self, slice[Any, Any, Any], /) -> Tuple[Any, ...]
+ lib/tests/streamlit/runtime/state/query_params_test.py:264:37: note:         def __getitem__(self, slice[Optional[SupportsIndex], Optional[SupportsIndex], Optional[SupportsIndex]], /) -> Tuple[Any, ...]

discord.py (https://github.com/Rapptz/discord.py)
- discord/http.py:234: note:     def __setitem__(self, slice[Any, Any, Any], Iterable[Embed], /) -> None
+ discord/http.py:234: note:     def __setitem__(self, slice[SupportsIndex | None, SupportsIndex | None, SupportsIndex | None], Iterable[Embed], /) -> None

operator (https://github.com/canonical/operator)
- ops/framework.py:1304: note:          def __getitem__(self, slice[Any, Any, Any], /) -> MutableSequence[Any]
+ ops/framework.py:1304: note:          def __getitem__(self, slice[SupportsIndex | None, SupportsIndex | None, SupportsIndex | None], /) -> MutableSequence[Any]
- ops/framework.py:1304: note:          def __getitem__(self, slice[Any, Any, Any], /) -> Sequence[Any]
+ ops/framework.py:1304: note:          def __getitem__(self, slice[SupportsIndex | None, SupportsIndex | None, SupportsIndex | None], /) -> Sequence[Any]
- ops/framework.py:1307: note:          def __setitem__(self, slice[Any, Any, Any], Iterable[Any], /) -> None
+ ops/framework.py:1307: note:          def __setitem__(self, slice[SupportsIndex | None, SupportsIndex | None, SupportsIndex | None], Iterable[Any], /) -> None
- ops/framework.py:1311: note:          def __delitem__(self, slice[Any, Any, Any], /) -> None
+ ops/framework.py:1311: note:          def __delitem__(self, slice[SupportsIndex | None, SupportsIndex | None, SupportsIndex | None], /) -> None

manticore (https://github.com/trailofbits/manticore)
- tests/wasm/json2mc.py:103: note:     def __getitem__(self, slice[Any, Any, Any], /) -> list[Any]
+ tests/wasm/json2mc.py:103: note:     def __getitem__(self, slice[SupportsIndex | None, SupportsIndex | None, SupportsIndex | None], /) -> list[Any]

dd-trace-py (https://github.com/DataDog/dd-trace-py)
- ddtrace/settings/config.py:904: note:     def __getitem__(self, SupportsIndex | slice[Any, Any, Any], /) -> str
+ ddtrace/settings/config.py:904: note:     def __getitem__(self, SupportsIndex | slice[SupportsIndex | None, SupportsIndex | None, SupportsIndex | None], /) -> str

@brianschubert
Copy link
Contributor

This seems like a good improvement to me. I imagine this would be very nice for replacing mypy's hardcoded SupportsIndex/None checks for slice indices (python/mypy#2410).

It looks like the pytype errors may be due to pytype having its own builtins stubs, which don't define slice as being generic: https://github.com/google/pytype/blob/97d949161b27259af283fc1d271f8cf056ddf9b6/pytype/stubs/builtins/builtins.pytd#L972

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.

2 participants