Skip to content

prefect.blocks.core

register_block

Register a block spec with an optional name and version.

Parameters:

Name Description Default
name

If provided, the block spec name. If not provided, the _block_spec_name private field will be checked, and if that is None, the block spec class name will be used.

str
None
version

If provided, the block spec version. If not provided, the _block_spec_version private field will be checked. If not found, an error will be raised.

str
None
Source code in prefect/blocks/core.py
def register_block(name: str = None, version: str = None):
    """
    Register a block spec with an optional name and version.

    Args:
        name (str): If provided, the block spec name. If not provided, the
            `_block_spec_name` private field will be checked, and if that is
            `None`, the block spec class name will be used.
        version (str): If provided, the block spec version. If not provided,
            the `_block_spec_version` private field will be checked. If not
            found, an error will be raised.
    """

    def wrapper(block):
        registered_name = name or block._block_spec_name
        if not registered_name:
            raise ValueError("No _block_spec_name set and no name provided.")
        registered_version = version or block._block_spec_version
        if not registered_version:
            raise ValueError("No _block_spec_version set and no version provided.")

        BLOCK_REGISTRY[(registered_name, registered_version)] = block
        block._block_spec_name = registered_name
        block._block_spec_version = registered_version
        return block

    return wrapper