Beartype

0.22.9 · active · verified Sat Mar 28

Beartype is an open-source, pure-Python, PEP-compliant, near-real-time runtime-static type-checker. It emphasizes efficiency, portability, and readability, ensuring O(1) non-amortized worst-case runtime complexity with negligible constant factors. The library has no runtime dependencies and is actively maintained with frequent patch releases addressing compatibility and bug fixes, as evidenced by its rapid `0.22.x` release cycle.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the core usage of the `@beartype` decorator. It shows a function `quote_wiggum` annotated with standard type hints. When called with valid parameters (a list of strings), it executes normally. When called with invalid parameters (a list of bytes), `beartype` intercepts the call and raises a `BeartypeCallHintPepParamException`, providing a clear error message about the type violation.

from beartype import beartype

@beartype
def quote_wiggum(lines: list[str]) -> None:
    print('“{}”\n\t— Police Chief Wiggum'.format("\n ".join(lines)))

# Valid call
quote_wiggum(["Okay, folks. Show's over!", "Nothing to see here."])

# Invalid call (will raise BeartypeCallHintPepParamException)
try:
    quote_wiggum([b"Oh, my God! A horrible plane crash!"])
except Exception as e:
    print(f"Caught expected error: {e.__class__.__name__}: {e}")

view raw JSON →