Skip to content

Comments

Return factory from hook factory decorator functions#724

Open
bysiber wants to merge 1 commit intopython-attrs:mainfrom
bysiber:fix/hook-factory-decorator-return
Open

Return factory from hook factory decorator functions#724
bysiber wants to merge 1 commit intopython-attrs:mainfrom
bysiber:fix/hook-factory-decorator-return

Conversation

@bysiber
Copy link

@bysiber bysiber commented Feb 20, 2026

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.

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

1 participant