Typeguard is a Python library that provides run-time type checking for functions and methods defined with PEP 484 type annotations, as well as for arbitrary objects. It acts as an additional layer of type safety alongside static type checkers like MyPy, catching type violations that can only be detected at run time. Currently at version 4.5.1, the library is actively maintained with regular updates.
pip install typeguardVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates using the `@typechecked` decorator on both functions and methods. It shows successful type checking and how `typeguard` raises a `TypeError` when type annotations are violated at runtime.
If encountering `TypeError: check_argument_types() got an unexpected keyword argument 'memo'`, consider downgrading to `typeguard==4.4.1` or refactoring code to use `@typechecked` or `check_type` which are less susceptible to this specific issue.
Be aware of this behavior and explicitly configure type checking for production if it's critical. If you need runtime checks in optimized mode, you would need to use `check_type()` explicitly or ensure the import hook is installed in a way that bypasses this optimization behavior (which is not the default for `@typechecked`).
Prefer using the `@typechecked` decorator for functions and methods, or the `check_type()` function for individual value checks, as these provide more robust and reliable runtime type enforcement.
Ensure your project runs on Python 3.9 or newer to use the latest versions of Typeguard. The `requires_python` metadata on PyPI reflects this, currently `>=3.9`.
Ensure that all values passed to functions or returned from them strictly adhere to their type annotations to avoid `TypeCheckError`. Use static type checkers (e.g., MyPy) during development to catch such issues preemptively, and implement careful input validation if external data is involved.
Ensure that arguments passed to type-checked functions and their return values strictly conform to their specified type hints to avoid `TypeCheckError`.
No dependency data recorded yet.