Skip to main content

Maximum performance runtime type enforcement using CPython C API.

Project description

🛡️ 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: @guard is ~3× faster than Beartype
  • Class models: shield is ~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.dataclass fully mirrors stdlib behavior
  • Frozen instance compatibility layer added
  • typing.dataclass_transform integration for IDE support

🛡️ Access Control & Encapsulation

  • Added @validator hook system
  • Extended frame-walking protection to class-level access

🛠️ Developer Experience

  • Recommended Linux/WSL 2 build pipeline
  • Adopted uv for ultra-fast environment management

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

guardian_type_enforcer-4.0.6-cp314-cp314t-win_amd64.whl (19.7 kB view details)

Uploaded CPython 3.14tWindows x86-64

guardian_type_enforcer-4.0.6-cp314-cp314t-win32.whl (18.9 kB view details)

Uploaded CPython 3.14tWindows x86

guardian_type_enforcer-4.0.6-cp314-cp314t-musllinux_1_2_x86_64.whl (51.3 kB view details)

Uploaded CPython 3.14tmusllinux: musl 1.2+ x86-64

guardian_type_enforcer-4.0.6-cp314-cp314t-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (52.6 kB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

guardian_type_enforcer-4.0.6-cp314-cp314t-macosx_11_0_arm64.whl (18.9 kB view details)

Uploaded CPython 3.14tmacOS 11.0+ ARM64

guardian_type_enforcer-4.0.6-cp314-cp314t-macosx_10_15_x86_64.whl (18.3 kB view details)

Uploaded CPython 3.14tmacOS 10.15+ x86-64

guardian_type_enforcer-4.0.6-cp314-cp314-win_amd64.whl (19.3 kB view details)

Uploaded CPython 3.14Windows x86-64

guardian_type_enforcer-4.0.6-cp314-cp314-win32.whl (18.5 kB view details)

Uploaded CPython 3.14Windows x86

guardian_type_enforcer-4.0.6-cp314-cp314-musllinux_1_2_x86_64.whl (40.3 kB view details)

Uploaded CPython 3.14musllinux: musl 1.2+ x86-64

guardian_type_enforcer-4.0.6-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (40.6 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

guardian_type_enforcer-4.0.6-cp314-cp314-macosx_11_0_arm64.whl (18.3 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

guardian_type_enforcer-4.0.6-cp314-cp314-macosx_10_15_x86_64.whl (17.7 kB view details)

Uploaded CPython 3.14macOS 10.15+ x86-64

File details

Details for the file guardian_type_enforcer-4.0.6-cp314-cp314t-win_amd64.whl.

File metadata

File hashes

Hashes for guardian_type_enforcer-4.0.6-cp314-cp314t-win_amd64.whl
Algorithm Hash digest
SHA256 5bdb1a1d1b20012290c91ad0d3b39ca6be02bafc9f3e94965382fe7d5d3a9275
MD5 fa80310db4b0638b1be4ec4a4a3acac2
BLAKE2b-256 109aea123afaae65a441b9be71ae517fb366b2700cdddcef564f4cfa89fcf39a

See more details on using hashes here.

File details

Details for the file guardian_type_enforcer-4.0.6-cp314-cp314t-win32.whl.

File metadata

File hashes

Hashes for guardian_type_enforcer-4.0.6-cp314-cp314t-win32.whl
Algorithm Hash digest
SHA256 4daaf13206950bcc95787ac34b7aa075826d780b0b54a700abeaf0964d007622
MD5 db8550de2238fea5a746965f1b8f1839
BLAKE2b-256 5b136483334f2ded10ead4b7dd21599f577d3d6ebd3563ed0f35c3678b1342e8

See more details on using hashes here.

File details

Details for the file guardian_type_enforcer-4.0.6-cp314-cp314t-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for guardian_type_enforcer-4.0.6-cp314-cp314t-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 44000cfbc549969167329bace0e7e5f52891da140a81c8abc94ef3a1c778297b
MD5 f9ba049a3602d3d55e8fd4424ac1e50d
BLAKE2b-256 b4827aa6593e23258db579ef93c78b15026a70c6b73448dc5974f3f7315eb804

See more details on using hashes here.

File details

Details for the file guardian_type_enforcer-4.0.6-cp314-cp314t-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for guardian_type_enforcer-4.0.6-cp314-cp314t-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 ceba087ef7dd91b1e733725b58a2b5d70d00e80ea36bdd4f3090fb83f040d78a
MD5 6085d06a100363686d2ec319b4e25da5
BLAKE2b-256 35d316b1f95ec41c1585019ce4cb5304359cb5cc30115449c4553f58a2dd35ce

See more details on using hashes here.

File details

Details for the file guardian_type_enforcer-4.0.6-cp314-cp314t-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for guardian_type_enforcer-4.0.6-cp314-cp314t-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bfe3206cfd9035c6295811600158f415d538fcf30011e694e4e5d98db37fee6d
MD5 46807a9236a954723f8b644725c8c64d
BLAKE2b-256 c68d21e77e160b7e04b034fda5e59e743ea478d2dc6277be9fd6d2bd17659633

See more details on using hashes here.

File details

Details for the file guardian_type_enforcer-4.0.6-cp314-cp314t-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for guardian_type_enforcer-4.0.6-cp314-cp314t-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 6f80dd5c527a99c52961925dab003b20e14933455d4d29ab92e99239fa975f4e
MD5 610e33736211b7ce3e8f4b8e3a51a05a
BLAKE2b-256 3668ee1e026c8a2b780f71c310b7f07f66174bca745b9e0aa159874db67cea0d

See more details on using hashes here.

File details

Details for the file guardian_type_enforcer-4.0.6-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for guardian_type_enforcer-4.0.6-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 7d72575d621aaa348025974e9be0889c29e3bfb043af29fb161bd3005a8a97f4
MD5 ebc598f6b3800b898c1ab62dfd1ace59
BLAKE2b-256 ec991c4938051e2e17aec59c00c7c034ea435aa5b9b60d497ff846ef4c911c53

See more details on using hashes here.

File details

Details for the file guardian_type_enforcer-4.0.6-cp314-cp314-win32.whl.

File metadata

File hashes

Hashes for guardian_type_enforcer-4.0.6-cp314-cp314-win32.whl
Algorithm Hash digest
SHA256 40eeee1f5544f46fe54190c47859c639d883ccc9da1dee8073830a4ce255a70a
MD5 3dfa284ac44218294ea8528f8d061de0
BLAKE2b-256 835da26066f282fe80a152d455766ce025162794ce1f6d12be65c08ebcafb22d

See more details on using hashes here.

File details

Details for the file guardian_type_enforcer-4.0.6-cp314-cp314-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for guardian_type_enforcer-4.0.6-cp314-cp314-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 1ab095109ad6017712e1e1db11c245ea3f17a057ba52f5d33332b189a43c427c
MD5 63f87ce52208e0d81d5d727f82be7806
BLAKE2b-256 819bbb0e5e1d6b803d214c09522ebcdc11b50958b231d8a66bed7f41115f76ec

See more details on using hashes here.

File details

Details for the file guardian_type_enforcer-4.0.6-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for guardian_type_enforcer-4.0.6-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 8bcd8dd2593aa8edc370db6071ea1cd42898068f123618634227d03583353365
MD5 3887366c705d727f32bee98e7229a0bd
BLAKE2b-256 38558d9fc8db969acdcd55c4d7fba1610ae9c5be87bc62ca820fb5c817a55ffb

See more details on using hashes here.

File details

Details for the file guardian_type_enforcer-4.0.6-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for guardian_type_enforcer-4.0.6-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 11798302ac68ab88e5eab101de2e5822f8100df486cc48a5f3608e24df64ee44
MD5 5dce517d24f6025e64f6c5a7c8fddf4e
BLAKE2b-256 7f4130b155e4715eba2732013134b8baaf1e8014f215ac3569721e376c0d8f00

See more details on using hashes here.

File details

Details for the file guardian_type_enforcer-4.0.6-cp314-cp314-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for guardian_type_enforcer-4.0.6-cp314-cp314-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 358f4246a381e731322f8caea4afb52dd3fafc08513cdf96cc96f994b50f030e
MD5 17bf5d5199179ed469026f7ffe1b222b
BLAKE2b-256 88a57c971808162458bdab7da28ff6a50a97702f0a93d379a2808c186a71c66f

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