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
Explicitly calling dataclass_type_validator from within your dataclass
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\'>"]'})
The same, but using the class decorator instead
from dataclasses import dataclass
from typing import List
from dataclass_type_validator import dataclass_validate
from dataclass_type_validator import TypeValidationError
@dataclass_validate
@dataclass()
class User:
id: int
name: str
friend_ids: List[int]
# 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\'>"]'})
You can also pass the strict
param (which defaults to False) to the decorator:
@dataclass_validate(strict=True)
@dataclass(frozen=True)
class SomeList:
values: List[str]
# Invalid item contained in typed List
try:
SomeList(values=["one", "two", 3])
except TypeValidationError as e:
print(e)
# => TypeValidationError: Dataclass Type Validation Error (errors = {
# 'x': 'must be an instance of typing.List[str], but there are some errors:
# ["must be an instance of <class \'str\'>, but received <class \'int\'>"]'})
You can also pass the before_post_init
param (which defaults to False) to the decorator,
to force the type validation to occur before __post_init__()
is called. This can be used
to ensure the types of the field values have been validated before your higher-level semantic
validation is performed in __post_init__()
.
@dataclass_validate(before_post_init=True)
@dataclass
class User:
id: int
name: str
def __post_init__(self):
# types of id and name have already been checked before this is called.
# Otherwise, the following check will throw a TypeError if user passed
# `id` as a string or other type that cannot be compared to int.
if id < 1:
raise ValueError("superuser not allowed")
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
File details
Details for the file dataclass-type-validator-0.1.2.tar.gz
.
File metadata
- Download URL: dataclass-type-validator-0.1.2.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.0.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 575f5ea89b5965ab5b3079cd67115b37a75a529fe221c35159e036e99faa0eb4 |
|
MD5 | 78c8a44bb97ba4197d712c856fdc1357 |
|
BLAKE2b-256 | 6eee2b7d90951d396145262e6db360dbdacec0a4784d7aa71fe9f4892f4c9910 |
File details
Details for the file dataclass_type_validator-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: dataclass_type_validator-0.1.2-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.0.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 85b759f17ee106245f8748b9f5381bd9ad225dbeef573feee3ce46cdbfaaa8a7 |
|
MD5 | a216f5a0be47527fb3b3fa574e460027 |
|
BLAKE2b-256 | a700b5e8fe114aad4fa1eb93efdcce87f1494f0ba6ba7275d676b5eaff12d842 |