Awesome
Method signature checking decorator for Python 3
Samples:
from typecheck import *
@typecheck
def foo(i: int, x = None, s: str = "default") -> bool:
...
@typecheck
def foo(*args, k1: int, k2: str = "default", k3 = None) -> nothing:
...
@typecheck
def foo(ostream: with_attr("write", "flush"), f: optional(callable) = None):
...
divisible_by_three = lambda x: x % 3 == 0
@typecheck
def foo(i: by_regex("^[0-9]+$")) -> divisible_by_three:
...
@typecheck
def reverse_2_tuple(t: (str, bytes)) -> (bytes, str):
...
@typecheck
def reverse_3_list(t: [int, float, bool]) -> [bool, float, int]:
...
@typecheck
def extract_from_dict(d: dict_of(int, str), k: tuple_of(int)) -> list_of(str):
...
@typecheck
def contains(x: int, xs: set_of(int)) -> bool:
...
@typecheck
def set_level(level: one_of(1, 2, 3)):
...
@typecheck
def accept_number(x: either(int, by_regex("^[0-9]+$"))):
...
@typecheck_with_exceptions(input_parameter_error = MemoryError)
def custom_input_error(x: int): # now custom_input_error("foo") throws MemoryError
...
@typecheck_with_exceptions(return_value_error = TypeError)
def custom_return_error() -> str: # now custom_return_error() throws TypeError
return 1