type-enforced
A pure python type enforcer for python type annotations
What it is and what it does
Type Enforced is a pure-Python runtime type checker that validates function and method signatures against their type annotations at execution time. It works as a decorator that can be applied to functions, methods, and classes, checking both input arguments and return values against their declared types. The package supports standard Python types, union types (both `int | str` and `typing.Union` syntax), nested generics like `dict[str, list[int]]`, and many typing module constructs including `Optional`, `Literal`, and `Any`.
The decorator offers configurable behavior: strict mode raises exceptions on type mismatches, non-strict mode logs warnings instead, and clean traceback mode hides internal stack frames for clarity. For large iterables, you can sample a percentage of items rather than checking all of them to reduce overhead. It requires Python 3.11 or higher for full feature support, though older versions can use earlier package releases with limited capabilities.
Use it for:
- Add runtime type safety to Python functions during development and debugging without modifying core logic
- Validate API endpoint inputs and outputs in web frameworks to catch type errors before they propagate
- Enforce type contracts in dataclass-based data pipelines to catch schema violations early
- Gradually migrate untyped Python codebases by decorating functions incrementally with runtime checks
- Disable type checking in production (via `enabled=False`) while keeping it active in test environments
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Enforces Python type annotations at runtime using decorators, validating function inputs, outputs, and nested generic types without requiring a compiler or external dependencies.
Yes, if you need runtime type validation for development or testing. The package is lightweight, actively maintained, has no dependencies, and works with modern Python. Best suited for catching type errors during development or in non-performance-critical code paths; consider static type checkers like mypy for production-grade type safety without runtime overhead.
Install
type-enforced on PyPI
pip
pip install type-enforceduv
uv add type-enforcedpoetry
poetry add type-enforcedInstalling type-enforced
Before you install
Low friction installation with no runtime dependencies. Active maintenance with recent release (71 days ago) and steady repository activity.
License in practice
MIT license permits unrestricted use, modification, and distribution in both open-source and proprietary projects.
Quickstart
pip install type_enforced
import type_enforced
@type_enforced.Enforcer(enabled=True, strict=True)
def my_fn(a: int, b: int | str = 2) -> None:
pass
my_fn(1, "test") # passes
my_fn("invalid", 2) # raises TypeError
Requires Python 3.11 or higher; earlier Python versions require older package versions with reduced feature support.
Verify before relying
- Performance overhead compared to static type checkers or other runtime enforcers at scale
- Whether sampling via iterable_sample_pct is sufficient for production type safety guarantees
- Support for forward references and string annotations in all contexts
Package facts
| License | not declared (permissive) |
| Python support | supports the current Python release (>=3.11) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | none |
| Maintenance | actively maintained — 71 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 295,015/month — #7,933 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: type_enforced-2.6.0-py3-none-any.whl
Tags
More Quality Assurance packages
Coverage.py measures which lines of Python code…
permissive · top 1,000 on PyPI
ruffRuff is a Python linter and code formatter…
permissive · top 1,000 on PyPI
pexpectPexpect spawns and controls interactive console…
permissive · top 1,000 on PyPI
blackBlack reformats Python source code to a…
permissive · top 1,000 on PyPI
pytest-xdistpytest-xdist distributes pytest tests across…
permissive · top 1,000 on PyPI
cfn-lintValidates AWS CloudFormation templates in YAML…
permissive · top 1,000 on PyPI
typing-utilsProvides utilities to inspect and validate…
permissive · top 5,000 on PyPI
typing-inspectProvides runtime inspection utilities for…
permissive · top 1,000 on PyPI
typing-jsonProvides type-aware JSON serialization and…
permissive · top 15,000 on PyPI
tachTach enforces module boundaries, dependency…
permissive · top 5,000 on PyPI
annotated-typesProvides reusable metadata objects for use with…
permissive · top 100 on PyPI
typepyTypepy checks, validates, and converts Python…
permissive · top 5,000 on PyPI
mypy-extensionsProvides type system extensions for Python…
unclear · top 1,000 on PyPI
beartypeBeartype is a runtime type checker that…
permissive · top 1,000 on PyPI
eval-type-backportEnables newer Python typing syntax (like `X |…
permissive · top 1,000 on PyPI
typing-validationRuntime validation of Python objects against…
copyleft · top 15,000 on PyPI