scalarstop.dataclasses

A forked version of the Python dataclasses module.

Create a dataclass with the decorator @sp.dataclass as follows:

import scalarstop as sp

@sp.dataclass
class Hyperparams(sp.HyperparamsType):
    val1: int
    val2: str

The Python dataclasses module uses singletons to compare certain values in internal code. This makes it difficult to cloudpickle a dataclass and then unpickle it.

This version of the dataclasses module is better-designed to be shared across different Python processes.

Module Contents

Functions

is_dataclass(obj: Any) → bool

Returns True if the input is a Python dataclass.

fields(class_or_instance: Any) → Tuple[(Any, …)]

Forked verson of dataclasses.fields().

asdict(obj: Any, *, dict_factory: type = dict) → Dict[(str, Any)]

Forked verson of dataclasses.asdict().

astuple(obj, *, tuple_factory=tuple) → Tuple[(Any, …)]

Forked verson of dataclasses.astuple().

dataclass
is_dataclass(obj: Any)bool

Returns True if the input is a Python dataclass.

fields(class_or_instance: Any)Tuple[Any, ]

Forked verson of dataclasses.fields().

Return a tuple describing the fields of this dataclass. Accepts a dataclass or an instance of one. Tuple elements are of type Field.

asdict(obj: Any, *, dict_factory: type = dict)Dict[str, Any]

Forked verson of dataclasses.asdict().

Return the fields of a dataclass() instance as a new tuple of field values.

Example usage:

  @dataclass
  class C:
      x: int
      y: int

c = C(1, 2)

assert asdict(c) == {'x': 1, 'y': 2}

If given, dict_factory will be used instead of built-in dict.

The function applies recursively to field values that are dataclass() instances. This will also look into built-in containers: tuple, list, and dict.

astuple(obj, *, tuple_factory=tuple)Tuple[Any, ]

Forked verson of dataclasses.astuple().

Return the fields of a dataclass() instance as a new tuple of field values.

Example usage:

@dataclass
class C:
    x: int
    y: int

c = C(1, 2)
assert astuple(c) == (1, 2)

If given, tuple_factory will be used instead of built-in tuple.

The function applies recursively to field values that are dataclass() instances. This will also look into built-in containers: tuple, list, and dict.