Python builtins.type() Examples

The following are 30 code examples of builtins.type(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module builtins , or try the search function .
Example #1
Source File: scoped_nodes.py    From python-netsurv with MIT License 6 votes vote down vote up
def scope_lookup(self, node, name, offset=0):
        """Lookup where the given variable is assigned.

        :param node: The node to look for assignments up to.
            Any assignments after the given node are ignored.
        :type node: NodeNG

        :param name: The name of the variable to find assignments for.
        :type name: str

        :param offset: The line offset to filter statements up to.
        :type offset: int

        :returns: This scope node and the list of assignments associated to the
            given name according to the scope where it has been found (locals,
            globals or builtin).
        :rtype: tuple(str, list(NodeNG))
        """
        if name in self.scope_attrs and name not in self.locals:
            try:
                return self, self.getattr(name)
            except exceptions.AttributeInferenceError:
                return self, ()
        return self._scope_lookup(node, name, offset) 
Example #2
Source File: scoped_nodes.py    From python-netsurv with MIT License 6 votes vote down vote up
def __init__(self, lineno=None, col_offset=None, parent=None):
        """
        :param lineno: The line that this node appears on in the source code.
        :type lineno: int or None

        :param col_offset: The column that this node appears on in the
            source code.
        :type col_offset: int or None

        :param parent: The parent node in the syntax tree.
        :type parent: NodeNG or None
        """
        self.locals = {}
        """A map of the name of a local variable to the node defining the local.

        :type: dict(str, NodeNG)
        """

        super(GeneratorExp, self).__init__(lineno, col_offset, parent) 
Example #3
Source File: scoped_nodes.py    From python-netsurv with MIT License 6 votes vote down vote up
def postinit(self, key=None, value=None, generators=None):
        """Do some setup after initialisation.

        :param key: What produces the keys.
        :type key: NodeNG or None

        :param value: What produces the values.
        :type value: NodeNG or None

        :param generators: The generators that are looped through.
        :type generators: list(Comprehension) or None
        """
        self.key = key
        self.value = value
        if generators is None:
            self.generators = []
        else:
            self.generators = generators 
Example #4
Source File: scoped_nodes.py    From python-netsurv with MIT License 6 votes vote down vote up
def __init__(self, lineno=None, col_offset=None, parent=None):
        """
        :param lineno: The line that this node appears on in the source code.
        :type lineno: int or None

        :param col_offset: The column that this node appears on in the
            source code.
        :type col_offset: int or None

        :param parent: The parent node in the syntax tree.
        :type parent: NodeNG or None
        """
        self.locals = {}
        """A map of the name of a local variable to the node defining the local.

        :type: dict(str, NodeNG)
        """

        super(SetComp, self).__init__(lineno, col_offset, parent) 
Example #5
Source File: scoped_nodes.py    From python-netsurv with MIT License 6 votes vote down vote up
def igetattr(self, name, context=None):
        """Infer the possible values of the given variable.

        :param name: The name of the variable to infer.
        :type name: str

        :returns: The inferred possible values.
        :rtype: iterable(NodeNG) or None
        """
        # set lookup name since this is necessary to infer on import nodes for
        # instance
        context = contextmod.copy_context(context)
        context.lookupname = name
        try:
            return bases._infer_stmts(self.getattr(name, context), context, frame=self)
        except exceptions.AttributeInferenceError as error:
            raise exceptions.InferenceError(
                error.message, target=self, attribute=name, context=context
            ) from error 
Example #6
Source File: scoped_nodes.py    From python-netsurv with MIT License 6 votes vote down vote up
def __init__(self, lineno=None, col_offset=None, parent=None):
        """
        :param lineno: The line that this node appears on in the source code.
        :type lineno: int or None

        :param col_offset: The column that this node appears on in the
            source code.
        :type col_offset: int or None

        :param parent: The parent node in the syntax tree.
        :type parent: NodeNG or None
        """
        self.locals = {}
        """A map of the name of a local variable to the node defining the local.

        :type: dict(str, NodeNG)
        """

        super(GeneratorExp, self).__init__(lineno, col_offset, parent) 
Example #7
Source File: scoped_nodes.py    From python-netsurv with MIT License 6 votes vote down vote up
def scope_lookup(self, node, name, offset=0):
        """Lookup where the given variable is assigned.

        :param node: The node to look for assignments up to.
            Any assignments after the given node are ignored.
        :type node: NodeNG

        :param name: The name of the variable to find assignments for.
        :type name: str

        :param offset: The line offset to filter statements up to.
        :type offset: int

        :returns: This scope node and the list of assignments associated to the
            given name according to the scope where it has been found (locals,
            globals or builtin).
        :rtype: tuple(str, list(NodeNG))
        """
        if name in self.scope_attrs and name not in self.locals:
            try:
                return self, self.getattr(name)
            except exceptions.AttributeInferenceError:
                return self, ()
        return self._scope_lookup(node, name, offset) 
Example #8
Source File: scoped_nodes.py    From python-netsurv with MIT License 6 votes vote down vote up
def __init__(self, lineno=None, col_offset=None, parent=None):
        """
        :param lineno: The line that this node appears on in the source code.
        :type lineno: int or None

        :param col_offset: The column that this node appears on in the
            source code.
        :type col_offset: int or None

        :param parent: The parent node in the syntax tree.
        :type parent: NodeNG or None
        """
        self.locals = {}
        """A map of the name of a local variable to the node defining the local.

        :type: dict(str, NodeNG)
        """

        super(DictComp, self).__init__(lineno, col_offset, parent) 
Example #9
Source File: scoped_nodes.py    From python-netsurv with MIT License 6 votes vote down vote up
def __init__(self, lineno=None, col_offset=None, parent=None):
        """
        :param lineno: The line that this node appears on in the source code.
        :type lineno: int or None

        :param col_offset: The column that this node appears on in the
            source code.
        :type col_offset: int or None

        :param parent: The parent node in the syntax tree.
        :type parent: NodeNG or None
        """
        self.locals = {}
        """A map of the name of a local variable to the node defining the local.

        :type: dict(str, NodeNG)
        """

        super(SetComp, self).__init__(lineno, col_offset, parent) 
Example #10
Source File: scoped_nodes.py    From python-netsurv with MIT License 6 votes vote down vote up
def local_attr_ancestors(self, name, context=None):
        """Iterate over the parents that define the given name.

        :param name: The name to find definitions for.
        :type name: str

        :returns: The parents that define the given name.
        :rtype: iterable(NodeNG)
        """
        # Look up in the mro if we can. This will result in the
        # attribute being looked up just as Python does it.
        try:
            ancestors = self.mro(context)[1:]
        except exceptions.MroError:
            # Fallback to use ancestors, we can't determine
            # a sane MRO.
            ancestors = self.ancestors(context=context)
        for astroid in ancestors:
            if name in astroid:
                yield astroid 
Example #11
Source File: scoped_nodes.py    From python-netsurv with MIT License 6 votes vote down vote up
def is_subtype_of(self, type_name, context=None):
        """Whether this class is a subtype of the given type.

        :param type_name: The name of the type of check against.
        :type type_name: str

        :returns: True if this class is a subtype of the given type,
            False otherwise.
        :rtype: bool
        """
        if self.qname() == type_name:
            return True
        for anc in self.ancestors(context=context):
            if anc.qname() == type_name:
                return True
        return False 
Example #12
Source File: scoped_nodes.py    From python-netsurv with MIT License 5 votes vote down vote up
def display_type(self):
        """A human readable type of this node.

        :returns: The type of this node.
        :rtype: str
        """
        if "method" in self.type:
            return "Method"
        return "Function" 
Example #13
Source File: scoped_nodes.py    From python-netsurv with MIT License 5 votes vote down vote up
def stream(self):
        """Get a stream to the underlying file or bytes.

        :type: file or io.BytesIO or None
        """
        return self._get_stream() 
Example #14
Source File: scoped_nodes.py    From python-netsurv with MIT License 5 votes vote down vote up
def postinit(self, args, body):
        """Do some setup after initialisation.

        :param args: The arguments that the function takes.
        :type args: Arguments

        :param body: The contents of the function body.
        :type body: list(NodeNG)
        """
        self.args = args
        self.body = body 
Example #15
Source File: scoped_nodes.py    From python-netsurv with MIT License 5 votes vote down vote up
def __contains__(self, name):
        """Check if a local is defined in this scope.

        :param name: The name of the local to check for.
        :type name: str

        :returns: True if this node has a local of the given name,
            False otherwise.
        :rtype: bool
        """
        return name in self.locals 
Example #16
Source File: scoped_nodes.py    From python-netsurv with MIT License 5 votes vote down vote up
def __init__(self, lineno=None, col_offset=None, parent=None):
        self.locals = {}
        """A map of the name of a local variable to the node defining it.

        :type: dict(str, NodeNG)
        """

        super(ListComp, self).__init__(lineno, col_offset, parent) 
Example #17
Source File: scoped_nodes.py    From python-netsurv with MIT License 5 votes vote down vote up
def implicit_parameters(self):
        return 0

    # function's type, 'function' | 'method' | 'staticmethod' | 'classmethod' 
Example #18
Source File: scoped_nodes.py    From python-netsurv with MIT License 5 votes vote down vote up
def __init__(self, lineno=None, col_offset=None, parent=None):
        """
        :param lineno: The line that this node appears on in the source code.
        :type lineno: int or None

        :param col_offset: The column that this node appears on in the
            source code.
        :type col_offset: int or None

        :param parent: The parent node in the syntax tree.
        :type parent: NodeNG or None
        """
        self.locals = {}
        """A map of the name of a local variable to the node defining it.

        :type: dict(str, NodeNG)
        """

        self.args = []
        """The arguments that the function takes.

        :type: Arguments or list
        """

        self.body = []
        """The contents of the function body.

        :type: list(NodeNG)
        """

        super(Lambda, self).__init__(lineno, col_offset, parent) 
Example #19
Source File: scoped_nodes.py    From python-netsurv with MIT License 5 votes vote down vote up
def block_range(self, lineno):
        """Get a range from the given line number to where this node ends.

        :param lineno: Unused.
        :type lineno: int

        :returns: The range of line numbers that this node belongs to,
        :rtype: tuple(int, int)
        """
        return self.fromlineno, self.tolineno 
Example #20
Source File: scoped_nodes.py    From python-netsurv with MIT License 5 votes vote down vote up
def __getitem__(self, item):
        """The first node the defines the given local.

        :param item: The name of the locally defined object.
        :type item: str

        :raises KeyError: If the name is not defined.
        """
        return self.locals[item][0] 
Example #21
Source File: scoped_nodes.py    From python-netsurv with MIT License 5 votes vote down vote up
def add_local_node(self, child_node, name=None):
        """Append a child that should alter the locals of this scope node.

        :param child_node: The child node that will alter locals.
        :type child_node: NodeNG

        :param name: The name of the local that will be altered by
            the given child node.
        :type name: str or None
        """
        if name != "__class__":
            # add __class__ node as a child will cause infinite recursion later!
            self._append_node(child_node)
        self.set_local(name or child_node.name, child_node) 
Example #22
Source File: scoped_nodes.py    From python-netsurv with MIT License 5 votes vote down vote up
def set_local(self, name, stmt):
        """Define that the given name is declared in the given statement node.

        .. seealso:: :meth:`scope`

        :param name: The name that is being defined.
        :type name: str

        :param stmt: The statement that defines the given name.
        :type stmt: NodeNG
        """
        # assert not stmt in self.locals.get(name, ()), (self, stmt)
        self.locals.setdefault(name, []).append(stmt) 
Example #23
Source File: scoped_nodes.py    From python-netsurv with MIT License 5 votes vote down vote up
def implicit_metaclass(self):
        """Get the implicit metaclass of the current class.

        For newstyle classes, this will return an instance of builtins.type.
        For oldstyle classes, it will simply return None, since there's
        no implicit metaclass there.

        :returns: The metaclass.
        :rtype: builtins.type or None
        """
        if self.newstyle:
            return builtin_lookup("type")[1][0]
        return None 
Example #24
Source File: scoped_nodes.py    From python-netsurv with MIT License 5 votes vote down vote up
def igetattr(self, name, context=None, class_context=True):
        """Infer the possible values of the given variable.

        :param name: The name of the variable to infer.
        :type name: str

        :returns: The inferred possible values.
        :rtype: iterable(NodeNG or Uninferable)
        """
        # set lookup name since this is necessary to infer on import nodes for
        # instance
        context = contextmod.copy_context(context)
        context.lookupname = name
        try:
            attr = self.getattr(name, context, class_context=class_context)[0]
            for inferred in bases._infer_stmts([attr], context, frame=self):
                # yield Uninferable object instead of descriptors when necessary
                if not isinstance(inferred, node_classes.Const) and isinstance(
                    inferred, bases.Instance
                ):
                    try:
                        inferred._proxied.getattr("__get__", context)
                    except exceptions.AttributeInferenceError:
                        yield inferred
                    else:
                        yield util.Uninferable
                else:
                    yield function_to_method(inferred, self)
        except exceptions.AttributeInferenceError as error:
            if not name.startswith("__") and self.has_dynamic_getattr(context):
                # class handle some dynamic attributes, return a Uninferable object
                yield util.Uninferable
            else:
                raise exceptions.InferenceError(
                    error.message, target=self, attribute=name, context=context
                ) 
Example #25
Source File: scoped_nodes.py    From python-netsurv with MIT License 5 votes vote down vote up
def _get_attribute_from_metaclass(self, cls, name, context):
        try:
            attrs = cls.getattr(name, context=context, class_context=True)
        except exceptions.AttributeInferenceError:
            return

        for attr in bases._infer_stmts(attrs, context, frame=cls):
            if not isinstance(attr, FunctionDef):
                yield attr
                continue

            if bases._is_property(attr):
                yield from attr.infer_call_result(self, context)
                continue
            if attr.type == "classmethod":
                # If the method is a classmethod, then it will
                # be bound to the metaclass, not to the class
                # from where the attribute is retrieved.
                # get_wrapping_class could return None, so just
                # default to the current class.
                frame = get_wrapping_class(attr) or self
                yield bases.BoundMethod(attr, frame)
            elif attr.type == "staticmethod":
                yield attr
            else:
                yield bases.BoundMethod(attr, self) 
Example #26
Source File: scoped_nodes.py    From python-netsurv with MIT License 5 votes vote down vote up
def instance_attr_ancestors(self, name, context=None):
        """Iterate over the parents that define the given name as an attribute.

        :param name: The name to find definitions for.
        :type name: str

        :returns: The parents that define the given name as
            an instance attribute.
        :rtype: iterable(NodeNG)
        """
        for astroid in self.ancestors(context=context):
            if name in astroid.instance_attrs:
                yield astroid 
Example #27
Source File: scoped_nodes.py    From python-netsurv with MIT License 5 votes vote down vote up
def basenames(self):
        """The names of the parent classes

        Names are given in the order they appear in the class definition.

        :type: list(str)
        """
        return [bnode.as_string() for bnode in self.bases] 
Example #28
Source File: scoped_nodes.py    From python-netsurv with MIT License 5 votes vote down vote up
def display_type(self):
        """A human readable type of this node.

        :returns: The type of this node.
        :rtype: str
        """
        return "Class" 
Example #29
Source File: scoped_nodes.py    From python-netsurv with MIT License 5 votes vote down vote up
def pytype(self):
        """Get the name of the type that this node represents.

        :returns: The name of the type.
        :rtype: str
        """
        if self.newstyle:
            return "%s.type" % BUILTINS
        return "%s.classobj" % BUILTINS 
Example #30
Source File: scoped_nodes.py    From python-netsurv with MIT License 5 votes vote down vote up
def blockstart_tolineno(self):
        """The line on which the beginning of this block ends.

        :type: int
        """
        if self.bases:
            return self.bases[-1].tolineno

        return self.fromlineno