Skip to content

configschemas

configschemas

Module for configuration schemes

Classes

ConfigSchemaConversionException

Bases: BaseException

Base class for exceptions when building config schema.

Functions

build_config_schema

build_config_schema(
    config_class, builders=_CONFIG_SCHEMA_BUILDERS
)

Build dagster config schema from an configuration class.

Here, a configuration class is

  • a scalar type (bool, float, int, str) or
  • a list or tuple of configuration classes or
  • a mapping from scalar type to a configuration class or
  • an attrs class whose attribute types are configuration classes.

For anything else, return dagster.Any.

Source code in niceml/config/configschemas.py
def build_config_schema(
    config_class: Any, builders: Iterable[Callable[..., Any]] = _CONFIG_SCHEMA_BUILDERS
):
    """Build dagster config schema from an configuration class.

    Here, a configuration class is

    - a scalar type (bool, float, int, str) or
    - a list or tuple of configuration classes or
    - a mapping from scalar type to a configuration class or
    - an attrs class whose attribute types are configuration classes.

    For anything else, return ``dagster.Any``.
    """
    builders_list = list(builders)
    for converter in builders_list:
        result = converter(config_class, builders_list)
        if result is not None:
            return result  # type: ignore
    return dagster.Any

define

define(
    maybe_cls=None,
    *,
    these=None,
    repr=None,
    hash=None,
    init=None,
    slots=True,
    frozen=False,
    weakref_slot=True,
    str=False,
    auto_attribs=None,
    kw_only=False,
    cache_hash=False,
    auto_exc=True,
    eq=None,
    order=False,
    auto_detect=True,
    getstate_setstate=None,
    on_setattr=None,
    field_transformer=None,
    match_args=True
)

Replacement of attr.define that updates class docstring with attributes.

Source code in niceml/config/configschemas.py
def define(  # pylint: disable=too-many-locals
    maybe_cls=None,
    *,
    these=None,
    repr=None,  # pylint: disable=redefined-builtin
    hash=None,  # pylint: disable=redefined-builtin
    init=None,
    slots=True,
    frozen=False,
    weakref_slot=True,
    str=False,  # pylint: disable=redefined-builtin
    auto_attribs=None,
    kw_only=False,
    cache_hash=False,
    auto_exc=True,
    eq=None,
    order=False,
    auto_detect=True,
    getstate_setstate=None,
    on_setattr=None,
    field_transformer=None,
    match_args=True,
):
    """Replacement of attr.define that updates class docstring with attributes."""

    def decode(cls):
        """Actual decorator function."""
        cls = attr.define(
            these=these,
            repr=repr,  # pylint: disable=redefined-builtin
            hash=hash,  # pylint: disable=redefined-builtin
            init=init,
            slots=slots,
            frozen=frozen,
            weakref_slot=weakref_slot,
            str=str,
            auto_attribs=auto_attribs,
            kw_only=kw_only,
            cache_hash=cache_hash,
            auto_exc=auto_exc,
            eq=eq,
            order=order,
            auto_detect=auto_detect,
            getstate_setstate=getstate_setstate,
            on_setattr=on_setattr,
            field_transformer=field_transformer,
            match_args=match_args,
        )(cls)

        cls.__doc__ = format_attrs_doc(cls)
        return cls

    return decode if maybe_cls is None else decode(maybe_cls)

field

field(
    *,
    default=NOTHING,
    validator=None,
    repr=True,
    hash=None,
    init=True,
    metadata=None,
    converter=None,
    factory=None,
    kw_only=False,
    eq=None,
    order=None,
    on_setattr=None,
    description=None
)

Replacement of attr.field that moves description into metadata.

Source code in niceml/config/configschemas.py
def field(  # pylint: disable=too-many-locals
    *,
    default=NOTHING,
    validator=None,
    repr=True,  # pylint: disable=redefined-builtin
    hash=None,  # pylint: disable=redefined-builtin
    init=True,
    metadata=None,
    converter=None,
    factory=None,
    kw_only=False,
    eq=None,
    order=None,
    on_setattr=None,
    description: Optional[str] = None,
):
    """Replacement of attr.field that moves ``description`` into metadata."""
    if description:
        metadata = {**(metadata or {}), "description": description}
    return attr.field(  # type: ignore
        default=default,
        validator=validator,
        repr=repr,  # pylint: disable=redefined-builtin
        hash=hash,  # pylint: disable=redefined-builtin
        init=init,
        metadata=metadata,
        converter=converter,
        factory=factory,
        kw_only=kw_only,
        eq=eq,
        order=order,
        on_setattr=on_setattr,
    )

format_attribute_doc

format_attribute_doc(attribute)

Generates and returns attributes of attribute as string

Source code in niceml/config/configschemas.py
def format_attribute_doc(attribute: attr.Attribute) -> str:
    """Generates and returns attributes of attribute as string"""
    attr_type = (
        attribute.type.__name__
        if attribute.type is not None and hasattr(attribute.type, "__name__")
        else str(attribute.type)
    )
    doc = f"{attribute.name}: {attr_type}\n\t"
    if "description" in attribute.metadata:
        doc += f"{attribute.metadata['description']}. "
    if attribute.default != NOTHING:
        doc += f"Default: {attribute.default}"
    return doc

format_attrs_doc

format_attrs_doc(cls)

Add attributes section to attrs class docstring.

Source code in niceml/config/configschemas.py
def format_attrs_doc(cls: Any) -> str:
    """Add attributes section to attrs class docstring."""
    doc = cls.__doc__ or ""
    try:
        attributes = list(attr.fields(cls))
    except (TypeError, attr.exceptions.NotAnAttrsClassError):
        return doc
    if attributes:
        doc += "\n\nAttributes\n----------\n\n"
        doc += "\n".join(map(format_attribute_doc, attributes))
    return doc

parse_config

parse_config(config, cls)

Parse configuration.

Source code in niceml/config/configschemas.py
def parse_config(config: Any, cls: Type[_Config]) -> _Config:
    """Parse configuration."""
    parsed: _Config = cattr.structure(config, cls)
    return parsed