[issue45380] help() appears confused about the module of typing.Annotated
New submission from Alex Waygood <Alex.Waygood@Gmail.com>: `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`) ``` ---------- assignee: docs@python components: Documentation, Library (Lib) messages: 403258 nosy: AlexWaygood, docs@python priority: normal severity: normal status: open title: help() appears confused about the module of typing.Annotated type: behavior versions: Python 3.10, Python 3.9 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue45380> _______________________________________
Alex Waygood <Alex.Waygood@Gmail.com> added the comment: 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. ---------- versions: +Python 3.11 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue45380> _______________________________________
Change by Alex Waygood <Alex.Waygood@Gmail.com>: ---------- nosy: +gvanrossum, kj _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue45380> _______________________________________
Change by jack1142 <kuba.kuczys@gmail.com>: ---------- nosy: +jack1142 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue45380> _______________________________________
Guido van Rossum <guido@python.org> added the comment: 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. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue45380> _______________________________________
participants (3)
-
Alex Waygood -
Guido van Rossum -
jack1142