Skip to main content

dataclasses with verifiers/converters

Project description


type verification and convert

from typing import *
from rx.operators import map as rxmap
from rxdata import dataclass, field, operators

def try_convert(n):
    return int(n) if isinstance(n, str) and n.isalpha() else n

class Data:
    # only int.
    number: int                         = field(default=1,

    # only int or string convertable to int. Always results in int.
    convert_or_die: Union[int]          = field(default=1,

    # If is string and convertable to int - perform conversion. `typing.Any` type defined explicitly during `invoke` (just for example).
    convert_or_accept: Union[int, str]  = field(default=1,

> data = Data(None)
TypeError: type of number must be int; got NoneType instead

> data = Data(convert_or_die=None)
TypeError: type of convert_or_die must be int; got NoneType instead

> Data(convert_or_accept=None)
Data(number=1, convert_or_die=1, convert_or_accept=None)

data = Data()

data.number = '3'
> TypeError: type of number must be int; got str instead

data.number = 2
> Data(number=2, convert_or_die=1, convert_or_accept=1)

data.convert_or_die = 2
> Data(number=2, convert_or_die=2, convert_or_accept=1)

data.convert_or_accept = '3'
> Data(number=2, convert_or_die=2, convert_or_accept=3)

data.convert_or_accept = '3s'
> Data(number=2, convert_or_die=2, convert_or_accept='3s')

data.convert_or_accept = type
> Data(number=2, convert_or_die=2, convert_or_accept=<class 'type'>)


reactive python3 dataclasses - with attrs-like behaviour

  • implicit and explicit typing verification during both init and/or setattr
  • customize preprocessing (convert) during both init and/or setattr
  • reactive attributes


> pip3 install rx-dataclasses

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

rx-dataclasses-0.0.4.tar.gz (18.2 kB view hashes)

Uploaded source

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