Skip to main content

Runtime type checking decorators with sync and async support.

Project description

typesentinel

pytype_check

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 pytype_check.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 pytype_check.decorator import type_check
from pytype_check.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 pytype_check.decorator import type_check
from pytype_check.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.


---

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

typesentinel-0.2.0.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

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

typesentinel-0.2.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file typesentinel-0.2.0.tar.gz.

File metadata

  • Download URL: typesentinel-0.2.0.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for typesentinel-0.2.0.tar.gz
Algorithm Hash digest
SHA256 1734507bb7849278b863a9f6f17f6bcc51171b9c698f326c3d52d0fe01ed1d34
MD5 650468e68ed41adaed667cdefe5e0ae0
BLAKE2b-256 fd9ccdc1690a5b32138d811a1293fccdad6785421b6b9138a6f578334b2d074c

See more details on using hashes here.

File details

Details for the file typesentinel-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: typesentinel-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for typesentinel-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 882e138b23e0c8821c61eb064ca7e4ea379b7ce9c461096727e5b4a84ba9245c
MD5 1d1a037dae2c88cc84d7fc991e5683bb
BLAKE2b-256 88914b4dc78d9c6583dc02f8401531b005ecaf49cf8b3b5e8990edf1f4d24dd9

See more details on using hashes here.

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