Given a mapping where values are iterables, yield items whose values contained references are not used as
    keys first:

    Example:
        >>> dct = {'two': ('two', 'one', 'foo'), 'one': ('hi', 'six', 'net'), 'six': ('three', 'four'), 'foo': ['bar']}
        >>> for k, v in _sorted_items(dct):
        ...     print(k, v)
        ...
        six ('three', 'four')
        foo ['bar']
        one ('hi', 'six', 'net')
        two ('two', 'one', 'foo')

def _sorted_items(mapping: typing.Mapping) -> typing.Generator:
    """Given a mapping where values are iterables, yield items whose values contained references are not used as
    keys first:

    Example:
        >>> dct = {'two': ('two', 'one', 'foo'), 'one': ('hi', 'six', 'net'), 'six': ('three', 'four'), 'foo': ['bar']}
        >>> for k, v in _sorted_items(dct):
        ...     print(k, v)
        ...
        six ('three', 'four')
        foo ['bar']
        one ('hi', 'six', 'net')
        two ('two', 'one', 'foo')
    """
    to_yield = set(mapping)
    while to_yield:
        for key, values in mapping.items():
            if key not in to_yield or (to_yield - {key} & set(values)):  # other keys left to yield before this one
                continue
            yield key, values
            to_yield.remove(key)