Ejemplo n.º 1
0
    def testClassSanity(self):
        ty = self.InferDedent("""
      class A(object):
        def __init__(self):
          self.x = 1

        def get_x(self):
          return self.x

        def set_x(self, x):
          self.x = x
      a = A()
      y = a.x
      x1 = a.get_x()
      a.set_x(1.2)
      x2 = a.get_x()
    """)
        self.assertHasSignature(
            ty.Lookup("A").Lookup("set_x"), (
                pytd.ClassType("A"),
                self.float,
            ), self.none_type)
        self.assertHasSignature(
            ty.Lookup("A").Lookup("get_x"), (pytd.ClassType("A"), ),
            self.intorfloat)
Ejemplo n.º 2
0
 def testMaybeFillInClasses(self):
     src = """
     class A:
       def a(self, a: A, b: B) -> A or B raises A, B
 """
     tree = self.Parse(src)
     ty_a = pytd.ClassType("A")
     visitors.FillInClasses(ty_a, tree)
     self.assertIsNotNone(ty_a.cls)
     ty_b = pytd.ClassType("B")
     visitors.FillInClasses(ty_b, tree)
     self.assertIsNone(ty_b.cls)
Ejemplo n.º 3
0
 def __init__(self, replace_unknown=False, force=False):
     self.class_types = []  # allow nested classes
     self.force = force
     self.replaced_self_types = (pytd.NamedType("object"),
                                 pytd.ClassType("object"))
     if replace_unknown:
         self.replaced_self_types += (pytd.AnythingType(), )
Ejemplo n.º 4
0
 def setUp(self):
   with open(utils.GetDataFile("examples/StringIO.py"), "r") as infile:
     self.ty = self.Infer(infile.read())
   try:
     self.stringio_cls = self.ty.Lookup("StringIO")
   except KeyError:
     self.stringio_cls = None
     # Continue to the test it will fail if it needs the cls
   self.stringio_type = pytd.ClassType("StringIO")
   self.stringio_type.cls = self.stringio_cls
Ejemplo n.º 5
0
    def VisitNamedType(self, node):
        """Converts a named type to a class type, to be filled in later.

    Args:
      node: The NamedType. This type only has a name.

    Returns:
      A ClassType. This ClassType will (temporarily) only have a name.
    """
        return pytd.ClassType(node.name)
Ejemplo n.º 6
0
 def testSetsSanity(self):
   ty = self.InferDedent("""
     def f():
       x = set([1])
       x.add(10)
       return x
     f()
   """)
   self.assertHasOnlySignatures(ty.Lookup("f"),
                                ((), pytd.GenericType(pytd.ClassType("set"),
                                                      (self.int,))))
Ejemplo n.º 7
0
  def setUp(self):
    self.bool = pytd.ClassType("bool")
    self.dict = pytd.ClassType("dict")
    self.float = pytd.ClassType("float")
    self.int = pytd.ClassType("int")
    self.list = pytd.ClassType("list")
    self.none_type = pytd.ClassType("NoneType")
    self.object = pytd.ClassType("object")
    self.str = pytd.ClassType("str")
    self.tuple = pytd.ClassType("tuple")

    self.intorfloat = pytd.UnionType((self.float, self.int))
    self.intorstr = pytd.UnionType((self.int, self.str))

    # Make get_pytd load _builtin_pytds
    self.builtin_pytds = parse.utils.GetBuiltins()
    for ty in (self.int, self.none_type, self.float,
               self.intorfloat, self.tuple, self.str,
               self.object, self.list, self.dict, self.bool):
      visitors.FillInClasses(ty, self.builtin_pytds)
Ejemplo n.º 8
0
    def get_subclasses(self, t):
        """Get all classes derived from this type.

    Args:
        t: A pytd.TYPE
    Returns:
        A list of pytd.TYPE.
    """
        if isinstance(t, pytd.ClassType):
            subclasses = self.direct_subclasses.get(t, [])
            return sum((self.get_subclasses(pytd.ClassType(c.name, c))
                        for c in subclasses), [t])
        else:
            raise NotImplementedError("Can't extract subclasses from %s",
                                      type(t))
Ejemplo n.º 9
0
 def testSets(self):
   ty = self.InferDedent("""
     def f():
       x = set([1,2,3])
       if x:
         x = x | set()
         y = x
         return x
       else:
         x.add(10)
         return x
     f()
   """)
   self.assertHasOnlySignatures(ty.Lookup("f"),
                                ((), pytd.GenericType(pytd.ClassType("set"),
                                                      (self.int,))))
Ejemplo n.º 10
0
 def setUp(self):
     self.int = pytd.ClassType("int")
     self.none_type = pytd.ClassType("NoneType")
     self.float = pytd.ClassType("float")
     self.list = pytd.ClassType("list")