Код: Выделить всё
from typing import Optional
def area_of_square(width: Optional[float] = None,
height: Optional[float] = None) -> float:
if width is None and height is None:
raise ValueError('You have not specified a width or height')
if width is not None and height is not None:
raise ValueError('Please specify a width or height, not both')
area = width**2 if width is not None else height**2
return area
Я мог бы добавить следующую строку чуть выше него:
Код: Выделить всё
height = typing.cast(int, height)
Лично я использую набор текста для удобства чтения и во избежание ошибок. Получение подобных ошибок (и часто с ленивой инициализацией и другим подобным использованием None) противоречит цели, поэтому я предпочитаю исправлять их, когда это имеет смысл.
Что какие стратегии люди используют в этом сценарии?
Подробнее здесь: https://stackoverflow.com/questions/728 ... y-and-none