Runtime type checking using type hints.
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file typechecked-0.11.1.tar.gz.
File metadata
- Download URL: typechecked-0.11.1.tar.gz
- Upload date:
- Size: 692.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
036dcb4680c424f79c8904a629bc0e45efb43a1021771ef2a74bdcad8afd7135
|
|
| MD5 |
01caaf8c5950d2e4fb23dde40688e084
|
|
| BLAKE2b-256 |
35e95fa81b10992cc7c4e8f523b02a06d206b996ba219ce82bc5d9dcfe0ded55
|
File details
Details for the file typechecked-0.11.1-py3-none-any.whl.
File metadata
- Download URL: typechecked-0.11.1-py3-none-any.whl
- Upload date:
- Size: 58.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e78bb963705f869960ac44b9c6197fda68e997910c812a37d2074e3833d50bf1
|
|
| MD5 |
134d6c65fccfaeb2f4f6cd7ed3edb917
|
|
| BLAKE2b-256 |
b7e04779a58e7ce68f8b74fe7228d1ce034949a5c2f6abb1d0434da30c5f48f1
|