CKTIS / Python
typing.Annotated
Добавя подредени метаданни към базов тип.
Версия: Python 3.14; repository image, production runtime not attested
Синтаксис
Annotated[T, metadata, ...]Добавя подредени метаданни към базов тип.
Проверяващ инструмент без поддръжка за тях използва базовия тип. Функцията get_type_hints по подразбиране премахва добавките, а с include_extras=True ги запазва.
Етикетът за лимит не проверява стойността 8.
Примери
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))
Резултат
True ('limit=5', 'units')
8Етикетът за лимит не проверява стойността 8.