Skip to main content

Maximum performance runtime type enforcement using CPython C API.

Reason this release was yanked:

Lack of support for python v3.13

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.10+.

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) and Beartype.


🚀 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)

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. shield (C-Native Data Models)

The shield class is a high-performance alternative to @dataclass or pydantic.BaseModel.
By inheriting from shield, your class becomes a native C-type (ShieldBase). It enforces strict typing on all annotated attributes and provides true protected/private access control.

from guardian.shield import shield
from guardian._guardian_core import GuardianTypeError, GuardianAccessError

class User(shield):
    username: str
    age: int
    _internal_id: int  # Attributes starting with '_' are private

    def __init__(self, username: str, age: int):
        self.username = username
        self.age = age
        self._internal_id = 9999

# --- 1. Type Enforcement ---
user = User("Michex", 25)

user.age = 26        # OK
user.age = "twenty"  # ❌ Raises GuardianTypeError

# --- 2. True Private Access Control ---
print(user._internal_id)  # ❌ Raises GuardianAccessError

How it works:
shield bypasses Python’s __setattr__. Memory assignment and type-checking happen directly in C via tp_setattro slots, taking less than 80 nanoseconds. Internal access is verified by walking the CPython frame stack to ensure the caller context owns the self pointer.


2. @guard (Boundary Type Enforcement)

Use @guard to enforce strict input and return types on your critical functions, API endpoints, or calculations.
Supports deeply nested types like dict[str, list[int | float]].

from typing import Union
from guardian.guard import guard

@guard
def process_sensor_data(payload: dict[str, list[Union[int, float]]]) -> bool:
    return True

# Valid payload
process_sensor_data({"sensor_A": [1, 2.5, 3]})

# Invalid payload
process_sensor_data({"sensor_A": [1, 2.5, "3.0"]})
# ❌ Raises GuardianTypeError

How it works:
Signatures are compiled into C-structs at import time. During runtime, validation is O(1) positional mapped natively in C via the vectorcall protocol (PEP 590).


3. @deepguard (Local State Profiling)

While @guard checks inputs and outputs, @deepguard acts as a rigorous state machine enforcer. It ensures that local variables inside your function never mutate into unexpected types during execution.

from guardian.deepguard import deepguard

@deepguard
def calculate_discount(price: float) -> float:
    discount: float = 0.0
    
    if price < 0:
        discount = "N/A"  # ❌ Raises GuardianTypeError when returning
        
    return price - discount

Note:
@deepguard utilizes PyEval_SetProfile to intercept the local dictionary state upon function return. Because it hooks into interpreter profiling, it carries a ~43µs overhead and should be reserved for high-stakes business logic.


🧠 Advanced Usage: The Compiler

Guardian automatically compiles complex type hints into flat, recursive integer tuples that the C-core switch statements can evaluate instantly.

Supported types include:

  • Primitives (int, str, float, bool)
  • Data structures (list, dict, set, tuple)
  • Typing constructs (Union, Optional, Any, Literal)
  • Deeply nested configurations (dict[str, tuple[int, ...]])

Primitives use branch-predicted fast paths (PyLong_CheckExact, PyUnicode_CheckExact) to skip isinstance() overhead entirely.


🤝 Contributing

Contributions, issues, and feature requests are welcome.

git clone https://github.com/yourusername/guardian-type-enforcer.git
pip install -e .
pytest tests/test.py -v

📄 License

This project is licensed under the MIT License.
See the LICENSE file 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 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-3.1.0-cp312-cp312-win_amd64.whl (16.7 kB view details)

Uploaded CPython 3.12Windows x86-64

guardian_type_enforcer-3.1.0-cp312-cp312-win32.whl (16.1 kB view details)

Uploaded CPython 3.12Windows x86

guardian_type_enforcer-3.1.0-cp312-cp312-musllinux_1_1_x86_64.whl (37.8 kB view details)

Uploaded CPython 3.12musllinux: musl 1.1+ x86-64

guardian_type_enforcer-3.1.0-cp312-cp312-musllinux_1_1_i686.whl (36.6 kB view details)

Uploaded CPython 3.12musllinux: musl 1.1+ i686

guardian_type_enforcer-3.1.0-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (34.6 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64manylinux: glibc 2.5+ x86-64

guardian_type_enforcer-3.1.0-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (33.0 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ i686manylinux: glibc 2.5+ i686

guardian_type_enforcer-3.1.0-cp312-cp312-macosx_11_0_arm64.whl (15.2 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

guardian_type_enforcer-3.1.0-cp312-cp312-macosx_10_9_x86_64.whl (14.6 kB view details)

Uploaded CPython 3.12macOS 10.9+ x86-64

guardian_type_enforcer-3.1.0-cp311-cp311-win_amd64.whl (16.7 kB view details)

Uploaded CPython 3.11Windows x86-64

guardian_type_enforcer-3.1.0-cp311-cp311-win32.whl (16.0 kB view details)

Uploaded CPython 3.11Windows x86

guardian_type_enforcer-3.1.0-cp311-cp311-musllinux_1_1_x86_64.whl (35.8 kB view details)

Uploaded CPython 3.11musllinux: musl 1.1+ x86-64

guardian_type_enforcer-3.1.0-cp311-cp311-musllinux_1_1_i686.whl (34.8 kB view details)

Uploaded CPython 3.11musllinux: musl 1.1+ i686

guardian_type_enforcer-3.1.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (32.5 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64manylinux: glibc 2.5+ x86-64

guardian_type_enforcer-3.1.0-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (31.2 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ i686manylinux: glibc 2.5+ i686

guardian_type_enforcer-3.1.0-cp311-cp311-macosx_11_0_arm64.whl (15.3 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

guardian_type_enforcer-3.1.0-cp311-cp311-macosx_10_9_x86_64.whl (14.5 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

File details

Details for the file guardian_type_enforcer-3.1.0-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for guardian_type_enforcer-3.1.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 fc284f169c46357d60ae2d20f999cb1c529b77c47ccd8ab0c0a67bf99f40f26c
MD5 9148a8ecf16bc4c0f70dc29587a72f73
BLAKE2b-256 d78b38d1f4b190e760d8ed3930ffb257fbf2f90587d944b4864fac3d01d6b29e

See more details on using hashes here.

File details

Details for the file guardian_type_enforcer-3.1.0-cp312-cp312-win32.whl.

File metadata

File hashes

Hashes for guardian_type_enforcer-3.1.0-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 46ac8d562632367358e0564da4cc1bcb2249fd16317987ece4c39cbeb5a67d7d
MD5 bafc460c64654f315025322ad3dbccf4
BLAKE2b-256 7fe8f564d697c0bbd3eda1a89d2ae2f7de0a147b668d3676e810aec3454664ce

See more details on using hashes here.

File details

Details for the file guardian_type_enforcer-3.1.0-cp312-cp312-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for guardian_type_enforcer-3.1.0-cp312-cp312-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 6ff1231151010b2cadd3e052c366ee7988af5571782a0d2d6497354bfbf9e551
MD5 701816df4d2d795369310f784e56a192
BLAKE2b-256 1e3cec2809f9b9056503a00324e7fad371f5a654f483c4783906628af61ddeb0

See more details on using hashes here.

File details

Details for the file guardian_type_enforcer-3.1.0-cp312-cp312-musllinux_1_1_i686.whl.

File metadata

File hashes

Hashes for guardian_type_enforcer-3.1.0-cp312-cp312-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 dbcff323edcf34fbb8b374d55761474c9e6a5659be92e9bf2bef78a68a17b101
MD5 47631e2076a22e5e49169bf62dba4fa4
BLAKE2b-256 b6b414c6edb7fb817e8e4ec80aa635f4a3a2c9f7210112d8094b5a89f84b75dc

See more details on using hashes here.

File details

Details for the file guardian_type_enforcer-3.1.0-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for guardian_type_enforcer-3.1.0-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0529de986e5a4b5ad2e384ed2c10dc57ee70154e21ca7c562481d2eacb8c05c8
MD5 007352cbbd6d3549034f967be2b394fe
BLAKE2b-256 5d423703eaded08be04fd4028ccc26b0e9204d5485bd3bac56a83ac95f6083cf

See more details on using hashes here.

File details

Details for the file guardian_type_enforcer-3.1.0-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for guardian_type_enforcer-3.1.0-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 73ce2be646a1ca8214f7a0393cd173e1f1a50ebe8e34e1dcf87e475fd615dc28
MD5 79f8d27c2cc39c6b8c35df7f6301d59a
BLAKE2b-256 f6580872fd54d2ea006f9674d8f9d2763e48e8ca1c70aa7d4a615544b9880f7d

See more details on using hashes here.

File details

Details for the file guardian_type_enforcer-3.1.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for guardian_type_enforcer-3.1.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ccec717c28f252f9f93e007421e4286928f64fa066f141b1e3ded91ff9d620f5
MD5 9b81cbbeebd1bc992d4744b8a5654117
BLAKE2b-256 be3c12a5fb920f6ea0b949e1e91f04083a5301fc5b14d9a035db301ebbddd7db

See more details on using hashes here.

File details

Details for the file guardian_type_enforcer-3.1.0-cp312-cp312-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for guardian_type_enforcer-3.1.0-cp312-cp312-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 a2ca271f6a9df572fa71bf5907210b651fece28af719cd9bac3e370635393703
MD5 ad9d3520c3bff587ccd304bc3dde8d14
BLAKE2b-256 36b265ba60c64091134743a324dda713f323d3173f06b28cc43bd650ea53d5c8

See more details on using hashes here.

File details

Details for the file guardian_type_enforcer-3.1.0-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for guardian_type_enforcer-3.1.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 c5da88f8d6da565c6411b99ae97ea1acfac97879b6e2888a166eaf100b46fa0d
MD5 d0efb079ad97d6c73304b3a614d3341d
BLAKE2b-256 30b3bc19adceaa94b0bd86aa7824b04db700a6b75a81b7800bbdf4f633e8bbb2

See more details on using hashes here.

File details

Details for the file guardian_type_enforcer-3.1.0-cp311-cp311-win32.whl.

File metadata

File hashes

Hashes for guardian_type_enforcer-3.1.0-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 4e093e8a56303731f653dd249f57a0ef2288d5fdeae5f775a2af4909d1dc3ad1
MD5 b971de696bbb87bd80087cc3aa1bbb2a
BLAKE2b-256 4662bffa051de93b26e48966a50e0b263a0f4284a2760651a901b339a20c5e78

See more details on using hashes here.

File details

Details for the file guardian_type_enforcer-3.1.0-cp311-cp311-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for guardian_type_enforcer-3.1.0-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 6a5d84716df5aa5f483eb8ed2b7ef8189f11027e35d1744fc3ba3a6da9dddb05
MD5 6b9d7b8219752f5c28f589c4a5c7fd6b
BLAKE2b-256 b06c517fbd4c4173ff0a6f54d428782d73f5cbc88053a9cac403755262e79260

See more details on using hashes here.

File details

Details for the file guardian_type_enforcer-3.1.0-cp311-cp311-musllinux_1_1_i686.whl.

File metadata

File hashes

Hashes for guardian_type_enforcer-3.1.0-cp311-cp311-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 7ad50fd59968e78ffd537084293d5d82db2f6fe7e0e887630668137278ef6c4c
MD5 95c71be4a9b1e8c8abdb1b4a5d7b2c06
BLAKE2b-256 df204bdae7938229b395a8f3f0a475365b4a241849293f3859358a3625bb8d3a

See more details on using hashes here.

File details

Details for the file guardian_type_enforcer-3.1.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for guardian_type_enforcer-3.1.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 60a65929308709ea7364a9fd96b302fd52a6539921ac972cf815adc94bdc75b8
MD5 839fe91acde23c88a0f88328486d2454
BLAKE2b-256 85d2b6cab5b1e85c2bcf75c7918db6ed7714b20bda684044a1443155ed0bb56a

See more details on using hashes here.

File details

Details for the file guardian_type_enforcer-3.1.0-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for guardian_type_enforcer-3.1.0-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 20e4545e5683fd4dc2d749a192318b153713d90a80e2eb137f7678da2c2e8fa2
MD5 9646e0f4cfdc4c2011f4775d451f9580
BLAKE2b-256 2d305fd265f93d1e5407bced80ac6e4a4eb1deca2805f99fff44f3558a01db84

See more details on using hashes here.

File details

Details for the file guardian_type_enforcer-3.1.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for guardian_type_enforcer-3.1.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3b84f153ad8f14f2b5437eee8fe7542d09f1d14991dfc77ceeb4b650e5ba4ef2
MD5 eca5837a0938fdbfef22330e6bedd6b5
BLAKE2b-256 851f79c87d1104d4d047a63707056eda250f0450384b2b8ae4cc7854dc694fdb

See more details on using hashes here.

File details

Details for the file guardian_type_enforcer-3.1.0-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for guardian_type_enforcer-3.1.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 7428ce9345e58616289577ec3590e39f71504711ac722908b38325813274b2ad
MD5 eae525ae5abd6ef9a96cb160b08c0ae4
BLAKE2b-256 97e3e055340c3774210f3a91346e6cb116b9f5f7b42e6c5f29684118337bbcc0

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