GithubHelp home page GithubHelp logo

Comments (3)

Tinche avatar Tinche commented on June 26, 2024

Howdy!

Yeah, like you've noticed, cattrs skips init=False fields by default.

This can be changed on a case-by-case basis by generating and registering an unstructure hook for a class.

from cattrs import Converter
from cattrs.gen import make_dict_unstructure_fn

def test_class_with_init_false():
    foo = ClassWithInitFalse(a=1, b="b")

    conv = cattrs.Converter()
    conv.register_unstructure_hook(
        ClassWithInitFalse,
        make_dict_unstructure_fn(
            ClassWithInitFalse, conv, _cattrs_include_init_false=True
        ),
    )

    serialized = conv.unstructure(foo)
    assert serialized == {"a": 1, "b": "b", "d": 2}

This can be applied wider (for example, to all attrs classes) using a hook factory that's similar to this. That's a little more complex but I can show you how if you need to.

I'm going to close this now to keep the issue list tidy, let me know if you have further questions!

from cattrs.

akefirad avatar akefirad commented on June 26, 2024

Thanks @Tinche for the reply.
Yes, I’d like to enable it for all classes in my app. (Out of curiosity; why is it not enabled by default for all?)
So I’d appreciate if you show how it can be done (or point me to docs if there’s any for this)
Thanks.

from cattrs.

Tinche avatar Tinche commented on June 26, 2024

Yes, I’d like to enable it for all classes in my app.

Instead of a single unstructure hook for ClassWithInitFalse, we're going to define an unstructure hook factory for all attrs classes. Here's the code, it looks somewhat similar to the single-class case:

from attrs import field, frozen, has

import cattrs
from cattrs.gen import make_dict_unstructure_fn


@frozen
class ClassWithInitFalse:
    a: int
    b: str
    d: int = field(init=False, default=2)


def test_class_with_init_false():
    foo = ClassWithInitFalse(a=1, b="b")

    conv = cattrs.Converter()
    conv.register_unstructure_hook_factory(
        has,
        lambda t: make_dict_unstructure_fn(t, conv, _cattrs_include_init_false=True),
    )

    serialized = conv.unstructure(foo)
    assert serialized == {"a": 1, "b": "b", "d": 2}

(Out of curiosity; why is it not enabled by default for all?)

I find folks usually use init=False attributes for computed attributes that get set in __attrs_post_init__.

point me to docs if there’s any for this

I guess we don't have exact docs for this, but you can look at https://catt.rs/en/latest/usage.html#using-factory-hooks for a larger example for customizing attrs handling using factory hooks.

from cattrs.

Related Issues (20)

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.