utype


Nameutype JSON
Version 0.5.1 PyPI version JSON
download
home_pagehttps://utype.io
SummaryDeclare & parse types / dataclasses / functions based on Python type annotations
upload_time2024-04-16 02:45:48
maintainerNone
docs_urlNone
authorXuLin Zhou
requires_python>=3.7
licenseApache 2.0
keywords utype type schema meta-type validation data-model type-transform parser json-schema
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # uType
[![Version](https://img.shields.io/pypi/v/utype)](https://pypi.org/project/utype/)
[![Python Requires](https://img.shields.io/pypi/pyversions/utype)](https://pypi.org/project/utype/)
[![License](https://img.shields.io/badge/license-Apache%202.0-blue)](https://github.com/utilmeta/utype/blob/main/LICENSE)
[![CI](https://img.shields.io/github/actions/workflow/status/utilmeta/utype/test.yaml?branch=main&label=CI)](https://github.com/utilmeta/utype/actions?query=branch%3Amain+)
[![Test Coverage](https://img.shields.io/codecov/c/github/utilmeta/utype?color=green)](https://app.codecov.io/github/utilmeta/utype)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![Downloads](https://pepy.tech/badge/utype/month)](https://pepy.tech/project/utype)

utype is a data types declaration & parsing library based on Python type annotations, 
enforce types and constraints for classes and functions at runtime

* Documentation: [https://utype.io](https://utype.io)
* 中文文档: [https://utype.io/zh](https://utype.io/zh)
* Source Code: [https://github.com/utilmeta/utype](https://github.com/utilmeta/utype)
* Author: [@voidZXL](https://github.com/voidZXL)
* License: Apache 2.0

### Core Features

* Enforce types, data classes, function params and result parsing at runtime based on Python type annotation
* Support a variety of constraints, logical operators and flexible parsing options
* Highly extensible, all type transformer can be register, extend and override

### Installation

```shell
pip install -U utype
```

> utype requires Python >= 3.7

### Usage Example

### Types and constraints
The utype support to add constraints on types, such as
```Python
from utype import Rule, exc

class PositiveInt(int, Rule):  
    gt = 0

assert PositiveInt(b'3') == 3

try:
    PositiveInt(-0.5)
except exc.ParseError as e:
    print(e)
    """
    Constraint: 0 violated
    """
``` 


Data that conforms to the type and constraints will complete the conversion, otherwise will throw a parse error indicating what went wrong

### Parsing dataclasses

utype supports the "dataclass" usage that convert a dict or JSON to a class instance, similar to `pydantic` and `attrs`
```python
from utype import Schema, Field, exc
from datetime import datetime

class UserSchema(Schema):
    username: str = Field(regex='[0-9a-zA-Z]{3,20}')
    signup_time: datetime

# 1. Valid input
data = {'username': 'bob', 'signup_time': '2022-10-11 10:11:12'}
print(UserSchema(**data))
#> UserSchema(username='bob', signup_time=datetime.datetime(2022, 10, 11, 10, 11, 12))

# 2. Invalid input
try:
    UserSchema(username='@invalid', signup_time='2022-10-11 10:11:12')
except exc.ParseError as e:
    print(e)
    """
    parse item: ['username'] failed: 
    Constraint: <regex>: '[0-9a-zA-Z]{3,20}' violated
    """
```

After a simple declaration, you can get

* Automatic `__init__` to take input data, perform validation and attribute assignment
* Providing  `__repr__` and `__str__` to get the clearly print output of the instance
* parse and protect attribute assignment and deletion to avoid dirty data

### Parsing functions

utype can also parse function params and result
```python
import utype
from typing import Optional

class PositiveInt(int, utype.Rule):  
    gt = 0

class ArticleSchema(utype.Schema):
    id: Optional[PositiveInt]
    title: str = utype.Field(max_length=100)
    slug: str = utype.Field(regex=r"[a-z0-9]+(?:-[a-z0-9]+)*")

@utype.parse
def get_article(id: PositiveInt = None, title: str = '') -> ArticleSchema:
    return {
        'id': id,
        'title': title,
        'slug': '-'.join([''.join(
            filter(str.isalnum, v)) for v in title.split()]).lower()
    }

print(get_article('3', title=b'My Awesome Article!'))
#> ArticleSchema(id=3, title='My Awesome Article!', slug='my-awesome-article')

try:
    get_article('-1')
except utype.exc.ParseError as e:
    print(e)
    """
    parse item: ['id'] failed: Constraint: : 0 violated
    """

try:
    get_article(title='*' * 101)
except utype.exc.ParseError as e:
    print(e)
    """
    parse item: ['<return>'] failed: 
    parse item: ['title'] failed: 
    Constraint: <max_length>: 100 violated
    """
```

> You can easily get type checking and code completion of IDEs (such as Pycharm, VS Code) during development

utype supports not only normal functions, but also generator functions, asynchronous functions, and asynchronous generator functions with the same usage
```python
import utype  
import asyncio  
from typing import AsyncGenerator  

@utype.parse  
async def waiter(rounds: int = utype.Param(gt=0)) -> AsyncGenerator[int, float]:  
    assert isinstance(rounds, int)  
    i = rounds  
    while i:  
        wait = yield str(i)  
        if wait:  
            assert isinstance(wait, float)  
            print(f'sleep for: {wait} seconds')
            await asyncio.sleep(wait)  
        i -= 1  
  
async def wait():  
    wait_gen = waiter('2')  
    async for index in wait_gen:  
        assert isinstance(index, int)  
        try:  
            await wait_gen.asend(b'0.5')  
            # sleep for: 0.5 seconds  
        except StopAsyncIteration:  
            return  
  
if __name__ == '__main__':  
    asyncio.run(wait())
```

> The `AsyncGenerator` type is used to annotate the return value of the asynchronous generator, which has two parameters: the type of the value output by `yield`, type of the value sent by `asend`

As you can see, the parameters passed to the function and the value received from `yield` were all converted to the expected type as declared


### Logical operation of type
utype supports logical operations on types and data structures using Python-native logical operators
```python
from utype import Schema, Field
from typing import Tuple

class User(Schema):  
    name: str = Field(max_length=10)  
    age: int

one_of_user = User ^ Tuple[str, int]

print(one_of_user({'name': 'test', 'age': '1'}))
# > User(name='test', age=1)

print(one_of_user([b'test', '1']))
# > ('test', 1)
```

The example uses the `^` exclusive or symbol to logically combine  `User` and `Tuple[str, int]`, and the new logical type gains the ability to convert data to one of those

### Register transformer for type
Type transformation and validation strictness required by each project may be different, so in utype, all types support registraton, extension and override, such as
```python
from utype import Rule, Schema, register_transformer
from typing import Type

class Slug(str, Rule):  
    regex = r"[a-z0-9]+(?:-[a-z0-9]+)*"

@register_transformer(Slug)
def to_slug(transformer, value, t: Type[Slug]):
    str_value = transformer(value, str)
    return t('-'.join([''.join(
    filter(str.isalnum, v)) for v in str_value.split()]).lower())


class ArticleSchema(Schema):
	slug: Slug

print(dict(ArticleSchema(slug=b'My Awesome Article!')))
# > {'slug': 'my-awesome-article'}
```

You can register transformers not only for custom types, but also for basic types (such as `str`, `int`, etc.) Or types in the standard library (such as `datetime`, `Enum`, etc.) To customize the conversion behavior

## RoadMap and Contribution
utype is still growing, and the following features are planned for implementation in the new version

* Improve the handling mechanism of parsing errors, including error handling hook functions, etc.
* Support the declaration and parse command line parameters
* Support for Python generics, type variables, and more type annotation syntax
* Develop Pycharm/VS Code plugin that supports IDE detection and hints for constraints, logical types, and nested types

You are also welcome to contribute features or submit issues.

## Applications

### UtilMeta Python Framework
UtilMeta Python Framework is a progressive meta-framework for backend applications, which efficiently builds declarative APIs based on the Python type annotation standard, and supports the integration of mainstream Python frameworks as runtime backend
* Homepage: [https://utilmeta.com/py](https://utilmeta.com/py)
* Source Code: [https://github.com/utilmeta/utilmeta-py](https://github.com/utilmeta/utilmeta-py)

## Community

utype is a project of [UtilMeta](https://utilmeta.com), so you can join the community in

* [Discord](https://discord.gg/JdmEkFS6dS)
* [X(Twitter)](https://twitter.com/utilmeta)
* [Reddit](https://www.reddit.com/r/utilmeta)
* [中文讨论区](https://lnzhou.com/channels/utilmeta/community)



            

Raw data

            {
    "_id": null,
    "home_page": "https://utype.io",
    "name": "utype",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": null,
    "keywords": "utype type schema meta-type validation data-model type-transform parser json-schema",
    "author": "XuLin Zhou",
    "author_email": "zxl@utilmeta.com",
    "download_url": "https://files.pythonhosted.org/packages/41/8d/5e6186fcee3ed7253ebd0668b144fad91bfb391335b8828fbabf5ce57f0b/utype-0.5.1.tar.gz",
    "platform": null,
    "description": "# uType\n[![Version](https://img.shields.io/pypi/v/utype)](https://pypi.org/project/utype/)\n[![Python Requires](https://img.shields.io/pypi/pyversions/utype)](https://pypi.org/project/utype/)\n[![License](https://img.shields.io/badge/license-Apache%202.0-blue)](https://github.com/utilmeta/utype/blob/main/LICENSE)\n[![CI](https://img.shields.io/github/actions/workflow/status/utilmeta/utype/test.yaml?branch=main&label=CI)](https://github.com/utilmeta/utype/actions?query=branch%3Amain+)\n[![Test Coverage](https://img.shields.io/codecov/c/github/utilmeta/utype?color=green)](https://app.codecov.io/github/utilmeta/utype)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n[![Downloads](https://pepy.tech/badge/utype/month)](https://pepy.tech/project/utype)\n\nutype is a data types declaration & parsing library based on Python type annotations, \nenforce types and constraints for classes and functions at runtime\n\n* Documentation: [https://utype.io](https://utype.io)\n* \u4e2d\u6587\u6587\u6863: [https://utype.io/zh](https://utype.io/zh)\n* Source Code: [https://github.com/utilmeta/utype](https://github.com/utilmeta/utype)\n* Author: [@voidZXL](https://github.com/voidZXL)\n* License: Apache 2.0\n\n### Core Features\n\n* Enforce types, data classes, function params and result parsing at runtime based on Python type annotation\n* Support a variety of constraints, logical operators and flexible parsing options\n* Highly extensible, all type transformer can be register, extend and override\n\n### Installation\n\n```shell\npip install -U utype\n```\n\n> utype requires Python >= 3.7\n\n### Usage Example\n\n### Types and constraints\nThe utype support to add constraints on types, such as\n```Python\nfrom utype import Rule, exc\n\nclass PositiveInt(int, Rule):  \n    gt = 0\n\nassert PositiveInt(b'3') == 3\n\ntry:\n    PositiveInt(-0.5)\nexcept exc.ParseError as e:\n    print(e)\n    \"\"\"\n    Constraint: 0 violated\n    \"\"\"\n``` \n\n\nData that conforms to the type and constraints will complete the conversion, otherwise will throw a parse error indicating what went wrong\n\n### Parsing dataclasses\n\nutype supports the \"dataclass\" usage that convert a dict or JSON to a class instance, similar to `pydantic` and `attrs`\n```python\nfrom utype import Schema, Field, exc\nfrom datetime import datetime\n\nclass UserSchema(Schema):\n    username: str = Field(regex='[0-9a-zA-Z]{3,20}')\n    signup_time: datetime\n\n# 1. Valid input\ndata = {'username': 'bob', 'signup_time': '2022-10-11 10:11:12'}\nprint(UserSchema(**data))\n#> UserSchema(username='bob', signup_time=datetime.datetime(2022, 10, 11, 10, 11, 12))\n\n# 2. Invalid input\ntry:\n    UserSchema(username='@invalid', signup_time='2022-10-11 10:11:12')\nexcept exc.ParseError as e:\n    print(e)\n    \"\"\"\n    parse item: ['username'] failed: \n    Constraint: <regex>: '[0-9a-zA-Z]{3,20}' violated\n    \"\"\"\n```\n\nAfter a simple declaration, you can get\n\n* Automatic `__init__` to take input data, perform validation and attribute assignment\n* Providing  `__repr__` and `__str__` to get the clearly print output of the instance\n* parse and protect attribute assignment and deletion to avoid dirty data\n\n### Parsing functions\n\nutype can also parse function params and result\n```python\nimport utype\nfrom typing import Optional\n\nclass PositiveInt(int, utype.Rule):  \n    gt = 0\n\nclass ArticleSchema(utype.Schema):\n    id: Optional[PositiveInt]\n    title: str = utype.Field(max_length=100)\n    slug: str = utype.Field(regex=r\"[a-z0-9]+(?:-[a-z0-9]+)*\")\n\n@utype.parse\ndef get_article(id: PositiveInt = None, title: str = '') -> ArticleSchema:\n    return {\n        'id': id,\n        'title': title,\n        'slug': '-'.join([''.join(\n            filter(str.isalnum, v)) for v in title.split()]).lower()\n    }\n\nprint(get_article('3', title=b'My Awesome Article!'))\n#> ArticleSchema(id=3, title='My Awesome Article!', slug='my-awesome-article')\n\ntry:\n    get_article('-1')\nexcept utype.exc.ParseError as e:\n    print(e)\n    \"\"\"\n    parse item: ['id'] failed: Constraint: : 0 violated\n    \"\"\"\n\ntry:\n    get_article(title='*' * 101)\nexcept utype.exc.ParseError as e:\n    print(e)\n    \"\"\"\n    parse item: ['<return>'] failed: \n    parse item: ['title'] failed: \n    Constraint: <max_length>: 100 violated\n    \"\"\"\n```\n\n> You can easily get type checking and code completion of IDEs (such as Pycharm, VS Code) during development\n\nutype supports not only normal functions, but also generator functions, asynchronous functions, and asynchronous generator functions with the same usage\n```python\nimport utype  \nimport asyncio  \nfrom typing import AsyncGenerator  \n\n@utype.parse  \nasync def waiter(rounds: int = utype.Param(gt=0)) -> AsyncGenerator[int, float]:  \n    assert isinstance(rounds, int)  \n    i = rounds  \n    while i:  \n        wait = yield str(i)  \n        if wait:  \n            assert isinstance(wait, float)  \n            print(f'sleep for: {wait} seconds')\n            await asyncio.sleep(wait)  \n        i -= 1  \n  \nasync def wait():  \n    wait_gen = waiter('2')  \n    async for index in wait_gen:  \n        assert isinstance(index, int)  \n        try:  \n            await wait_gen.asend(b'0.5')  \n            # sleep for: 0.5 seconds  \n        except StopAsyncIteration:  \n            return  \n  \nif __name__ == '__main__':  \n    asyncio.run(wait())\n```\n\n> The `AsyncGenerator` type is used to annotate the return value of the asynchronous generator, which has two parameters: the type of the value output by `yield`, type of the value sent by `asend`\n\nAs you can see, the parameters passed to the function and the value received from `yield` were all converted to the expected type as declared\n\n\n### Logical operation of type\nutype supports logical operations on types and data structures using Python-native logical operators\n```python\nfrom utype import Schema, Field\nfrom typing import Tuple\n\nclass User(Schema):  \n    name: str = Field(max_length=10)  \n    age: int\n\none_of_user = User ^ Tuple[str, int]\n\nprint(one_of_user({'name': 'test', 'age': '1'}))\n# > User(name='test', age=1)\n\nprint(one_of_user([b'test', '1']))\n# > ('test', 1)\n```\n\nThe example uses the `^` exclusive or symbol to logically combine  `User` and `Tuple[str, int]`, and the new logical type gains the ability to convert data to one of those\n\n### Register transformer for type\nType transformation and validation strictness required by each project may be different, so in utype, all types support registraton, extension and override, such as\n```python\nfrom utype import Rule, Schema, register_transformer\nfrom typing import Type\n\nclass Slug(str, Rule):  \n    regex = r\"[a-z0-9]+(?:-[a-z0-9]+)*\"\n\n@register_transformer(Slug)\ndef to_slug(transformer, value, t: Type[Slug]):\n    str_value = transformer(value, str)\n    return t('-'.join([''.join(\n    filter(str.isalnum, v)) for v in str_value.split()]).lower())\n\n\nclass ArticleSchema(Schema):\n\tslug: Slug\n\nprint(dict(ArticleSchema(slug=b'My Awesome Article!')))\n# > {'slug': 'my-awesome-article'}\n```\n\nYou can register transformers not only for custom types, but also for basic types (such as `str`, `int`, etc.) Or types in the standard library (such as `datetime`, `Enum`, etc.) To customize the conversion behavior\n\n## RoadMap and Contribution\nutype is still growing, and the following features are planned for implementation in the new version\n\n* Improve the handling mechanism of parsing errors, including error handling hook functions, etc.\n* Support the declaration and parse command line parameters\n* Support for Python generics, type variables, and more type annotation syntax\n* Develop Pycharm/VS Code plugin that supports IDE detection and hints for constraints, logical types, and nested types\n\nYou are also welcome to contribute features or submit issues.\n\n## Applications\n\n### UtilMeta Python Framework\nUtilMeta Python Framework is a progressive meta-framework for backend applications, which efficiently builds declarative APIs based on the Python type annotation standard, and supports the integration of mainstream Python frameworks as runtime backend\n* Homepage: [https://utilmeta.com/py](https://utilmeta.com/py)\n* Source Code: [https://github.com/utilmeta/utilmeta-py](https://github.com/utilmeta/utilmeta-py)\n\n## Community\n\nutype is a project of [UtilMeta](https://utilmeta.com), so you can join the community in\n\n* [Discord](https://discord.gg/JdmEkFS6dS)\n* [X(Twitter)](https://twitter.com/utilmeta)\n* [Reddit](https://www.reddit.com/r/utilmeta)\n* [\u4e2d\u6587\u8ba8\u8bba\u533a](https://lnzhou.com/channels/utilmeta/community)\n\n\n",
    "bugtrack_url": null,
    "license": "Apache 2.0",
    "summary": "Declare & parse types / dataclasses / functions based on Python type annotations",
    "version": "0.5.1",
    "project_urls": {
        "Documentation": "https://utype.io",
        "Homepage": "https://utype.io",
        "Project Home": "https://utype.io",
        "Source Code": "https://github.com/utilmeta/utype"
    },
    "split_keywords": [
        "utype",
        "type",
        "schema",
        "meta-type",
        "validation",
        "data-model",
        "type-transform",
        "parser",
        "json-schema"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f75e32936c7d163547e82f1b8769ad473ca732e5c0357988d700f916ad846fad",
                "md5": "f8208b82567df29558fdc5cdeb1d69cd",
                "sha256": "603b4f51b40c00db3a745171b96884a5929022e1a5ce22342cbe683fae6737f0"
            },
            "downloads": -1,
            "filename": "utype-0.5.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f8208b82567df29558fdc5cdeb1d69cd",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 79534,
            "upload_time": "2024-04-16T02:45:38",
            "upload_time_iso_8601": "2024-04-16T02:45:38.532553Z",
            "url": "https://files.pythonhosted.org/packages/f7/5e/32936c7d163547e82f1b8769ad473ca732e5c0357988d700f916ad846fad/utype-0.5.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "418d5e6186fcee3ed7253ebd0668b144fad91bfb391335b8828fbabf5ce57f0b",
                "md5": "9836206fa43af6358623f9aa0abb045f",
                "sha256": "30d60057a2e1aca306cb567a581c8ce52df78ead22b63dfaa7001de57a4f996a"
            },
            "downloads": -1,
            "filename": "utype-0.5.1.tar.gz",
            "has_sig": false,
            "md5_digest": "9836206fa43af6358623f9aa0abb045f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 74740,
            "upload_time": "2024-04-16T02:45:48",
            "upload_time_iso_8601": "2024-04-16T02:45:48.791589Z",
            "url": "https://files.pythonhosted.org/packages/41/8d/5e6186fcee3ed7253ebd0668b144fad91bfb391335b8828fbabf5ce57f0b/utype-0.5.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-16 02:45:48",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "utilmeta",
    "github_project": "utype",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "utype"
}
        
Elapsed time: 0.24032s