import dataclasses


def to_arg_format(cfg):
    """
    Converts the values of a config in dictionary format to tuples (type, value). This
    is needed to separate dtype from default value for argument parsing.

    We treat `None` and `MISSING` as `str`, which is a useful default for parsing
    command line arguments.

    Example:
    ```
    >>> cfg = {"a": 3, "b": {"c": "s"}}
    >>> print(to_arg_format(cfg))
    {"a": (int, 3), "b": {"c": (str, "s")}}
    ```
    """
    res_cfg = {}
    for key, val in cfg.items():
        if isinstance(val, dict):
            res_cfg[key] = to_arg_format(val)
        else:
            tp = type(val) if val not in {None, dataclasses.MISSING} else str
            res_cfg[key] = (tp, val)
    return res_cfg