pip install returns-decorator
from returns import returns
@returns(int)
def func(...):
...
The return value will be converted into int
before returning.
It's the equivalent of:
return int(return_value)
On every return statement.
It's most useful to convert a generator function into a tuple
/list
/set
/dict
.
@returns(tuple)
def generator_func():
yield ...
Doing this has a better performance than list.append
.
When None
is a possible return value,
you can pass allow_none=True
into the decorator.
@returns(int, allow_none=True)
def func(...):
if ...:
return None
return ...