Utilities for providing compatibility with many dataclass-like libraries
Project description
dataclass-compat
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
-
dataclasses
-
collections.namedtuple
-
pydantic
(v1 and v2) -
attrs
-
msgspec
-
dataclassy
-
sqlmodel
(it's just pydantic)
... maybe someday?
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for dataclass_compat-0.0.1rc1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7450ff81e7742b2d69d66364d13a5a6b483099d12bda5619004a439dd4bc1c36 |
|
MD5 | ab21148f9e919b11e9a8164ce8cb765a |
|
BLAKE2b-256 | 8de6b869385f40133c5057a43a58999b4dd060e319e47138f88dc1c937920440 |
Hashes for dataclass_compat-0.0.1rc1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a52a4f0280fc932c20b4b685cd60770f796742f62b9ff12d15b67764fa2d955 |
|
MD5 | 70702b660f9eca594522a35433749865 |
|
BLAKE2b-256 | 98cee42ad1c9cb629d3fe66ee6f9ea32d324f75e184df4f21f98cb824362df54 |