With most type checkers the following works without explicitly saying that the value is nonable:
def do_something_cool(number1: int, letter1: str, seed: int = None) -> str:
return
However, you can also explicitly specify that the value is nonable:
from typing import Optional
def do_something_cool(number1: int, letter1: str, seed: Optional[int] = None) -> str:
return
Big warning here: typing.Optional
actually means Nonable and has nothing to do with the fact that your value has a default value (so it does not mean "optional"). See this discussion. One way to remember it is to think of Optional
as applying to the type, not to the value.
PEP484 was previously recommending the implicit writing, but now recommends that you explicitly states Optional
when your value is Nonable. See here.