Registry / type-stubs / type-enforced

type-enforced

JSON →
library2.9.0pypypi✓ verified 23d ago

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-enforced
INSTALL
IMPORT
SIG · TYPE-ENFORCED
T
type-enforced
type-stubspythonv2.9.0
Install
3.4s avg
Import
45ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.10.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.046s · 17.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 3.4s · import 0.044s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Enforcer
from type_enforced import Enforcer

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}")
Debug
Known issues
breakingVersion 2.0.0 introduced significant breaking changes. Python 3.10 support was dropped (requiring >=3.11). List-based union types (e.g., `[int, float]`) are no longer supported; use `int | float` or `typing.Union[int, float]` instead. Dictionary type hints now strictly require both key and value types (e.g., `dict[str, int]`).
fix
Upgrade Python to 3.11+; replace list-based unions with standard `|` or `typing.Union`; ensure dict type hints specify both key and value types.
affects: >=2.0.0
breakingVersion 1.10.0 dropped support for Python 3.9. Users on Python 3.9 or older must use `type_enforced<=1.9.0` for full compatibility.
fix
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.
affects: >=1.10.0
gotchaWhen using forward references in type annotations (e.g., `def my_func(arg: 'MyClass')`), the type_enforced library lazily evaluates these types on the first call. Ensure the referenced class/type is defined and resolvable in the scope *before* the first execution of the decorated function/method to avoid resolution issues.
fix
Define all forward-referenced types before their first use in a decorated function, or ensure they are accessible in the module's global scope.
affects: All versions
gotchaBy default since version 2.1.0, `type_enforced` checks for subclasses when validating types. If you define `arg: ParentClass`, passing an instance of `ChildClass(ParentClass)` will pass validation. If strict exact type matching is required, this default behavior might be unexpected.
fix
Explicitly define the exact type if subclass matching is not desired, or be aware of the default behavior.
affects: >=2.1.0
gotchaThe `@Enforcer` decorator has `enabled` and `strict` parameters (both `True` by default). Setting `enabled=False` disables all type checking for that function/class. Setting `strict=False` will print a warning to the console on type mismatch instead of raising a `TypeError`.
fix
Be mindful of these parameters, especially in production environments, to ensure type enforcement is active and exceptions are raised as expected.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'type_enforced'
The 'type-enforced' library has not been installed in the current Python environment.
fix
pip install type-enforced
type_enforced.errors.TypeEnforcedError: Argument 'some_param' must be of type <class 'int'>, not <class 'str'>
A function or method decorated with `@enforce_types` was called with an argument whose runtime type does not match its annotated type hint.
fix
Ensure the argument passed matches the type hint, or modify the type hint to correctly reflect expected input types.
type_enforced.errors.TypeEnforcedError: The enforce_types decorator must be applied to a function, method, or class.
The `@enforce_types` decorator was applied to an object that is not a function, method, or class.
fix
Ensure the `@enforce_types` decorator is applied only to valid function, method, or class definitions.
type_enforced.errors.TypeEnforcedError: Argument 'value' must be of type <class 'str'>, not <class 'NoneType'>
A function or method expected a specific type (e.g., `str`), but `None` was passed without `None` being explicitly allowed in the type annotation.
fix
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.
Upgrade
Version history
2.9.0latest on PyPI · released Aug 26, 2026
Audit
Dependencies
pythonrequiredRequires Python 3.11 or higher. Older versions of type-enforced supported older Python versions (e.g., <=1.9.0 for 3.9, <=1.10.2 for 3.10).
Agent activity
24 hits · last 30 days
node
20
Amazon
1
OpenAI (training)
1
Resources
type-enforced — pip install type-enforced · libregistry