def RemoveMutableParameters(ast): """Change all mutable parameters in a pytd AST to a non-mutable form.""" ast = ast.Visit(optimize.AbsorbMutableParameters()) ast = ast.Visit(optimize.CombineContainers()) ast = ast.Visit(optimize.MergeTypeParameters()) ast = ast.Visit(visitors.AdjustSelf(force=True)) return ast
def RemoveMutableParameters(ast): """Change all mutable parameters in a pytd AST to a non-mutable form.""" # late import, because optimize uses utils.py. from pytype.pytd import optimize # pylint: disable=g-import-not-at-top ast = ast.Visit(optimize.AbsorbMutableParameters()) ast = ast.Visit(optimize.CombineContainers()) ast = ast.Visit(optimize.MergeTypeParameters()) ast = ast.Visit(visitors.AdjustSelf(force=True)) return ast
def test_absorb_mutable_parameters_from_methods(self): # This is a test for intermediate data. See AbsorbMutableParameters class # pydoc about how AbsorbMutableParameters works on methods. src = pytd_src(""" from typing import Any T = TypeVar('T') NEW = TypeVar('NEW') class MyClass(typing.Generic[T], object): def append(self, x: NEW) -> Any: self = MyClass[Union[T, NEW]] """) tree = self.Parse(src) new_tree = tree.Visit(optimize.AbsorbMutableParameters()) new_tree = new_tree.Visit(optimize.CombineContainers()) self_type = new_tree.Lookup("MyClass").Lookup( "append").signatures[0].params[0].type self.assertEqual(pytd_utils.Print(self_type), "MyClass[Union[T, NEW]]")
def testAbsorbMutableParameters(self): src = textwrap.dedent(""" def popall(x: list[?]) -> ?: x := list[nothing] def add_float(x: list[int]) -> ?: x := list[int or float] def f(x: list[int]) -> ?: x := list[int or float] """) expected = textwrap.dedent(""" def popall(x: list[?]) -> ? def add_float(x: list[int or float]) -> ? def f(x: list[int or float]) -> ? """) tree = self.Parse(src) new_tree = tree.Visit(optimize.AbsorbMutableParameters()) new_tree = new_tree.Visit(optimize.CombineContainers()) self.AssertSourceEquals(new_tree, expected)
def testAbsorbMutableParametersFromMethods(self): # This is a test for intermediate data. See AbsorbMutableParameters class # pydoc about how AbsorbMutableParameters works on methods. src = textwrap.dedent(""" T = TypeVar('T') NEW = TypeVar('NEW') class MyClass(typing.Generic[T], object): def append(self, x: NEW) -> ?: self := MyClass[T or NEW] """) expected = textwrap.dedent(""" T = TypeVar('T') NEW = TypeVar('NEW') class MyClass(typing.Generic[T], object): def append(self: MyClass[T or NEW], x: NEW) -> ? """) tree = self.Parse(src) new_tree = tree.Visit(optimize.AbsorbMutableParameters()) new_tree = new_tree.Visit(optimize.CombineContainers()) self.AssertSourceEquals(new_tree, expected)
def test_absorb_mutable_parameters(self): src = pytd_src(""" from typing import Any def popall(x: list[Any]) -> Any: x = list[nothing] def add_float(x: list[int]) -> Any: x = list[Union[int, float]] def f(x: list[int]) -> Any: x = list[Union[int, float]] """) expected = pytd_src(""" from typing import Any def popall(x: list[Any]) -> Any: ... def add_float(x: list[Union[int, float]]) -> Any: ... def f(x: list[Union[int, float]]) -> Any: ... """) tree = self.Parse(src) new_tree = tree.Visit(optimize.AbsorbMutableParameters()) new_tree = new_tree.Visit(optimize.CombineContainers()) self.AssertSourceEquals(new_tree, expected)
def test_absorb_mutable_parameters_from_methods(self): # This is a test for intermediate data. See AbsorbMutableParameters class # pydoc about how AbsorbMutableParameters works on methods. src = pytd_src(""" from typing import Any T = TypeVar('T') NEW = TypeVar('NEW') class MyClass(typing.Generic[T], object): def append(self, x: NEW) -> Any: self = MyClass[Union[T, NEW]] """) expected = pytd_src(""" from typing import Any T = TypeVar('T') NEW = TypeVar('NEW') class MyClass(typing.Generic[T], object): def append(self: MyClass[Union[T, NEW]], x: NEW) -> Any: ... """) tree = self.Parse(src) new_tree = tree.Visit(optimize.AbsorbMutableParameters()) new_tree = new_tree.Visit(optimize.CombineContainers()) self.AssertSourceEquals(new_tree, expected)