python-attrs / python-attrs/cattrs

omit_if_default ignores attrs' converters

Open
#695 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.1k
Forks
159
Avg merge
12h 21m
Merged PRs (30d)
6

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with cattrs.Converter configured with omit_if_default=True and its unstructure path, using the attrs.define examples and attrs.field converters as the reproducer. Determine whether default comparison should use the converted value, then add or update coverage for both default="0", converter=int and default=0; done means the output consistently omits or retains b according to the agreed behavior.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.