Tiny runtime checker for typing annotations (dict[str,str], list[T], unions, literals) with an Option-like API.
Project description
typecheck-runtime
Tiny runtime checker for typing annotations with an Option-like API.
py-typecheck solves a very specific problem:
I have a value (object or Any) and I want to check if the value refers to specific typing annotation.
Does not parse data. Does not transforms data.
Just checks structure and returns refered value or
None if validation failed.
Installation
pip install typecheck-runtime
Python ≥ 3.10
Core idea
Main function is checked
from py_typecheck import checked
Acts like Option[T] or Rust-like Some(T, None):
- returns value if matches the type
- returns
Noneif validation failed
No exceptions, no side-efects.
Basic usage
value: object = {"a": 1}
if (d := checked(value, dict[str, int])) is not None:
print(d["a"] + 1)
Important
- always compare against
Nonewhen usingchecked - do not rely on truthiness (0, False, [] are valid values!)
- bool is treated as not matching int (so True wont pass as int)
Supported typing constructs
py-typecheck supports common runtime-validable constructs from typing:
Primitive types
checked(1, int) # 1
checked("x", int) # None
checked(True, int) # None
Note: bool is not considered an int in this library.
Union (X | Y, typing.Union)
checked(1, int | str) # 1
checked("x", int | str) # "x"
checked(1.5, int | str) # None
List / Set / FrozenSet
checked([1, 2], list[int]) # [1, 2]
checked({1, 2}, set[int]) # {1, 2}
checked(frozenset({1}), frozenset[int])
Nested structures work as expected:
checked([[1, 2], [3]], list[list[int]])
Dict
checked({"a": 1}, dict[str, int])
checked({"a": "x"}, dict[str, int]) # None
Supports unions and nesting:
checked({"a": [1, 2]}, dict[str, list[int]])
Tuple
Fixed-length:
checked((1, "x"), tuple[int, str])
Variadic:
checked((1, 2, 3), tuple[int, ...])
Literal
from typing import Literal
checked(True, Literal[True]) # True
checked(False, Literal[True]) # None
Any
from typing import Any
checked(object(), Any) # always matches
is_type
There is also a boolean helper:
from py_typecheck import is_type
if is_type(value, dict[str, int]):
...
- It returns only True / False.
- For most code paths, checked is preferred, because it avoids type confusion and double-checking mistakes.
Design principles
- Explicit is better than clever
- No exceptions for control flow
- No implicit casting
- No data mutation
- No dependency on dataclasses or models
- One function, one responsibility
This is not a validator framework. This is a runtime structural check with a safe return value.
Non-goals
py-typecheck intentionally does not:
- coerce types ("1" → 1)
- fill defaults
- validate constraints (ranges, regexes, etc.)
- replace pydantic or attrs
If you want parsing + validation + coercion → use Pydantic.
If you want “is this already the right shape?” → use this.
Comparison
| Tool | Purpose |
|---|---|
| isinstance | Runtime type only |
| typing | Static type checking |
| pydantic | Parsing + validation + coercion |
| py-typecheck | Runtime structural check only |
Development status
- Fully typed (basedpyright strict)
- Tested against Python 3.10 – 3.13
- CI + lint + coverage
- Small surface area, easy to audit
License MIT
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 typecheck_runtime-0.1.1.tar.gz.
File metadata
- Download URL: typecheck_runtime-0.1.1.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4958bf6ad42a5d0977113ef355f9b67d712d04f1aad4ecec9954a07e1020185b
|
|
| MD5 |
178fba73fe916306856bc770f36313a9
|
|
| BLAKE2b-256 |
011cb9e9e46bc31241196203aa6a6deedf0c5933621d03679ca4425f63591e4b
|
Provenance
The following attestation bundles were made for typecheck_runtime-0.1.1.tar.gz:
Publisher:
release.yml on schizza/py-typecheck
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
typecheck_runtime-0.1.1.tar.gz -
Subject digest:
4958bf6ad42a5d0977113ef355f9b67d712d04f1aad4ecec9954a07e1020185b - Sigstore transparency entry: 780800651
- Sigstore integration time:
-
Permalink:
schizza/py-typecheck@7fb0ca456b68fa73a5d10e92dd8ef20feb90c176 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/schizza
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@7fb0ca456b68fa73a5d10e92dd8ef20feb90c176 -
Trigger Event:
push
-
Statement type:
File details
Details for the file typecheck_runtime-0.1.1-py3-none-any.whl.
File metadata
- Download URL: typecheck_runtime-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d27f2c7d9fd854344228550808e9a8ce90f4b1d5e0617778c5a3604ddc76961c
|
|
| MD5 |
02ce33c28c15e51755746cd82fa37c77
|
|
| BLAKE2b-256 |
290c9e585c9ee2f9f8c2f1be085de9daa3686af6c7cd759f4071e8e7002f938a
|
Provenance
The following attestation bundles were made for typecheck_runtime-0.1.1-py3-none-any.whl:
Publisher:
release.yml on schizza/py-typecheck
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
typecheck_runtime-0.1.1-py3-none-any.whl -
Subject digest:
d27f2c7d9fd854344228550808e9a8ce90f4b1d5e0617778c5a3604ddc76961c - Sigstore transparency entry: 780800653
- Sigstore integration time:
-
Permalink:
schizza/py-typecheck@7fb0ca456b68fa73a5d10e92dd8ef20feb90c176 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/schizza
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@7fb0ca456b68fa73a5d10e92dd8ef20feb90c176 -
Trigger Event:
push
-
Statement type: