Skip to content

fix: treat dataclass default_factory as a default in union disambiguation - #800

Open
Eryx-Z wants to merge 1 commit into
python-attrs:mainfrom
Eryx-Z:fix/dataclass-default-factory-disambiguation
Open

Eryx-Z wants to merge 1 commit into
python-attrs:mainfrom
Eryx-Z:fix/dataclass-default-factory-disambiguation

Conversation

@Eryx-Z

@Eryx-Z Eryx-Z commented Oct 16, 2026 •

Copy link
Copy Markdown

Changes

Fixes #796

create_default_dis_func used fields_dict, so a dataclass Field with only default_factory kept .default as MISSING and was treated as required. With omit_if_default=True, a round-trip of D1(x=1, tags=[]) against Union[D1, D2] silently became D2(x=1).

Use adapted_fields, which already maps default_factory to Factory, matching attrs Factory and plain dataclass default. The union then raises the same "Could not disambiguate" error as the attrs equivalent.

Checklist

  • Tests added / relevant tests pass
  • I own and understand these changes; no third-party co-author tags

…tion

create_default_dis_func used fields_dict, so a dataclass Field with only
default_factory kept .default as MISSING and was treated as required. That
made omit_if_default payloads with those fields silently pick the other
union member.

Use adapted_fields, which maps default_factory to Factory, matching attrs
Factory and plain dataclass defaults.

Fixes python-attrs#796

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

Dataclass default_factory field treated as required in union disambiguation

2 participants