Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Module typing

Express static relationships using annotations, unions, generics and protocols, and inspect them at runtime.

Version: Python 3.14; repository image, production runtime not attested

Syntax

typing.get_type_hints(function, include_extras=True)

Express static relationships using annotations, unions, generics and protocols, and inspect them at runtime.

Standard Python does not automatically check annotated calls. The example attaches metadata with Annotated and retrieves it with get_type_hints and get_args.

Omitting include_extras strips the metadata while leaving the base type.

Examples

import typing
def identify(code: typing.Annotated[int, "tag"]) -> int:
    return code
base = typing.get_type_hints(identify)["code"]
full = typing.get_type_hints(identify, include_extras=True)["code"]
print(base is int, typing.get_args(full)[1:])
print(identify("outside annotation"))

Result

True ('tag',)
outside annotation

Omitting include_extras strips the metadata while leaving the base type.

Sources