Skip to main content

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

Project details


Download files

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

Source Distribution

classvalidator-0.0.1.tar.gz (3.9 kB view hashes)

Uploaded Source

Built Distribution

classvalidator-0.0.1-py3-none-any.whl (4.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page