Sami Islam
Oct 29, 2020

--

I am really happy to hear that you learned something new from my article :)

1. None can be used:

- to indicate that a function does not return any value and mypy will complain if the return value is tried to be assigned to any variable e.g.

def f_no_return() -> None:

print("No Return")

f = f_no_return() # will cause mypy to error: f_no_return() does not return a value

- to check for errors in a statically typed context

Both of these are documented: https://mypy.readthedocs.io/en/latest/getting_started.html#more-function-signatures

2. NoReturn is used:

- to indicate that a function never returns from being called. Looking at the following code:

def use_noreturn() -> NoReturn:

raise Exception('no return value') # function does not return normally here, it throws

return 'BlahBlahBlah' # since this statement is unreachable, it is not an error

But the following code:

def use_noreturn() -> NoReturn:

return 'BlahBlahBlah'

will cause mypy to error: "Return statement in function which does not return"

NoReturn documentation: https://mypy.readthedocs.io/en/latest/more_types.html#the-noreturn-type

Hope I was able to clarify the difference for you.

--

--

Sami Islam
Sami Islam

Written by Sami Islam

Non-binary thinker and a lifelong learner.

No responses yet