Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-45680: Improve docs on subscriptions w.r.t. GenericAlias objects #29479

Merged
merged 8 commits into from
Mar 8, 2022

Conversation

AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Nov 8, 2021

This PR is part of the project to improve documentation on GenericAlias objects and __class_getitem__, started in #29335.

https://bugs.python.org/issue45680

@AlexWaygood AlexWaygood marked this pull request as ready for review November 19, 2021 00:26
@AlexWaygood AlexWaygood changed the title bpo-45680: Improve docs on 'subscriptions' bpo-45680: Improve docs on 'subscriptions' w.r.t. GenericAlias objects Dec 4, 2021
@AlexWaygood AlexWaygood changed the title bpo-45680: Improve docs on 'subscriptions' w.r.t. GenericAlias objects bpo-45680: Improve docs on 'subscriptions' w.r.t. GenericAlias objects Dec 4, 2021
@AlexWaygood AlexWaygood changed the title bpo-45680: Improve docs on 'subscriptions' w.r.t. GenericAlias objects bpo-45680: Improve docs on subscriptions w.r.t. GenericAlias objects Dec 6, 2021
@AlexWaygood
Copy link
Member Author

@JelleZijlstra, reckon this can be merged soon? :)

@JelleZijlstra JelleZijlstra self-assigned this Feb 17, 2022
@JelleZijlstra
Copy link
Member

Looking at this again I'm not so sure, sorry. This adds more text than was there before, but the new text is a little repetitive. I do like the additional links though.

I feel like a broader problem with the language reference is that it veers too much into describing how concrete built-in types handle an operation, and not enough into how exactly the operation works. Here, for example, there is no precise description of when __class_getitem__ is called instead of __getitem__. That description exists only in the docs for __class_getitem__, but logically it should be part of the description of the subscript operator. The fact that the key is a tuple unless there is exactly one element shows up in a parenthetical in the text around mappings. (It's also wrong: obj[1,] is a tuple key, not an integer. The next section on slicing gets this right.) Also, type[int] works through deep magic in the interpreter, since type has neither a __getitem__ nor a __class_getitem__ magic, but nothing in the reference mentions this.

@JelleZijlstra
Copy link
Member

JelleZijlstra commented Feb 19, 2022

See also bpo-46754.

@AlexWaygood
Copy link
Member Author

Looking at this again I'm not so sure, sorry.

Thanks for taking a look! I'll have a think tomorrow about whether I want to try revising this PR, or just close it.

@AlexWaygood
Copy link
Member Author

Here, for example, there is no precise description of when __class_getitem__ is called instead of __getitem__. That description exists only in the docs for __class_getitem__, but logically it should be part of the description of the subscript operator.

Perhaps you're right that that information might be better found in this document, but tbqh I don't have the energy to move that section from the data model to here after spending so long adding that section to the data model in GH-29389 :(

@AlexWaygood
Copy link
Member Author

AlexWaygood commented Mar 5, 2022

@JelleZijlstra I've had another bash at it. I've tried to make the section less repetitive and increase the accuracy of the section regarding the "expression list".

The core aims of this PR are to:

  1. Make more prominent mention of the fact that subscriptions can also result in __class_getitem__ being called, as well as __getitem__.
  2. Add more links to to docs elsewhere on __class_getitem__.

Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

Thanks! Just one comment.

@@ -815,30 +815,44 @@ Subscriptions
object: dictionary
pair: sequence; item

Subscription of a sequence (string, tuple or list) or mapping (dictionary)
object usually selects an item from the collection:
The subscription of an instance of a :ref:`container class <sequence-types>`
Copy link
Member

Choose a reason for hiding this comment

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

Seems weird to link this to sequence types. Maybe just drop the link? The later explanation is clearer about sequences and mappings.

Suggested change
The subscription of an instance of a :ref:`container class <sequence-types>`
The subscription of an instance of a container class

Copy link
Member Author

@AlexWaygood AlexWaygood Mar 6, 2022

Choose a reason for hiding this comment

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

I think the section in the docs linked to with sequence-types perhaps originally only discussed sequence types. Now, however, it discusses all kinds of container types -- the link takes us to here: https://docs.python.org/3/reference/datamodel.html#emulating-container-types

Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

Thanks! I'm happy to merge this now (cc @gvanrossum).

Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

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

(Because the other meaning of "subscription" (e.g. a magazine) is so much more common I have a slight problem with using it to refer to the a[i] syntax, but it seems commonly used in our docs so it's fine. And the unambiguous alternative, "subscripting", is used much less frequently in our docs.)

@JelleZijlstra JelleZijlstra merged commit 5073129 into python:main Mar 8, 2022
@miss-islington
Copy link
Contributor

Thanks @AlexWaygood for the PR, and @JelleZijlstra for merging it 🌮🎉.. I'm working now to backport this PR to: 3.9, 3.10.
🐍🍒⛏🤖

@bedevere-bot
Copy link

GH-31743 is a backport of this pull request to the 3.10 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.9 only security fixes label Mar 8, 2022
@bedevere-bot
Copy link

GH-31744 is a backport of this pull request to the 3.9 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Mar 8, 2022
pythonGH-29479)

(cherry picked from commit 5073129)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Mar 8, 2022
pythonGH-29479)

(cherry picked from commit 5073129)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
miss-islington added a commit to miss-islington/cpython that referenced this pull request Mar 8, 2022
pythonGH-29479)

(cherry picked from commit 5073129)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
miss-islington added a commit that referenced this pull request Mar 8, 2022
GH-29479)

(cherry picked from commit 5073129)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@AlexWaygood AlexWaygood deleted the subscription-docs branch March 8, 2022 07:56
@AlexWaygood
Copy link
Member Author

Thanks so much for helping get this over the line @JelleZijlstra!

hello-adam pushed a commit to hello-adam/cpython that referenced this pull request Jun 2, 2022
pythonGH-29479)

(cherry picked from commit 5073129)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants