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.
pip install type-enforcedVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic usage of the `@Enforcer` decorator on a function. Type mismatches at runtime will raise a `TypeError`.
Upgrade Python to 3.11+; replace list-based unions with standard `|` or `typing.Union`; ensure dict type hints specify both key and value types.
Upgrade Python to 3.10+ (for `type_enforced>=1.10.0` but <2.0.0) or 3.11+ (for `type_enforced>=2.0.0`), or pin `type_enforced` to a compatible older version.
Define all forward-referenced types before their first use in a decorated function, or ensure they are accessible in the module's global scope.
Explicitly define the exact type if subclass matching is not desired, or be aware of the default behavior.
Be mindful of these parameters, especially in production environments, to ensure type enforcement is active and exceptions are raised as expected.
pip install type-enforced
Ensure the argument passed matches the type hint, or modify the type hint to correctly reflect expected input types.
Ensure the `@enforce_types` decorator is applied only to valid function, method, or class definitions.
Update the type hint to `Optional[str]` or `Union[str, None]` if `None` is an allowed value, or ensure a non-None value is passed.