ImportError: cannot import name 'get_ref_type' from 'omegaconf._utils' を解決する。

RVC-betaを使うのに、'get_ref_type' のエラーが越えられなかったのでどこかのバージョンからコピペ実装。

def get_ref_type(obj: Any, key: Any = None) -> Optional[Type[Any]]:
    from omegaconf import DictConfig, ListConfig
    from omegaconf.base import Container, Node
    from omegaconf.nodes import ValueNode
    def none_as_any(t: Optional[Type[Any]]) -> Union[Type[Any], Any]:
        if t is None:
            return Any
        else:
            return t
    if isinstance(obj, Container) and key is not None:
        obj = obj._get_node(key)
    is_optional = True
    ref_type = None
    if isinstance(obj, ValueNode):
        is_optional = obj._is_optional()
        ref_type = obj._metadata.ref_type
    elif isinstance(obj, Container):
        if isinstance(obj, Node):
            ref_type = obj._metadata.ref_type
        is_optional = obj._is_optional()
        kt = none_as_any(obj._metadata.key_type)
        vt = none_as_any(obj._metadata.element_type)
        if (
            ref_type is Any
            and kt is Any
            and vt is Any
            and not obj._is_missing()
            and not obj._is_none()
        ):
            ref_type = Any  # type: ignore
        elif not is_structured_config(ref_type):
            if kt is Any:
                kt = Union[str, Enum]
            if isinstance(obj, DictConfig):
                ref_type = Dict[kt, vt]  # type: ignore
            elif isinstance(obj, ListConfig):
                ref_type = List[vt]  # type: ignore
    else:
        if isinstance(obj, dict):
            ref_type = Dict[Union[str, Enum], Any]
        elif isinstance(obj, (list, tuple)):
            ref_type = List[Any]
        else:
            ref_type = get_type_of(obj)
    ref_type = none_as_any(ref_type)
    if is_optional and ref_type is not Any:
        ref_type = Optional[ref_type]  # type: ignore
    return ref_type

いいなと思ったら応援しよう!