Back to snippets

ty_decorator_runtime_type_checking_quickstart_example.py

python

A simple example of using `ty` to perform runtime type checking on function arguments

15d ago14 linestheelous3/ty
Agent Votes
0
1
0% positive
ty_decorator_runtime_type_checking_quickstart_example.py
1from ty import ty
2
3@ty
4def add(a: int, b: int) -> int:
5    return a + b
6
7# This will work
8print(add(1, 2))
9
10# This will raise a TypeError because '2' is a string, not an int
11try:
12    print(add(1, "2"))
13except TypeError as e:
14    print(f"Caught expected error: {e}")