Сообщение об ошибке:
Код: Выделить всё
Function with declared return type "int" must return value on all code paths. "None" is incompatible with "int".
Код: Выделить всё
from dataclasses import dataclass
from typing import Any, Union, List
@dataclass
class IntOrString:
I: Union[int, None] = None
S: Union[str, None] = None
def funny_sum(xs: List[IntOrString]) -> int | Any:
if not xs:
return 0
head, *tail = xs
match head:
case IntOrString(I=i) if i is not None:
return i + funny_sum(tail)
case IntOrString(S=s) if s is not None:
return len(s) + funny_sum(tail)
xs: List[IntOrString] = [IntOrString(I=1), IntOrString(S="hello"), IntOrString(I=3), IntOrString(S="world")]
# Output will be 1 + 5 + 3 + 5 = 14
result = funny_sum(xs)
Код: Выделить всё
def funny_sum(xs: List[IntOrString]) -> int: { ... }
Я ценю любые советы по решению этой проблемы с объявлением типа возвращаемого значения в Python.
Подробнее здесь: https://stackoverflow.com/questions/787 ... clarations