You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
help() appears confused about the module of typing.Annotated #89543
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2021-10-05.18:29:03.194>labels= ['type-bug', '3.9', '3.10', '3.11', 'library', 'docs']
title='help() appears confused about the module of typing.Annotated'updated_at=<Date2022-02-03.23:18:38.705>user='https://github.com/AlexWaygood'
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`)
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.
__metadata__ attribute appears to still not be documented so if it's not tracked elsewhere, it would make sense to make an issue for tracking the documentation about runtime inspection of Annotated.
__metadata__ attribute appears to still not be documented so if it's not tracked elsewhere, it would make sense to make an issue for tracking the documentation about runtime inspection of Annotated.
(To update on this: the __metadata__ field is now documented; see the PRs attached to #97797.)
Activity
AlexWaygood commentedon Oct 5, 2021
help()appears confused about the module oftyping.Annotated. If you callhelp()on a parameterised "instance" oftyping.Annotated, it will claim thatAnnotatedbelongs to whatever module the annotated type is from. Additionally,help()appears not to know about the__metadata__attribute oftyping.Annotated.AlexWaygood commentedon Oct 10, 2021
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 atyping._AnnotatedAliasinstance. But the__metadata__attribute is undocumented -- there is no mention of it in the docstring for_AnnotatedAlias, in the documentation fortyping.Annotated(https://docs.python.org/3.11/library/typing.html#typing.Annotated), or PEP-593, which introducedtyping.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_hintson a function/class where an argument/attribute is annotated with an_AnnotatedAliasinstance is a way of retrieving the type hints for the function/class with the_AnnotatedAliasinstance unresolved. However, if you callget_type_hintson the_AnnotatedAliasinstance directly, this fails.gvanrossum commentedon Feb 3, 2022
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.
martindemello commentedon Oct 3, 2022
looks like #96080 fixed this
Jackenmen commentedon Oct 3, 2022
__metadata__attribute appears to still not be documented so if it's not tracked elsewhere, it would make sense to make an issue for tracking the documentation about runtime inspection of Annotated.martindemello commentedon Oct 3, 2022
also for
x = Annotated[int | str, "some metadata"],help(x)returnswhich is less than ideal.
AlexWaygood commentedon Jun 7, 2023
(To update on this: the
__metadata__field is now documented; see the PRs attached to #97797.)