Skip to content

Shared Modules

hera.shared

The shared module of Hera provides control over global configurations, hooks, and some base mixins.

GlobalConfig module-attribute

GlobalConfig = _GlobalConfig()

global_config module-attribute

global_config = _GlobalConfig()

register_pre_build_hook module-attribute

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):
        """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."""
        defaults = global_config._get_class_defaults(cls)
        for key, value in defaults.items():
            if values.get(key) is None:
                values[key] = value
        return values

Config

Config class dictates the behavior of the underlying Pydantic model.

See Pydantic documentation for more info.

Source code in src/hera/shared/_pydantic.py
class Config:
    """Config class dictates the behavior of the underlying Pydantic model.

    See Pydantic documentation for more info.
    """

    allow_population_by_field_name = True
    """support populating Hera object fields by their Field alias"""

    allow_mutation = True
    """supports mutating Hera objects post instantiation"""

    use_enum_values = True
    """supports using enums, which are then unpacked to obtain the actual `.value`, on Hera objects"""

    arbitrary_types_allowed = True
    """supports specifying arbitrary types for any field to support Hera object fields processing"""

    smart_union = True
    """uses smart union for matching a field's specified value to the underlying type that's part of a union"""

allow_mutation class-attribute instance-attribute

allow_mutation = True

supports mutating Hera objects post instantiation

allow_population_by_field_name class-attribute instance-attribute

allow_population_by_field_name = True

support populating Hera object fields by their Field alias

arbitrary_types_allowed class-attribute instance-attribute

arbitrary_types_allowed = True

supports specifying arbitrary types for any field to support Hera object fields processing

smart_union class-attribute instance-attribute

smart_union = True

uses smart union for matching a field’s specified value to the underlying type that’s part of a union

use_enum_values class-attribute instance-attribute

use_enum_values = True

supports using enums, which are then unpacked to obtain the actual .value, on Hera objects

Comments