help() appears confused about the module of typing.Annotated #89543
Copy link
Copy link
Open
Labels
3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes3.9 (EOL)end of lifeend of lifedocsDocumentation in the Doc dirDocumentation in the Doc dirstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-typingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
| BPO | 45380 |
|---|---|
| Nosy | @gvanrossum, @jack1142, @Fidget-Spinner, @AlexWaygood |
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 = None
closed_at = None
created_at = <Date 2021-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 = <Date 2022-02-03.23:18:38.705>
user = 'https://github.com/AlexWaygood'bugs.python.org fields:
activity = <Date 2022-02-03.23:18:38.705>
actor = 'gvanrossum'
assignee = 'docs@python'
closed = False
closed_date = None
closer = None
components = ['Documentation', 'Library (Lib)']
creation = <Date 2021-10-05.18:29:03.194>
creator = 'AlexWaygood'
dependencies = []
files = []
hgrepos = []
issue_num = 45380
keywords = []
message_count = 3.0
messages = ['403258', '403602', '412485']
nosy_count = 5.0
nosy_names = ['gvanrossum', 'docs@python', 'jack1142', 'kj', 'AlexWaygood']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue45380'
versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes3.9 (EOL)end of lifeend of lifedocsDocumentation in the Doc dirDocumentation in the Doc dirstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-typingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
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.)