Skip to main content

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 None if 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 None when using checked
  • 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

typecheck_runtime-0.1.1.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

typecheck_runtime-0.1.1-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

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

Hashes for typecheck_runtime-0.1.1.tar.gz
Algorithm Hash digest
SHA256 4958bf6ad42a5d0977113ef355f9b67d712d04f1aad4ecec9954a07e1020185b
MD5 178fba73fe916306856bc770f36313a9
BLAKE2b-256 011cb9e9e46bc31241196203aa6a6deedf0c5933621d03679ca4425f63591e4b

See more details on using hashes here.

Provenance

The following attestation bundles were made for typecheck_runtime-0.1.1.tar.gz:

Publisher: release.yml on schizza/py-typecheck

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file typecheck_runtime-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for typecheck_runtime-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d27f2c7d9fd854344228550808e9a8ce90f4b1d5e0617778c5a3604ddc76961c
MD5 02ce33c28c15e51755746cd82fa37c77
BLAKE2b-256 290c9e585c9ee2f9f8c2f1be085de9daa3686af6c7cd759f4071e8e7002f938a

See more details on using hashes here.

Provenance

The following attestation bundles were made for typecheck_runtime-0.1.1-py3-none-any.whl:

Publisher: release.yml on schizza/py-typecheck

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page