https://github.com/python/cpython/commit/265381b7e8bba00fefb40339d55fbe88133d4c04 commit: 265381b7e8bba00fefb40339d55fbe88133d4c04 branch: main author: James Hilton-Balfe <[email protected]> committer: JelleZijlstra <[email protected]> date: 2026-01-11T11:27:24-08:00 summary:
gh-128335: Make slice generic at runtime (#128336) Co-authored-by: Jelle Zijlstra <[email protected]> files: A Misc/NEWS.d/next/Core_and_Builtins/2024-12-29-21-33-08.gh-issue-128334.3c5Nou.rst M Doc/reference/datamodel.rst M Doc/whatsnew/3.15.rst M Lib/test/test_genericalias.py M Objects/sliceobject.c diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index 488fbc6b1f68cd..1bfe6b7375bcf7 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -1819,6 +1819,12 @@ Slice objects are used to represent slices for :meth:`~object.__getitem__` methods. They are also created by the built-in :func:`slice` function. +.. versionadded:: 3.15 + + The :func:`slice` type now supports :ref:`subscription <subscriptions>`. For + example, ``slice[float]`` may be used in type annotations to indicate a slice + containing :type:`float` objects. + .. index:: single: start (slice object attribute) single: stop (slice object attribute) diff --git a/Doc/whatsnew/3.15.rst b/Doc/whatsnew/3.15.rst index 115b7b3c86e034..a4eeb568dae433 100644 --- a/Doc/whatsnew/3.15.rst +++ b/Doc/whatsnew/3.15.rst @@ -402,6 +402,9 @@ Other language changes :class:`tuple` (including classes created by :func:`collections.namedtuple`). (Contributed by Serhiy Storchaka in :gh:`41779`.) +* The :class:`slice` type now supports subscription, + making it a :term:`generic type`. + (Contributed by James Hilton-Balfe in :gh:`128335`.) New modules =========== diff --git a/Lib/test/test_genericalias.py b/Lib/test/test_genericalias.py index 9df9296e26ad5c..0017c093166dd2 100644 --- a/Lib/test/test_genericalias.py +++ b/Lib/test/test_genericalias.py @@ -102,6 +102,7 @@ class BaseTest(unittest.TestCase): """Test basics.""" generic_types = [type, tuple, list, dict, set, frozenset, enumerate, memoryview, + slice, defaultdict, deque, SequenceMatcher, dircmp, diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-12-29-21-33-08.gh-issue-128334.3c5Nou.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-12-29-21-33-08.gh-issue-128334.3c5Nou.rst new file mode 100644 index 00000000000000..2908db863e076f --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2024-12-29-21-33-08.gh-issue-128334.3c5Nou.rst @@ -0,0 +1 @@ +Make the :class:`slice` class subscriptable at runtime to be consistent with typing implementation. diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c index 5186ff4f6f0cf5..2a402bb3347d60 100644 --- a/Objects/sliceobject.c +++ b/Objects/sliceobject.c @@ -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"}, {NULL, NULL} }; _______________________________________________ Python-checkins mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3//lists/python-checkins.python.org Member address: [email protected]
