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.0.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.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file typecheck_runtime-0.1.0.tar.gz.

File metadata

  • Download URL: typecheck_runtime-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 73eb0eb11811c2742110639f5c5242d0ceee5f4ab8eae8ae6d1d68422f7f094c
MD5 ef90ff5354407247071089a4442582db
BLAKE2b-256 04ed1219c9cfaa3de66e4cdd05342ecd6a3d356d0f58c5ee7a587a096abbffb1

See more details on using hashes here.

Provenance

The following attestation bundles were made for typecheck_runtime-0.1.0.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.0-py3-none-any.whl.

File metadata

File hashes

Hashes for typecheck_runtime-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7f25caafc451f9c6cb77fca9754c698c3e06ec6fd0ee2abf98803c49363a3e35
MD5 883e4f06c345b26a97994c072fe76295
BLAKE2b-256 fe6564daae9bd39e52f7d9fea4b27b84661b3c887141f338658889d6188928fb

See more details on using hashes here.

Provenance

The following attestation bundles were made for typecheck_runtime-0.1.0-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