From 6baf87841cf0ceb7532aba0970459f02c02c380f Mon Sep 17 00:00:00 2001 From: James Hilton-Balfe Date: Sun, 29 Dec 2024 21:26:00 +0000 Subject: [PATCH 1/8] Make slice generic --- Objects/sliceobject.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c index 4fef0af93fe095..d1060302a4b7d0 100644 --- a/Objects/sliceobject.c +++ b/Objects/sliceobject.c @@ -560,6 +560,7 @@ static PyMethodDef slice_methods[] = { METH_O, slice_indices_doc}, {"__reduce__", (PyCFunction)slice_reduce, METH_NOARGS, reduce_doc}, + {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, "See PEP 585"}, {NULL, NULL} }; From 61fbf37543c5626ae21b655e6ac2c5a51cc656e8 Mon Sep 17 00:00:00 2001 From: James Hilton-Balfe Date: Sun, 29 Dec 2024 21:30:13 +0000 Subject: [PATCH 2/8] Add associated test --- Lib/test/test_genericalias.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_genericalias.py b/Lib/test/test_genericalias.py index 3048d038c782d4..ed9af75852c4b2 100644 --- a/Lib/test/test_genericalias.py +++ b/Lib/test/test_genericalias.py @@ -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, From f24164383b398568d636542349b9b462d5ef4146 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 29 Dec 2024 21:33:12 +0000 Subject: [PATCH 3/8] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2024-12-29-21-33-08.gh-issue-128334.3c5Nou.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2024-12-29-21-33-08.gh-issue-128334.3c5Nou.rst 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..5bb0a1fdffc74e --- /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 slice type subscriptable at runtime From 112d407cb16f8cc61d92a49ab6cde7a36d902a29 Mon Sep 17 00:00:00 2001 From: James Hilton-Balfe Date: Mon, 30 Dec 2024 17:11:12 +0000 Subject: [PATCH 4/8] Update Misc/NEWS.d/next/Core_and_Builtins/2024-12-29-21-33-08.gh-issue-128334.3c5Nou.rst Co-authored-by: Jelle Zijlstra --- .../2024-12-29-21-33-08.gh-issue-128334.3c5Nou.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 5bb0a1fdffc74e..c5495d2928eece 100644 --- 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 @@ -1 +1 @@ -Make slice type subscriptable at runtime +Make :class:`slice` objects subscriptable at runtime. From 60a9c083eb4ebe7fb33f8c62efcfe0fc727f9798 Mon Sep 17 00:00:00 2001 From: James Hilton-Balfe Date: Mon, 30 Dec 2024 23:14:53 +0000 Subject: [PATCH 5/8] Update 2024-12-29-21-33-08.gh-issue-128334.3c5Nou.rst --- .../2024-12-29-21-33-08.gh-issue-128334.3c5Nou.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index c5495d2928eece..2908db863e076f 100644 --- 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 @@ -1 +1 @@ -Make :class:`slice` objects subscriptable at runtime. +Make the :class:`slice` class subscriptable at runtime to be consistent with typing implementation. From 7a95cc9e353cc166543c34a290d4b30bad0e0f50 Mon Sep 17 00:00:00 2001 From: Gobot1234 Date: Tue, 14 Oct 2025 16:21:12 +0100 Subject: [PATCH 6/8] Add more docs --- Doc/reference/datamodel.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index f1b7d33655c591..91391070bdb9e2 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -1824,6 +1824,10 @@ 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 + + :class:`slice` now supports :ref:`subscription`. + .. index:: single: start (slice object attribute) single: stop (slice object attribute) From 4da984286f7eb4b9384340f54ebea0c623ab584e Mon Sep 17 00:00:00 2001 From: Gobot1234 Date: Tue, 14 Oct 2025 16:26:20 +0100 Subject: [PATCH 7/8] oops that probably doesn't work --- Doc/reference/datamodel.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index e905e4d7f82dcf..48908d8e7848bd 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -1833,7 +1833,7 @@ methods. They are also created by the built-in :func:`slice` function. .. versionadded:: 3.15 - :class:`slice` now supports :ref:`subscription`. + :func:`slice` now supports :ref:`subscription`. .. index:: single: start (slice object attribute) From 21e8ee8d707e405bf35c882453eecb2dcb8df42f Mon Sep 17 00:00:00 2001 From: James Hilton-Balfe Date: Wed, 15 Oct 2025 17:08:54 +0100 Subject: [PATCH 8/8] Fix documentation typo --- Doc/reference/datamodel.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index 48908d8e7848bd..c527c1196d3d03 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -1833,7 +1833,7 @@ methods. They are also created by the built-in :func:`slice` function. .. versionadded:: 3.15 - :func:`slice` now supports :ref:`subscription`. + :func:`slice` now supports :ref:`subscription `. .. index:: single: start (slice object attribute)