Gyver Attrs
Gyver-attrs is a Python library that provides a more flexible and feature-rich alternative to the built-in attrs and dataclasses libraries for defining classes. The main function provided by Gyver-attrs is define, which allows users to define classes with a range of options and features.
Installation
To install Gyver-attrs, you can use pip:
pip install gyver-attrs
Usage
The primary function in Gyver-attrs is define. It takes a list of field definitions and returns a new class with those fields defined as attributes. Here is a basic example:
from gyver_attrs import define, info
@define
class Person:
name: str
age: int = info(default=18)
This defines a new class Person with two attributes, name and age.
Features
Gyver-attrs provides a range of features that can be used to customize how classes are defined. These features include:
- maybe_cls: This option allows you to optionally specify the class to be created with the features applied. If it is None, it returns a callable that can wrap classes in the same way.
- frozen: This option replaces
__setattr__and__getattr__of the class with frozen versions that raise AttributeError when attributes are accessed or modified. This can be useful for creating immutable objects. - kw_only: This option makes the class's
__init__not accept positional arguments, forcing all arguments to be passed as keyword arguments. - slots: This option adds a
__slots__to the class with the necessary validations and compliances with inheritance. It also validates possible descriptors with__set_name__to add the expected name to the__slots__. - repr: This option adds a
__repr__method to the resulting class. - eq: This option provides
__eq__and__ne__methods for the resulting class, comparing all values against each other. Each field can have a parser or opt-out using field(eq=False) or field(eq=my_converter). - order: This option adds rich comparison support and supports the same mechanism of opt-out/converter as eq.
- hash: This option adds a hash function if all values are hashable and considers the converter from eq as well.
- pydantic: This option adds
__get_validators__to the class to make Pydantic support the classes by default. - dataclass_support: This option adds
__dataclass_fields__with each field converted to a dataclass.Field before returning it on a dict. This way, the class becomes a drop-in replacement for dataclasses.
Warning: dataclass_fields with pydantic=False will fail when trying to use with pydantic.
Methods
Gyver-attrs will add the following methods to your class.
gyver_attrs: This is a dictionary that maps each attribute name to its corresponding Field object. The Field object contains metadata about the attribute such as its name, type, default value, etc.
parse_dict(): This is a method that is used to parse the instance into a dict, recursively. Don't use it directly, instead call asdict(self).
iter(): This will yield (key, value) for all fields directly included in the class, and not any fields of nested objects. You can use as dict(self)
gserialize(): This is a class method that is used to serialize a dict into an instance of the class. Don't use it directly, instead call fromdict(self).
pydantic_validate(): This will validate inputs to support pydantic integration.
get_validators(): This is a classmethod to make your class pydantic-compatible.
modify_schema(): This is a class method that create schemas for your class when using Pydantic/FastAPI.
Also Gyver-attrs will not override custom defined functions instead, on conflict, you can still find them with the prefix __gattrs_.
Examples:
__init__ => __gattrs_init__
__hash__ => __gattrs_hash__
__parse_dict__ => __gattrs_parse_dict__
Helper Functions
Gyver-attrs provides helpers to integrate easily with your project
- Shortcuts
mutable: same asdefinebut with frozen=Truekw_only: same asdefinebut with kw_only=True
- Camel
define_camel: same asdefinebut with alias automatically as camel case. This can be either lower or upper camel which can be done bystyle="upper"orstyle="lower. By defaultdefine_cameluses lower.
- Helpers:
call_init: calls__gattrs_init__without mypy or pyright complainingfields: returns adict[str, Field]of the class, by returning__gyver_attrs__
- Factory:
mark_factory: decorator to mark function as factory
- Converters:
fromdict/fromjson: creates instance of class based on a dict/json(using orjson). Use asfromdict(YourClass, mapping)orfromjson(YourClass, yourjson)asdict/asjson: returns your instance as dict/json(using orjson) recursively. Use asasdict(instance, by_alias=True)orasdict(instance, by_alias=False)
Conclusion
Gyver-attrs provides a powerful and flexible way to define classes in Python, with a range of options and features that can be used to customize the behavior of the resulting classes. Whether you're building small scripts or large applications, Gyver-attrs can help you create classes that are tailored to your specific needs.
Metadata
Release files for gyver-attrs 0.9.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| gyver_attrs-0.9.4.tar.gz | 25.9 kB | Without provenance | Details for gyver_attrs-0.9.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| gyver_attrs-0.9.4-py3-none-any.whl | Python 3 | none | any | Details for gyver_attrs-0.9.4-py3-none-any.whl |
Total release size: 55.2 kB
Release files / gyver_attrs-0.9.4.tar.gz
| Download URL | gyver_attrs-0.9.4.tar.gz |
|---|---|
| Size | 25.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
160fa3dcc59cdeac11096681892d6ee1dffc22cf5c231ca7d148e06fdc513ca7
|
|
BLAKE2b-256 checksum How to use checksums |
64906de25152bab562145bfcbf767be91200089fdcef1ee5c31750cc92fc8443
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.12.4 Linux/6.9.5-arch1-1
|
Release files / gyver_attrs-0.9.4-py3-none-any.whl
| Download URL | gyver_attrs-0.9.4-py3-none-any.whl |
|---|---|
| Size | 29.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
47a1a7c4fb39c9394de47d4086d852237603ef71e9e3753aac8c9da34f32f1e4
|
|
BLAKE2b-256 checksum How to use checksums |
d2fb258d12422167bc65885ed8e841642e141115bc52c8f6cdb42b38898c7214
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.12.4 Linux/6.9.5-arch1-1
|
Security for gyver-attrs 0.9.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.