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 attrs

@attrs.define
class SomeAttrsModel:
    a: int = 0
    b: str = "b"
    c: list[int] = attrs.field(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.1.3.tar.gz (18.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fieldz-0.1.3-py3-none-any.whl (18.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fieldz-0.1.3.tar.gz
  • Upload date:
  • Size: 18.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fieldz-0.1.3.tar.gz
Algorithm Hash digest
SHA256 bc21013b2bd5f8a4a782ecef440343ff059fc0a4a2033ae4d616311675260585
MD5 f6ca3e381788e0ad5c30caa65b461145
BLAKE2b-256 78c29d35a747f439051bff41511178b0b527f1466c7fd4b5a5f7357c136e82bc

See more details on using hashes here.

Provenance

The following attestation bundles were made for fieldz-0.1.3.tar.gz:

Publisher: ci.yml on pyapp-kit/fieldz

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: fieldz-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 18.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fieldz-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f975fc6f250958966e39504f89bf09bad41479b4c5e0b06b36d13fd92fbac792
MD5 896cb3a47709ebc6efbe871e81a59264
BLAKE2b-256 12a05db990f52226aa4e01e268f285fa56ddf233b3b27b993b4ecc751cf2216f

See more details on using hashes here.

Provenance

The following attestation bundles were made for fieldz-0.1.3-py3-none-any.whl:

Publisher: ci.yml on pyapp-kit/fieldz

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page