cattrs icon indicating copy to clipboard operation
cattrs copied to clipboard

omit_if_default ignores attrs' converters

Open antony-frolov opened this issue 1 month ago • 2 comments

Hi! It seems like currently converter with omit_if_default=True does not apply attrs' converters when checking if the value is equal to the default one.

import attrs
import cattrs

c = cattrs.Converter(omit_if_default=True)


@attrs.define
class A:
    b: int = attrs.field(default="0", converter=int)


c.unstructure(A())
# {'b': 0}


@attrs.define
class A:
    b: int = attrs.field(default=0, converter=int)


c.unstructure(A())
# {}

Is this the expected behavior? If so, would you consider changing it?

antony-frolov avatar Oct 28 '25 18:10 antony-frolov

I hadn't really considered this. I'd be open to changing it.

Tinche avatar Oct 29 '25 09:10 Tinche

I hadn't really considered this. I'd be open to changing it.

Glad that you're open to changing it! I think i've managed to make it work, here's a pull request with my changes

https://github.com/python-attrs/cattrs/pull/696

antony-frolov avatar Nov 04 '25 22:11 antony-frolov