def test_canonical_ordering_visitor(self): src1 = """ from typing import Any, TypeVar, Union def f() -> Any: raise MemoryError() raise IOError() def f(x: list[a]) -> Any: ... def f(x: list[Union[b, c]]) -> Any: ... def f(x: list[tuple[d]]) -> Any: ... A = TypeVar("A") C = TypeVar("C") B = TypeVar("B") D = TypeVar("D") def f(d: A, c: B, b: C, a: D) -> Any: ... """ src2 = """ from typing import Any, Union def f() -> Any: raise IOError() raise MemoryError() def f(x: list[tuple[d]]) -> Any: ... def f(x: list[a]) -> Any: ... def f(x: list[Union[b, c]]) -> Any: ... A = TypeVar("A") C = TypeVar("C") B = TypeVar("B") D = TypeVar("D") def f(d: A, c: B, b: C, a: D) -> Any: ... """ tree1 = self.Parse(src1) tree1 = tree1.Visit( pytd_visitors.CanonicalOrderingVisitor(sort_signatures=True)) tree2 = self.Parse(src2) tree2 = tree2.Visit( pytd_visitors.CanonicalOrderingVisitor(sort_signatures=True)) self.AssertSourceEquals(tree1, tree2) self.assertEqual( tree1.Lookup("f").signatures[0].template, tree2.Lookup("f").signatures[0].template)
def CanonicalOrdering(n, sort_signatures=False): """Convert a PYTD node to a canonical (sorted) ordering.""" return n.Visit( pytd_visitors.CanonicalOrderingVisitor(sort_signatures=sort_signatures))