Skip to main content

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-value types are not validated
  • For Tuples with Elipsis, the element types will not be validated

Release files for classvalidator 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for classvalidator 0.0.1
File Size Uploaded
classvalidator-0.0.1.tar.gz 3.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for classvalidator 0.0.1
File Interpreter ABI Platform
classvalidator-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 8.3 kB

Release files / classvalidator-0.0.1.tar.gz

Download URL classvalidator-0.0.1.tar.gz
Size 3.9 kB
Tags Source
SHA-256 checksum
How to use checksums
f2978a9dd647f00e8c26a85f16434838d85a98db85617d806d7a2fa45a4aceb2
BLAKE2b-256 checksum
How to use checksums
6d6e2127204e890216ab64e53e433d8e2cfe2f8649dbe433ba918d7be39da99c
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / classvalidator-0.0.1-py3-none-any.whl

Download URL classvalidator-0.0.1-py3-none-any.whl
Size 4.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8b8f3ce307409998415067b7b04081dde372dd56662a7085b53dcef62a6b0170
BLAKE2b-256 checksum
How to use checksums
bc65787dc1f56d4ec46ea350682b37e749a974cafa95b485fd2d9050c9eb7963
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page