Skip to content

Return factory from hook factory decorator functions#724

Merged
Tinche merged 2 commits into
python-attrs:mainfrom
bysiber:fix/hook-factory-decorator-return
Jun 13, 2026
Merged

Return factory from hook factory decorator functions#724
Tinche merged 2 commits into
python-attrs:mainfrom
bysiber:fix/hook-factory-decorator-return

Conversation

@bysiber

@bysiber bysiber commented Feb 20, 2026

Copy link
Copy Markdown
Contributor

register_unstructure_hook_factory and register_structure_hook_factory can be used as decorators (documented since 24.1.0), but the inner decorator function doesn't return the factory. This means:

@converter.register_structure_hook_factory(has)
def my_factory(type):
    ...

my_factory(SomeClass)  # TypeError: 'NoneType' is not callable

The type annotations already declare the correct contract (Callable[[Factory], Factory]), and internal uses discard the return value, so this only affects external users who use the decorator syntax and later reference the decorated function.

Fix: add return factory at the end of both decorator functions.

@codspeed-hq

codspeed-hq Bot commented Feb 24, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 64 untouched benchmarks


Comparing bysiber:fix/hook-factory-decorator-return (4d77763) with main (fcd732f)

Open in CodSpeed

@Tinche

Tinche commented Feb 24, 2026

Copy link
Copy Markdown
Member

Ooph good catch. Please add a changelog entry though.

register_unstructure_hook_factory and register_structure_hook_factory
can be used as decorators, but the inner decorator function doesn't
return the factory. This means:

    @converter.register_structure_hook_factory(has)
    def my_factory(type):
        ...

    # my_factory is now None

The type annotations declare these decorators should return the factory
(Callable[[Factory], Factory]) and the non-decorator path already
works correctly. This just adds the missing return statement.
@Tinche Tinche force-pushed the fix/hook-factory-decorator-return branch from 804d45f to d826f85 Compare June 13, 2026 21:19
Signed-off-by: Tin Tvrtkovic <tinchester@gmail.com>
@Tinche

Tinche commented Jun 13, 2026

Copy link
Copy Markdown
Member

I'm starting to prepare the next release so I got this myself. Thanks!

@Tinche Tinche merged commit c8608b8 into python-attrs:main Jun 13, 2026
14 checks passed
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.

2 participants