Runtime type checking decorators for function arguments
Project description
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
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
@guard/@guard(enabled=True)— Decorator for runtime type checkingenable()/disable()— Global toggleTypeGuardError— Raised on type mismatch (subclass ofTypeError)
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file philiprehberger_type_guard-0.1.3.tar.gz.
File metadata
- Download URL: philiprehberger_type_guard-0.1.3.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9c81fff3f7b696f6d9ea5c795da36aa0dde0a0e8fe5cc506e1480935c4a5f625
|
|
| MD5 |
ee31ebced620518958bdae0d6cf280f6
|
|
| BLAKE2b-256 |
564b656017ba8b833cd0e2999c9ea495b555b0fe67f2dec149593953f3fbbac5
|
File details
Details for the file philiprehberger_type_guard-0.1.3-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_type_guard-0.1.3-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b69fef2ae13f38be0d28d352483bde90157e58137fb5711a498f3900e5052120
|
|
| MD5 |
0326d6ad826f9a92bdd0779bff87581c
|
|
| BLAKE2b-256 |
c02935960cdd6a419296c8b3f39d55c3f41631ced83ba1efb3254d279f7dab2b
|