FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-128335: Make slice generic at runtime by Gobot1234 · Pull Request #128336 · python/cpython · GitHub

Repository navigation

Merged
6 changes: 6 additions & 0 deletions Doc/reference/datamodel.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Is something like slice[float, float, int] also supported? This is necessary for, e.g., pandas.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Yes that should work fine. The number of arguments passed in isn't usually checked at runtime.

containing :type:`float` objects.

.. index::
single: start (slice object attribute)
single: stop (slice object attribute)
Expand Down
3 changes: 3 additions & 0 deletions Doc/whatsnew/3.15.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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
===========
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_genericalias.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Gobot1234 Dec 29, 2024 •
edited
Loading

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Back | FazBrowse Home | New Git URL