Esempio n. 1
0
 def call(self, node, _, args):
     posargs = args.posargs
     if isinstance(args.namedargs, dict):
         namedargs = args.namedargs
     else:
         namedargs = self.vm.convert.value_to_constant(args.namedargs, dict)
     if namedargs and self.vm.python_version < (3, 6):
         errmsg = "Keyword syntax for NamedTuple is only supported in Python 3.6+"
         self.vm.errorlog.invalid_namedtuple_arg(self.vm.frames,
                                                 err_msg=errmsg)
     if namedargs and len(posargs) == 1:
         namedargs = [
             abstract.Tuple((self.vm.convert.build_string(node, k), v),
                            self.vm).to_variable(node)
             for k, v in namedargs.items()
         ]
         namedargs = abstract.List(namedargs, self.vm).to_variable(node)
         posargs += (namedargs, )
         args = function.Args(posargs)
     elif namedargs:
         errmsg = ("Either list of fields or keywords can be provided to "
                   "NamedTuple, not both")
         self.vm.errorlog.invalid_namedtuple_arg(self.vm.frames,
                                                 err_msg=errmsg)
     return self.namedtuple.call(node, None, args)
Esempio n. 2
0
 def testToTypeWithViewAndEmptyParam(self):
     instance = abstract.List([], self._vm)
     view = {instance.cls: instance.cls.bindings[0]}
     pytd_type = instance.to_type(self._vm.root_cfg_node,
                                  seen=None,
                                  view=view)
     self.assertEqual("__builtin__.list", pytd_type.base_type.name)
     self.assertSequenceEqual((pytd.NothingType(), ), pytd_type.parameters)
Esempio n. 3
0
 def test_compatible_with_list(self):
     i = abstract.List([], self._vm)
     # Empty list is not compatible with True.
     self.assertFalsy(i)
     # Once a type parameter is set, list is compatible with True and False.
     i.merge_instance_type_parameter(
         self._node, abstract_utils.T,
         self._vm.convert.object_type.to_variable(self._vm.root_cfg_node))
     self.assertAmbiguous(i)
Esempio n. 4
0
 def test_compatible_with_list(self):
     i = abstract.List([], self._vm)
     # Empty list is not compatible with True.
     self.assertIs(False, i.compatible_with(True))
     self.assertIs(True, i.compatible_with(False))
     # Once a type parameter is set, list is compatible with True and False.
     i.merge_type_parameter(
         self._node, abstract.T,
         self._vm.convert.object_type.to_variable(self._vm.root_cfg_node))
     self.assertIs(True, i.compatible_with(True))
     self.assertIs(True, i.compatible_with(False))
Esempio n. 5
0
 def test_to_type_with_view1(self):
   # to_type(<instance of List[int or unsolvable]>, view={T: int})
   instance = abstract.List([], self._vm)
   instance.merge_instance_type_parameter(
       self._vm.root_cfg_node, abstract_utils.T, self._vm.program.NewVariable(
           [self._vm.convert.unsolvable], [], self._vm.root_cfg_node))
   param_binding = instance.get_instance_type_parameter(
       abstract_utils.T).AddBinding(
           self._vm.convert.primitive_class_instances[int], [],
           self._vm.root_cfg_node)
   view = {
       instance.get_instance_type_parameter(abstract_utils.T): param_binding}
   pytd_type = instance.to_type(self._vm.root_cfg_node, seen=None, view=view)
   self.assertEqual("__builtin__.list", pytd_type.base_type.name)
   self.assertSetEqual({"__builtin__.int"},
                       {t.name for t in pytd_type.parameters})
Esempio n. 6
0
 def testToTypeWithView1(self):
   # to_type(<instance of List[int or unsolvable]>, view={T: int})
   instance = abstract.List([], self._vm)
   instance.merge_type_parameter(
       self._vm.root_cfg_node, abstract.T, self._vm.program.NewVariable(
           [self._vm.convert.unsolvable], [], self._vm.root_cfg_node))
   param_binding = instance.type_parameters[abstract.T].AddBinding(
       self._vm.convert.primitive_class_instances[int], [],
       self._vm.root_cfg_node)
   view = {instance.cls: instance.cls.bindings[0],
           instance.type_parameters[abstract.T]: param_binding,
           param_binding.data.cls: param_binding.data.cls.bindings[0]}
   pytd_type = instance.to_type(self._vm.root_cfg_node, seen=None, view=view)
   self.assertEqual("__builtin__.list", pytd_type.base_type.name)
   self.assertSetEqual({"__builtin__.int"},
                       {t.name for t in pytd_type.parameters})
Esempio n. 7
0
 def build_list(self, node, content):
     """Create a VM list from the given sequence."""
     # TODO(rechen): set T to empty if there is nothing in content
     content = [var.AssignToNewVariable(node) for var in content]
     return abstract.List(content, self.vm).to_variable(node)
Esempio n. 8
0
 def test_to_type_with_view_and_empty_param(self):
   instance = abstract.List([], self._vm)
   pytd_type = instance.to_type(self._vm.root_cfg_node, seen=None, view={})
   self.assertEqual("__builtin__.list", pytd_type.base_type.name)
   self.assertSequenceEqual((pytd.NothingType(),), pytd_type.parameters)
Esempio n. 9
0
 def build_list(self, node, content):
     """Create a VM list from the given sequence."""
     # TODO(rechen): set T to empty if there is nothing in content
     content = list(content)  # content might be a generator
     return abstract.List(content, self.vm).to_variable(node)