Skip to content

Allow converters without default hook factories - #825

Closed
Aschenbath wants to merge 1 commit into
python-attrs:mainfrom
Aschenbath:fix/no-default-hook-factories
Closed

Aschenbath wants to merge 1 commit into
python-attrs:mainfrom
Aschenbath:fix/no-default-hook-factories

Conversation

@Aschenbath

Copy link
Copy Markdown

Summary

Fixes #793. A chained converter (fallback factories pointing at a parent converter) could not actually pick up the parent's custom hook factories for types the built-in factories already handle: the default attrs factory matches first, so the fallback is never consulted. The issue asks for a way to make a converter without the default hook factories, and this adds exactly that flag.

Change

  • BaseConverter and Converter accept use_default_hook_factories=True (default, unchanged behaviour). With False, no built-in hook factories are registered for either direction, so dispatch goes to hooks registered afterwards, then to the fallback factories. A converter built this way with fallback factories pointing at a parent now delegates attrs classes to the parent's custom hooks.
  • copy() now preserves the flag and the fallback factories, so a copied chained converter still delegates. This also fixes FunctionDispatch.copy_to with skip=0, where [:-0] wrongly produced an empty list.
  • Docs: a note in docs/indepth.md next to the chained-converter example, and a HISTORY.md entry under NEXT.

How I checked it

  • Reproduced the issue's case first: a child Converter with structure_fallback_factory=parent.get_structure_hook structured Foo as Foo(1) (the built-in attrs result) instead of the parent's Foo(101). With use_default_hook_factories=False it gives Foo(101), and unstructuring follows the parent's hook too.
  • New tests in tests/test_converter.py: chained delegation (both BaseConverter and Converter), copy keeping the fallback, and user-registered hooks still winning on a no-default converter — 16 passed for the fallback/no_default selection.
  • Broader suites: tests/test_converter.py, test_copy.py, test_function_dispatch.py, test_multistrategy_dispatch.py, test_baseconverter.py, test_factory_hooks.py — 96 passed.
  • ruff check and ruff format pass on the touched files.

@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.

Make a Converter without the default hook factories

1 participant