Baza wiedzyCentrum egzaminacyjne
PLPolski
Wybierz język13

CKTIS / Python

Moduł typing

Opisuje zależności typów za pomocą adnotacji, unii, typów generycznych i protokołów oraz udostępnia ich metadane podczas działania.

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

Składnia

typing.get_type_hints(function, include_extras=True)

Opisuje zależności typów za pomocą adnotacji, unii, typów generycznych i protokołów oraz udostępnia ich metadane podczas działania.

Standardowy Python sam nie sprawdza typów wywołania. Przykład odczytuje metadane Annotated przez get_type_hints i get_args.

Bez include_extras pozostaje tylko typ bazowy.

Przykłady

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"))

Wynik

True ('tag',)
outside annotation

Bez include_extras pozostaje tylko typ bazowy.

Źródła