inspect.get_annotations
Prev: inspect.FullArgSpec | Up: inspect | Next: inspect.getabsfile
Rel: inspect.ArgInfo, inspect.Arguments, inspect.Attribute, inspect.BlockFinder
Ref: docs.python.org — inspect.get_annotations
Tags: #public #python3 #stdlib #inspect
Import
import inspect
from inspect import get_annotations
Signature & Overview
inspect.get_annotations(obj, *, globals=None, locals=None, eval_str=False, format=<Format.VALUE: 1>)
Compute the annotations dict for an object.
Usage
import inspect
def fn(x: int) -> str:
return str(x)
assert inspect.get_annotations(fn) == {"x": int, "return": str}
Gotchas
- Requires Python 3.10+: Safer than
obj.__annotations__because on classes it never accidentally inherits a parent class's annotations; passeval_str=Trueto resolve stringified hints.
REPL Inspect
from inspect import get_annotations
help(get_annotations)
Linked from
Related notes
- tuts/python3/stdlib_module/inspect/FullArgSpec Linked here; links to this page; shared tags: #inspect, #stdlib
- tuts/python3/stdlib_module/inspect/getabsfile Linked here; links to this page; shared tags: #inspect, #stdlib
- tuts/python3/stdlib_module/inspect Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/inspect/ArgInfo Linked here; shared tags: #inspect, #stdlib
- tuts/python3/stdlib_module/inspect/Arguments Linked here; shared tags: #inspect, #stdlib
- tuts/python3/stdlib_module/inspect/Attribute Linked here; shared tags: #inspect, #stdlib
- tuts/python3/stdlib_module/inspect/BlockFinder Linked here; shared tags: #inspect, #stdlib
- tuts/python3/stdlib_module/inspect/BoundArguments shared tags: #inspect, #stdlib
- tuts/python3/stdlib_module/inspect/BufferFlags shared tags: #inspect, #stdlib
- tuts/python3/stdlib_module/inspect/ClassFoundException shared tags: #inspect, #stdlib