Skip to main content

Serialize/deserialize Python objects from/to typed structures.

Project description

serdataclasses

image-version image-license image image-ci

This library has the following goals:

  1. "Deserialize" unstructured Python types into structured, type-hinted Python types (dataclasses.dataclass, typing.NamedTuples).
  2. "Serialize" structured, type-hinted Python objects into unstructured Python types (eg, the reverse)
  3. Provide the user clear error messages in the event that serde fails.
  4. Require no type changes on the part of the user. No need to give your containers a special type to help this library perform serde, it works out of the box.
  5. [Optionally] automatically convert primitive types, but stop converting when ambiguous types are encountered (Union), but handle the special case of Optional, which is used in many codebases.
  6. Work correctly for all forms of NamedTuples and dataclasses. Unfortunately, prior to Python 3.8, the dataclasses had some deficiencies. Mainly, dataclasses.InitVar was a singleton whose contained type could not be inspected at runtime. For this reason, only Python 3.8+ is supported.

No external dependencies. Python 3.8+.

Installation

# With pip
pip install serdataclasses

# With poetry
poetry add serdataclasses

Usage

import dataclasses
import typing
import serdataclasses

@dataclasses.dataclass
class SmallContainer:
    my_str: str

@dataclasses.dataclass
class BigContainer:
    my_int: int
    my_list: typing.List[SmallContainer]

MY_DATA = {
    "my_int": 1,
    "my_list": [
        { "my_str": "rawr" },
        { "my_str": "woof" },
    ],
}

# Deserialization
MY_STRUCTURED_DATA = serdataclasses.load(MY_DATA, BigContainer)
print("Deserialization:", MY_STRUCTURED_DATA)

# Serialization
MY_UNSTRUCTURED_DATA_AGAIN = serdataclasses.dump(MY_STRUCTURED_DATA)
print("Serialization:", MY_UNSTRUCTURED_DATA_AGAIN)

Result:

Deserialization: BigContainer(my_int=1, my_list=[SmallContainer(my_str='rawr'), SmallContainer(my_str='woof')])
Serialization: {'my_int': 1, 'my_list': [{'my_str': 'rawr'}, {'my_str': 'woof'}]}

Local Development

Local development for this project is quite simple.

Dependencies

Install the following tools manually.

Recommended

Set up development environment

make setup

Run Tests

make test

Notes

  • Initially inspired by undictify and a PR I helped with. serdataclasses's goals are different; it's exclusively focused on serde instead of general function signature overrides.
  • I also notice some striking similarities with a library called typedload (great minds think alike, I guess :p). I renamed my top-level functions to "load" and "dump" in typedload's homage. Unfortunately, as of version 1.20, typedload does not handle all types of dataclasses elegantly (mainly, InitVar). Since typedload supports Python 3.5+, it never will elegantly handle all dataclasses without lots of unfortunate conditionals in the codebase. If you must use Python 3.7-, I suggest looking into typedload.

Written by

Samuel Roeca samuel.roeca@gmail.com

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

serdataclasses-0.7.0.tar.gz (10.1 kB view hashes)

Uploaded Source

Built Distribution

serdataclasses-0.7.0-py3-none-any.whl (9.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