Repository navigation
Honor dict_factory in generated unstructure hooks - #826
Closed
Aschenbath wants to merge 1 commit into
Closed
Aschenbath wants to merge 1 commit into
Aschenbath wants to merge 1 commit into
Conversation
Author
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 #228.
Converter(formerlyGenConverter) accepted adict_factoryargument, but the generated unstructure hooks always built a plaindictliteral, soConverter(dict_factory=OrderedDict).unstructure(obj)returned plain dicts. The interpretedBaseConverter.unstructure_attrs_asdictpath did honor the factory.make_dict_unstructure_fn_from_attrsnow checks the converter's factory: with the defaultdictfactory the generated code is unchanged (a dict literal); with any other factory the generated function builds the result through the factory (res = __c_dict_factory()followed by item assignments, mirroringunstructure_attrs_asdict), including for fields omitted viaomit_if_default. Nested attrs classes generated from the same converter use the factory too.Tests:
test_dict_factory_used_by_generated_hookstotests/test_gen_dict.py, covering a converter-level factory for nested classes, an explicitly generated hook, and combination withomit_if_default=True.tests/test_gen_dict.py,tests/test_dicts.py,tests/test_baseconverter.py(132 passed);tests/test_preconf.py,tests/test_typeddicts.py,tests/test_structure.py,tests/test_unstructure.py,tests/test_unstructure_collections.py,tests/test_generics.py,tests/test_copy.py(211 passed, 20 skipped; 2 pre-existingtest_tomllib_converter*failures also occur on unmodified upstreammainin this environment).ruff checkandruff format --checkon the changed files: pass.A changelog entry is included in
HISTORY.md.