Skip to main content

typesentinel

typesentinel

typesentinel is a lightweight, dependency-free library for runtime type checking of Python function arguments. It supports both synchronous and asynchronous functions, Union types, custom failure handlers, and signature-aware error messages.

pip install typesentinel

🧠 Why typesentinel?

Other libraries like Pydantic, Beartype, Enforce, or typeguard offer runtime validation, but often come with heavy dependencies, global monkey-patching, performance costs, or complicated configuration.

typesentinel focuses on one thing and does it well:

  • ✔ Minimal — zero dependencies, tiny footprint
  • ✔ Explicit — works only where you decorate; never global
  • ✔ Async-friendly — supports async functions & async failure handlers
  • ✔ Precise errors — error messages include the real parameter names
  • ✔ Flexible — annotations, shorthand, or explicit TypeCheck objects
  • ✔ Safe — never mutates your function’s signature or typing info

If you want simple, predictable runtime validation with no overhead, typesentinel is built for you.


🚀 Quick Start

Type checking from function annotations

from typesentinel.decorator import type_check

@type_check
def greet(name: str, excited: bool = False):
    return f"Hello, {name}{'!' if excited else ''}"

greet("Alice")       # OK
greet(123)           # ❌ Invalid type for argument 'name': expected str, got int

Shorthand keyword type checks

@type_check(name=str, times=int)
async def repeat(name, times):
    return ", ".join(name for _ in range(times))

🔍 Union Type Support

from typing import Union

@type_check(a=Union[int, str])
def fn(a):
    return a

@type_check(a=int | str)
def fn(a):
    return a

Error message:

Invalid type for argument 'a': expected int | str, got float

🛠 Custom Failure Handling

from typesentinel.decorator import type_check
from typesentinel.type_check import TypeCheckResult

async def capture(*fails: TypeCheckResult):
    raise ValueError("validation failed")

@type_check(a=int, on_failure=capture)
async def double(a):
    return a * 2

🧩 Explicit TypeCheck Objects

from typesentinel.decorator import type_check
from typesentinel.type_check import TypeCheck, ArgKind

checks = [
    TypeCheck(0, int, ArgKind.POSITIONAL),
    TypeCheck("label", str, ArgKind.KEYWORD, message="label must be a string"),
]

@type_check(checks)
def render(value, *, label):
    return f"{label}: {value}"

🧪 Testing

python -m pytest

📄 License

MIT License. See LICENSE for details.


---

Metadata

Release files for typesentinel 0.2.3

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

Source distribution (sdist)

Source distribution for typesentinel 0.2.3
File Size Uploaded Provenance
typesentinel-0.2.3.tar.gz 11.5 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 20.4 kB

Release files / typesentinel-0.2.3.tar.gz

Download URL typesentinel-0.2.3.tar.gz
Size 11.5 kB
Tags Source
SHA-256 checksum
How to use checksums
532adc78e53f375f534745ef08c4be09853ce7dfa432ea314bb5cef7596d4207
BLAKE2b-256 checksum
How to use checksums
38782e33e990a101e838afb83b87ea4946da4d6c391b2512237317a13ffa99bd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.0

Release files / typesentinel-0.2.3-py3-none-any.whl

Download URL typesentinel-0.2.3-py3-none-any.whl
Size 9.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f8595c068feb68b4089812c6f5710c04be3c1d240058c96dc28dcce7fba72fc7
BLAKE2b-256 checksum
How to use checksums
a9068f0d72b65cfa33d40bf7445277314891ee15e5a6b39ec84e7233e66d0ca7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.0

Security for typesentinel 0.2.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

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