pdm-project / pdm-backend

The build backend used by PDM that supports latest packaging standards.
https://backend.pdm-project.org
MIT License
70 stars 39 forks source link

a way to apply hooks from a `BuildHookInterface` #247

Closed DetachHead closed 5 months ago

DetachHead commented 5 months ago

Is your feature/enhancement proposal related to a problem? Please describe.

in my pdm_build.py, i want to extend BuildHookInterface so that my type checker ensures that my hook methods have the correct function signatures:

# pdm_build.py
from pdm.backend.hooks.base import BuildHookInterface
from typing_extensions import override

class Hooks(BuildHookInterface):
    @override
    def pdm_build_update_files(self, context: Context, files: dict[str, Path]):
        ...

# but there doesn't seem to be a nice way to register these hooks when defining them this way,
# so i have to add this:
pdm_build_update_files = Hook().pdm_build_update_files
# this isn't ideal if i have several hook functions because i have to do this with each one.

Describe the solution you'd like

a way to register the hooks from the class. maybe something like this, which can be added to the bottom of pdm_build.py:

Hooks.register()

or it could be a class decorator:

@register_hooks
class Hooks(BuildHookInterface):
    ...
frostming commented 5 months ago
# but there doesn't seem to be a nice way to register these hooks when defining them this way,
# so i have to add this:
pdm_build_update_files = Hook().pdm_build_update_files

There is:

hooks = Hooks()
def __getattr__(attr):
    return getattr(hooks, attr)

def __dir__():
    return dir(hooks)