Dataclass Type Validator Library
Project description
dataclass-type-validator
The dataclass-type-validator
is a type validation library for the properties of dataclasses.dataclass
using Python type hint information.
Installation
pip install dataclass-type-validator
or add dataclass-type-validator
line to requirements.txt
A Simple Example
from dataclasses import dataclass
from typing import List
from dataclass_type_validator import dataclass_type_validator
from dataclass_type_validator import TypeValidationError
@dataclass()
class User:
id: int
name: str
friend_ids: List[int]
def __post_init__(self):
dataclass_type_validator(self)
# Valid User
User(id=10, name='John Smith', friend_ids=[1, 2])
# => User(id=10, name='John Smith', friend_ids=[1, 2])
# Invalid User
try:
User(id='a', name=['John', 'Smith'], friend_ids=['a'])
except TypeValidationError as e:
print(e)
# => TypeValidationError: Dataclass Type Validation (errors = {
# 'id': "must be an instance of <class 'int'>, but received <class 'str'>",
# 'name': "must be an instance of <class 'str'>, but received <class 'list'>",
# 'friend_ids': 'must be an instance of typing.List[int], but there are some errors:
# ["must be an instance of <class \'int\'>, but received <class \'str\'>"]'})
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for dataclass-type-validator-0.0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | ac20d7dc899524a6dce26d5dac252e16c99d8c236a7b33f333ef5341cdaecaa0 |
|
MD5 | 1a907fca82c61cae5b1d86c7a123b10d |
|
BLAKE2b-256 | c38975458b0c0fe054b8a83352984a870a20ad5889f3672021d2cd49b6098149 |
Close
Hashes for dataclass_type_validator-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eabd1e75e724bdf45c68c2ac535b3ee3733a7782dccd6aacb5b5fabd5cdf1d86 |
|
MD5 | 2fc39096012892dbbca89e9e3d2b9289 |
|
BLAKE2b-256 | e766de071bacd0777f3565c1dc451ddd5d8bccead4899e33ab53c379c12bc056 |