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?
Metadata
Release files for dataclass-compat 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| dataclass_compat-0.0.1.tar.gz | 16.3 kB | Without provenance | Details for dataclass_compat-0.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| dataclass_compat-0.0.1-py3-none-any.whl | Python 3 | none | any | Details for dataclass_compat-0.0.1-py3-none-any.whl |
Total release size: 33.2 kB
Release files / dataclass_compat-0.0.1.tar.gz
| Download URL | dataclass_compat-0.0.1.tar.gz |
|---|---|
| Size | 16.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
282396691244ebd9c59a788497ae1f411e5cfa61ef86bd56d52a4b6e070b49d8
|
|
BLAKE2b-256 checksum How to use checksums |
bc9d1a8256322b6bbed04147ba5e09c14975d6e96a8f050b4628e73560d2b2e1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/4.0.2 CPython/3.11.5
|
Release files / dataclass_compat-0.0.1-py3-none-any.whl
| Download URL | dataclass_compat-0.0.1-py3-none-any.whl |
|---|---|
| Size | 16.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c1b03af6137a37ec660c557b02f4cd0ddab4fa8c6f5314dd7cb67a71640e902d
|
|
BLAKE2b-256 checksum How to use checksums |
8fee86930a3b7fa639bedc3e1ed9aeecd9a61ad43873e59429fc6052a4cea11d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/4.0.2 CPython/3.11.5
|
Security for dataclass-compat 0.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.