Source code for opinf.utils._requires
# utils/_requires.py
"""Wrappers for methods that require an attribute to be initialized."""
__all__ = [
"requires",
"requires2",
]
import typing
import functools
[docs]
def requires2(attr: str, message: str) -> typing.Callable:
"""Wrapper for methods that require an attribute to be initialized.
Parameters
----------
attr : str
Name of the required attribute.
message : str
Message in the error.
"""
def _wrapper(func):
@functools.wraps(func)
def _decorator(self, *args, **kwargs):
if not hasattr(self, attr) or getattr(self, attr) is None:
raise AttributeError(message)
return func(self, *args, **kwargs)
return _decorator
return _wrapper
[docs]
def requires(attr: str) -> typing.Callable:
"""Wrapper for methods that require an attribute to be initialized.
Parameters
----------
attr : str
Name of the required attribute.
"""
return requires2(attr, f"required attribute '{attr}' not set")