Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should a Python function that doesn't return a value have `-> None` in its signature?

Tags:

python

mypy

When writing a typed function in Python that does not return a value (like "void" in other languages), is it best and/or conventional to mark it as follows?

def say_hello(name: str) -> None
    print(f"Hello, {name}.")

Alternatively, should one just omit the -> None?

def say_hello(name: str)

I suppose what I'm asking is whether omitting the -> None leaves the return type unspecified, or it is assumed to be NoneType anyway, and thus unnecessary?

like image 866
Noldorin Avatar asked Apr 10 '26 03:04

Noldorin


1 Answers

The default for a missing annotation is Any, not None. If you leave out the return annotation, type checkers will treat your function as returning Any.

For a checked function, the default annotation for arguments and for the return type is Any.

like image 135
user2357112 supports Monica Avatar answered Apr 11 '26 15:04

user2357112 supports Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!