Repository navigation
Fix register_structure_hook for ForwardRef targets - #846
Open
Vinayak19112003 wants to merge 1 commit into
Open
Vinayak19112003 wants to merge 1 commit into
Vinayak19112003 wants to merge 1 commit into
Conversation
Signed-off-by: Vinayak Deshmuk <118991845+Vinayak19112003@users.noreply.github.com>
This branch has not been deployed
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 #206.
BaseConverter.register_structure_hookraisedTypeError: Invalid first argument to 'register()'. ForwardRef('...') is not a class or union type.when registering a hook for atyping.ForwardRef(orannotationlib.ForwardRef) target, because it fell through toregister_cls_list/functools.singledispatch, which has rejected non-class arguments since the CPython bpo-46032 fix (Python 3.9.10 / 3.10.2).The fix routes
ForwardReftargets throughregister_func_listwith an equality predicate, mirroring the existing handling for type aliases and newtypes — except it uses==instead ofis, so a hook registered forForwardRef("MyClass")also matches an equivalentForwardRef("MyClass")constructed later.Why now, when #206's 2022 discussion pointed at
register_structure_hook_funcinstead: the landscape has changed since then. cattrs now has first-classannotationlib.ForwardRefsupport on main (#741), and on Python 3.14+ (PEP 649/749)ForwardRefannotations show up in attrs-generated constructors withoutfrom __future__ import annotations— for example in circular-import scenarios — so users hit this on the documentedregister_structure_hookAPI rather than the predicate escape hatch. This is distinct from #201, which is about structuring failing to resolve a forward reference; #206 is about registering a hook for one.register_unstructure_hookis out of scope for this PR.Tests: new
test_register_structure_hook_for_forward_refintests/test_converter.py, parametrized over explicit/decorator registration andBaseConverter/Converter, asserting the registered hook is actually used for structuring: 8 passed after, 8 failed before (with the issue'sTypeError). Full test suite on Python 3.12: 986 passed, 15 xfailed. Ruff check and format clean.