tenforce


Nametenforce JSON
Version 0.1.4 PyPI version JSON
download
home_page
SummaryType enforcement for Python
upload_time2023-05-24 20:07:09
maintainer
docs_urlNone
author
requires_python>=3.11
licenseMIT License Copyright (c) 2023 Hunter LaFaille Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords type enforcement validation fast types cython
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Tenforce

**T**ype **enforce**ment for Python.

# Installation
`pip install tenforce`

# Usage
```python
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
<img width="713" alt="image" src="https://github.com/hlafaille/tenforce/assets/5008650/6b6b625a-d46e-40e1-841c-f285d99373a2">

# 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()`

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "tenforce",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.11",
    "maintainer_email": "",
    "keywords": "type enforcement,validation,fast,types,cython",
    "author": "",
    "author_email": "Hunter LaFaille <hunterlafaille@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/17/64/75dfd97fc8878e74e3ef680aada5551d7be3929f19a899c1154cdf15c908/tenforce-0.1.4.tar.gz",
    "platform": null,
    "description": "# Tenforce\n\n**T**ype **enforce**ment for Python.\n\n# Installation\n`pip install tenforce`\n\n# Usage\n```python\nfrom uuid import UUID, uuid4\nfrom tenforce.enforcer import check\n\n# define a class\nclass CreateOrderRequest:\n    user_id: UUID\n    skus: list[int]\n    ship_service_level: str\n\nif __name__ == \"__main__\":\n    # populate your class that needs type enforcement\n    request = CreateOrderRequest()\n    request.user_id = uuid4()\n    request.skus = [1234567890, 0987213]\n    ship_service_level = \"Overnight\"\n    \n    # call check() on it\n    check(request)\n    \n    # or, automatically cast compatible types (ex: numeric strings with an int annotation)\n    check(request, auto_cast=True)\n```\n\n# Reason for development\n\nI developed this package because I had issues with Pydantic and handling large amounts of base models. I eventually\nplan to add serialization/deserialization to this. For example, populating **one billion** `BaseModel` objects in\nPydantic takes roughly 30-35 seconds on my M1 Mac, versus 3-7 seconds with Tenforce. Pydantic is supposed to get a \nrewrite in rust with V2 though, so let's see how that turns out :)\n\n### Results\n<img width=\"713\" alt=\"image\" src=\"https://github.com/hlafaille/tenforce/assets/5008650/6b6b625a-d46e-40e1-841c-f285d99373a2\">\n\n# Principles\n\nThis package is designed to enforce the types of a Python class and its class variables (through type hints). Written\nmainly in Cython, it follows a few design principles.\n\n1. Sacrifice certain dynamic language features for speed and simplicity\n    * Things like Unions (except for `None` unions & `Optional`) & subscripted type annotations (ex: `list[str]`) for the sake of simplicity and speed\n2. Opt-in helpers, not opt-out\n    * By default, we try to run was little code as possible when calling `check()` on an object. We do have extra arguments\n      like `auto_cast` to automatically cast variables (assuming it can be a successful cast)\n3. Support only the most popular patterns for API development\n   * In my experience doing backend in a dynamic language such as Python, it is often you don't find yourself ever needing \n      a list able to contain multiple types (ex: `list[str | int]`)\n   * Building in lots of features will inevitably add slowdowns to `check()`\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2023 Hunter LaFaille  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
    "summary": "Type enforcement for Python",
    "version": "0.1.4",
    "project_urls": null,
    "split_keywords": [
        "type enforcement",
        "validation",
        "fast",
        "types",
        "cython"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "176475dfd97fc8878e74e3ef680aada5551d7be3929f19a899c1154cdf15c908",
                "md5": "952870a6d61f6aa6ae81d2830da46bcb",
                "sha256": "e77e38bbd70ff256cbe084151fe04f84ddefc51659f821d612cd77c9a8ea29d3"
            },
            "downloads": -1,
            "filename": "tenforce-0.1.4.tar.gz",
            "has_sig": false,
            "md5_digest": "952870a6d61f6aa6ae81d2830da46bcb",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11",
            "size": 148464,
            "upload_time": "2023-05-24T20:07:09",
            "upload_time_iso_8601": "2023-05-24T20:07:09.536727Z",
            "url": "https://files.pythonhosted.org/packages/17/64/75dfd97fc8878e74e3ef680aada5551d7be3929f19a899c1154cdf15c908/tenforce-0.1.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-05-24 20:07:09",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "tenforce"
}
        
Elapsed time: 0.06939s