Ejemplo n.º 1
0
  def test_rename_module(self):
    module_name = "foo.bar"
    src = """
        import module2
        from module2 import f
        from typing import List

        constant = True

        x = List[int]
        b = List[int]

        class SomeClass(object):
          def __init__(self, a: module2.ObjectMod2):
            pass

        def ModuleFunction():
          pass
    """
    ast = self.Parse(src, name=module_name)
    new_ast = ast.Visit(pytd_visitors.RenameModuleVisitor(module_name,
                                                          "other.name"))

    self.assertEqual("other.name", new_ast.name)
    self.assertTrue(new_ast.Lookup("other.name.SomeClass"))
    self.assertTrue(new_ast.Lookup("other.name.constant"))
    self.assertTrue(new_ast.Lookup("other.name.ModuleFunction"))

    with self.assertRaises(KeyError):
      new_ast.Lookup("foo.bar.SomeClass")
Ejemplo n.º 2
0
  def test_rename_module_with_type_parameter(self):
    module_name = "foo.bar"
    src = """
      import typing

      T = TypeVar('T')

      class SomeClass(typing.Generic[T]):
        def __init__(self, foo: T) -> None:
          pass
    """
    ast = self.Parse(src, name=module_name)
    new_ast = ast.Visit(pytd_visitors.RenameModuleVisitor(module_name,
                                                          "other.name"))

    some_class = new_ast.Lookup("other.name.SomeClass")
    self.assertTrue(some_class)
    init_function = some_class.Lookup("__init__")
    self.assertTrue(init_function)
    self.assertEqual(len(init_function.signatures), 1)
    signature, = init_function.signatures
    _, param2 = signature.params
    self.assertEqual(param2.type.scope, "other.name.SomeClass")