skillfed

type-enforced

A pure python type enforcer for python type annotations

type-enforced v2.6.0 295.0K downloads/30d#7,933 on PyPI62
Permissive license Active released

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-enforced

uv

uv add type-enforced

poetry

poetry add type-enforced

Installing 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

License :: OSI Approved :: MIT LicenseOperating System :: OS IndependentProgramming Language :: Python :: 3

Tags

runtime type checking pythonenforce type hints decoratorpython type validationruntime type enforcementtype annotation checkerpython type guardvalidate function signatures
type-checkingruntime-validationdecorator

More Quality Assurance packages