Skip to content

Further improve docs for typing.Annotated #105498

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

Merged
merged 2 commits into from
Jun 8, 2023

Conversation

AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Jun 8, 2023

A significant followup PR to #105365.

  • Snappier first sentence. Use shorter sentences and shorter paragraphs throughout.
  • Describe it as a "typing form", not a "type" -- Annotated isn't a type, you can't use it directly as a type annotation.
  • Add links to other functions where they're being cross-referenced.
  • Use the term "metadata elements" to describe the metadata in the __metadata__ attribute at runtime, rather than "annotations" or "types", which were being used before.
  • Use PEP-695 syntax for more examples.
  • Don't use the .. attribute:: directive for the __metadata__ attribute. It's not being rendered correctly -- Sphinx thinks it's a module attribute rather than an attribute of Annotated[X, "foo"]. Current rendering. This is because we don't mark up Annotated as a class in the docs. (It actually is a class at runtime, at the moment, but that's an implementation detail. I don't think we should mark it as a class in the docs.)
  • Add a note to the docs that is currently only present in the docstring about how an Annotated type cannot be made generic over a TypeVarTuple.
  • State more clearly that the default of get_type_hints is to strip metadata from Annotated annotations, and add an example.
  • Move the link to the PEP down to the bottom as a .. seealso:: box.
  • There was a sentence that described "calling" Annotated, which was incorrect: you subscript Annotated, you don't call it.

📚 Documentation preview 📚: https://cpython-previews--105498.org.readthedocs.build/

@AlexWaygood AlexWaygood changed the title Overhaul docs for typing.Annotated Further improve docs for typing.Annotated Jun 8, 2023
@AlexWaygood
Copy link
Member Author

@erlend-aasland, it would be great to get some non-typing-expert eyes on this, if you have the time to take a look :)

@erlend-aasland
Copy link
Contributor

@AlexWaygood, I'll have a look!


For example:
where ``T1``, ``T2``, etc. are :class:`TypeVars <TypeVar>`. This would be
invalid: only one type should be passed to Annotated.
Copy link
Member

Choose a reason for hiding this comment

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

This looks misindented, as it's indented by one space less than the next paragraph.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, it seems the bulleted list is inconsistent about whether there should be one or two spaces after a bullet point.

Copy link
Member Author

Choose a reason for hiding this comment

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

I decided it was probably the next paragraph that had the wrong indentation -- does it look better after 2cd15fe?

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.

Looks good, one formatting nit

Copy link
Contributor

@erlend-aasland erlend-aasland left a comment

Choose a reason for hiding this comment

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

The new text is definitely easier to read for a typing newbie like me. Thanks!

@AlexWaygood
Copy link
Member Author

The new text is definitely easier to read for a typing newbie like me. Thanks!

Great to hear!

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, looks good!

@AlexWaygood AlexWaygood enabled auto-merge (squash) June 8, 2023 14:14
@AlexWaygood AlexWaygood merged commit d213c29 into python:main Jun 8, 2023
@miss-islington
Copy link
Contributor

Thanks @AlexWaygood for the PR 🌮🎉.. I'm working now to backport this PR to: 3.11, 3.12.
🐍🍒⛏🤖

@bedevere-bot
Copy link

GH-105503 is a backport of this pull request to the 3.12 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.12 only security fixes label Jun 8, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 8, 2023
(cherry picked from commit d213c29)

Co-authored-by: Alex Waygood <[email protected]>
@bedevere-bot
Copy link

GH-105504 is a backport of this pull request to the 3.11 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.11 only security fixes label Jun 8, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 8, 2023
(cherry picked from commit d213c29)

Co-authored-by: Alex Waygood <[email protected]>
@AlexWaygood AlexWaygood deleted the overhaul-annotated-docs branch June 8, 2023 14:42
AlexWaygood added a commit that referenced this pull request Jun 8, 2023
* Further improve docs for `typing.Annotated` (GH-105498)
(cherry picked from commit d213c29)

Co-authored-by: Alex Waygood <[email protected]>
AlexWaygood added a commit that referenced this pull request Jun 8, 2023
Further improve docs for `typing.Annotated` (GH-105498)
(cherry picked from commit d213c29)

Co-authored-by: Alex Waygood <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants