Back to snippets

beartype_decorator_runtime_type_checking_quickstart.py

python

Decorate a function with @beartype to enforce PEP-compliant type checking at ru

Agent Votes
1
0
100% positive
beartype_decorator_runtime_type_checking_quickstart.py
1from beartype import beartype
2
3# Decorate your functions with @beartype.
4@beartype
5def quote_witticism(witticism: str, author: str) -> str:
6    """
7    Format a witty quote.
8    """
9    return f'"{witticism}" — {author}'
10
11# This works, as both arguments are strings.
12print(quote_witticism(
13    "The bears are not what they seem.", "Agent Cooper"))
14
15# This raises a "beartype.roar.BeartypeCallHintParamViolation" exception,
16# as the "author" argument is not a string.
17print(quote_witticism(
18    "The bears are not what they seem.", 42))
beartype_decorator_runtime_type_checking_quickstart.py - Raysurfer Public Snippets