Inherit omit_if_default when generating dict unstructure hooks - #788
Merged
Merged
Conversation
Member
|
LGTM! Thanks. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #238.
With
Converter(omit_if_default=True), manually registering a dict unstructure hook to rename a field currently makes default-valued fields reappear unless_cattrs_omit_if_default=Trueis repeated in the generator call.make_dict_unstructure_fnandmake_dict_unstructure_fn_from_attrsnow inherit the converter's setting when this argument is omitted, following the existing"from_converter"convention. An explicitTrueorFalsestill overrides the converter, and per-attribute overrides retain highest precedence.BaseConverterfalls back toFalse. The setting is resolved when the hook is generated.This also changes
include_subclasses(..., overrides=...): its generated hooks now honoromit_if_default=True. If a default-valuedLiteralfield is used to select a subclass during structuring, preserve it with a field override, for exampleoverrides={"kind": override(omit_if_default=False)}. Other default-valued fields can still be omitted. The documentation and subclass round-trip tests cover this compatibility change.Tests cover attrs classes and dataclasses, both generator entry points, default/true/false converter settings, explicit overrides, renamed fields, per-attribute precedence, and subclass round trips with default and tagged union strategies.
The 54 new cases produce 8 failures and 46 passes on the base commit.
Validation on Windows/Python 3.12.2:
tests, excludingtests/test_preconf.pyandtests/preconf).uv.lock: full project lint and format checks pass.The optional preconfigured-converter suites and other Python versions were not run locally.
AI assistance: Codex drafted the implementation, tests, documentation and this description, and ran the local checks. A separate Codex agent reviewed the change; its compatibility finding was addressed. I reviewed and understand the changes.