python-attrs / cattrs

Composable custom class converters for attrs, dataclasses and friends.
https://catt.rs
MIT License
808 stars 113 forks source link

Register multiple hooks for a class #541

Closed isohedronpipeline closed 4 months ago

isohedronpipeline commented 4 months ago

I mentioned this at the end of another ticket, but I thought it deserved its own issue.

I'd like to register multiple unstructure hooks to a class. Specifically, I want to register subclasses and also omit defaults.

How can I combine this: include_subclasses(Toy, c, union_strategy=configure_tagged_union) with this: hook = make_dict_unstructure_fn(Toy, c, _cattrs_omit_if_default=True)

Thanks :-)

Ben

Tinche commented 4 months ago

Hello!

Can you create your converter with Converter(omit_if_default=True)? That's the easiest way. If not we can brainstorm further.

isohedronpipeline commented 4 months ago

I think so! Thanks Tin :-)

Tinche commented 4 months ago

Alright, closing this to keep the issue list more manageable ;)