Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Unified API for working with multiple dataclass-like libraries
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...
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.
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
)
dataclasses
collections.namedtuple
pydantic
(v1 and v2)attrs
msgspec
dataclassy
sqlmodel
(it's just pydantic)... maybe someday?
FAQs
Utilities for providing compatibility with many dataclass-like libraries
We found that fieldz demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.