Skip to content

Metadata hook plugins


Metadata hooks allow for the modification of project metadata after it has been loaded.

Built-in

Custom

This is a custom class in a given Python file that inherits from the MetadataHookInterface.

Configuration

The metadata hook plugin name is custom.

[tool.hatch.metadata.hooks.custom]
[metadata.hooks.custom]

An option path is used to specify the path of the Python file, defaulting to hatch_build.py.

Example

from hatchling.metadata.plugin.interface import MetadataHookInterface


class CustomMetadataHook(MetadataHookInterface):
    ...

If multiple subclasses are found, you must define a function named get_metadata_hook that returns the desired build hook.

Note

Any defined PLUGIN_NAME is ignored and will always be custom.

MetadataHookInterface (ABC)

Example usage:

from hatchling.metadata.plugin.interface import MetadataHookInterface


class SpecialMetadataHook(MetadataHookInterface):
    PLUGIN_NAME = 'special'
    ...
from hatchling.plugin import hookimpl

from .plugin import SpecialMetadataHook


@hookimpl
def hatch_register_metadata_hook():
    return SpecialMetadataHook
Source code in hatchling/metadata/plugin/interface.py
class MetadataHookInterface(ABC):  # no cov
    """
    Example usage:

    === ":octicons-file-code-16: plugin.py"

        ```python
        from hatchling.metadata.plugin.interface import MetadataHookInterface


        class SpecialMetadataHook(MetadataHookInterface):
            PLUGIN_NAME = 'special'
            ...
        ```

    === ":octicons-file-code-16: hooks.py"

        ```python
        from hatchling.plugin import hookimpl

        from .plugin import SpecialMetadataHook


        @hookimpl
        def hatch_register_metadata_hook():
            return SpecialMetadataHook
        ```
    """

    PLUGIN_NAME = ''
    """The name used for selection."""

    def __init__(self, root, config):
        self.__root = root
        self.__config = config

    @property
    def root(self):
        """
        The root of the project tree.
        """
        return self.__root

    @property
    def config(self):
        """
        The hook configuration.

        === ":octicons-file-code-16: pyproject.toml"

            ```toml
            [tool.hatch.metadata.hooks.<PLUGIN_NAME>]
            ```

        === ":octicons-file-code-16: hatch.toml"

            ```toml
            [metadata.hooks.<PLUGIN_NAME>]
            ```
        """
        return self.__config

    @abstractmethod
    def update(self, metadata: dict):
        """
        This updates the metadata mapping of the `project` table in-place.
        """

PLUGIN_NAME

The name used for selection.

config property readonly

The hook configuration.

[tool.hatch.metadata.hooks.<PLUGIN_NAME>]
[metadata.hooks.<PLUGIN_NAME>]

root property readonly

The root of the project tree.

update(self, metadata: dict)

This updates the metadata mapping of the project table in-place.

Source code in hatchling/metadata/plugin/interface.py
@abstractmethod
def update(self, metadata: dict):
    """
    This updates the metadata mapping of the `project` table in-place.
    """

Last update: March 13, 2022