Base di conoscenzaCentro esami
ITItaliano
Scegli la lingua13

CKTIS / Python

typing.Annotated

Associa metadati ordinati a un tipo di base.

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

Sintassi

Annotated[T, metadata, ...]

Associa metadati ordinati a un tipo di base.

Un verificatore che li ignora tratta l’annotazione come tipo di base. La funzione get_type_hints elimina gli extra salvo con include_extras=True.

L’etichetta del limite non verifica il valore 8.

Esempi

from typing import Annotated, get_args, get_type_hints

def quantity(value: Annotated[int, "limit=5", "units"]) -> int:
    return value

base = get_type_hints(quantity)["value"]
full = get_type_hints(quantity, include_extras=True)["value"]
print(base is int, get_args(full)[1:])
print(quantity(8))

Risultato

True ('limit=5', 'units')
8

L’etichetta del limite non verifica il valore 8.

Fonti