Exemplo n.º 1
0
def make_type_info(name: str,
                   is_abstract: bool = False,
                   mro: List[TypeInfo] = None,
                   bases: List[Instance] = None,
                   typevars: List[str] = None) -> TypeInfo:
    """Make a TypeInfo suitable for use in unit tests."""

    class_def = ClassDef(name, Block([]), None, [])
    class_def.fullname = name

    if typevars:
        v = []  # type: List[TypeVarDef]
        id = 1
        for n in typevars:
            v.append(TypeVarDef(n, id, None))
            id += 1
        class_def.type_vars = v

    info = TypeInfo(SymbolTable(), class_def)
    if mro is None:
        mro = []
    info.mro = [info] + mro
    if bases is None:
        if mro:
            # By default, assume that there is a single non-generic base.
            bases = [Instance(mro[0], [])]
        else:
            bases = []
    info.bases = bases

    return info
Exemplo n.º 2
0
def make_type_info(name: str,
                   is_abstract: bool = False,
                   mro: List[TypeInfo] = None,
                   bases: List[Instance] = None,
                   typevars: List[str] = None) -> TypeInfo:
    """Make a TypeInfo suitable for use in unit tests."""

    class_def = ClassDef(name, Block([]), None, [])
    class_def.fullname = name

    if typevars:
        v = []  # type: List[TypeVarDef]
        id = 1
        for n in typevars:
            v.append(TypeVarDef(n, id, None))
            id += 1
        class_def.type_vars = v

    info = TypeInfo(SymbolTable(), class_def)
    if mro is None:
        mro = []
    info.mro = [info] + mro
    if bases is None:
        if mro:
            # By default, assume that there is a single non-generic base.
            bases = [Instance(mro[0], [])]
        else:
            bases = []
    info.bases = bases

    return info
Exemplo n.º 3
0
 def visit_class_def(self, node: ClassDef) -> None:
     """Strip class body and type info, but don't strip methods."""
     self.strip_type_info(node.info)
     node.type_vars = []
     node.base_type_exprs.extend(node.removed_base_type_exprs)
     node.removed_base_type_exprs = []
     with self.enter_class(node.info):
         super().visit_class_def(node)
Exemplo n.º 4
0
    def visit_class_def(self, node: ClassDef) -> None:
        """Strip class body and type info, but don't strip methods."""
        to_delete = set(self.strip_type_info(node.info))
        node.type_vars = []
        node.base_type_exprs.extend(node.removed_base_type_exprs)
        node.removed_base_type_exprs = []
        node.defs.body = [s for s in node.defs.body if s not in to_delete]

        with self.enter_class(node.info):
            super().visit_class_def(node)
Exemplo n.º 5
0
    def visit_class_def(self, node: ClassDef) -> None:
        """Strip class body and type info, but don't strip methods."""
        to_delete = set(self.strip_type_info(node.info))
        node.type_vars = []
        node.base_type_exprs.extend(node.removed_base_type_exprs)
        node.removed_base_type_exprs = []
        node.defs.body = [s for s in node.defs.body
                          if s not in to_delete]

        with self.enter_class(node.info):
            super().visit_class_def(node)
Exemplo n.º 6
0
    def make_type_info(self,
                       name: str,
                       module_name: Optional[str] = None,
                       is_abstract: bool = False,
                       mro: Optional[List[TypeInfo]] = None,
                       bases: Optional[List[Instance]] = None,
                       typevars: Optional[List[str]] = None,
                       variances: Optional[List[int]] = None) -> TypeInfo:
        """Make a TypeInfo suitable for use in unit tests."""

        class_def = ClassDef(name, Block([]), None, [])
        class_def.fullname = name

        if module_name is None:
            if '.' in name:
                module_name = name.rsplit('.', 1)[0]
            else:
                module_name = '__main__'

        if typevars:
            v: List[TypeVarLikeType] = []
            for id, n in enumerate(typevars, 1):
                if variances:
                    variance = variances[id - 1]
                else:
                    variance = COVARIANT
                v.append(TypeVarType(n, n, id, [], self.o, variance=variance))
            class_def.type_vars = v

        info = TypeInfo(SymbolTable(), class_def, module_name)
        if mro is None:
            mro = []
            if name != 'builtins.object':
                mro.append(self.oi)
        info.mro = [info] + mro
        if bases is None:
            if mro:
                # By default, assume that there is a single non-generic base.
                bases = [Instance(mro[0], [])]
            else:
                bases = []
        info.bases = bases

        return info
Exemplo n.º 7
0
    def make_type_info(self, name: str,
                       module_name: Optional[str] = None,
                       is_abstract: bool = False,
                       mro: Optional[List[TypeInfo]] = None,
                       bases: Optional[List[Instance]] = None,
                       typevars: Optional[List[str]] = None,
                       variances: Optional[List[int]] = None) -> TypeInfo:
        """Make a TypeInfo suitable for use in unit tests."""

        class_def = ClassDef(name, Block([]), None, [])
        class_def.fullname = name

        if module_name is None:
            if '.' in name:
                module_name = name.rsplit('.', 1)[0]
            else:
                module_name = '__main__'

        if typevars:
            v = []  # type: List[TypeVarDef]
            for id, n in enumerate(typevars, 1):
                if variances:
                    variance = variances[id - 1]
                else:
                    variance = COVARIANT
                v.append(TypeVarDef(n, n, id, [], self.o, variance=variance))
            class_def.type_vars = v

        info = TypeInfo(SymbolTable(), class_def, module_name)
        if mro is None:
            mro = []
            if name != 'builtins.object':
                mro.append(self.oi)
        info.mro = [info] + mro
        if bases is None:
            if mro:
                # By default, assume that there is a single non-generic base.
                bases = [Instance(mro[0], [])]
            else:
                bases = []
        info.bases = bases

        return info
Exemplo n.º 8
0
 def visit_class_def(self, node: ClassDef) -> None:
     """Strip class body and type info, but don't strip methods."""
     # We need to save the implicitly defined instance variables,
     # i.e. those defined as attributes on self. Otherwise, they would
     # be lost if we only reprocess top-levels (this kills TypeInfos)
     # but not the methods that defined those variables.
     if not self.recurse_into_functions:
         self.save_implicit_attributes(node)
     # We need to delete any entries that were generated by plugins,
     # since they will get regenerated.
     to_delete = {v.node for v in node.info.names.values() if v.plugin_generated}
     node.type_vars = []
     node.base_type_exprs.extend(node.removed_base_type_exprs)
     node.removed_base_type_exprs = []
     node.defs.body = [s for s in node.defs.body
                       if s not in to_delete]
     with self.enter_class(node.info):
         super().visit_class_def(node)
     TypeState.reset_subtype_caches_for(node.info)
     # Kill the TypeInfo, since there is none before semantic analysis.
     node.info = CLASSDEF_NO_INFO
Exemplo n.º 9
0
 def visit_class_def(self, node: ClassDef) -> None:
     """Strip class body and type info, but don't strip methods."""
     # We need to save the implicitly defined instance variables,
     # i.e. those defined as attributes on self. Otherwise, they would
     # be lost if we only reprocess top-levels (this kills TypeInfos)
     # but not the methods that defined those variables.
     if not self.recurse_into_functions:
         self.prepare_implicit_var_patches(node)
     # We need to delete any entries that were generated by plugins,
     # since they will get regenerated.
     to_delete = {v.node for v in node.info.names.values() if v.plugin_generated}
     node.type_vars = []
     node.base_type_exprs.extend(node.removed_base_type_exprs)
     node.removed_base_type_exprs = []
     node.defs.body = [s for s in node.defs.body
                       if s not in to_delete]
     with self.enter_class(node.info):
         super().visit_class_def(node)
     TypeState.reset_subtype_caches_for(node.info)
     # Kill the TypeInfo, since there is none before semantic analysis.
     node.info = CLASSDEF_NO_INFO
Exemplo n.º 10
0
    def make_type_info(self,
                       name: str,
                       is_abstract: bool = False,
                       mro: List[TypeInfo] = None,
                       bases: List[Instance] = None,
                       typevars: List[str] = None,
                       variances: List[int] = None) -> TypeInfo:
        """Make a TypeInfo suitable for use in unit tests."""

        class_def = ClassDef(name, Block([]), None, [])
        class_def.fullname = name

        if typevars:
            v = []  # type: List[TypeVarDef]
            for id, n in enumerate(typevars, 1):
                if variances:
                    variance = variances[id - 1]
                else:
                    variance = COVARIANT
                v.append(TypeVarDef(n, id, None, self.o, variance=variance))
            class_def.type_vars = v

        info = TypeInfo(SymbolTable(), class_def)
        if mro is None:
            mro = []
            if name != 'builtins.object':
                mro.append(self.oi)
        info.mro = [info] + mro
        if bases is None:
            if mro:
                # By default, assume that there is a single non-generic base.
                bases = [Instance(mro[0], [])]
            else:
                bases = []
        info.bases = bases

        return info