GithubHelp home page GithubHelp logo

pyapp-kit / fieldz Goto Github PK

View Code? Open in Web Editor NEW
7.0 2.0 1.0 66 KB

Unified API for working with multiple dataclass-like libraries

License: BSD 3-Clause "New" or "Revised" License

Python 100.00%
attrs dataclasses msgspec pydantic

fieldz's Introduction

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!

import dataclasses

@dataclasses.dataclass
class SomeDataclass:
    a: int = 0
    b: str = "b"
    c: list[int] = dataclasses.field(default_factory=list)
import pydantic

class SomePydanticModel(pydantic.BaseModel):
    a: int = 0
    b: str = "b"
    c: list[int] = pydantic.Field(default_factory=list)
import attr

@attr.define
class SomeAttrsModel:
    a: int = 0
    b: str = "b"
    c: list[int] = attr.field(default=attr.Factory(list))
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?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.