Skip to content

Honor dict_factory in generated unstructure hooks - #826

Closed
Aschenbath wants to merge 1 commit into
python-attrs:mainfrom
Aschenbath:fix-228-dict-factory
Closed

Aschenbath wants to merge 1 commit into
python-attrs:mainfrom
Aschenbath:fix-228-dict-factory

Conversation

@Aschenbath

Copy link
Copy Markdown

Fixes #228.

Converter (formerly GenConverter) accepted a dict_factory argument, but the generated unstructure hooks always built a plain dict literal, so Converter(dict_factory=OrderedDict).unstructure(obj) returned plain dicts. The interpreted BaseConverter.unstructure_attrs_asdict path did honor the factory.

make_dict_unstructure_fn_from_attrs now checks the converter's factory: with the default dict factory 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, mirroring unstructure_attrs_asdict), including for fields omitted via omit_if_default. Nested attrs classes generated from the same converter use the factory too.

Tests:

  • Added test_dict_factory_used_by_generated_hooks to tests/test_gen_dict.py, covering a converter-level factory for nested classes, an explicitly generated hook, and combination with omit_if_default=True.
  • Regression runs: 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-existing test_tomllib_converter* failures also occur on unmodified upstream main in this environment).
  • ruff check and ruff format --check on the changed files: pass.

A changelog entry is included in HISTORY.md.

@Aschenbath

Copy link
Copy Markdown
Author

Closing this one myself: I opened too many PRs here in a short time, which isn't fair to your review queue — sorry about that. I'm leaving #827 and #822 open. If this fix is something you'd want, I'm happy to reopen or rebase it later.

@Aschenbath Aschenbath closed this Nov 4, 2026
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.

GenConverter's dict_factory argument has no effect

1 participant