Skip to content

Is there a way to recursively prefer _cattrs_use_alias=True? #596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
BrianPugh opened this issue Nov 5, 2024 · 3 comments
Closed

Is there a way to recursively prefer _cattrs_use_alias=True? #596

BrianPugh opened this issue Nov 5, 2024 · 3 comments

Comments

@BrianPugh
Copy link

In my application, i have several nested structures that have attrs aliases set (e.g. field(..., alias="foo")). I am aware that I could individually register a hook for every class with:

converter.register_structure_hook(
    MyClass,
    make_dict_structure_fn(MyClass, converter, _cattrs_use_alias=True),
)

However, this requires the developer to remember to do that for every new class. Is there a way of applying this setting for the whole converter?

@BrianPugh
Copy link
Author

BrianPugh commented Nov 5, 2024

I think the solution is:

import attrs
import cattrs

converter.register_structure_hook_factory(
    attrs.has,
    lambda cl: cattrs.gen.make_dict_structure_fn(cl, converter, _cattrs_use_alias=True)
)

would love to have @Tinche to chime in if this is the intended solution (and then close this issue)! Thanks!

@Tinche
Copy link
Member

Tinche commented Nov 5, 2024

You got to it before I did! That's exactly what I would have suggested.

@BrianPugh
Copy link
Author

thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants