Skip to content

gh-128335: Make slice generic at runtime #128336

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Lib/test/test_genericalias.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
class BaseTest(unittest.TestCase):
"""Test basics."""
generic_types = [type, tuple, list, dict, set, frozenset, enumerate, memoryview,
slice,
defaultdict, deque,
SequenceMatcher,
dircmp,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make the :class:`slice` class subscriptable at runtime to be consistent with typing implementation.
1 change: 1 addition & 0 deletions Objects/sliceobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
static PyMethodDef slice_methods[] = {
{"indices", slice_indices, METH_O, slice_indices_doc},
{"__reduce__", slice_reduce, METH_NOARGS, reduce_doc},
{"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, "See PEP 585"},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This docstring seems misleading, as PEP 585 doesn't mention the slice class.

(Maybe PEP 585 could refer to https://typing.readthedocs.io/en/latest/guides/modernizing.html#generics-in-the-typing-module? Right now it links to documentation for __class_getitem__ that on its own isn't a replacement for PEP 585 from a reader's perspective)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed it is maybe a little silly to be putting PEPs in these things when they aren't active docs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PRs to the PEPs repo improving things like this are welcome!

Copy link
Contributor Author

@Gobot1234 Gobot1234 Dec 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious if @AlexWaygood / @JelleZijlstra have opinions on this. This is done in other places. I seem to have time travelled here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do agree that our docstrings probably also shouldn't be referencing PEPs, which are, as you both note, historical documents recording decisions that were made at one point in time rather than living documentation. But all the other __class_getitem__ docstrings say this currently, so I think it makes sense to follow suit here and change all the __class_getitem__ docstrings to something better all at once as a followup

{NULL, NULL}
};

Expand Down
Loading