Repository navigation
Structure missing optional attributes without defaults as None - #833
Closed
DresdenGman wants to merge 1 commit into
Closed
DresdenGman wants to merge 1 commit into
DresdenGman wants to merge 1 commit into
Conversation
Member
|
Thanks for the contribution but I don't think this is a good change; it's injecting non-obvious behavior into the default path. The type of an attribute shouldn't change how missing keys get handled. What we could do is introduce a recipe into the recipes doc showing how this can be configured on a particular converter; that I would accept gladly. |
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 #249.
Summary
Structuring a class with an optional attribute that has no default used to fail when the key was missing from the input:
Adding
= Noneto the attribute is not always an option: in a frozen class, an optional attribute sitting between attributes without defaults cannot gain a default without reordering the fields, which is a breaking change (see the example by @kulych in #249).Behavior
None(Optional[X],Union[X, Y, None],X | None, also underAnnotated).None: the value still goes through the attribute's structure hook.None).Implementation
cattrs.gen: inmake_dict_structure_fn_from_attrs, the affected attributes read their value witho.get(key)instead ofo[key], in both the detailed and non-detailed code paths. (Accesses for attributes with defaults were already guarded bykey in ochecks and are untouched.)cattrs.converters: the simpleBaseConverterpath (structure_attrs_fromdict) used to skip missing keys and then fail at instantiation; it now structuresNonefor affected attributes, so both implementation paths behave the same.cattrs._compat: newincludes_none_typehelper. The existingis_optionalonly recognizes two-argument unions, which does not cover cases likeUnion[bool, str, None]from the issue discussion.Tests
New tests in
tests/test_optionals.pycover: the example from the issue, an optional attribute between required ones in a frozen class, defaults taking precedence, required attributes still failing, multi-member unions includingNone, PEP 604 unions, kw-only and underscore attributes (with and withoutuse_alias), rename overrides,init=Falseattributes, annotated optionals, dataclasses, and the detailed-validation error collection. They run against both converter implementations and both validation modes where applicable.Full test suite: 1100 passed, 2 failed — the 2 failures (
test_preconf.py::test_tomllib_converterandtest_tomllib_converter_dumps) fail identically on unmodifiedmainin the same environment and are unrelated to this change.