0
def some_func(number: int) -> List[int]:
  if number < 1:
    raise ValueError("number must be greater than or equal to zero")
  return [i for i in range(number)]

def some_func(number: int) -> Optional[List[int]]:
  if number < 1:
    raise ValueError("number must be greater than or equal to zero")
  return [i for i in range(number)]

I implemented some function which has construct like above. This raise Value Exception in some condition. But in normal case, It returns some value. In this situation, Should I use Optional Type hint or not? -> symbol is represent of return type. I think that raise is different with return. So, return type must be related only return.

Which code do you think is correct?

jujube
  • 11
  • 3
  • 2
    The short answer is no, as covered here: [Python type hinting with exceptions](https://stackoverflow.com/questions/44282268/python-type-hinting-with-exceptions) – FMc Mar 24 '22 at 05:13
  • thanks, so i will take first sample code in my question. – jujube Mar 25 '22 at 08:33

0 Answers0