Skip to main content

Type enforcement for Python

Project description

Tenforce

Type enforcement for Python.

Installation

pip install tenforce

Usage

from uuid import UUID, uuid4
from tenforce.enforcer import check

# define a class
class CreateOrderRequest:
    user_id: UUID
    skus: list[int]
    ship_service_level: str

if __name__ == "__main__":
    # populate your class that needs type enforcement
    request = CreateOrderRequest()
    request.user_id = uuid4()
    request.skus = [1234567890, 0987213]
    ship_service_level = "Overnight"
    
    # call check() on it
    check(request)
    
    # or, automatically cast compatible types (ex: numeric strings with an int annotation)
    check(request, auto_cast=True)

Reason for development

I developed this package because I had issues with Pydantic and handling large amounts of base models. I eventually plan to add serialization/deserialization to this. For example, populating one billion BaseModel objects in Pydantic takes roughly 30-35 seconds on my M1 Mac, versus 3-7 seconds with Tenforce. Pydantic is supposed to get a rewrite in rust with V2 though, so let's see how that turns out :)

Results

image

Principles

This package is designed to enforce the types of a Python class and its class variables (through type hints). Written mainly in Cython, it follows a few design principles.

  1. Sacrifice certain dynamic language features for speed and simplicity
    • Things like Unions (except for None unions & Optional) & subscripted type annotations (ex: list[str]) for the sake of simplicity and speed
  2. Opt-in helpers, not opt-out
    • By default, we try to run was little code as possible when calling check() on an object. We do have extra arguments like auto_cast to automatically cast variables (assuming it can be a successful cast)
  3. Support only the most popular patterns for API development
    • In my experience doing backend in a dynamic language such as Python, it is often you don't find yourself ever needing a list able to contain multiple types (ex: list[str | int])
    • Building in lots of features will inevitably add slowdowns to check()

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

tenforce-0.1.4.tar.gz (148.5 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page