Skip to main content

A comprehensive runtime type hint checker for use with Python 3.10 and above.

This library provides pure-Python functions and base classes to check type hints at runtime, helping to catch type-related errors during data processing and manipulation.

It is not intended to be used for validating function parameters in production code. It can be used for that purpose, but it is not optimized for performance in that scenario.

Other tools such as typeguard and beartype are much better suited for that purpose as they are specifically optimized for performance in checking function parameters at call time by injecting compiled type checks into function call sites.

It is intended for use in a more general context, such as validating complex data structures, config files, or complex objects where type correctness is critical such as during data ingestion, transformation, or serialization/deserialization.

It supports checking of standard type hints as defined in PEP 484, PEP 585, PEP 586, PEP 589, PEP 591, PEP 593, and PEP 604 among others, as well as more complex type constructs such as nested generics, unions, literals, typed dictionaries, and recursive types. These are support for both built-in and user-defined classes and dataclasses.

Supported Typing Constructs

  • Basic types: int, str, float, bool, etc.

  • Generic types: Set, Mapping, Sequence, Iterable, Callable, Collection

  • Specific types: list, dict, OrderedDict, frozenset, tuple, etc.

  • Generic aliases: list[int], dict[str, float], etc.

  • Tuple types: Tuple, tuple, Tuple[int, …], Tuple[int, str], etc

  • Union types: Union, Optional, |, etc.

  • Final types: Final

  • TypeVar

  • NewType types: NewType

  • Annotated types: Annotated

  • Literal types: Literal

  • Typed dictionaries: TypedDict, NotRequired, Required, ReadOnly, total, extras

  • Runtime Protocols: Protocol

  • Nested generics: List[Dict[str, Union[int, str]]], etc.

  • User-defined classes and dataclasses

  • Nested combinations of the above

It especially shines in scenarios where data structures are deeply nested immutable structures (lists, tuples, dicts, sets, frozensets) containing various combinations of types that need to be validated at runtime because it caches type information for objects for improved performance on repeated checks and subtree validations to avoid redundant checks.

If you check a subcontainer (e.g., a tuple within a MappingProxyType within a frozenset), it will cache the results of each subtree so that if the same subtree is encountered again, it can skip re-validating that subtree.

This library can be used in data processing pipelines, ETL processes, or any scenario where data integrity and type correctness are paramount.

It also provides deep immutability checking for deep immutable types via the typechecked.immutable submodule to validate data structures that should not be modified after creation. This is useful for ensuring that certain data structures remain unchanged throughout their lifecycle, preventing accidental mutations that could lead to bugs or data corruption. It checks not just the top-level container, but also all nested containers and their contents to ensure complete immutability.

It provides a base class Immutable that can be inherited by user-defined classes to label them as immutable and have their instances marked as immutable during type checking.

It can check TypedDict definitions against any Mapping type at runtime, allowing for validation of dictionary-like structures against TypedDict schemas.

Installation

You can install TypeChecked via pip:

pip install typechecked

It has few external dependencies and relies on the Python standard library except for the use of typing-extensions to allow older Python versions to use newer typing features when available.

Usage

from typechecked import isinstance_of_typehint

MyDataStructure = dict[str, list[int]]

data = {"numbers": [1, 2, 3]}
if isinstance_of_typehint(data, MyDataStructure):
    print("Data is valid")
else:
    print("Data is invalid")

Contributing

This project is open to contributions! If you find a bug or have a feature request, please open an issue on GitHub. Pull requests are also welcome.

Bootstrapping Development Environment

Fork a copy of the repository on GitHub.

Clone your fork the cloned repository: git clone https://github.com/your-username/python-typechecked.git

Change to the project directory: cd python-typechecked

Run the bootstrap script:

python bootstrap.py

And follow the prompts.

This will create a virtual environment in the project root without modifying your system or user Python installation.

It installs the necessary minimum tooling to get started working with development tasks such as testing, linting, and building documentation.

Metadata

Release files for typechecked 0.11.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for typechecked 0.11.1
File Size Uploaded
typechecked-0.11.1.tar.gz 692.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for typechecked 0.11.1
File Interpreter ABI Platform
typechecked-0.11.1-py3-none-any.whl Python 3 none any Details

Total release size: 750.6 kB

Release files / typechecked-0.11.1.tar.gz

Download URL typechecked-0.11.1.tar.gz
Size 692.4 kB
Tags Source
SHA-256 checksum
How to use checksums
036dcb4680c424f79c8904a629bc0e45efb43a1021771ef2a74bdcad8afd7135
BLAKE2b-256 checksum
How to use checksums
35e95fa81b10992cc7c4e8f523b02a06d206b996ba219ce82bc5d9dcfe0ded55
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / typechecked-0.11.1-py3-none-any.whl

Download URL typechecked-0.11.1-py3-none-any.whl
Size 58.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e78bb963705f869960ac44b9c6197fda68e997910c812a37d2074e3833d50bf1
BLAKE2b-256 checksum
How to use checksums
b7e04779a58e7ce68f8b74fe7228d1ce034949a5c2f6abb1d0434da30c5f48f1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page