Skip to content

utils

requires_metadata(func)

Decorator to ensure that the data object has metadata before executing the function. If metadata is missing, raises an error.

Source code in src/torch_structure/data/utils.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
def requires_metadata(func):
    """
    Decorator to ensure that the data object has metadata before executing the function.
    If metadata is missing, raises an error.
    """

    def wrapper(self, *args, **kwargs):
        """Check for metadata on ``self`` before delegating to the wrapped function."""
        if not getattr(self, "metadata", None):
            raise AttributeError(
                f"'{self.__class__.__name__}' object has no metadata. "
                f"Ensure that the data object is properly initialized with metadata before calling '{func.__name__}'."
            )
        return func(self, *args, **kwargs)

    return wrapper