The shared module of Hera provides control over global configurations, hooks, and some base mixins.
global_config
global_config = _GlobalConfig()
register_pre_build_hook
register_pre_build_hook = register_pre_build_hook
BaseMixin
Source code in src/hera/shared/_global_config.py
| class BaseMixin(BaseModel):
def _init_private_attributes(self):
"""A pydantic private method called after `__init__`.
Notes:
-----
In order to inject `__hera_init__` after `__init__` without destroying the autocomplete, we opted for
this method. We also tried other ways including creating a metaclass that invokes hera_init after init,
but that always broke auto-complete for IDEs like VSCode.
"""
super()._init_private_attributes() # type: ignore
self.__hera_init__()
def __hera_init__(self) -> None:
"""A method that is optionally implemented and invoked by `BaseMixin` subclasses to perform some post init."""
...
@root_validator(pre=True)
def _set_defaults(cls, values):
"""Sets the user-provided defaults of Hera objects."""
# In a Pydantic validator function, the first parameter (cls) is the class itself, not an instance of it
# but mypy/linting sees it as an instance
defaults = global_config._get_class_defaults(cls) # type: ignore
for key, value in defaults.items():
if values.get(key) is None:
values[key] = value
return values
|