Type Enforced

2.3.0 · active · verified Sun Apr 12

Type-enforced is a pure Python library that provides runtime type enforcement for Python type annotations. It uses a decorator-based approach to validate type hints in functions, methods, and classes, ensuring data integrity at runtime. The library is currently at version 2.3.0 and maintains an active release cadence with frequent updates and bug fixes.

Warnings

Install

Imports

Quickstart

Demonstrates basic usage of the `@Enforcer` decorator on a function. Type mismatches at runtime will raise a `TypeError`.

from type_enforced import Enforcer

@Enforcer()
def greet(name: str, age: int) -> str:
    return f"Hello, {name}! You are {age} years old."

print(greet("Alice", 30))

try:
    greet("Bob", "twenty") # This will raise a TypeError
except TypeError as e:
    print(f"Caught expected error: {e}")

view raw JSON →