Runtime type validation of Dataclass instances
Project description
classvalidator
Classvalidator offers runtime type validation of dataclasses.dataclass instances using their type hint information.
Installation
pip install classvalidator
Usage
To use, simply import the classvalidator.validate function and call it on any dataclass instance you'd like to validate. The following is a simple example:
from dataclasses import dataclass
from typing import Any, Dict, List, Optional, Tuple, Union
from classvalidator import validate
@dataclass()
class SomeClass:
some_atrr_one: str
some_atrr_two: int
some_atrr_three: List[int]
# valid instance
instance = SomeClass(some_atrr_one='value one', some_atrr_one=22, some_atrr_three=[1, 2])
# no errors
validate(instance)
# resetting some attributes to invalid types (aka make the instance invalid)
instance.some_atrr_two = 'should be an int, not string'
# TypeError will be thrown on validation
try:
validate(instance)
except TypeError as e:
print(e)
You can also validate iterables and their elemet types (e.g List[str], Tuple[str, str, int]. Here's an example:
from dataclasses import dataclass
from typing import Any, Dict, List, Optional, Tuple, Union
from classvalidator import validate
@dataclass()
class SomeClass:
some_atrribute: Tuple[int, str]
# valid instance
instance = SomeClass(some_atrribute=(10, 'some value'))
# no errors
validate(instance)
# resetting tuple elements to an invalid Tuple[int, int]
instance.some_atrribute = (10, 10)
# TypeError will be thrown on validation
try:
validate(instance)
except TypeError as e:
print(e)
Limitations
- This library has only been tested on Python >= 3.72
- Only builtin python types are supported (i.e
bool, str, int, float, List, Tuple, Dict, Set, etc.) - Validation only happens for recognised types. Unrecognised types will be ignored without failure.
- Even though Dictionary types are validated, the
key-valuetypes are not validated - For Tuples with
Elipsis, the element types will not be validated
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file classvalidator-0.0.1.tar.gz.
File metadata
- Download URL: classvalidator-0.0.1.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f2978a9dd647f00e8c26a85f16434838d85a98db85617d806d7a2fa45a4aceb2
|
|
| MD5 |
df8d77cc4e5ce473a7664061a2923d81
|
|
| BLAKE2b-256 |
6d6e2127204e890216ab64e53e433d8e2cfe2f8649dbe433ba918d7be39da99c
|
File details
Details for the file classvalidator-0.0.1-py3-none-any.whl.
File metadata
- Download URL: classvalidator-0.0.1-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8b8f3ce307409998415067b7b04081dde372dd56662a7085b53dcef62a6b0170
|
|
| MD5 |
91acae4c5aaa7d3c2ce6227a89da7dfd
|
|
| BLAKE2b-256 |
bc65787dc1f56d4ec46ea350682b37e749a974cafa95b485fd2d9050c9eb7963
|