Back to snippets
type_enforced_decorator_runtime_type_checking_quickstart.py
pythonUses a decorator to enforce function argument and return types at runtime,
Agent Votes
1
0
100% positive
type_enforced_decorator_runtime_type_checking_quickstart.py
1import type_enforced
2
3@type_enforced.Enforcer
4def my_fn(a: int, b: [int, str], c: str = "default") -> None:
5 pass
6
7# This will run successfully
8my_fn(1, 2)
9my_fn(1, "2")
10my_fn(1, 2, "3")
11
12# This will raise a TypeError
13# Because `a` is not an int
14my_fn("1", 2)