Example #1
0
  def test_remove_mutable_dict(self):
    # Test for RemoveMutableParameters, with simplified dict class.
    src = textwrap.dedent("""
      from typing import Union
      K = TypeVar('K')
      V = TypeVar('V')
      T = TypeVar('T')
      K2 = TypeVar('K2')
      V2 = TypeVar('V2')

      class MyDict(typing.Generic[K, V], object):
          def getitem(self, k: K, default: T) -> Union[V, T]: ...
          def setitem(self, k: K2, value: V2) -> NoneType:
              self = dict[Union[K, K2], Union[V, V2]]
          def getanykeyorvalue(self) -> Union[K, V]: ...
          def setdefault(self, k: K2, v: V2) -> Union[V, V2]:
              self = dict[Union[K, K2], Union[V, V2]]
    """)
    expected = textwrap.dedent("""
      from typing import Union
      K = TypeVar('K')
      V = TypeVar('V')
      T = TypeVar('T')
      K2 = TypeVar('K2')
      V2 = TypeVar('V2')

      class MyDict(typing.Generic[K, V], object):
          def getitem(self, k: K, default: V) -> V: ...
          def setitem(self, k: K, value: V) -> NoneType: ...
          def getanykeyorvalue(self) -> Union[K, V]: ...
          def setdefault(self, k: K, v: V) -> V: ...
    """)
    ast = self.Parse(src)
    ast = transforms.RemoveMutableParameters(ast)
    self.AssertSourceEquals(ast, expected)
Example #2
0
  def test_remove_mutable_list(self):
    # Simple test for RemoveMutableParameters, with simplified list class
    src = textwrap.dedent("""
      from typing import Union
      T = TypeVar('T')
      T2 = TypeVar('T2')

      class TrivialList(typing.Generic[T], object):
        def append(self, v: T2) -> NoneType:
          self = Union[T, T2]

      class TrivialList2(typing.Generic[T], object):
        def append(self, v: T2) -> NoneType:
          self = Union[T, T2]
        def get_first(self) -> T: ...
    """)
    expected = textwrap.dedent("""
      T = TypeVar('T')
      T2 = TypeVar('T2')

      class TrivialList(typing.Generic[T], object):
          def append(self, v: T) -> NoneType: ...

      class TrivialList2(typing.Generic[T], object):
          def append(self, v: T) -> NoneType: ...
          def get_first(self) -> T: ...
    """)
    ast = self.Parse(src)
    ast = transforms.RemoveMutableParameters(ast)
    self.AssertSourceEquals(ast, expected)
Example #3
0
    def testRemoveMutableList(self):
        # Simple test for RemoveMutableParameters, with simplified list class
        src = textwrap.dedent("""
      T = TypeVar('T')
      T2 = TypeVar('T2')

      class TrivialList(typing.Generic[T], object):
        def append(self, v: T2) -> NoneType:
          self := T or T2

      class TrivialList2(typing.Generic[T], object):
        def __init__(self, x: T) -> NoneType
        def append(self, v: T2) -> NoneType:
          self := T or T2
        def get_first(self) -> T
    """)
        expected = textwrap.dedent("""
      T = TypeVar('T')
      T2 = TypeVar('T2')

      class TrivialList(typing.Generic[T], object):
          def append(self, v: T) -> NoneType

      class TrivialList2(typing.Generic[T], object):
          def __init__(self, x: T) -> NoneType
          def append(self, v: T) -> NoneType
          def get_first(self) -> T
    """)
        ast = self.Parse(src)
        ast = transforms.RemoveMutableParameters(ast)
        self.AssertSourceEquals(ast, expected)
Example #4
0
    def testRemoveMutableDict(self):
        # Test for RemoveMutableParameters, with simplified dict class.
        src = textwrap.dedent("""
      K = TypeVar('K')
      V = TypeVar('V')
      T = TypeVar('T')
      K2 = TypeVar('K2')
      V2 = TypeVar('V2')

      class MyDict(typing.Generic[K, V], object):
          def getitem(self, k: K, default: T) -> V or T
          def setitem(self, k: K2, value: V2) -> NoneType:
              self := dict[K or K2, V or V2]
          def getanykeyorvalue(self) -> K or V
          def setdefault(self, k: K2, v: V2) -> V or V2:
              self := dict[K or K2, V or V2]
    """)
        expected = textwrap.dedent("""
      K = TypeVar('K')
      V = TypeVar('V')
      T = TypeVar('T')
      K2 = TypeVar('K2')
      V2 = TypeVar('V2')

      class MyDict(typing.Generic[K, V], object):
          def getitem(self, k: K, default: V) -> V
          def setitem(self, k: K, value: V) -> NoneType
          def getanykeyorvalue(self) -> K or V
          def setdefault(self, k: K, v: V) -> V
    """)
        ast = self.Parse(src)
        ast = transforms.RemoveMutableParameters(ast)
        self.AssertSourceEquals(ast, expected)
Example #5
0
def solve(ast, builtins_pytd, protocols_pytd):
  """Solve the unknowns in a pytd AST using the standard Python builtins.

  Args:
    ast: A pytd.TypeDeclUnit, containing classes named ~unknownXX.
    builtins_pytd: A pytd for builtins.
    protocols_pytd: A pytd for protocols.

  Returns:
    A tuple of (1) a dictionary (str->str) mapping unknown class names to known
    class names and (2) a pytd.TypeDeclUnit of the complete classes in ast.
  """
  builtins_pytd = transforms.RemoveMutableParameters(builtins_pytd)
  builtins_pytd = visitors.LookupClasses(builtins_pytd)
  protocols_pytd = visitors.LookupClasses(protocols_pytd)
  ast = visitors.LookupClasses(ast, builtins_pytd)
  return TypeSolver(
      ast, builtins_pytd, protocols_pytd).solve(), extract_local(ast)
Example #6
0
def solve(ast, builtins_pytd):
    """Solve the unknowns in a pytd AST using the standard Python builtins.

  Args:
    ast: A pytd.TypeDeclUnit, containing classes named ~unknownXX.
    builtins_pytd: A pytd for builtins.

  Returns:
    A tuple of (1) a dictionary (str->str) mapping unknown class names to known
    class names and (2) a pytd.TypeDeclUnit of the complete classes in ast.
  """
    builtins_pytd = transforms.RemoveMutableParameters(builtins_pytd)
    builtins_pytd = builtins_pytd.Visit(visitors.NamedTypeToClassType())
    builtins_pytd = builtins_pytd.Visit(
        visitors.LookupFullNames([builtins_pytd]))
    builtins_pytd.Visit(visitors.VerifyLookup())
    ast = ast.Visit(visitors.NamedTypeToClassType())
    ast = ast.Visit(visitors.LookupFullNames([builtins_pytd, ast]))
    ast.Visit(visitors.VerifyLookup())
    return TypeSolver(ast, builtins_pytd).solve(), extract_local(ast)