from typing import Dict
from typing import Any


def pop_item_from_dict(d: Dict[str, Any], key: str, default: Any = None, must_exists: bool = False):
    """
    Pops key from dict d if key exists, returns d otherwise.

    Args:
        d: (``Dict``) Dictionary for key to be removed.
        key: (``str``) Key name to remove from dictionary d.
        default: (``Any``) Default value to return if key not found.
        must_exists: (``bool``) Raises an exception if True when given key does not exists.

    Returns: Popped value for key if found, None otherwise.
    """
    if key not in d and must_exists:
        raise KeyError(f"'{key}' not found in '{d}'.")
    val = d.pop(key) if key in d else default
    return val