Skip to main content

Utilities for providing compatibility with many dataclass-like libraries

Project description

fieldz

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.

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

... 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

fieldz-0.0.2.tar.gz (17.0 kB view details)

Uploaded Source

Built Distribution

fieldz-0.0.2-py3-none-any.whl (17.4 kB view details)

Uploaded Python 3

File details

Details for the file fieldz-0.0.2.tar.gz.

File metadata

  • Download URL: fieldz-0.0.2.tar.gz
  • Upload date:
  • Size: 17.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for fieldz-0.0.2.tar.gz
Algorithm Hash digest
SHA256 dad3a0998fbf414acd9468d8110d8e9680428f2f8223f83a08641c43251ab9cc
MD5 eb6131e1d19e062de218ed6f18cfc3b3
BLAKE2b-256 dcb50d17dd7ed2372d2c932399f41272c25387c8ea6ec4d4dcbfd7cd6cf998c3

See more details on using hashes here.

File details

Details for the file fieldz-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: fieldz-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 17.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for fieldz-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cf385cc75765370819cc6a87522316a6a3283d8bab5361b173b21dd0d921aec1
MD5 fe195c4b44e89870441ce5161efcf919
BLAKE2b-256 e8f21da33434728411e1686b6f23914f080f108514442019a8b4329bc410f41d

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