This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: help() appears confused about the module of typing.Annotated
Type: behavior Stage:
Components: Documentation, Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: AlexWaygood, docs@python, gvanrossum, jack1142, kj
Priority: normal Keywords:

Created on 2021-10-05 18:29 by AlexWaygood, last changed 2022-04-11 14:59 by admin.

Messages (3)
msg403258 - (view) Author: Alex Waygood (AlexWaygood) * (Python triager) Date: 2021-10-05 18:29
`help()` appears confused about the module of `typing.Annotated`. If you call `help()` on a parameterised "instance" of `typing.Annotated`, it will claim that `Annotated` belongs to whatever module the annotated type is from. Additionally, `help()` appears not to know about the `__metadata__` attribute of `typing.Annotated`.

```
>>> from typing import Annotated, Callable
>>> t = Annotated[int | str, "Some metadata"]
>>> help(t)
Help on _AnnotatedAlias in module types:

Annotated = int | str
>>> u = Annotated[Callable[[int], str], "Some metadata"]
>>> help(u)
Help on _AnnotatedAlias in module typing:

Annotated = typing.Callable[[int], str]
>>> s = Annotated[int, "Some metadata"]
Help on _AnnotatedAlias in module builtins:

Annotated = class int(object)
 |  int([x]) -> integer
 |  int(x, base=10) -> integer

# (etc., giving the entire output of help() for `int`)
```
msg403602 - (view) Author: Alex Waygood (AlexWaygood) * (Python triager) Date: 2021-10-10 16:40
It actually appears as though there is no documented way to retrieve the metadata from an annotated alias. Is this intentional?

The metadata is stored in a `__metadata__` attribute of a `typing._AnnotatedAlias` instance. But the `__metadata__` attribute is undocumented -- there is no mention of it in the docstring for `_AnnotatedAlias`, in the documentation for `typing.Annotated` (https://docs.python.org/3.11/library/typing.html#typing.Annotated), or PEP 593, which introduced `typing.Annotated` (https://www.python.org/dev/peps/pep-0593/).

The documentation says: "Passing include_extras=True to get_type_hints() lets one access the extra annotations at runtime." However, this is misleading. Calling `get_type_hints` on a function/class where an argument/attribute is annotated with an `_AnnotatedAlias` instance is a way of retrieving the type hints for the function/class with the `_AnnotatedAlias` instance unresolved. However, if you call `get_type_hints` on the `_AnnotatedAlias` instance directly, this fails.
msg412485 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2022-02-03 23:18
It looks like __metadata__ was *meant* to be a public attribute, but somehow overseen when the PEP and docs were written. :-(

I don't know anything about this class, really.
History
Date User Action Args
2022-04-11 14:59:50adminsetgithub: 89543
2022-02-03 23:18:38gvanrossumsetmessages: + msg412485
2022-02-03 10:35:01jack1142setnosy: + jack1142
2021-10-10 16:52:07AlexWaygoodsetnosy: + gvanrossum, kj
2021-10-10 16:40:20AlexWaygoodsetmessages: + msg403602
versions: + Python 3.11
2021-10-05 18:29:03AlexWaygoodcreate