Skip to main content

The dataclass_plus is a fastest type validation library for the dataclass

Project description

dataclass_plus

Python 3.6 pypi travis-badge Coverage Status

The dataclass_plus is a fastest type validation library for the dataclass

Install

pip install dataclass-plus

Example

Basic Example

from dataclass_plus import dataclass_plus
from typing import List, Dict, Tuple


@dataclass_plus
class Model:
    id: int
    name: str
    dict_example: Dict[str, str]
    list_example: List[int] # this field is required
    tuple_example: Tuple[str, float] = None # this field is not required because set default None


Model(
    id=1, 
    name='Test Test', 
    dict_example={"test": "test"}, 
    list_example=[1,2],
    tuple_example=("test", 1.2)
)
# => Model(id=1, name='Test Test', dict_example={"test": "test"}, list_example=[1,2], tuple_example=("test", 1.2))

# Invalid Model
Model(
    id=1, 
    name='Test Test', 
    dict_example={"test": 1}, 
    list_example=[1,2],
    tuple_example=("test", 1.2)
)
# => TypeError: {'test': 1} is not typing.Dict[str, str]

Nested Example

from dataclass_plus import dataclass_plus
from typing import List, Dict, Tuple


@dataclass_plus
class Model:
    nested_example: List[List[str]]
    multi_nested_example: Dict[str, List[Tuple[str, List[int]]]]


Model(
    nested_example=[["test"], ["test_2"]],
    multi_nested_example={"test": [("test",[1, 2])]}
)
# => Model(nested_example=[['test'], ['test_2']], multi_nested_example={'test': [('test', [1, 2])]})

# Invalid Model
Model(
    nested_example=[["test"], ["test_2"]],
    multi_nested_example={"test": [("test",[1, "test"])]}
)
# => TypeError: [1, 'test'] is not typing.List[int]

Note: Can use every feature of dataclass (Example: frozen=True, init=True ...)

Project details


Download files

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

Files for dataclass-plus, version 2.0.0
Filename, size File type Python version Upload date Hashes
Filename, size dataclass_plus-2.0.0-py3-none-any.whl (17.5 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size dataclass_plus-2.0.0.tar.gz (5.4 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page