def _hparam_constructible_class()

in summarize_from_feedback/utils/hyperparams.py [0:0]


def _hparam_constructible_class(ty):
    """
    Given a type, returns:
    - an unambiguous HParam subtype, if one exists,
    - None, if there are no HParam subtypes
    If there is ambiguity, throws a TypeError.
    """
    subtypes = _union_subtypes(ty)

    hparam_subtypes = [ty for ty in subtypes if is_hparam_type(ty)]

    if len(hparam_subtypes) > 1:
        raise TypeError(f"Unions with multiple HParam subtypes unsupported")
    if len(hparam_subtypes) == 0:
        return None
    assert len(hparam_subtypes) == 1
    hparam_ty = hparam_subtypes[0]
    if dict in subtypes:
        # avoid ambiguity for nested dict construction
        raise TypeError(f"Unions with both HParam and dict subtypes unsupported")
    if str in subtypes:
        # avoid ambiguity for "on"/"off"
        raise TypeError(f"Unions with both HParam and str subtypes unsupported")
    return hparam_ty