Skip to content

Fix union disambiguation for dataclass default_factory fields (#796) - #801

Open
sammist2 wants to merge 1 commit into
python-attrs:mainfrom
sammist2:fix/796-default-factory-disambiguation
Open

sammist2 wants to merge 1 commit into
python-attrs:mainfrom
sammist2:fix/796-default-factory-disambiguation

Conversation

@sammist2

Copy link
Copy Markdown

Fixes #796.

create_default_dis_func read each field's default from the raw dataclasses.Field via fields_dict. A field that sets only default_factory has Field.default == MISSING, so it was treated as a required field: the class could never be chosen as the disambiguation fallback, and a payload omitting that field raised Couldn't disambiguate instead of structuring as that class. The attrs equivalent (factory=...) already behaved correctly, so dataclasses and attrs disagreed.

Defaults are now read from adapted_fields, which maps default_factory to a Factory, matching attrs factory fields. fields_dict and the MISSING import are no longer used here and are removed.

Added test_dataclasses_default_factory, covering both union orders across all converter configurations (it fails before the change and passes after), and a HISTORY.md entry.

…-attrs#796)

A dataclass field that sets only `default_factory` has a raw
`Field.default` of `MISSING`, so the disambiguator treated it as a
required field and a payload omitting it failed to structure as that
class. Read defaults from `adapted_fields` instead, which normalizes
`default_factory` to a `Factory`, matching the behavior of attrs
`factory` fields.

Adds a regression test and a HISTORY.md entry.

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

1 participant