Skip to main content

Utilities for providing compatibility with many dataclass-like libraries

Project description

dataclass-compat

License PyPI Python Version CI codecov

Unified API for working with multiple dataclass-like libraries

Dataclass patterns

There are many libraries that implement a similar dataclass-like pattern!

dataclasses.dataclass

import dataclasses

@dataclasses.dataclass
class SomeDataclass:
    a: int = 0
    b: str = "b"
    c: list[int] = dataclasses.field(default_factory=list)

pydantic.BaseModel

import pydantic

class SomePydanticModel(pydantic.BaseModel):
    a: int = 0
    b: str = "b"
    c: list[int] = pydantic.Field(default_factory=list)

attrs.define

import attr

@attr.define
class SomeAttrsModel:
    a: int = 0
    b: str = "b"
    c: list[int] = attr.field(default=attr.Factory(list))

msgspec.Struct

import msgspec

class SomeMsgspecStruct(msgspec.Struct):
    a: int = 0
    b: str = "b"
    c: list[int] = msgspec.field(default_factory=list)

etc...

Unified API

These are all awesome libraries, and each has its own strengths and weaknesses. Sometimes, however, you just want to be able to query basic information about a dataclass-like object, such as getting field names or types, or converting it to a dictionary.

dataclass-compat provides a unified API for these operations (following or extending the API from dataclasses when possible).

def fields(obj: Any) -> tuple[Field, ...]:
    """Return a tuple of dataclass_compat.Field objects for the object."""

def replace(obj: Any, /, **changes: Any) -> Any:
    """Return a copy of obj with the specified changes."""

def asdict(obj: Any) -> dict[str, Any]:
    """Return a dict representation of obj."""

def astuple(obj: Any) -> tuple[Any, ...]:
    """Return a tuple representation of obj."""

def params(obj: Any) -> DataclassParams:
    """Return parameters used to define the dataclass."""

The dataclass_compat.Field and dataclass_compat.DataclassParam objects are simple dataclasses that match the protocols of dataclasses.Field and the (private) dataclasses._DataclassParams objects, respectively. The field object also adds a native_field attribute that is the original field object from the underlying library.

Example

from dataclass_compat import Field, fields

standardized_fields = (
    Field(name="a", type=int, default=0),
    Field(name="b", type=str, default="b"),
    Field(name="c", type=list[int], default_factory=list),
)

assert (
    fields(SomeDataclass)
    == fields(SomePydanticModel)
    == fields(SomeAttrsModel)
    == fields(SomeMsgspecStruct)
    == standardized_fields
)

Supported libraries

... maybe someday?

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

dataclass_compat-0.0.1rc2.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

dataclass_compat-0.0.1rc2-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

Details for the file dataclass_compat-0.0.1rc2.tar.gz.

File metadata

  • Download URL: dataclass_compat-0.0.1rc2.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for dataclass_compat-0.0.1rc2.tar.gz
Algorithm Hash digest
SHA256 9d8b5aa56be0eae34cabc85ef378f919e7cedb746d7f1443c9c14a553ffd097a
MD5 022718f13f7c0ebfaa3d49f100841c08
BLAKE2b-256 d19ce34b5825e895ad266b4b4bbe47bf1d275d97487e34c08bca15bf66814a46

See more details on using hashes here.

File details

Details for the file dataclass_compat-0.0.1rc2-py3-none-any.whl.

File metadata

File hashes

Hashes for dataclass_compat-0.0.1rc2-py3-none-any.whl
Algorithm Hash digest
SHA256 8290bee7af1b13036bb5f10acf9bdd6aabea4b49e93d43091808acac0a2e72bb
MD5 1a77cd5cea2b3535777e5a59df1ebf96
BLAKE2b-256 c0cf9bc53136e1e64707d212a677d3496f98b9f2a7a9c63f978bd84ef13b0750

See more details on using hashes here.

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