: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)
: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
===========
class BaseTest(unittest.TestCase):
"""Test basics."""
generic_types = [type, tuple, list, dict, set, frozenset, enumerate, memoryview,
+ slice,
defaultdict, deque,
SequenceMatcher,
dircmp,
--- /dev/null
+Make the :class:`slice` class subscriptable at runtime to be consistent with typing implementation.
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}
};