Skip to content
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

Feature request: "flatten" operation for cattrs.gen #352

Open
gmacon opened this issue Apr 12, 2023 · 0 comments
Open

Feature request: "flatten" operation for cattrs.gen #352

gmacon opened this issue Apr 12, 2023 · 0 comments

Comments

@gmacon
Copy link

gmacon commented Apr 12, 2023

For a thing at work, I implemented some [un]structure hook factories that wrap make_dict_structure_fn and make_dict_unstructure_fn to support flattening an attribute, like this:

@define
class A:
    a: int
    b: int

@define
class C:
    a: A
    c: int

converter = ...  # set up a converter with my wrappers

assert converter.unstructure(C(A(1, 2), 3)) == {'a': 1, 'b': 2, 'c': 3}

I'd like to upstream this support. The basic idea is to implement functionality similar to Serde's Struct flattening by extending the override function to take a flatten: bool argument that defaults to False and then adjusting make_dict_structure_fn and make_dict_unstructure_fn to use it.

I'm willing to work on this, but wanted to get feedback from folks here before I start.

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

1 participant