def test_topsort(self) -> None: a = frozenset({'A'}) b = frozenset({'B'}) c = frozenset({'C'}) d = frozenset({'D'}) data = {a: {b, c}, b: {d}, c: {d}} # type: Dict[AbstractSet[str], Set[AbstractSet[str]]] res = list(topsort(data)) assert_equal(res, [{d}, {b, c}, {a}])
def test_topsort(self) -> None: a = frozenset({'A'}) b = frozenset({'B'}) c = frozenset({'C'}) d = frozenset({'D'}) data = {a: {b, c}, b: {d}, c: {d}} # type: Dict[AbstractSet[str], Set[AbstractSet[str]]] res = list(topsort(data)) assert_equal(res, [{d}, {b, c}, {a}])
def sort_with_subclasses_first( impls: List[RegisterImplInfo] ) -> Iterator[RegisterImplInfo]: # graph with edges pointing from every class to their subclasses graph = {typ: set(typ.mro[1:]) for typ, _ in impls} dispatch_types = topsort(graph) impl_dict = {typ: func for typ, func in impls} for group in reversed(list(dispatch_types)): yield from ((typ, impl_dict[typ]) for typ in group if typ in impl_dict)