Latest Results
fix: carry over the fallback hook factories in Converter.copy()
Problem
-------
`BaseConverter.copy()` and `Converter.copy()` (and therefore `copy.deepcopy()`,
which delegates to `copy()`) silently drop the converter's
`unstructure_fallback_factory` and `structure_fallback_factory`. A copy of a
converter configured with custom fallbacks falls back to the built-in defaults
instead: unstructuring returns the object as is and structuring raises
`StructureHandlerNotFoundError`.
Every other constructor option - `dict_factory`, `unstruct_strat`,
`omit_if_default`, `forbid_extra_keys`, `type_overrides`,
`unstruct_collection_overrides`, `prefer_attrib_converters`,
`detailed_validation` and `use_alias` - is carried over, and the fallback
factories were simply never threaded through, so this is an oversight rather
than a design decision. `Converter.copy` and `BaseConverter.copy` are also the
documented way of cloning a converter ("Converters may be cloned using the
`Converter.copy()` method", `docs/indepth.md`).
Cause
-----
`MultiStrategyDispatch` receives the fallback factory in its constructor, but
`BaseConverter.__init__` never stored it on the converter, and `copy()` does not
pass the factories to the new instance. The new converter therefore falls back
to the defaults of the `__init__` signature.
Fix
---
Store both factories on the converter in `BaseConverter.__init__`, and add
`unstructure_fallback_factory` and `structure_fallback_factory` parameters to
`BaseConverter.copy` and `Converter.copy` that default to the current values, so
they are both carried over and individually overridable on the copy. The new
parameters are appended to the signatures, keeping existing positional calls
valid.
Tests
-----
`tests/test_copy.py`:
* `test_copy_fallback_factories` - custom fallbacks still apply after `copy()`
and `deepcopy()`, for both `BaseConverter` and `Converter`.
* `test_copy_fallback_factories_overrides` - the factories can be overridden on
the copy without affecting the original.
Both fail on `main` and pass with the fix; the rest of the suite is unaffected.
Signed-off-by: simpleqt <2015900818@qq.com>simpleqt:fix/copy-fallback-factories Latest Branches
0%
mmporong:fix-hook-cache-result-docs 0%
simpleqt:fix/copy-fallback-factories +1%
© 2026 CodSpeed Technology