Код: Выделить всё
from dataclasses import dataclass
from typing import Annotated
@dataclass
class Pizza:
price: Annotated[float, "money"]
size: Annotated[float, "dimension"]
@dataclass
class Topping:
sauce: Annotated[str, "matter"]
Код: Выделить всё
def bake_pizza(pizza: Pizza) -> Pizza:
some_operation
return new_pizza
def check_if_vegetarian(topping: Pizza.Topping) -> bool:
some_operation
return True
Теперь моя проблема в том, что если я использую атрибуты, я, очевидно, не смогу сделать то же самое, поэтому, например, я не могу написать функция типа:
Код: Выделить всё
def calculate_cost(cost: Pizza.price) -> float:
some_calculation
return cost
Код: Выделить всё
def calculate_cost(cost: Pizza.__annotations__["price"]) -> float:
some_calculation
return cost
Подробнее здесь: https://stackoverflow.com/questions/790 ... -the-class