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, kj
Priority: normal Keywords:

Created on 2021-10-05 18:29 by AlexWaygood, last changed 2021-10-10 16:52 by AlexWaygood.

Messages (2)
msg403258 - (view) Author: Alex Waygood (AlexWaygood) * 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) * 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.
History
Date User Action Args
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