8000 Document `Annotated.__origin__` · Issue #124120 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

Document Annotated.__origin__ #124120

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

Closed
ncoghlan opened this issue Sep 16, 2024 · 4 comments
Closed

Document Annotated.__origin__ #124120

ncoghlan opened this issue Sep 16, 2024 · 4 comments
Labels
docs Documentation in the Doc dir topic-typing

Comments

@ncoghlan
Copy link
Contributor
ncoghlan commented Sep 16, 2024

The typing module docs cover using Annotated.__metadata__ to retrieve the annotations, but they do not currently cover the use of Annotated.__origin__ to retrieve the underlying type hint that is being annotated.

>>> from typing import Annotated
>>> Annotated[int, ""].__metadata__
('',)
>>> Annotated[int, ""].__origin__
<class 'int'>

Linked PRs

@ncoghlan ncoghlan added the docs Documentation in the Doc dir label Sep 16, 2024
@JelleZijlstra
Copy link
Member

You should not use this directly, but use typing.get_origin instead. However, we could document this explicitly.

@JelleZijlstra
Copy link
Member

Actually, looking at the code some more this is not true. For some reason there is a special case where .__origin__ and get_origin() (as well as __args__ and get_args()) are not aligned for Annotated.

>>> a = Annotated[int, "x"]
>>> a.__origin__
<class 'int'>
>>> a.__args__
(<class 'int'>,)
>>> typing.get_origin(a)
<class 'typing.Annotated'>
>>> typing.get_args(a)
(<class 'int'>, 'x')

This aligns with the documentation, which promises that for X[Y, ...], get_origin() will return X. However, it is confusing that it does not also align with the __origin_ attribute.

Since __origin__ is otherwise undocumented, it's probably best to document it specifically for Annotated as @ncoghlan originally suggested.

@sobolevn
Copy link
Member

I made this mistake in the first version of #124125

But, then I realized the same thing as in #124120 (comment)

Why? Because of these two lines:

cpython/Lib/typing.py

Lines 2507 to 2508 in 9aa1f60

if isinstance(tp, _AnnotatedAlias):
return Annotated

I think that it makes sense:

Get the unsubscripted version of a type.

So, Annotated is the unsubscripted version of a type. And any T wrapped by it is just the first type arg.

sobolevn added a commit that referenced this issue Sep 24, 2024
Co-authored-by: Brian Schubert <brianm.schubert@gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Sep 24, 2024
(cherry picked from commit faef3fa)

Co-authored-by: sobolevn <mail@sobolevn.me>
Co-authored-by: Brian Schubert <brianm.schubert@gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Sep 24, 2024
(cherry picked from commit faef3fa)

Co-authored-by: sobolevn <mail@sobolevn.me>
Co-authored-by: Brian Schubert <brianm.schubert@gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
sobolevn added a commit that referenced this issue Sep 24, 2024
gh-124120: Document `Annotated.__origin__` (GH-124125)
(cherry picked from commit faef3fa)

Co-authored-by: sobolevn <mail@sobolevn.me>
Co-authored-by: Brian Schubert <brianm.schubert@gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Yhg1s pushed a commit that referenced this issue Sep 24, 2024
gh-124120: Document `Annotated.__origin__` (GH-124125)
(cherry picked from commit faef3fa)

Co-authored-by: sobolevn <mail@sobolevn.me>
Co-authored-by: Brian Schubert <brianm.schubert@gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@ncoghlan
Copy link
Contributor Author

Backports have been merged, so marking this as complete. Thanks, folks!

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 topic-typing
Projects
None yet
Development

No branches or pull requests

4 participants
0