def _normalized_import_cycle(cycle_as_list, sort_candidates):
    """Given an import cycle specified as a list, return a normalized form.

    You represent a cycle as a list like so: [A, B, C, A].  This is
    equivalent to [B, C, A, B]: they're both the same cycle.  But they
    don't look the same to python `==`.  So we normalize this list to
    a data structure where different representations of the cycle
    *are* equal.  We do this by rearranging the cycle so that a
    canonical node comes first.  We pick the node to be the node in
    cycle_as_list that is also in sort_candidates.  If there are
    multiple such nodes, we take the one that's first alphabetically.

    We assume a simple cycle (that is, one where each node has only
    one incoming edge and one outgoing edge), which means that each
    node only occurs here once, so the sort order is uniquely defined.
    """
    sort_elts = [node for node in cycle_as_list if node in sort_candidates]
    if not sort_elts:     # probably impossible, but best to be safe
        sort_elts = cycle_as_list
    min_index = cycle_as_list.index(min(sort_elts))
    # The weird "-1" here is because A occurs twice in the input
    # cycle_as_list, but we want min_elt to occur twice in the output.
    return tuple(cycle_as_list[min_index:-1] + cycle_as_list[:min_index + 1])