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
File details
Details for the file dataclass_compat-0.0.1.tar.gz
.
File metadata
- Download URL: dataclass_compat-0.0.1.tar.gz
- Upload date:
- Size: 16.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 282396691244ebd9c59a788497ae1f411e5cfa61ef86bd56d52a4b6e070b49d8 |
|
MD5 | ebff53289e69722c210f0f2a4193439c |
|
BLAKE2b-256 | bc9d1a8256322b6bbed04147ba5e09c14975d6e96a8f050b4628e73560d2b2e1 |
File details
Details for the file dataclass_compat-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: dataclass_compat-0.0.1-py3-none-any.whl
- Upload date:
- Size: 16.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c1b03af6137a37ec660c557b02f4cd0ddab4fa8c6f5314dd7cb67a71640e902d |
|
MD5 | 51cc50df285e35efc2da4895e699ebf7 |
|
BLAKE2b-256 | 8fee86930a3b7fa639bedc3e1ed9aeecd9a61ad43873e59429fc6052a4cea11d |