def _build_mappings(<EOL>self, classes: Sequence[type]<EOL>) -> Tuple[Mapping[type, Sequence[type]], Mapping[type, Sequence[type]]]:
parents_to_children: MutableMapping[type, Set[type]] = {}<EOL>children_to_parents: MutableMapping[type, Set[type]] = {}<EOL>visited_classes: Set[type] = set()<EOL>class_stack = list(classes)<EOL>while class_stack:<EOL><INDENT>class_ = class_stack.pop()<EOL>if class_ in visited_classes:<EOL><INDENT>continue<EOL><DEDENT>visited_classes.add(class_)<EOL>for base in class_.__bases__:<EOL><INDENT>if base not in visited_classes:<EOL><INDENT>class_stack.append(base)<EOL><DEDENT>parents_to_children.setdefault(base, set()).add(class_)<EOL>children_to_parents.setdefault(class_, set()).add(base)<EOL><DEDENT><DEDENT>sorted_parents_to_children: MutableMapping[<EOL>type, List[type]<EOL>] = collections.OrderedDict()<EOL>for parent, children in sorted(<EOL>parents_to_children.items(), key=lambda x: (x[<NUM_LIT:0>].__module__, x[<NUM_LIT:0>].__name__)<EOL>):<EOL><INDENT>sorted_parents_to_children[parent] = sorted(<EOL>children, key=lambda x: (x.__module__, x.__name__)<EOL>)<EOL><DEDENT>sorted_children_to_parents: MutableMapping[<EOL>type, List[type]<EOL>] = collections.OrderedDict()<EOL>for child, parents in sorted(<EOL>children_to_parents.items(), key=lambda x: (x[<NUM_LIT:0>].__module__, x[<NUM_LIT:0>].__name__)<EOL>):<EOL><INDENT>sorted_children_to_parents[child] = sorted(<EOL>parents, key=lambda x: (x.__module__, x.__name__)<EOL>)<EOL><DEDENT>return sorted_parents_to_children, sorted_children_to_parents<EOL>
Collect all bases and organize into parent/child mappings.