1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
from typing import Callable
from typeguard import typechecked
@typechecked
def foo(x: str) -> str:
return "hello " + x
def takes_callable(f: Callable[[str], str]) -> str:
return f("typeguard")
takes_callable(foo)
@typechecked
def has_valid_arguments(x: int, y: str) -> None:
pass
def has_valid_return_type(y: str) -> str:
return y
@typechecked
class MyClass:
def __init__(self, x: int) -> None:
self.x = x
def add(self, y: int) -> int:
return self.x + y
def get_value(c: MyClass) -> int:
return c.x
@typechecked
def get_value_checked(c: MyClass) -> int:
return c.x
def create_myclass(x: int) -> MyClass:
return MyClass(x)
@typechecked
def create_myclass_checked(x: int) -> MyClass:
return MyClass(x)
get_value(create_myclass(3))
get_value_checked(create_myclass_checked(1))
|