from typing import Any
from typing import Optional


def _remove_names(obj: Any, parent: Optional[str]) -> Any:
    """
    Remove `name` keys from dictionary objects, where that dictionary is already keyed by name

    E.g. in the following:

    .. code-block:: yaml

      classes:
        Person:
          name: Person
          description: ...

    translated to:

    .. code-block:: yaml

      classes:
        Person:
          description: ...

    Also compacts representation of prefixes

    :param obj: dictionary object to recursively transform
    :param parent: key for parent dict
    :return:
    """
    if isinstance(obj, dict):
        return {k: _remove_names(v, k) for k, v in obj.items() if k != 'name' or parent is None}
    elif isinstance(obj, list):
        return [_remove_names(x, parent) for x in obj]
    else:
        return obj