philiprehberger-type-guard
Runtime type checking decorators for function arguments.
Installation
pip install philiprehberger-type-guard
Usage
Basic Usage
from philiprehberger_type_guard import guard
@guard
def greet(name: str, times: int = 1) -> str:
return name * times
greet("hello", 3) # Works fine
greet(123, 3) # Raises TypeGuardError
Generic Types
@guard
def process(items: list[int], lookup: dict[str, float]):
...
process([1, 2, 3], {"a": 1.0}) # OK
process(["a"], {}) # TypeGuardError
Union Types
@guard
def flexible(value: int | str | None):
...
flexible(42) # OK
flexible("hi") # OK
flexible(None) # OK
flexible(3.14) # TypeGuardError
Non-raising and assertion checks
For one-off checks outside the @guard decorator, use is_type() (boolean) or assert_type() (raises on mismatch).
from philiprehberger_type_guard import is_type, assert_type
is_type(5, int) # True
is_type("x", int) # False
is_type([1, 2], list[int]) # True
is_type(None, int | None) # True
assert_type(5, int) # returns None
assert_type("x", int) # raises TypeGuardError
assert_type("x", int, name="user_id") # custom name in message
assert_type() honors the global enable() / disable() toggle.
Global Toggle
from philiprehberger_type_guard import enable, disable
disable() # Turn off all guards (e.g., in production)
enable() # Turn back on
Error Details
from philiprehberger_type_guard import TypeGuardError
try:
greet(123, 3)
except TypeGuardError as e:
print(e.param) # "name"
print(e.expected) # "str"
print(e.actual) # <class 'int'>
print(e.value) # 123
API
| Function / Class | Description |
|---|---|
@guard / @guard(enabled=True) |
Decorator for runtime type checking |
is_type(value, expected) |
Non-raising check; returns True / False |
assert_type(value, expected, name="value") |
Raises TypeGuardError if value does not match expected |
enable() / disable() |
Global toggle |
TypeGuardError |
Raised on type mismatch (subclass of TypeError) |
Development
pip install -e .
python -m pytest tests/ -v
Support
If you find this project useful:
License
Metadata
Release files for philiprehberger-type-guard 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| philiprehberger_type_guard-0.2.0.tar.gz | 183.9 kB | Without provenance | Details for philiprehberger_type_guard-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| philiprehberger_type_guard-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for philiprehberger_type_guard-0.2.0-py3-none-any.whl |
Total release size: 189.3 kB
Release files / philiprehberger_type_guard-0.2.0.tar.gz
| Download URL | philiprehberger_type_guard-0.2.0.tar.gz |
|---|---|
| Size | 183.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e229a45422913a9df59a06746cdfd82c389c08f63aec5906efe0091e3c3643ff
|
|
BLAKE2b-256 checksum How to use checksums |
e35cd312af4252947d1df9ccae56e0d41664fe531fcd250ea05e2503b06ff486
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.13
|
Release files / philiprehberger_type_guard-0.2.0-py3-none-any.whl
| Download URL | philiprehberger_type_guard-0.2.0-py3-none-any.whl |
|---|---|
| Size | 5.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ba7ffd0a7fec6bc134cdc82a6ab7a2136a77fa890f9a14289a4d96e02b34ce9a
|
|
BLAKE2b-256 checksum How to use checksums |
7437dcbf93917fc8a84483a10433e0ece41eb612a6b9a8366c5078e91465c88a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.13
|
Security for philiprehberger-type-guard 0.2.0
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.
To learn more about protecting yourself from malicious actors, visit our package security documentation.