🛡️ Guardian — Sub-Microsecond Runtime Type Enforcement & Access Control for Python
Guardian is a blazing-fast runtime type enforcer and data validation framework for Python 3.11+.
Unlike traditional Python validation libraries that rely on slow dictionary wrappers, getattr proxies, or __dict__ parsing, Guardian is written entirely as a native C-extension. It intercepts assignments directly at the memory level via the CPython API, delivering O(1) validation that outperforms industry standards like Pydantic (Rust).
🚀 Performance Benchmarks
Guardian is engineered for high-throughput backend APIs and data pipelines where nanosecond performance matters. (Benchmarks run for 100,000 iterations against standard Python 3.13 on native Linux)
| Operation / Library | Time (ns / op) | Relative Speed |
|---|---|---|
| Standard Python Function | 30.61 ns | 1.00x |
@guard Function |
80.79 ns | 2.64x |
| Beartype Function | 241.10 ns | 7.88x |
Standard / shield Init |
653.75 ns | N/A |
| Pydantic v2 Init (Rust) | 824.64 ns | N/A |
shield Attribute Set |
76.26 ns | N/A |
| Pydantic v2 Attribute Set | 194.05 ns | N/A |
Highlights
- Function decorators:
@guardis ~3× faster than Beartype - Class models:
shieldis ~2.5× faster than Pydantic v2 at attribute assignment
📦 Installation
Guardian distributes pre-compiled wheels for Linux, macOS, and Windows.
pip install guardian-type-enforcer
🛠️ Core Features
1. Guardian Dataclasses & Domain Validators
Guardian wraps the standard Python library @dataclass to provide native C-speed type enforcement with zero namespace pollution.
from guardian.dataclasses import dataclass, validator
@dataclass(frozen=True)
class DatabaseConfig:
host: str
port: int
@validator("host")
def sanitize_host(cls, value: str):
return value.strip().lower()
config = DatabaseConfig(host=" LOCALHOST ", port=5432)
print(config.host) # "localhost"
# ❌ Raises FrozenInstanceError
config.port = 8080
2. shield (C-Native Data Models & Encapsulation)
from guardian.shield import Shield
from guardian._guardian_core import GuardianTypeError, GuardianAccessError
class Account(Shield):
username: str
_balance: float
_total_accounts: int = 0
def __init__(self, username: str):
self.username = username
self._balance = 0.0
Account._total_accounts += 1
account = Account("Michex")
account.username = "Mike" # OK
account.username = 123 # Raises GuardianTypeError
print(account._balance) # Raises GuardianAccessError
Account._total_accounts = 999 # Raises GuardianAccessError
3. @guard (Boundary Type Enforcement)
from typing import Union
from guardian import guard
@guard
def process_sensor_data(payload: dict[str, list[Union[int, float]]]) -> bool:
return True
process_sensor_data({"sensor_A": [1, 2.5, 3]})
process_sensor_data({"sensor_A": [1, 2.5, "3.0"]})
4. @deepguard (Local State Profiling)
from guardian import deepguard
@deepguard
def calculate_discount(price: float) -> float:
discount: float = 0.0
if price < 0:
discount = "N/A" # Type error
return price - discount
🧠 Advanced Usage: The Compiler
Guardian compiles complex type hints into optimized internal representations for C-level evaluation.
🤝 Contributing
We recommend WSL 2 / Linux for development.
# Clone repository
git clone https://github.com/yourusername/guardian-type-enforcer.git
cd guardian-type-enforcer
# Create environment
uv venv --python 3.14
source .venv/bin/activate
# Install + build
uv pip install -e .
pytest tests/ -v
📄 License
MIT License — see LICENSE file.
🔄 Guardian Update Ledger
⚡ C-Core Memory & Profiling Optimizations
- Zero-allocation string routing using optimized CPython mappings
- PEP 667 compatibility for Python 3.13+ FrameLocalsProxy
- Read-path acceleration via native CPython attribute lookup
🏗️ Dataclass Engine & Static Typing
guardian.dataclasses.dataclassfully mirrors stdlib behavior- Frozen instance compatibility layer added
typing.dataclass_transformintegration for IDE support
🛡️ Access Control & Encapsulation
- Added
@validatorhook system - Extended frame-walking protection to class-level access
🛠️ Developer Experience
- Recommended Linux/WSL 2 build pipeline
- Adopted
uvfor ultra-fast environment management
Metadata
Release files for guardian-type-enforcer 4.0.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distributions (wheels)
Total release size: 334.4 kB
Release files / guardian_type_enforcer-4.0.6-cp314-cp314t-win_amd64.whl
| Download URL | guardian_type_enforcer-4.0.6-cp314-cp314t-win_amd64.whl |
|---|---|
| Size | 19.7 kB |
| Tags | CPython 3.14 CPython 3.14 free-threading Windows x86-64 |
|
SHA-256 checksum How to use checksums |
5bdb1a1d1b20012290c91ad0d3b39ca6be02bafc9f3e94965382fe7d5d3a9275
|
|
BLAKE2b-256 checksum How to use checksums |
109aea123afaae65a441b9be71ae517fb366b2700cdddcef564f4cfa89fcf39a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / guardian_type_enforcer-4.0.6-cp314-cp314t-win32.whl
| Download URL | guardian_type_enforcer-4.0.6-cp314-cp314t-win32.whl |
|---|---|
| Size | 18.9 kB |
| Tags | CPython 3.14 CPython 3.14 free-threading Windows x86-32 |
|
SHA-256 checksum How to use checksums |
4daaf13206950bcc95787ac34b7aa075826d780b0b54a700abeaf0964d007622
|
|
BLAKE2b-256 checksum How to use checksums |
5b136483334f2ded10ead4b7dd21599f577d3d6ebd3563ed0f35c3678b1342e8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / guardian_type_enforcer-4.0.6-cp314-cp314t-musllinux_1_2_x86_64.whl
| Download URL | guardian_type_enforcer-4.0.6-cp314-cp314t-musllinux_1_2_x86_64.whl |
|---|---|
| Size | 51.3 kB |
| Tags | CPython 3.14 CPython 3.14 free-threading Linux musl 1.2+ x86-64 |
|
SHA-256 checksum How to use checksums |
44000cfbc549969167329bace0e7e5f52891da140a81c8abc94ef3a1c778297b
|
|
BLAKE2b-256 checksum How to use checksums |
b4827aa6593e23258db579ef93c78b15026a70c6b73448dc5974f3f7315eb804
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / guardian_type_enforcer-4.0.6-cp314-cp314t-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
| Download URL | guardian_type_enforcer-4.0.6-cp314-cp314t-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl |
|---|---|
| Size | 52.6 kB |
| Tags | CPython 3.14 CPython 3.14 free-threading Linux glibc 2.28+ x86-64 Linux glibc 2.5+ x86-64 |
|
SHA-256 checksum How to use checksums |
ceba087ef7dd91b1e733725b58a2b5d70d00e80ea36bdd4f3090fb83f040d78a
|
|
BLAKE2b-256 checksum How to use checksums |
35d316b1f95ec41c1585019ce4cb5304359cb5cc30115449c4553f58a2dd35ce
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / guardian_type_enforcer-4.0.6-cp314-cp314t-macosx_11_0_arm64.whl
| Download URL | guardian_type_enforcer-4.0.6-cp314-cp314t-macosx_11_0_arm64.whl |
|---|---|
| Size | 18.9 kB |
| Tags | CPython 3.14 CPython 3.14 free-threading macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
bfe3206cfd9035c6295811600158f415d538fcf30011e694e4e5d98db37fee6d
|
|
BLAKE2b-256 checksum How to use checksums |
c68d21e77e160b7e04b034fda5e59e743ea478d2dc6277be9fd6d2bd17659633
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / guardian_type_enforcer-4.0.6-cp314-cp314t-macosx_10_15_x86_64.whl
| Download URL | guardian_type_enforcer-4.0.6-cp314-cp314t-macosx_10_15_x86_64.whl |
|---|---|
| Size | 18.3 kB |
| Tags | CPython 3.14 CPython 3.14 free-threading macOS 10.15+ x86-64 |
|
SHA-256 checksum How to use checksums |
6f80dd5c527a99c52961925dab003b20e14933455d4d29ab92e99239fa975f4e
|
|
BLAKE2b-256 checksum How to use checksums |
3668ee1e026c8a2b780f71c310b7f07f66174bca745b9e0aa159874db67cea0d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / guardian_type_enforcer-4.0.6-cp314-cp314-win_amd64.whl
| Download URL | guardian_type_enforcer-4.0.6-cp314-cp314-win_amd64.whl |
|---|---|
| Size | 19.3 kB |
| Tags | CPython 3.14 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
7d72575d621aaa348025974e9be0889c29e3bfb043af29fb161bd3005a8a97f4
|
|
BLAKE2b-256 checksum How to use checksums |
ec991c4938051e2e17aec59c00c7c034ea435aa5b9b60d497ff846ef4c911c53
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / guardian_type_enforcer-4.0.6-cp314-cp314-win32.whl
| Download URL | guardian_type_enforcer-4.0.6-cp314-cp314-win32.whl |
|---|---|
| Size | 18.5 kB |
| Tags | CPython 3.14 Windows x86-32 |
|
SHA-256 checksum How to use checksums |
40eeee1f5544f46fe54190c47859c639d883ccc9da1dee8073830a4ce255a70a
|
|
BLAKE2b-256 checksum How to use checksums |
835da26066f282fe80a152d455766ce025162794ce1f6d12be65c08ebcafb22d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / guardian_type_enforcer-4.0.6-cp314-cp314-musllinux_1_2_x86_64.whl
| Download URL | guardian_type_enforcer-4.0.6-cp314-cp314-musllinux_1_2_x86_64.whl |
|---|---|
| Size | 40.3 kB |
| Tags | CPython 3.14 Linux musl 1.2+ x86-64 |
|
SHA-256 checksum How to use checksums |
1ab095109ad6017712e1e1db11c245ea3f17a057ba52f5d33332b189a43c427c
|
|
BLAKE2b-256 checksum How to use checksums |
819bbb0e5e1d6b803d214c09522ebcdc11b50958b231d8a66bed7f41115f76ec
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / guardian_type_enforcer-4.0.6-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
| Download URL | guardian_type_enforcer-4.0.6-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl |
|---|---|
| Size | 40.6 kB |
| Tags | CPython 3.14 Linux glibc 2.28+ x86-64 Linux glibc 2.5+ x86-64 |
|
SHA-256 checksum How to use checksums |
8bcd8dd2593aa8edc370db6071ea1cd42898068f123618634227d03583353365
|
|
BLAKE2b-256 checksum How to use checksums |
38558d9fc8db969acdcd55c4d7fba1610ae9c5be87bc62ca820fb5c817a55ffb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / guardian_type_enforcer-4.0.6-cp314-cp314-macosx_11_0_arm64.whl
| Download URL | guardian_type_enforcer-4.0.6-cp314-cp314-macosx_11_0_arm64.whl |
|---|---|
| Size | 18.3 kB |
| Tags | CPython 3.14 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
11798302ac68ab88e5eab101de2e5822f8100df486cc48a5f3608e24df64ee44
|
|
BLAKE2b-256 checksum How to use checksums |
7f4130b155e4715eba2732013134b8baaf1e8014f215ac3569721e376c0d8f00
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / guardian_type_enforcer-4.0.6-cp314-cp314-macosx_10_15_x86_64.whl
| Download URL | guardian_type_enforcer-4.0.6-cp314-cp314-macosx_10_15_x86_64.whl |
|---|---|
| Size | 17.7 kB |
| Tags | CPython 3.14 macOS 10.15+ x86-64 |
|
SHA-256 checksum How to use checksums |
358f4246a381e731322f8caea4afb52dd3fafc08513cdf96cc96f994b50f030e
|
|
BLAKE2b-256 checksum How to use checksums |
88a57c971808162458bdab7da28ff6a50a97702f0a93d379a2808c186a71c66f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Security for guardian-type-enforcer 4.0.6
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.