python-attrs / python-attrs/cattrs
Adding a `make_dict_unstructure_fn` breaks `omit_if_default=True`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.1k
- Forks
- 159
- Avg merge
- 12h 21m
- Merged PRs (30d)
- 6
Description
- cattrs version: 1.10.0
- attrs version: 21.4.0
- Python version: 3.8.12
- Operating System: Manjaro Linux
Description
I'm yet to get a minimal working example, but it's something like so:
@attrs.define
class Whatever:
foo: str = "foo"
bar: str = "bar"
thing: str = "thing"
u_thing: str = "_thing"
whatever = Whatever(bar="baz")
c = cattrs.GenConverter(omit_if_default=True)
c.unstructure(whatever) # Does omit if default!
unstruct_hook = cattr.gen.make_dict_unstructure_fn(
Whatever,
c,
**{"u_thing": cattr.override(rename="_thing")}
)
c.register_unstructure_hook(Whatever, unstruct_hook)
c.unstructure(whatever) # Does NOT omit if default :c
I do a lot of other things to the converters, but from my testing, this is what's breaking it.
I need those extra hooks because I need both thing and _thing to show up on the output, and attrs doesn't seem to allow that, so I need to rename it on the fly.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the shown example with attrs, cattrs.GenConverter, and make_dict_unstructure_fn, then compare the generated hook with the converter's default behavior. Trace how the renamed u_thing field is handled and verify that omit_if_default=True still omits unchanged defaults while preserving the rename.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100