The new fastest runtime Python Type Validation: Fast where it counts, thorough where it matters.
Project details
type_enforced offers a powerful and efficient solution for runtime validation of Python type annotations. Designed specifically for Python 3.11+, it brings uncompromising performance with zero dependencies, making it an ideal choice for developers who value speed and reliability in their applications.
type_enforced is a blazing-fast runtime type enforcement library for Python 3.11 and later, designed to enhance the safety and reliability of code through rigorous validation of type annotations. By eliminating external runtime dependencies and offering uncompromising performance, it provides a robust solution for developers looking to enforce type safety without sacrificing speed.
@Enforcer) or near instant O(1) sampled validation (@FastEnforcer with 'first', 'last', 'bookend', 'log', or percentage sampling).|), nested generics, Literal, Self, TypedDict, NewType, TypeGuard/TypeIs, Callable, dataclasses, and custom Constraint rules.Static type checkers (like mypy or pyright) catch errors during development, but offer zero protection at runtime against dynamic payloads, untyped API responses, or user data.
Existing runtime type checkers force an unnecessary compromise:
type_enforced eliminates this compromise by delivering both the fastest full validation and the fastest sampled validation across Python.
Easily enforce functions, methods, classes, or entire modules:
import type_enforced
# Complete validation (validates all items in collections)
@type_enforced.Enforcer
def greet(name: list[str], repeat: int = 1) -> str:
return f"Hello {', '.join(name)}!" * repeat
greet(["Alice"], 2) # Returns "Hello Alice!Hello Alice!"
greet(["Alice"], "twice") # Raises TypeError
# Fast O(1) sampled validation
@type_enforced.FastEnforcer
def process_tags(tags: list[str]) -> int:
return len(tags)
# Enforce an entire module in a single line
import my_package
type_enforced.ModuleEnforcer(my_package)
Added differential validation overhead in nanoseconds (ns). Note: lower is better:
| Type | Size | type_enforced (Sample=1) | Beartype (Sample=1) | type_enforced (100%) | Pydantic (100%) | Typeguard (100%) |
|---|---|---|---|---|---|---|
int | — | 10.6 ns | 194.4 ns | 10.5 ns | 446.0 ns | 1846.8 ns |
Union[int, float] | — | 16.1 ns | 211.3 ns | 16.2 ns | 503.0 ns | 3899.4 ns |
str | — | 10.9 ns | 196.0 ns | 10.5 ns | 440.6 ns | 1865.4 ns |
list[int] | 1 000 items | 27.6 ns | 334.8 ns | 462.4 ns | 10991.3 ns | 1044342.6 ns |
dict[str, int] | 1 000 keys | 27.6 ns | 340.4 ns | 3087.4 ns | 39815.9 ns | 2044397.2 ns |
list[list[int]] | 10 x 100 items | 25.2 ns | 371.7 ns | 374.9 ns | 11485.1 ns | 1044535.2 ns |
type_enforced.FastEnforcer is up to ~15x faster than Beartype.type_enforced.Enforcer is up to ~40x faster than Pydantic on scalars and up to ~20x faster on nested collections, while outperforming Typeguard by orders of magnitude.int, str, float, bool, int | str, int | None.list[int], dict[str, list[int]], set[str], tuple[int, ...], etc.Self, TypedDict, NewType, LiteralString, NoReturn, TypeGuard/TypeIs, TypeVar.type[Class], Callable[[int, str], bool], and @dataclass.Constraint(ge=0, le=100)), regex patterns (Constraint(pattern=...)), and custom lambda predicates (GenericConstraint).In summary, type_enforced stands as the fastest and most flexible runtime type enforcer for Python, giving you airtight runtime safety without the performance penalty.
Comments
0Start the conversation
Share the first comment.