Uh oh!
There was an error while loading. Please reload this page.
[doc] bpo-45680: Disambiguate __getitem__ and __class_getitem__ in the data model. - #29389
Conversation
…`` in the data model. The documentation explaining Python's data model does not adequately explain the differences between ``__getitem__`` and ``__class_getitem__``, nor does it explain when each is called. There is an attempt at explaining ``__class_getitem__`` in the documentation for ``GenericAlias`` objects, but this does not give sufficient clarity into how the method works. Moreover, it is the wrong place for that information to be found; the explanation of ``__class_getitem__`` should be in the documentation explaining the data model. This PR has been split off from python#29335.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
__class_getitem__ in the data model.__getitem__ and __class_getitem__ in the data model.AlexWaygood
commented
Nov 4, 2021
Thank you <3 |
ambv
left a comment
There was a problem hiding this comment.
This is pretty cool, thanks for being so thorough.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
AlexWaygood
commented
Nov 7, 2021
Massive thanks for the detailed review! I've rewritten large chunks in response 😅 (Wow, writing documentation is hard!) |
| Usually, the :ref:`subscription<subscriptions>` of an object using square | ||
| brackets will call the :meth:`~object.__getitem__` instance method defined on | ||
| the object's class. However, if the object being subscribed is itself a class, |
There was a problem hiding this comment.
Should it be "object being subscribed", or "object being subscripted"?
| def subscribe(obj, x): | ||
| class_of_obj = type(obj) | ||
| # If the class of `obj` defines `__getitem__()`, | ||
| # call `type(obj).__getitem__()` | ||
| if hasattr(class_of_obj, '__getitem__'): | ||
| return class_of_obj.__getitem__(obj, x) | ||
| # Else, if `obj` defines `__class_getitem__()`, | ||
| # call `obj.__class_getitem__()` | ||
| elif hasattr(obj, '__class_getitem__'): | ||
| return obj.__class_getitem__(x) | ||
| # Else, raise an exception | ||
| else: | ||
| raise TypeError( | ||
| f"'{class_of_obj.__name__}' object is not subscriptable" | ||
| ) |
There was a problem hiding this comment.
Please correct me if this isn't quite right -- I'm in a little over my head here!
AlexWaygood
commented
Nov 9, 2021
Okay, the CI failures are because |
miss-islington
commented
Nov 18, 2021
Thanks @AlexWaygood for the PR, and @ambv for merging it 🌮🎉.. I'm working now to backport this PR to: 3.9. |
miss-islington
commented
Nov 18, 2021
Thanks @AlexWaygood for the PR, and @ambv for merging it 🌮🎉.. I'm working now to backport this PR to: 3.10. |
bedevere-bot
commented
Nov 18, 2021
GH-29619 is a backport of this pull request to the 3.9 branch. |
…`` in the data model (pythonGH-29389) The documentation explaining Python's data model does not adequately explain the differences between ``__getitem__`` and ``__class_getitem__``, nor does it explain when each is called. There is an attempt at explaining ``__class_getitem__`` in the documentation for ``GenericAlias`` objects, but this does not give sufficient clarity into how the method works. Moreover, it is the wrong place for that information to be found; the explanation of ``__class_getitem__`` should be in the documentation explaining the data model. This PR has been split off from pythonGH-29335. (cherry picked from commit 31b3a70) Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
bedevere-bot
commented
Nov 18, 2021
GH-29620 is a backport of this pull request to the 3.10 branch. |
…`` in the data model (pythonGH-29389) The documentation explaining Python's data model does not adequately explain the differences between ``__getitem__`` and ``__class_getitem__``, nor does it explain when each is called. There is an attempt at explaining ``__class_getitem__`` in the documentation for ``GenericAlias`` objects, but this does not give sufficient clarity into how the method works. Moreover, it is the wrong place for that information to be found; the explanation of ``__class_getitem__`` should be in the documentation explaining the data model. This PR has been split off from pythonGH-29335. (cherry picked from commit 31b3a70) Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
…titem__`` in the data model (GH-29389) (GH-29620) The documentation explaining Python's data model does not adequately explain the differences between ``__getitem__`` and ``__class_getitem__``, nor does it explain when each is called. There is an attempt at explaining ``__class_getitem__`` in the documentation for ``GenericAlias`` objects, but this does not give sufficient clarity into how the method works. Moreover, it is the wrong place for that information to be found; the explanation of ``__class_getitem__`` should be in the documentation explaining the data model. This PR has been split off from GH-29335. (cherry picked from commit 31b3a70) Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com> Co-authored-by: Łukasz Langa <lukasz@langa.pl>
…item__`` in the data model (GH-29389) (GH-29619) The documentation explaining Python's data model does not adequately explain the differences between ``__getitem__`` and ``__class_getitem__``, nor does it explain when each is called. There is an attempt at explaining ``__class_getitem__`` in the documentation for ``GenericAlias`` objects, but this does not give sufficient clarity into how the method works. Moreover, it is the wrong place for that information to be found; the explanation of ``__class_getitem__`` should be in the documentation explaining the data model. This PR has been split off from GH-29335. (cherry picked from commit 31b3a70) Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com> Co-authored-by: Łukasz Langa <lukasz@langa.pl>
…`` in the data model (pythonGH-29389) The documentation explaining Python's data model does not adequately explain the differences between ``__getitem__`` and ``__class_getitem__``, nor does it explain when each is called. There is an attempt at explaining ``__class_getitem__`` in the documentation for ``GenericAlias`` objects, but this does not give sufficient clarity into how the method works. Moreover, it is the wrong place for that information to be found; the explanation of ``__class_getitem__`` should be in the documentation explaining the data model. This PR has been split off from pythonGH-29335.
The documentation explaining Python's data model does not adequately explain
the differences between
__getitem__and__class_getitem__, nor does itexplain when each is called. There is an attempt at explaining
__class_getitem__in the documentation forGenericAliasobjects, butthis does not give sufficient clarity into how the method works. Moreover, it
is the wrong place for that information to be found; the explanation of
__class_getitem__should be in the documentation explaining the data model.This PR has been split off from #29335.
https://bugs.python.org/issue45680