Utilities for providing compatibility with many dataclass-like libraries
Project description
fieldz
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.
fieldz
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 fieldz.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 fieldz.Field
and fieldz.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 fieldz 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 fieldz-0.1.0.tar.gz
.
File metadata
- Download URL: fieldz-0.1.0.tar.gz
- Upload date:
- Size: 17.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee3352534bc5d04ee8e1cafa5f59add2c11bea0fc2ecefe18454ccfdc7e21fb2 |
|
MD5 | 0e4e1528b9a8a9a52e3bf91b6ddbbcf5 |
|
BLAKE2b-256 | 24556b5cc16fcd242610bed76486980594e9f0ab2350dac041fcc6cb63daa38f |
File details
Details for the file fieldz-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: fieldz-0.1.0-py3-none-any.whl
- Upload date:
- Size: 17.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0825a45396877769b107ce016e63f6fedbe625a0ace3e56fe2f9d6d61c7ea8df |
|
MD5 | d0387f5c8c37ab115f4fc9ed9218d76f |
|
BLAKE2b-256 | 862d90a713b473206f5505e01fbf86d881b3856cb3461503d9b073bf55b19ce6 |