Beispiel #1
0
 def testBasic(self):
     self.assertMatch(abstract.Empty(self.vm), abstract.Empty(self.vm))
Beispiel #2
0
    def __init__(self, vm):
        super(Converter, self).__init__(vm)
        self.vm.convert = self  # to make constant_to_value calls below work
        self.pytd_convert = output.Converter(vm)

        self._convert_cache = {}
        self._resolved_late_types = {}  # performance cache

        # Initialize primitive_classes to empty to allow constant_to_value to run.
        self.primitive_classes = ()

        # object_type is needed to initialize the primitive class values.
        self.object_type = self.constant_to_value(object)

        if self.vm.PY2:
            version_specific = [compat.UnicodeType]
        else:
            version_specific = [compat.BytesType]

        # Now fill primitive_classes with the real values using constant_to_value.
        self.primitive_classes = {
            v: self.constant_to_value(v)
            for v in [
                int, float, str, object, frozenset, compat.NoneType, complex,
                bool, slice, types.CodeType, compat.EllipsisType,
                compat.OldStyleClassType, super
            ] + version_specific
        }
        self.primitive_class_names = [
            self._type_to_name(x) for x in self.primitive_classes
        ]
        self.none = abstract.AbstractOrConcreteValue(
            None, self.primitive_classes[compat.NoneType], self.vm)
        self.true = abstract.AbstractOrConcreteValue(
            True, self.primitive_classes[bool], self.vm)
        self.false = abstract.AbstractOrConcreteValue(
            False, self.primitive_classes[bool], self.vm)
        self.ellipsis = abstract.AbstractOrConcreteValue(
            Ellipsis, self.primitive_classes[compat.EllipsisType], self.vm)

        self.primitive_class_instances = {}
        for name, cls in self.primitive_classes.items():
            if name == compat.NoneType:
                # This is possible because all None instances are the same.
                # Without it pytype could not reason that "x is None" is always true, if
                # x is indeed None.
                instance = self.none
            elif name == compat.EllipsisType:
                instance = self.ellipsis
            else:
                instance = abstract.Instance(cls, self.vm)
            self.primitive_class_instances[name] = instance
            self._convert_cache[(abstract.Instance, cls.pytd_cls)] = instance

        self.none_type = self.primitive_classes[compat.NoneType]
        self.oldstyleclass_type = self.primitive_classes[
            compat.OldStyleClassType]
        self.super_type = self.primitive_classes[super]
        self.str_type = self.primitive_classes[str]
        self.int_type = self.primitive_classes[int]

        self.unsolvable = abstract.Unsolvable(self.vm)
        self.empty = abstract.Empty(self.vm)
        self.no_return = typing_overlay.NoReturn(self.vm)

        self.list_type = self.constant_to_value(list)
        self.set_type = self.constant_to_value(set)
        self.dict_type = self.constant_to_value(dict)
        self.type_type = self.constant_to_value(type)
        self.module_type = self.constant_to_value(types.ModuleType)
        self.function_type = self.constant_to_value(types.FunctionType)
        self.tuple_type = self.constant_to_value(tuple)
        self.generator_type = self.constant_to_value(types.GeneratorType)
        self.iterator_type = self.constant_to_value(compat.IteratorType)
        self.bool_values = {
            True: self.true,
            False: self.false,
            None: self.primitive_class_instances[bool],
        }
        if self.vm.PY2:
            self.unicode_type = self.primitive_classes[compat.UnicodeType]
            self.bytes_type = self.str_type
            self.next_attr = "next"
        else:
            self.unicode_type = self.str_type
            self.bytes_type = self.primitive_classes[compat.BytesType]
            self.next_attr = "__next__"
Beispiel #3
0
 def testEmptyAgainstUnsolvable(self):
     var = self.vm.program.NewVariable()
     right = abstract.Empty(self.vm)
     result = self.vm.matcher.match_var_against_type(
         var, right, {}, self.vm.root_cfg_node, {})
     self.assertEqual(result, {})
Beispiel #4
0
    def __init__(self, vm):
        self.vm = vm
        self.vm.convert = self  # to make constant_to_value calls below work
        self.pytd_convert = output.Converter()

        self._convert_cache = {}
        self._resolved_late_types = {}  # performance cache

        # Initialize primitive_classes to empty to allow constant_to_value to run.
        self.primitive_classes = ()

        # object_type is needed to initialize the primitive class values.
        self.object_type = self.constant_to_value(object)

        # Now fill primitive_classes with the real values using constant_to_value.
        self.primitive_classes = {
            v: self.constant_to_value(v)
            for v in [
                int, float, str, unicode, object, types.NoneType, complex,
                bool, slice, types.CodeType, types.EllipsisType,
                types.ClassType, super
            ]
        }
        self.primitive_class_names = [
            x.__module__ + "." + x.__name__ for x in self.primitive_classes
        ]
        self.none = abstract.AbstractOrConcreteValue(
            None, self.primitive_classes[types.NoneType], self.vm)
        self.true = abstract.AbstractOrConcreteValue(
            True, self.primitive_classes[bool], self.vm)
        self.false = abstract.AbstractOrConcreteValue(
            False, self.primitive_classes[bool], self.vm)
        self.ellipsis = abstract.AbstractOrConcreteValue(
            Ellipsis, self.primitive_classes[types.EllipsisType], self.vm)

        self.primitive_class_instances = {}
        for name, cls in self.primitive_classes.items():
            if name == types.NoneType:
                # This is possible because all None instances are the same.
                # Without it pytype could not reason that "x is None" is always true, if
                # x is indeed None.
                instance = self.none
            elif name == types.EllipsisType:
                instance = self.ellipsis
            else:
                instance = abstract.Instance(cls, self.vm)
            self.primitive_class_instances[name] = instance
            self._convert_cache[(abstract.Instance, cls.pytd_cls)] = instance

        self.none_type = self.primitive_classes[types.NoneType]
        self.oldstyleclass_type = self.primitive_classes[types.ClassType]
        self.super_type = self.primitive_classes[super]
        self.str_type = self.primitive_classes[str]
        self.int_type = self.primitive_classes[int]

        self.nothing = abstract.Nothing(self.vm)
        self.unsolvable = abstract.Unsolvable(self.vm)
        self.empty = abstract.Empty(self.vm)

        self.list_type = self.constant_to_value(list)
        self.set_type = self.constant_to_value(set)
        self.dict_type = self.constant_to_value(dict)
        self.type_type = self.constant_to_value(type)
        self.module_type = self.constant_to_value(types.ModuleType)
        self.function_type = self.constant_to_value(types.FunctionType)
        self.tuple_type = self.constant_to_value(tuple)
        self.generator_type = self.constant_to_value(types.GeneratorType)
        # TODO(dbaum): There isn't a types.IteratorType.  This can probably be
        # based on typing.Iterator, but that will also require changes to
        # convert.py since that assumes all types can be looked up in
        # __builtin__.
        self.iterator_type = self.constant_to_value(types.ObjectType)
        self.bool_values = {
            True: self.true,
            False: self.false,
            None: self.primitive_class_instances[bool],
        }
Beispiel #5
0
    def __init__(self, vm):
        self.vm = vm
        self.vm.convert = self  # to make constant_to_var calls below work
        self.pytd_convert = output.Converter()

        self._convert_cache = {}

        # Initialize primitive_classes to empty to allow constant_to_var to run
        self.primitive_classes = ()
        # Now fill primitive_classes with the real values using constant_to_var
        self.primitive_classes = {
            v: self.constant_to_var(v.__name__, v)
            for v in [
                int, float, str, unicode, object, types.NoneType, complex,
                bool, slice, types.CodeType, types.EllipsisType,
                types.ClassType, super
            ]
        }

        self.none = abstract.AbstractOrConcreteValue(
            None, self.primitive_classes[types.NoneType], self.vm,
            self.vm.root_cfg_node)
        self.true = abstract.AbstractOrConcreteValue(
            True, self.primitive_classes[bool], self.vm, self.vm.root_cfg_node)
        self.false = abstract.AbstractOrConcreteValue(
            False, self.primitive_classes[bool], self.vm,
            self.vm.root_cfg_node)
        self.ellipsis = abstract.AbstractOrConcreteValue(
            Ellipsis, self.primitive_classes[types.EllipsisType], self.vm,
            self.vm.root_cfg_node)

        self.primitive_class_instances = {}
        for name, clsvar in self.primitive_classes.items():
            if name == types.NoneType:
                # This is possible because all None instances are the same.
                # Without it pytype could not reason that "x is None" is always true, if
                # x is indeed None.
                instance = self.none
            elif name == types.EllipsisType:
                instance = self.ellipsis
            else:
                instance = abstract.Instance(clsvar, self.vm,
                                             self.vm.root_cfg_node)
            self.primitive_class_instances[name] = instance
            clsval, = clsvar.bindings
            self._convert_cache[(abstract.Instance,
                                 clsval.data.pytd_cls)] = instance

        self.none_type = self.primitive_classes[types.NoneType]
        self.object_type = self.primitive_classes[object]
        self.oldstyleclass_type = self.primitive_classes[types.ClassType]
        self.super_type = self.primitive_classes[super]
        self.str_type = self.primitive_classes[str]
        self.int_type = self.primitive_classes[int]

        self.nothing = abstract.Nothing(self.vm)
        self.unsolvable = abstract.Unsolvable(self.vm)
        self.empty = abstract.Empty(self.vm)

        self.tuple_type = self.constant_to_var("tuple", tuple)
        self.list_type = self.constant_to_var("list", list)
        self.set_type = self.constant_to_var("set", set)
        self.frozenset_type = self.constant_to_var("frozenset", frozenset)
        self.dict_type = self.constant_to_var("dict", dict)
        self.type_type = self.constant_to_var("type", type)
        self.module_type = self.constant_to_var("module", types.ModuleType)
        self.function_type = self.constant_to_var("function",
                                                  types.FunctionType)
        self.generator_type = self.constant_to_var("generator",
                                                   types.GeneratorType)
        # TODO(dbaum): There isn't a types.IteratorType.  This can probably be
        # based on typing.Iterator, but that will also require changes to
        # convert.py since that assumes all types can be looked up in
        # __builtin__.
        self.iterator_type = self.constant_to_var("iterator", types.ObjectType)
        self.bool_values = {
            True: self.true,
            False: self.false,
            None: self.primitive_class_instances[bool],
        }
        self.empty_type = self.empty.to_variable(self.vm.root_cfg_node)
        object_val, = self.object_type.data
        object_val.load_lazy_attribute("__new__")
        self.object_new, = object_val.members["__new__"].data
        self.typing_overlay = typing.TypingOverlay(self.vm,
                                                   self.vm.root_cfg_node)
Beispiel #6
0
 def test_empty_var_against_empty(self):
     var = self.vm.program.NewVariable()
     right = abstract.Empty(self.vm)
     result = self.vm.matcher(self.vm.root_node).match_var_against_type(
         var, right, {}, {})
     self.assertEqual(result, {})
Beispiel #7
0
 def test_singleton_subclass(self):
   self.assertIs(abstract.Empty(self._vm), abstract.Empty(self._vm))
   self.assertIsNot(abstract.Deleted(self._vm), abstract.Empty(self._vm))