2083 lines
67 KiB
Python
2083 lines
67 KiB
Python
# Copyright (c) 2009-2011, 2013-2014 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
|
|
# Copyright (c) 2013-2014, 2016 Google, Inc.
|
|
# Copyright (c) 2014-2016 Claudiu Popa <pcmanticore@gmail.com>
|
|
# Copyright (c) 2015-2016 Cara Vinson <ceridwenv@gmail.com>
|
|
# Copyright (c) 2016 Jakub Wilk <jwilk@jwilk.net>
|
|
|
|
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
|
|
# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER
|
|
|
|
"""Module for some node classes. More nodes in scoped_nodes.py
|
|
"""
|
|
|
|
import abc
|
|
import pprint
|
|
import warnings
|
|
try:
|
|
from functools import singledispatch as _singledispatch
|
|
except ImportError:
|
|
from singledispatch import singledispatch as _singledispatch
|
|
|
|
import six
|
|
|
|
from astroid import as_string
|
|
from astroid import bases
|
|
from astroid import context as contextmod
|
|
from astroid import decorators
|
|
from astroid import exceptions
|
|
from astroid import manager
|
|
from astroid import mixins
|
|
from astroid import util
|
|
|
|
|
|
BUILTINS = six.moves.builtins.__name__
|
|
MANAGER = manager.AstroidManager()
|
|
|
|
|
|
@decorators.raise_if_nothing_inferred
|
|
def unpack_infer(stmt, context=None):
|
|
"""recursively generate nodes inferred by the given statement.
|
|
If the inferred value is a list or a tuple, recurse on the elements
|
|
"""
|
|
if isinstance(stmt, (List, Tuple)):
|
|
for elt in stmt.elts:
|
|
if elt is util.Uninferable:
|
|
yield elt
|
|
continue
|
|
for inferred_elt in unpack_infer(elt, context):
|
|
yield inferred_elt
|
|
# Explicit StopIteration to return error information, see comment
|
|
# in raise_if_nothing_inferred.
|
|
raise StopIteration(dict(node=stmt, context=context))
|
|
# if inferred is a final node, return it and stop
|
|
inferred = next(stmt.infer(context))
|
|
if inferred is stmt:
|
|
yield inferred
|
|
# Explicit StopIteration to return error information, see comment
|
|
# in raise_if_nothing_inferred.
|
|
raise StopIteration(dict(node=stmt, context=context))
|
|
# else, infer recursively, except Uninferable object that should be returned as is
|
|
for inferred in stmt.infer(context):
|
|
if inferred is util.Uninferable:
|
|
yield inferred
|
|
else:
|
|
for inf_inf in unpack_infer(inferred, context):
|
|
yield inf_inf
|
|
raise StopIteration(dict(node=stmt, context=context))
|
|
|
|
|
|
def are_exclusive(stmt1, stmt2, exceptions=None): # pylint: disable=redefined-outer-name
|
|
"""return true if the two given statements are mutually exclusive
|
|
|
|
`exceptions` may be a list of exception names. If specified, discard If
|
|
branches and check one of the statement is in an exception handler catching
|
|
one of the given exceptions.
|
|
|
|
algorithm :
|
|
1) index stmt1's parents
|
|
2) climb among stmt2's parents until we find a common parent
|
|
3) if the common parent is a If or TryExcept statement, look if nodes are
|
|
in exclusive branches
|
|
"""
|
|
# index stmt1's parents
|
|
stmt1_parents = {}
|
|
children = {}
|
|
node = stmt1.parent
|
|
previous = stmt1
|
|
while node:
|
|
stmt1_parents[node] = 1
|
|
children[node] = previous
|
|
previous = node
|
|
node = node.parent
|
|
# climb among stmt2's parents until we find a common parent
|
|
node = stmt2.parent
|
|
previous = stmt2
|
|
while node:
|
|
if node in stmt1_parents:
|
|
# if the common parent is a If or TryExcept statement, look if
|
|
# nodes are in exclusive branches
|
|
if isinstance(node, If) and exceptions is None:
|
|
if (node.locate_child(previous)[1]
|
|
is not node.locate_child(children[node])[1]):
|
|
return True
|
|
elif isinstance(node, TryExcept):
|
|
c2attr, c2node = node.locate_child(previous)
|
|
c1attr, c1node = node.locate_child(children[node])
|
|
if c1node is not c2node:
|
|
first_in_body_caught_by_handlers = (
|
|
c2attr == 'handlers'
|
|
and c1attr == 'body'
|
|
and previous.catch(exceptions))
|
|
second_in_body_caught_by_handlers = (
|
|
c2attr == 'body'
|
|
and c1attr == 'handlers'
|
|
and children[node].catch(exceptions))
|
|
first_in_else_other_in_handlers = (
|
|
c2attr == 'handlers' and c1attr == 'orelse')
|
|
second_in_else_other_in_handlers = (
|
|
c2attr == 'orelse' and c1attr == 'handlers')
|
|
if any((first_in_body_caught_by_handlers,
|
|
second_in_body_caught_by_handlers,
|
|
first_in_else_other_in_handlers,
|
|
second_in_else_other_in_handlers)):
|
|
return True
|
|
elif c2attr == 'handlers' and c1attr == 'handlers':
|
|
return previous is not children[node]
|
|
return False
|
|
previous = node
|
|
node = node.parent
|
|
return False
|
|
|
|
|
|
# getitem() helpers.
|
|
|
|
_SLICE_SENTINEL = object()
|
|
|
|
|
|
def _slice_value(index, context=None):
|
|
"""Get the value of the given slice index."""
|
|
|
|
if isinstance(index, Const):
|
|
if isinstance(index.value, (int, type(None))):
|
|
return index.value
|
|
elif index is None:
|
|
return None
|
|
else:
|
|
# Try to infer what the index actually is.
|
|
# Since we can't return all the possible values,
|
|
# we'll stop at the first possible value.
|
|
try:
|
|
inferred = next(index.infer(context=context))
|
|
except exceptions.InferenceError:
|
|
pass
|
|
else:
|
|
if isinstance(inferred, Const):
|
|
if isinstance(inferred.value, (int, type(None))):
|
|
return inferred.value
|
|
|
|
# Use a sentinel, because None can be a valid
|
|
# value that this function can return,
|
|
# as it is the case for unspecified bounds.
|
|
return _SLICE_SENTINEL
|
|
|
|
|
|
def _infer_slice(node, context=None):
|
|
lower = _slice_value(node.lower, context)
|
|
upper = _slice_value(node.upper, context)
|
|
step = _slice_value(node.step, context)
|
|
if all(elem is not _SLICE_SENTINEL for elem in (lower, upper, step)):
|
|
return slice(lower, upper, step)
|
|
|
|
raise exceptions.AstroidTypeError(
|
|
message='Could not infer slice used in subscript',
|
|
node=node, index=node.parent, context=context)
|
|
|
|
|
|
def _container_getitem(instance, elts, index, context=None):
|
|
"""Get a slice or an item, using the given *index*, for the given sequence."""
|
|
try:
|
|
if isinstance(index, Slice):
|
|
index_slice = _infer_slice(index, context=context)
|
|
new_cls = instance.__class__()
|
|
new_cls.elts = elts[index_slice]
|
|
new_cls.parent = instance.parent
|
|
return new_cls
|
|
elif isinstance(index, Const):
|
|
return elts[index.value]
|
|
except IndexError:
|
|
util.reraise(exceptions.AstroidIndexError(
|
|
message='Index {index!s} out of range',
|
|
node=instance, index=index, context=context))
|
|
except TypeError as exc:
|
|
util.reraise(exceptions.AstroidTypeError(
|
|
message='Type error {error!r}', error=exc,
|
|
node=instance, index=index, context=context))
|
|
|
|
raise exceptions.AstroidTypeError(
|
|
'Could not use %s as subscript index' % index
|
|
)
|
|
|
|
|
|
class NodeNG(object):
|
|
"""Base Class for all Astroid node classes.
|
|
|
|
It represents a node of the new abstract syntax tree.
|
|
"""
|
|
is_statement = False
|
|
optional_assign = False # True for For (and for Comprehension if py <3.0)
|
|
is_function = False # True for FunctionDef nodes
|
|
# attributes below are set by the builder module or by raw factories
|
|
lineno = None
|
|
col_offset = None
|
|
# parent node in the tree
|
|
parent = None
|
|
# attributes containing child node(s) redefined in most concrete classes:
|
|
_astroid_fields = ()
|
|
# attributes containing non-nodes:
|
|
_other_fields = ()
|
|
# attributes containing AST-dependent fields:
|
|
_other_other_fields = ()
|
|
# instance specific inference function infer(node, context)
|
|
_explicit_inference = None
|
|
|
|
def __init__(self, lineno=None, col_offset=None, parent=None):
|
|
self.lineno = lineno
|
|
self.col_offset = col_offset
|
|
self.parent = parent
|
|
|
|
def infer(self, context=None, **kwargs):
|
|
"""main interface to the interface system, return a generator on inferred
|
|
values.
|
|
|
|
If the instance has some explicit inference function set, it will be
|
|
called instead of the default interface.
|
|
"""
|
|
if self._explicit_inference is not None:
|
|
# explicit_inference is not bound, give it self explicitly
|
|
try:
|
|
# pylint: disable=not-callable
|
|
return self._explicit_inference(self, context, **kwargs)
|
|
except exceptions.UseInferenceDefault:
|
|
pass
|
|
|
|
if not context:
|
|
return self._infer(context, **kwargs)
|
|
|
|
key = (self, context.lookupname,
|
|
context.callcontext, context.boundnode)
|
|
if key in context.inferred:
|
|
return iter(context.inferred[key])
|
|
|
|
return context.cache_generator(key, self._infer(context, **kwargs))
|
|
|
|
def _repr_name(self):
|
|
"""return self.name or self.attrname or '' for nice representation"""
|
|
return getattr(self, 'name', getattr(self, 'attrname', ''))
|
|
|
|
def __str__(self):
|
|
rname = self._repr_name()
|
|
cname = type(self).__name__
|
|
if rname:
|
|
string = '%(cname)s.%(rname)s(%(fields)s)'
|
|
alignment = len(cname) + len(rname) + 2
|
|
else:
|
|
string = '%(cname)s(%(fields)s)'
|
|
alignment = len(cname) + 1
|
|
result = []
|
|
for field in self._other_fields + self._astroid_fields:
|
|
value = getattr(self, field)
|
|
width = 80 - len(field) - alignment
|
|
lines = pprint.pformat(value, indent=2,
|
|
width=width).splitlines(True)
|
|
|
|
inner = [lines[0]]
|
|
for line in lines[1:]:
|
|
inner.append(' ' * alignment + line)
|
|
result.append('%s=%s' % (field, ''.join(inner)))
|
|
|
|
return string % {'cname': cname,
|
|
'rname': rname,
|
|
'fields': (',\n' + ' ' * alignment).join(result)}
|
|
|
|
def __repr__(self):
|
|
rname = self._repr_name()
|
|
if rname:
|
|
string = '<%(cname)s.%(rname)s l.%(lineno)s at 0x%(id)x>'
|
|
else:
|
|
string = '<%(cname)s l.%(lineno)s at 0x%(id)x>'
|
|
return string % {'cname': type(self).__name__,
|
|
'rname': rname,
|
|
'lineno': self.fromlineno,
|
|
'id': id(self)}
|
|
|
|
def accept(self, visitor):
|
|
func = getattr(visitor, "visit_" + self.__class__.__name__.lower())
|
|
return func(self)
|
|
|
|
def get_children(self):
|
|
for field in self._astroid_fields:
|
|
attr = getattr(self, field)
|
|
if attr is None:
|
|
continue
|
|
if isinstance(attr, (list, tuple)):
|
|
for elt in attr:
|
|
yield elt
|
|
else:
|
|
yield attr
|
|
|
|
def last_child(self):
|
|
"""an optimized version of list(get_children())[-1]"""
|
|
for field in self._astroid_fields[::-1]:
|
|
attr = getattr(self, field)
|
|
if not attr: # None or empty listy / tuple
|
|
continue
|
|
if isinstance(attr, (list, tuple)):
|
|
return attr[-1]
|
|
|
|
return attr
|
|
return None
|
|
|
|
def parent_of(self, node):
|
|
"""return true if i'm a parent of the given node"""
|
|
parent = node.parent
|
|
while parent is not None:
|
|
if self is parent:
|
|
return True
|
|
parent = parent.parent
|
|
return False
|
|
|
|
def statement(self):
|
|
"""return the first parent node marked as statement node"""
|
|
if self.is_statement:
|
|
return self
|
|
return self.parent.statement()
|
|
|
|
def frame(self):
|
|
"""return the first parent frame node (i.e. Module, FunctionDef or
|
|
ClassDef)
|
|
|
|
"""
|
|
return self.parent.frame()
|
|
|
|
def scope(self):
|
|
"""return the first node defining a new scope (i.e. Module,
|
|
FunctionDef, ClassDef, Lambda but also GenExpr)
|
|
|
|
"""
|
|
return self.parent.scope()
|
|
|
|
def root(self):
|
|
"""return the root node of the tree, (i.e. a Module)"""
|
|
if self.parent:
|
|
return self.parent.root()
|
|
return self
|
|
|
|
def child_sequence(self, child):
|
|
"""search for the right sequence where the child lies in"""
|
|
for field in self._astroid_fields:
|
|
node_or_sequence = getattr(self, field)
|
|
if node_or_sequence is child:
|
|
return [node_or_sequence]
|
|
# /!\ compiler.ast Nodes have an __iter__ walking over child nodes
|
|
if (isinstance(node_or_sequence, (tuple, list))
|
|
and child in node_or_sequence):
|
|
return node_or_sequence
|
|
|
|
msg = 'Could not find %s in %s\'s children'
|
|
raise exceptions.AstroidError(msg % (repr(child), repr(self)))
|
|
|
|
def locate_child(self, child):
|
|
"""return a 2-uple (child attribute name, sequence or node)"""
|
|
for field in self._astroid_fields:
|
|
node_or_sequence = getattr(self, field)
|
|
# /!\ compiler.ast Nodes have an __iter__ walking over child nodes
|
|
if child is node_or_sequence:
|
|
return field, child
|
|
if isinstance(node_or_sequence, (tuple, list)) and child in node_or_sequence:
|
|
return field, node_or_sequence
|
|
msg = 'Could not find %s in %s\'s children'
|
|
raise exceptions.AstroidError(msg % (repr(child), repr(self)))
|
|
# FIXME : should we merge child_sequence and locate_child ? locate_child
|
|
# is only used in are_exclusive, child_sequence one time in pylint.
|
|
|
|
def next_sibling(self):
|
|
"""return the next sibling statement"""
|
|
return self.parent.next_sibling()
|
|
|
|
def previous_sibling(self):
|
|
"""return the previous sibling statement"""
|
|
return self.parent.previous_sibling()
|
|
|
|
def nearest(self, nodes):
|
|
"""return the node which is the nearest before this one in the
|
|
given list of nodes
|
|
"""
|
|
myroot = self.root()
|
|
mylineno = self.fromlineno
|
|
nearest = None, 0
|
|
for node in nodes:
|
|
assert node.root() is myroot, \
|
|
'nodes %s and %s are not from the same module' % (self, node)
|
|
lineno = node.fromlineno
|
|
if node.fromlineno > mylineno:
|
|
break
|
|
if lineno > nearest[1]:
|
|
nearest = node, lineno
|
|
# FIXME: raise an exception if nearest is None ?
|
|
return nearest[0]
|
|
|
|
# these are lazy because they're relatively expensive to compute for every
|
|
# single node, and they rarely get looked at
|
|
|
|
@decorators.cachedproperty
|
|
def fromlineno(self):
|
|
if self.lineno is None:
|
|
return self._fixed_source_line()
|
|
|
|
return self.lineno
|
|
|
|
@decorators.cachedproperty
|
|
def tolineno(self):
|
|
if not self._astroid_fields:
|
|
# can't have children
|
|
lastchild = None
|
|
else:
|
|
lastchild = self.last_child()
|
|
if lastchild is None:
|
|
return self.fromlineno
|
|
|
|
return lastchild.tolineno
|
|
|
|
def _fixed_source_line(self):
|
|
"""return the line number where the given node appears
|
|
|
|
we need this method since not all nodes have the lineno attribute
|
|
correctly set...
|
|
"""
|
|
line = self.lineno
|
|
_node = self
|
|
try:
|
|
while line is None:
|
|
_node = next(_node.get_children())
|
|
line = _node.lineno
|
|
except StopIteration:
|
|
_node = self.parent
|
|
while _node and line is None:
|
|
line = _node.lineno
|
|
_node = _node.parent
|
|
return line
|
|
|
|
def block_range(self, lineno):
|
|
"""handle block line numbers range for non block opening statements
|
|
"""
|
|
return lineno, self.tolineno
|
|
|
|
def set_local(self, name, stmt):
|
|
"""delegate to a scoped parent handling a locals dictionary"""
|
|
self.parent.set_local(name, stmt)
|
|
|
|
def nodes_of_class(self, klass, skip_klass=None):
|
|
"""return an iterator on nodes which are instance of the given class(es)
|
|
|
|
klass may be a class object or a tuple of class objects
|
|
"""
|
|
if isinstance(self, klass):
|
|
yield self
|
|
for child_node in self.get_children():
|
|
if skip_klass is not None and isinstance(child_node, skip_klass):
|
|
continue
|
|
for matching in child_node.nodes_of_class(klass, skip_klass):
|
|
yield matching
|
|
|
|
def _infer_name(self, frame, name):
|
|
# overridden for ImportFrom, Import, Global, TryExcept and Arguments
|
|
return None
|
|
|
|
def _infer(self, context=None):
|
|
"""we don't know how to resolve a statement by default"""
|
|
# this method is overridden by most concrete classes
|
|
raise exceptions.InferenceError('No inference function for {node!r}.',
|
|
node=self, context=context)
|
|
|
|
def inferred(self):
|
|
'''return list of inferred values for a more simple inference usage'''
|
|
return list(self.infer())
|
|
|
|
def infered(self):
|
|
warnings.warn('%s.infered() is deprecated and slated for removal '
|
|
'in astroid 2.0, use %s.inferred() instead.'
|
|
% (type(self).__name__, type(self).__name__),
|
|
PendingDeprecationWarning, stacklevel=2)
|
|
return self.inferred()
|
|
|
|
def instantiate_class(self):
|
|
"""instantiate a node if it is a ClassDef node, else return self"""
|
|
return self
|
|
|
|
def has_base(self, node):
|
|
return False
|
|
|
|
def callable(self):
|
|
return False
|
|
|
|
def eq(self, value):
|
|
return False
|
|
|
|
def as_string(self):
|
|
return as_string.to_code(self)
|
|
|
|
def repr_tree(self, ids=False, include_linenos=False,
|
|
ast_state=False, indent=' ', max_depth=0, max_width=80):
|
|
"""Returns a string representation of the AST from this node.
|
|
|
|
:param ids: If true, includes the ids with the node type names.
|
|
|
|
:param include_linenos: If true, includes the line numbers and
|
|
column offsets.
|
|
|
|
:param ast_state: If true, includes information derived from
|
|
the whole AST like local and global variables.
|
|
|
|
:param indent: A string to use to indent the output string.
|
|
|
|
:param max_depth: If set to a positive integer, won't return
|
|
nodes deeper than max_depth in the string.
|
|
|
|
:param max_width: Only positive integer values are valid, the
|
|
default is 80. Attempts to format the output string to stay
|
|
within max_width characters, but can exceed it under some
|
|
circumstances.
|
|
"""
|
|
@_singledispatch
|
|
def _repr_tree(node, result, done, cur_indent='', depth=1):
|
|
"""Outputs a representation of a non-tuple/list, non-node that's
|
|
contained within an AST, including strings.
|
|
"""
|
|
lines = pprint.pformat(node,
|
|
width=max(max_width - len(cur_indent),
|
|
1)).splitlines(True)
|
|
result.append(lines[0])
|
|
result.extend([cur_indent + line for line in lines[1:]])
|
|
return len(lines) != 1
|
|
|
|
# pylint: disable=unused-variable; doesn't understand singledispatch
|
|
@_repr_tree.register(tuple)
|
|
@_repr_tree.register(list)
|
|
def _repr_seq(node, result, done, cur_indent='', depth=1):
|
|
"""Outputs a representation of a sequence that's contained within an AST."""
|
|
cur_indent += indent
|
|
result.append('[')
|
|
if not node:
|
|
broken = False
|
|
elif len(node) == 1:
|
|
broken = _repr_tree(node[0], result, done, cur_indent, depth)
|
|
elif len(node) == 2:
|
|
broken = _repr_tree(node[0], result, done, cur_indent, depth)
|
|
if not broken:
|
|
result.append(', ')
|
|
else:
|
|
result.append(',\n')
|
|
result.append(cur_indent)
|
|
broken = (_repr_tree(node[1], result, done, cur_indent, depth)
|
|
or broken)
|
|
else:
|
|
result.append('\n')
|
|
result.append(cur_indent)
|
|
for child in node[:-1]:
|
|
_repr_tree(child, result, done, cur_indent, depth)
|
|
result.append(',\n')
|
|
result.append(cur_indent)
|
|
_repr_tree(node[-1], result, done, cur_indent, depth)
|
|
broken = True
|
|
result.append(']')
|
|
return broken
|
|
|
|
# pylint: disable=unused-variable; doesn't understand singledispatch
|
|
@_repr_tree.register(NodeNG)
|
|
def _repr_node(node, result, done, cur_indent='', depth=1):
|
|
"""Outputs a strings representation of an astroid node."""
|
|
if node in done:
|
|
result.append(indent + '<Recursion on %s with id=%s' %
|
|
(type(node).__name__, id(node)))
|
|
return False
|
|
else:
|
|
done.add(node)
|
|
if max_depth and depth > max_depth:
|
|
result.append('...')
|
|
return False
|
|
depth += 1
|
|
cur_indent += indent
|
|
if ids:
|
|
result.append('%s<0x%x>(\n' % (type(node).__name__, id(node)))
|
|
else:
|
|
result.append('%s(' % type(node).__name__)
|
|
fields = []
|
|
if include_linenos:
|
|
fields.extend(('lineno', 'col_offset'))
|
|
fields.extend(node._other_fields)
|
|
fields.extend(node._astroid_fields)
|
|
if ast_state:
|
|
fields.extend(node._other_other_fields)
|
|
if not fields:
|
|
broken = False
|
|
elif len(fields) == 1:
|
|
result.append('%s=' % fields[0])
|
|
broken = _repr_tree(getattr(node, fields[0]), result, done,
|
|
cur_indent, depth)
|
|
else:
|
|
result.append('\n')
|
|
result.append(cur_indent)
|
|
for field in fields[:-1]:
|
|
result.append('%s=' % field)
|
|
_repr_tree(getattr(node, field), result, done, cur_indent,
|
|
depth)
|
|
result.append(',\n')
|
|
result.append(cur_indent)
|
|
result.append('%s=' % fields[-1])
|
|
_repr_tree(getattr(node, fields[-1]), result, done, cur_indent,
|
|
depth)
|
|
broken = True
|
|
result.append(')')
|
|
return broken
|
|
|
|
result = []
|
|
_repr_tree(self, result, set())
|
|
return ''.join(result)
|
|
|
|
def bool_value(self):
|
|
"""Determine the bool value of this node
|
|
|
|
The boolean value of a node can have three
|
|
possible values:
|
|
|
|
* False. For instance, empty data structures,
|
|
False, empty strings, instances which return
|
|
explicitly False from the __nonzero__ / __bool__
|
|
method.
|
|
* True. Most of constructs are True by default:
|
|
classes, functions, modules etc
|
|
* Uninferable: the inference engine is uncertain of the
|
|
node's value.
|
|
"""
|
|
return util.Uninferable
|
|
|
|
|
|
class Statement(NodeNG):
|
|
"""Statement node adding a few attributes"""
|
|
is_statement = True
|
|
|
|
def next_sibling(self):
|
|
"""return the next sibling statement"""
|
|
stmts = self.parent.child_sequence(self)
|
|
index = stmts.index(self)
|
|
try:
|
|
return stmts[index +1]
|
|
except IndexError:
|
|
pass
|
|
|
|
def previous_sibling(self):
|
|
"""return the previous sibling statement"""
|
|
stmts = self.parent.child_sequence(self)
|
|
index = stmts.index(self)
|
|
if index >= 1:
|
|
return stmts[index -1]
|
|
|
|
|
|
|
|
@six.add_metaclass(abc.ABCMeta)
|
|
class _BaseContainer(mixins.ParentAssignTypeMixin,
|
|
NodeNG, bases.Instance):
|
|
"""Base class for Set, FrozenSet, Tuple and List."""
|
|
|
|
_astroid_fields = ('elts',)
|
|
|
|
def __init__(self, lineno=None, col_offset=None, parent=None):
|
|
self.elts = []
|
|
super(_BaseContainer, self).__init__(lineno, col_offset, parent)
|
|
|
|
def postinit(self, elts):
|
|
self.elts = elts
|
|
|
|
@classmethod
|
|
def from_constants(cls, elts=None):
|
|
node = cls()
|
|
if elts is None:
|
|
node.elts = []
|
|
else:
|
|
node.elts = [const_factory(e) for e in elts]
|
|
return node
|
|
|
|
def itered(self):
|
|
return self.elts
|
|
|
|
def bool_value(self):
|
|
return bool(self.elts)
|
|
|
|
@abc.abstractmethod
|
|
def pytype(self):
|
|
pass
|
|
|
|
|
|
class LookupMixIn(object):
|
|
"""Mixin looking up a name in the right scope
|
|
"""
|
|
|
|
def lookup(self, name):
|
|
"""lookup a variable name
|
|
|
|
return the 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)
|
|
|
|
The lookup is starting from self's scope. If self is not a frame itself
|
|
and the name is found in the inner frame locals, statements will be
|
|
filtered to remove ignorable statements according to self's location
|
|
"""
|
|
return self.scope().scope_lookup(self, name)
|
|
|
|
def ilookup(self, name):
|
|
"""inferred lookup
|
|
|
|
return an iterator on inferred values of the statements returned by
|
|
the lookup method
|
|
"""
|
|
frame, stmts = self.lookup(name)
|
|
context = contextmod.InferenceContext()
|
|
return bases._infer_stmts(stmts, context, frame)
|
|
|
|
def _filter_stmts(self, stmts, frame, offset):
|
|
"""filter statements to remove ignorable statements.
|
|
|
|
If self is not a frame itself and the name is found in the inner
|
|
frame locals, statements will be filtered to remove ignorable
|
|
statements according to self's location
|
|
"""
|
|
# if offset == -1, my actual frame is not the inner frame but its parent
|
|
#
|
|
# class A(B): pass
|
|
#
|
|
# we need this to resolve B correctly
|
|
if offset == -1:
|
|
myframe = self.frame().parent.frame()
|
|
else:
|
|
myframe = self.frame()
|
|
# If the frame of this node is the same as the statement
|
|
# of this node, then the node is part of a class or
|
|
# a function definition and the frame of this node should be the
|
|
# the upper frame, not the frame of the definition.
|
|
# For more information why this is important,
|
|
# see Pylint issue #295.
|
|
# For example, for 'b', the statement is the same
|
|
# as the frame / scope:
|
|
#
|
|
# def test(b=1):
|
|
# ...
|
|
|
|
if self.statement() is myframe and myframe.parent:
|
|
myframe = myframe.parent.frame()
|
|
mystmt = self.statement()
|
|
# line filtering if we are in the same frame
|
|
#
|
|
# take care node may be missing lineno information (this is the case for
|
|
# nodes inserted for living objects)
|
|
if myframe is frame and mystmt.fromlineno is not None:
|
|
assert mystmt.fromlineno is not None, mystmt
|
|
mylineno = mystmt.fromlineno + offset
|
|
else:
|
|
# disabling lineno filtering
|
|
mylineno = 0
|
|
_stmts = []
|
|
_stmt_parents = []
|
|
for node in stmts:
|
|
stmt = node.statement()
|
|
# line filtering is on and we have reached our location, break
|
|
if mylineno > 0 and stmt.fromlineno > mylineno:
|
|
break
|
|
assert hasattr(node, 'assign_type'), (node, node.scope(),
|
|
node.scope().locals)
|
|
assign_type = node.assign_type()
|
|
if node.has_base(self):
|
|
break
|
|
|
|
_stmts, done = assign_type._get_filtered_stmts(self, node, _stmts, mystmt)
|
|
if done:
|
|
break
|
|
|
|
optional_assign = assign_type.optional_assign
|
|
if optional_assign and assign_type.parent_of(self):
|
|
# we are inside a loop, loop var assignment is hiding previous
|
|
# assignment
|
|
_stmts = [node]
|
|
_stmt_parents = [stmt.parent]
|
|
continue
|
|
|
|
# XXX comment various branches below!!!
|
|
try:
|
|
pindex = _stmt_parents.index(stmt.parent)
|
|
except ValueError:
|
|
pass
|
|
else:
|
|
# we got a parent index, this means the currently visited node
|
|
# is at the same block level as a previously visited node
|
|
if _stmts[pindex].assign_type().parent_of(assign_type):
|
|
# both statements are not at the same block level
|
|
continue
|
|
# if currently visited node is following previously considered
|
|
# assignment and both are not exclusive, we can drop the
|
|
# previous one. For instance in the following code ::
|
|
#
|
|
# if a:
|
|
# x = 1
|
|
# else:
|
|
# x = 2
|
|
# print x
|
|
#
|
|
# we can't remove neither x = 1 nor x = 2 when looking for 'x'
|
|
# of 'print x'; while in the following ::
|
|
#
|
|
# x = 1
|
|
# x = 2
|
|
# print x
|
|
#
|
|
# we can remove x = 1 when we see x = 2
|
|
#
|
|
# moreover, on loop assignment types, assignment won't
|
|
# necessarily be done if the loop has no iteration, so we don't
|
|
# want to clear previous assignments if any (hence the test on
|
|
# optional_assign)
|
|
if not (optional_assign or are_exclusive(_stmts[pindex], node)):
|
|
del _stmt_parents[pindex]
|
|
del _stmts[pindex]
|
|
if isinstance(node, AssignName):
|
|
if not optional_assign and stmt.parent is mystmt.parent:
|
|
_stmts = []
|
|
_stmt_parents = []
|
|
elif isinstance(node, DelName):
|
|
_stmts = []
|
|
_stmt_parents = []
|
|
continue
|
|
if not are_exclusive(self, node):
|
|
_stmts.append(node)
|
|
_stmt_parents.append(stmt.parent)
|
|
return _stmts
|
|
|
|
|
|
# Name classes
|
|
|
|
class AssignName(LookupMixIn, mixins.ParentAssignTypeMixin, NodeNG):
|
|
"""class representing an AssignName node"""
|
|
_other_fields = ('name',)
|
|
|
|
def __init__(self, name=None, lineno=None, col_offset=None, parent=None):
|
|
self.name = name
|
|
super(AssignName, self).__init__(lineno, col_offset, parent)
|
|
|
|
|
|
class DelName(LookupMixIn, mixins.ParentAssignTypeMixin, NodeNG):
|
|
"""class representing a DelName node"""
|
|
_other_fields = ('name',)
|
|
|
|
def __init__(self, name=None, lineno=None, col_offset=None, parent=None):
|
|
self.name = name
|
|
super(DelName, self).__init__(lineno, col_offset, parent)
|
|
|
|
|
|
class Name(LookupMixIn, NodeNG):
|
|
"""class representing a Name node"""
|
|
_other_fields = ('name',)
|
|
|
|
def __init__(self, name=None, lineno=None, col_offset=None, parent=None):
|
|
self.name = name
|
|
super(Name, self).__init__(lineno, col_offset, parent)
|
|
|
|
|
|
class Arguments(mixins.AssignTypeMixin, NodeNG):
|
|
"""class representing an Arguments node"""
|
|
if six.PY3:
|
|
# Python 3.4+ uses a different approach regarding annotations,
|
|
# each argument is a new class, _ast.arg, which exposes an
|
|
# 'annotation' attribute. In astroid though, arguments are exposed
|
|
# as is in the Arguments node and the only way to expose annotations
|
|
# is by using something similar with Python 3.3:
|
|
# - we expose 'varargannotation' and 'kwargannotation' of annotations
|
|
# of varargs and kwargs.
|
|
# - we expose 'annotation', a list with annotations for
|
|
# for each normal argument. If an argument doesn't have an
|
|
# annotation, its value will be None.
|
|
|
|
_astroid_fields = ('args', 'defaults', 'kwonlyargs',
|
|
'kw_defaults', 'annotations', 'varargannotation',
|
|
'kwargannotation', 'kwonlyargs_annotations')
|
|
varargannotation = None
|
|
kwargannotation = None
|
|
else:
|
|
_astroid_fields = ('args', 'defaults', 'kwonlyargs', 'kw_defaults')
|
|
_other_fields = ('vararg', 'kwarg')
|
|
|
|
def __init__(self, vararg=None, kwarg=None, parent=None):
|
|
super(Arguments, self).__init__(parent=parent)
|
|
self.vararg = vararg
|
|
self.kwarg = kwarg
|
|
self.args = []
|
|
self.defaults = []
|
|
self.kwonlyargs = []
|
|
self.kw_defaults = []
|
|
self.annotations = []
|
|
self.kwonlyargs_annotations = []
|
|
|
|
def postinit(self, args, defaults, kwonlyargs, kw_defaults,
|
|
annotations,
|
|
kwonlyargs_annotations=None,
|
|
varargannotation=None,
|
|
kwargannotation=None):
|
|
self.args = args
|
|
self.defaults = defaults
|
|
self.kwonlyargs = kwonlyargs
|
|
self.kw_defaults = kw_defaults
|
|
self.annotations = annotations
|
|
self.kwonlyargs_annotations = kwonlyargs_annotations
|
|
self.varargannotation = varargannotation
|
|
self.kwargannotation = kwargannotation
|
|
|
|
def _infer_name(self, frame, name):
|
|
if self.parent is frame:
|
|
return name
|
|
return None
|
|
|
|
@decorators.cachedproperty
|
|
def fromlineno(self):
|
|
lineno = super(Arguments, self).fromlineno
|
|
return max(lineno, self.parent.fromlineno or 0)
|
|
|
|
def format_args(self):
|
|
"""return arguments formatted as string"""
|
|
result = []
|
|
if self.args:
|
|
result.append(
|
|
_format_args(self.args, self.defaults,
|
|
getattr(self, 'annotations', None))
|
|
)
|
|
if self.vararg:
|
|
result.append('*%s' % self.vararg)
|
|
if self.kwonlyargs:
|
|
if not self.vararg:
|
|
result.append('*')
|
|
result.append(_format_args(
|
|
self.kwonlyargs,
|
|
self.kw_defaults,
|
|
self.kwonlyargs_annotations
|
|
))
|
|
if self.kwarg:
|
|
result.append('**%s' % self.kwarg)
|
|
return ', '.join(result)
|
|
|
|
def default_value(self, argname):
|
|
"""return the default value for an argument
|
|
|
|
:raise `NoDefault`: if there is no default value defined
|
|
"""
|
|
i = _find_arg(argname, self.args)[0]
|
|
if i is not None:
|
|
idx = i - (len(self.args) - len(self.defaults))
|
|
if idx >= 0:
|
|
return self.defaults[idx]
|
|
i = _find_arg(argname, self.kwonlyargs)[0]
|
|
if i is not None and self.kw_defaults[i] is not None:
|
|
return self.kw_defaults[i]
|
|
raise exceptions.NoDefault(func=self.parent, name=argname)
|
|
|
|
def is_argument(self, name):
|
|
"""return True if the name is defined in arguments"""
|
|
if name == self.vararg:
|
|
return True
|
|
if name == self.kwarg:
|
|
return True
|
|
return (self.find_argname(name, True)[1] is not None or
|
|
self.kwonlyargs and _find_arg(name, self.kwonlyargs, True)[1] is not None)
|
|
|
|
def find_argname(self, argname, rec=False):
|
|
"""return index and Name node with given name"""
|
|
if self.args: # self.args may be None in some cases (builtin function)
|
|
return _find_arg(argname, self.args, rec)
|
|
return None, None
|
|
|
|
def get_children(self):
|
|
"""override get_children to skip over None elements in kw_defaults"""
|
|
for child in super(Arguments, self).get_children():
|
|
if child is not None:
|
|
yield child
|
|
|
|
|
|
def _find_arg(argname, args, rec=False):
|
|
for i, arg in enumerate(args):
|
|
if isinstance(arg, Tuple):
|
|
if rec:
|
|
found = _find_arg(argname, arg.elts)
|
|
if found[0] is not None:
|
|
return found
|
|
elif arg.name == argname:
|
|
return i, arg
|
|
return None, None
|
|
|
|
|
|
def _format_args(args, defaults=None, annotations=None):
|
|
values = []
|
|
if args is None:
|
|
return ''
|
|
if annotations is None:
|
|
annotations = []
|
|
if defaults is not None:
|
|
default_offset = len(args) - len(defaults)
|
|
packed = six.moves.zip_longest(args, annotations)
|
|
for i, (arg, annotation) in enumerate(packed):
|
|
if isinstance(arg, Tuple):
|
|
values.append('(%s)' % _format_args(arg.elts))
|
|
else:
|
|
argname = arg.name
|
|
if annotation is not None:
|
|
argname += ':' + annotation.as_string()
|
|
values.append(argname)
|
|
|
|
if defaults is not None and i >= default_offset:
|
|
if defaults[i-default_offset] is not None:
|
|
values[-1] += '=' + defaults[i-default_offset].as_string()
|
|
return ', '.join(values)
|
|
|
|
|
|
class AssignAttr(mixins.ParentAssignTypeMixin, NodeNG):
|
|
"""class representing an AssignAttr node"""
|
|
_astroid_fields = ('expr',)
|
|
_other_fields = ('attrname',)
|
|
expr = None
|
|
|
|
def __init__(self, attrname=None, lineno=None, col_offset=None, parent=None):
|
|
self.attrname = attrname
|
|
super(AssignAttr, self).__init__(lineno, col_offset, parent)
|
|
|
|
def postinit(self, expr=None):
|
|
self.expr = expr
|
|
|
|
|
|
class Assert(Statement):
|
|
"""class representing an Assert node"""
|
|
_astroid_fields = ('test', 'fail',)
|
|
test = None
|
|
fail = None
|
|
|
|
def postinit(self, test=None, fail=None):
|
|
self.fail = fail
|
|
self.test = test
|
|
|
|
|
|
class Assign(mixins.AssignTypeMixin, Statement):
|
|
"""class representing an Assign node"""
|
|
_astroid_fields = ('targets', 'value',)
|
|
targets = None
|
|
value = None
|
|
|
|
def postinit(self, targets=None, value=None):
|
|
self.targets = targets
|
|
self.value = value
|
|
|
|
|
|
class AnnAssign(mixins.AssignTypeMixin, Statement):
|
|
"""Class representing an AnnAssign node"""
|
|
|
|
_astroid_fields = ('target', 'annotation', 'value',)
|
|
_other_fields = ('simple',)
|
|
target = None
|
|
annotation = None
|
|
value = None
|
|
simple = None
|
|
|
|
def postinit(self, target, annotation, simple, value=None):
|
|
self.target = target
|
|
self.annotation = annotation
|
|
self.value = value
|
|
self.simple = simple
|
|
|
|
|
|
class AugAssign(mixins.AssignTypeMixin, Statement):
|
|
"""class representing an AugAssign node"""
|
|
_astroid_fields = ('target', 'value')
|
|
_other_fields = ('op',)
|
|
target = None
|
|
value = None
|
|
|
|
def __init__(self, op=None, lineno=None, col_offset=None, parent=None):
|
|
self.op = op
|
|
super(AugAssign, self).__init__(lineno, col_offset, parent)
|
|
|
|
def postinit(self, target=None, value=None):
|
|
self.target = target
|
|
self.value = value
|
|
|
|
# This is set by inference.py
|
|
def _infer_augassign(self, context=None):
|
|
raise NotImplementedError
|
|
|
|
def type_errors(self, context=None):
|
|
"""Return a list of TypeErrors which can occur during inference.
|
|
|
|
Each TypeError is represented by a :class:`BadBinaryOperationMessage`,
|
|
which holds the original exception.
|
|
"""
|
|
try:
|
|
results = self._infer_augassign(context=context)
|
|
return [result for result in results
|
|
if isinstance(result, util.BadBinaryOperationMessage)]
|
|
except exceptions.InferenceError:
|
|
return []
|
|
|
|
|
|
class Repr(NodeNG):
|
|
"""class representing a Repr node"""
|
|
_astroid_fields = ('value',)
|
|
value = None
|
|
|
|
def postinit(self, value=None):
|
|
self.value = value
|
|
|
|
|
|
class BinOp(NodeNG):
|
|
"""class representing a BinOp node"""
|
|
_astroid_fields = ('left', 'right')
|
|
_other_fields = ('op',)
|
|
left = None
|
|
right = None
|
|
|
|
def __init__(self, op=None, lineno=None, col_offset=None, parent=None):
|
|
self.op = op
|
|
super(BinOp, self).__init__(lineno, col_offset, parent)
|
|
|
|
def postinit(self, left=None, right=None):
|
|
self.left = left
|
|
self.right = right
|
|
|
|
# This is set by inference.py
|
|
def _infer_binop(self, context=None):
|
|
raise NotImplementedError
|
|
|
|
def type_errors(self, context=None):
|
|
"""Return a list of TypeErrors which can occur during inference.
|
|
|
|
Each TypeError is represented by a :class:`BadBinaryOperationMessage`,
|
|
which holds the original exception.
|
|
"""
|
|
try:
|
|
results = self._infer_binop(context=context)
|
|
return [result for result in results
|
|
if isinstance(result, util.BadBinaryOperationMessage)]
|
|
except exceptions.InferenceError:
|
|
return []
|
|
|
|
|
|
class BoolOp(NodeNG):
|
|
"""class representing a BoolOp node"""
|
|
_astroid_fields = ('values',)
|
|
_other_fields = ('op',)
|
|
values = None
|
|
|
|
def __init__(self, op=None, lineno=None, col_offset=None, parent=None):
|
|
self.op = op
|
|
super(BoolOp, self).__init__(lineno, col_offset, parent)
|
|
|
|
def postinit(self, values=None):
|
|
self.values = values
|
|
|
|
|
|
class Break(Statement):
|
|
"""class representing a Break node"""
|
|
|
|
|
|
class Call(NodeNG):
|
|
"""class representing a Call node"""
|
|
_astroid_fields = ('func', 'args', 'keywords')
|
|
func = None
|
|
args = None
|
|
keywords = None
|
|
|
|
def postinit(self, func=None, args=None, keywords=None):
|
|
self.func = func
|
|
self.args = args
|
|
self.keywords = keywords
|
|
|
|
@property
|
|
def starargs(self):
|
|
args = self.args or []
|
|
return [arg for arg in args if isinstance(arg, Starred)]
|
|
|
|
@property
|
|
def kwargs(self):
|
|
keywords = self.keywords or []
|
|
return [keyword for keyword in keywords if keyword.arg is None]
|
|
|
|
|
|
class Compare(NodeNG):
|
|
"""class representing a Compare node"""
|
|
_astroid_fields = ('left', 'ops',)
|
|
left = None
|
|
ops = None
|
|
|
|
def postinit(self, left=None, ops=None):
|
|
self.left = left
|
|
self.ops = ops
|
|
|
|
def get_children(self):
|
|
"""override get_children for tuple fields"""
|
|
yield self.left
|
|
for _, comparator in self.ops:
|
|
yield comparator # we don't want the 'op'
|
|
|
|
def last_child(self):
|
|
"""override last_child"""
|
|
# XXX maybe if self.ops:
|
|
return self.ops[-1][1]
|
|
#return self.left
|
|
|
|
|
|
class Comprehension(NodeNG):
|
|
"""class representing a Comprehension node"""
|
|
_astroid_fields = ('target', 'iter', 'ifs')
|
|
_other_fields = ('is_async',)
|
|
target = None
|
|
iter = None
|
|
ifs = None
|
|
is_async = None
|
|
|
|
def __init__(self, parent=None):
|
|
super(Comprehension, self).__init__()
|
|
self.parent = parent
|
|
|
|
# pylint: disable=redefined-builtin; same name as builtin ast module.
|
|
def postinit(self, target=None, iter=None, ifs=None, is_async=None):
|
|
self.target = target
|
|
self.iter = iter
|
|
self.ifs = ifs
|
|
self.is_async = is_async
|
|
|
|
optional_assign = True
|
|
def assign_type(self):
|
|
return self
|
|
|
|
def ass_type(self):
|
|
warnings.warn('%s.ass_type() is deprecated and slated for removal'
|
|
'in astroid 2.0, use %s.assign_type() instead.'
|
|
% (type(self).__name__, type(self).__name__),
|
|
PendingDeprecationWarning, stacklevel=2)
|
|
return self.assign_type()
|
|
|
|
def _get_filtered_stmts(self, lookup_node, node, stmts, mystmt):
|
|
"""method used in filter_stmts"""
|
|
if self is mystmt:
|
|
if isinstance(lookup_node, (Const, Name)):
|
|
return [lookup_node], True
|
|
|
|
elif self.statement() is mystmt:
|
|
# original node's statement is the assignment, only keeps
|
|
# current node (gen exp, list comp)
|
|
|
|
return [node], True
|
|
|
|
return stmts, False
|
|
|
|
|
|
class Const(NodeNG, bases.Instance):
|
|
"""represent a constant node like num, str, bool, None, bytes"""
|
|
_other_fields = ('value',)
|
|
|
|
def __init__(self, value, lineno=None, col_offset=None, parent=None):
|
|
self.value = value
|
|
super(Const, self).__init__(lineno, col_offset, parent)
|
|
|
|
def getitem(self, index, context=None):
|
|
if isinstance(index, Const):
|
|
index_value = index.value
|
|
elif isinstance(index, Slice):
|
|
index_value = _infer_slice(index, context=context)
|
|
|
|
else:
|
|
raise exceptions.AstroidTypeError(
|
|
'Could not use type {} as subscript index'.format(type(index))
|
|
)
|
|
|
|
try:
|
|
if isinstance(self.value, six.string_types):
|
|
return Const(self.value[index_value])
|
|
if isinstance(self.value, bytes) and six.PY3:
|
|
# Bytes aren't instances of six.string_types
|
|
# on Python 3. Also, indexing them should return
|
|
# integers.
|
|
return Const(self.value[index_value])
|
|
except IndexError as exc:
|
|
util.reraise(exceptions.AstroidIndexError(
|
|
message='Index {index!r} out of range', error=exc,
|
|
node=self, index=index, context=context))
|
|
except TypeError as exc:
|
|
util.reraise(exceptions.AstroidTypeError(
|
|
message='Type error {error!r}', error=exc,
|
|
node=self, index=index, context=context))
|
|
|
|
raise exceptions.AstroidTypeError(
|
|
'%r (value=%s)' % (self, self.value)
|
|
)
|
|
|
|
def has_dynamic_getattr(self):
|
|
return False
|
|
|
|
def itered(self):
|
|
if isinstance(self.value, six.string_types):
|
|
return self.value
|
|
raise TypeError()
|
|
|
|
def pytype(self):
|
|
return self._proxied.qname()
|
|
|
|
def bool_value(self):
|
|
return bool(self.value)
|
|
|
|
|
|
class Continue(Statement):
|
|
"""class representing a Continue node"""
|
|
|
|
|
|
class Decorators(NodeNG):
|
|
"""class representing a Decorators node"""
|
|
_astroid_fields = ('nodes',)
|
|
nodes = None
|
|
|
|
def postinit(self, nodes):
|
|
self.nodes = nodes
|
|
|
|
def scope(self):
|
|
# skip the function node to go directly to the upper level scope
|
|
return self.parent.parent.scope()
|
|
|
|
|
|
class DelAttr(mixins.ParentAssignTypeMixin, NodeNG):
|
|
"""class representing a DelAttr node"""
|
|
_astroid_fields = ('expr',)
|
|
_other_fields = ('attrname',)
|
|
expr = None
|
|
|
|
def __init__(self, attrname=None, lineno=None, col_offset=None, parent=None):
|
|
self.attrname = attrname
|
|
super(DelAttr, self).__init__(lineno, col_offset, parent)
|
|
|
|
def postinit(self, expr=None):
|
|
self.expr = expr
|
|
|
|
|
|
class Delete(mixins.AssignTypeMixin, Statement):
|
|
"""class representing a Delete node"""
|
|
_astroid_fields = ('targets',)
|
|
targets = None
|
|
|
|
def postinit(self, targets=None):
|
|
self.targets = targets
|
|
|
|
|
|
class Dict(NodeNG, bases.Instance):
|
|
"""class representing a Dict node"""
|
|
_astroid_fields = ('items',)
|
|
|
|
def __init__(self, lineno=None, col_offset=None, parent=None):
|
|
self.items = []
|
|
super(Dict, self).__init__(lineno, col_offset, parent)
|
|
|
|
def postinit(self, items):
|
|
self.items = items
|
|
|
|
@classmethod
|
|
def from_constants(cls, items=None):
|
|
node = cls()
|
|
if items is None:
|
|
node.items = []
|
|
else:
|
|
node.items = [(const_factory(k), const_factory(v))
|
|
for k, v in items.items()]
|
|
return node
|
|
|
|
def pytype(self):
|
|
return '%s.dict' % BUILTINS
|
|
|
|
def get_children(self):
|
|
"""get children of a Dict node"""
|
|
# overrides get_children
|
|
for key, value in self.items:
|
|
yield key
|
|
yield value
|
|
|
|
def last_child(self):
|
|
"""override last_child"""
|
|
if self.items:
|
|
return self.items[-1][1]
|
|
return None
|
|
|
|
def itered(self):
|
|
return self.items[::2]
|
|
|
|
def getitem(self, index, context=None):
|
|
for key, value in self.items:
|
|
# TODO(cpopa): no support for overriding yet, {1:2, **{1: 3}}.
|
|
if isinstance(key, DictUnpack):
|
|
try:
|
|
return value.getitem(index, context)
|
|
except (exceptions.AstroidTypeError, exceptions.AstroidIndexError):
|
|
continue
|
|
for inferredkey in key.infer(context):
|
|
if inferredkey is util.Uninferable:
|
|
continue
|
|
if isinstance(inferredkey, Const) and isinstance(index, Const):
|
|
if inferredkey.value == index.value:
|
|
return value
|
|
|
|
raise exceptions.AstroidIndexError(index)
|
|
|
|
def bool_value(self):
|
|
return bool(self.items)
|
|
|
|
|
|
class Expr(Statement):
|
|
"""class representing a Expr node"""
|
|
_astroid_fields = ('value',)
|
|
value = None
|
|
|
|
def postinit(self, value=None):
|
|
self.value = value
|
|
|
|
|
|
class Ellipsis(NodeNG): # pylint: disable=redefined-builtin
|
|
"""class representing an Ellipsis node"""
|
|
|
|
def bool_value(self):
|
|
return True
|
|
|
|
|
|
class EmptyNode(NodeNG):
|
|
"""class representing an EmptyNode node"""
|
|
|
|
object = None
|
|
|
|
|
|
class ExceptHandler(mixins.AssignTypeMixin, Statement):
|
|
"""class representing an ExceptHandler node"""
|
|
_astroid_fields = ('type', 'name', 'body',)
|
|
type = None
|
|
name = None
|
|
body = None
|
|
|
|
# pylint: disable=redefined-builtin; had to use the same name as builtin ast module.
|
|
def postinit(self, type=None, name=None, body=None):
|
|
self.type = type
|
|
self.name = name
|
|
self.body = body
|
|
|
|
@decorators.cachedproperty
|
|
def blockstart_tolineno(self):
|
|
if self.name:
|
|
return self.name.tolineno
|
|
elif self.type:
|
|
return self.type.tolineno
|
|
|
|
return self.lineno
|
|
|
|
def catch(self, exceptions): # pylint: disable=redefined-outer-name
|
|
if self.type is None or exceptions is None:
|
|
return True
|
|
for node in self.type.nodes_of_class(Name):
|
|
if node.name in exceptions:
|
|
return True
|
|
|
|
|
|
class Exec(Statement):
|
|
"""class representing an Exec node"""
|
|
_astroid_fields = ('expr', 'globals', 'locals',)
|
|
expr = None
|
|
globals = None
|
|
locals = None
|
|
|
|
# pylint: disable=redefined-builtin; had to use the same name as builtin ast module.
|
|
def postinit(self, expr=None, globals=None, locals=None):
|
|
self.expr = expr
|
|
self.globals = globals
|
|
self.locals = locals
|
|
|
|
|
|
class ExtSlice(NodeNG):
|
|
"""class representing an ExtSlice node"""
|
|
_astroid_fields = ('dims',)
|
|
dims = None
|
|
|
|
def postinit(self, dims=None):
|
|
self.dims = dims
|
|
|
|
|
|
class For(mixins.BlockRangeMixIn, mixins.AssignTypeMixin, Statement):
|
|
"""class representing a For node"""
|
|
_astroid_fields = ('target', 'iter', 'body', 'orelse',)
|
|
target = None
|
|
iter = None
|
|
body = None
|
|
orelse = None
|
|
|
|
# pylint: disable=redefined-builtin; had to use the same name as builtin ast module.
|
|
def postinit(self, target=None, iter=None, body=None, orelse=None):
|
|
self.target = target
|
|
self.iter = iter
|
|
self.body = body
|
|
self.orelse = orelse
|
|
|
|
optional_assign = True
|
|
@decorators.cachedproperty
|
|
def blockstart_tolineno(self):
|
|
return self.iter.tolineno
|
|
|
|
|
|
class AsyncFor(For):
|
|
"""Asynchronous For built with `async` keyword."""
|
|
|
|
|
|
class Await(NodeNG):
|
|
"""Await node for the `await` keyword."""
|
|
|
|
_astroid_fields = ('value', )
|
|
value = None
|
|
|
|
def postinit(self, value=None):
|
|
self.value = value
|
|
|
|
|
|
class ImportFrom(mixins.ImportFromMixin, Statement):
|
|
"""class representing a ImportFrom node"""
|
|
_other_fields = ('modname', 'names', 'level')
|
|
|
|
def __init__(self, fromname, names, level=0, lineno=None,
|
|
col_offset=None, parent=None):
|
|
self.modname = fromname
|
|
self.names = names
|
|
self.level = level
|
|
super(ImportFrom, self).__init__(lineno, col_offset, parent)
|
|
|
|
|
|
class Attribute(NodeNG):
|
|
"""class representing a Attribute node"""
|
|
_astroid_fields = ('expr',)
|
|
_other_fields = ('attrname',)
|
|
expr = None
|
|
|
|
def __init__(self, attrname=None, lineno=None, col_offset=None, parent=None):
|
|
self.attrname = attrname
|
|
super(Attribute, self).__init__(lineno, col_offset, parent)
|
|
|
|
def postinit(self, expr=None):
|
|
self.expr = expr
|
|
|
|
|
|
class Global(Statement):
|
|
"""class representing a Global node"""
|
|
_other_fields = ('names',)
|
|
|
|
def __init__(self, names, lineno=None, col_offset=None, parent=None):
|
|
self.names = names
|
|
super(Global, self).__init__(lineno, col_offset, parent)
|
|
|
|
def _infer_name(self, frame, name):
|
|
return name
|
|
|
|
|
|
class If(mixins.BlockRangeMixIn, Statement):
|
|
"""class representing an If node"""
|
|
_astroid_fields = ('test', 'body', 'orelse')
|
|
test = None
|
|
body = None
|
|
orelse = None
|
|
|
|
def postinit(self, test=None, body=None, orelse=None):
|
|
self.test = test
|
|
self.body = body
|
|
self.orelse = orelse
|
|
|
|
@decorators.cachedproperty
|
|
def blockstart_tolineno(self):
|
|
return self.test.tolineno
|
|
|
|
def block_range(self, lineno):
|
|
"""handle block line numbers range for if statements"""
|
|
if lineno == self.body[0].fromlineno:
|
|
return lineno, lineno
|
|
if lineno <= self.body[-1].tolineno:
|
|
return lineno, self.body[-1].tolineno
|
|
return self._elsed_block_range(lineno, self.orelse,
|
|
self.body[0].fromlineno - 1)
|
|
|
|
|
|
class IfExp(NodeNG):
|
|
"""class representing an IfExp node"""
|
|
_astroid_fields = ('test', 'body', 'orelse')
|
|
test = None
|
|
body = None
|
|
orelse = None
|
|
|
|
def postinit(self, test=None, body=None, orelse=None):
|
|
self.test = test
|
|
self.body = body
|
|
self.orelse = orelse
|
|
|
|
|
|
class Import(mixins.ImportFromMixin, Statement):
|
|
"""class representing an Import node"""
|
|
_other_fields = ('names',)
|
|
|
|
def __init__(self, names=None, lineno=None, col_offset=None, parent=None):
|
|
self.names = names
|
|
super(Import, self).__init__(lineno, col_offset, parent)
|
|
|
|
|
|
class Index(NodeNG):
|
|
"""class representing an Index node"""
|
|
_astroid_fields = ('value',)
|
|
value = None
|
|
|
|
def postinit(self, value=None):
|
|
self.value = value
|
|
|
|
|
|
class Keyword(NodeNG):
|
|
"""class representing a Keyword node"""
|
|
_astroid_fields = ('value',)
|
|
_other_fields = ('arg',)
|
|
value = None
|
|
|
|
def __init__(self, arg=None, lineno=None, col_offset=None, parent=None):
|
|
self.arg = arg
|
|
super(Keyword, self).__init__(lineno, col_offset, parent)
|
|
|
|
def postinit(self, value=None):
|
|
self.value = value
|
|
|
|
|
|
class List(_BaseContainer):
|
|
"""class representing a List node"""
|
|
_other_fields = ('ctx',)
|
|
|
|
def __init__(self, ctx=None, lineno=None,
|
|
col_offset=None, parent=None):
|
|
self.ctx = ctx
|
|
super(List, self).__init__(lineno, col_offset, parent)
|
|
|
|
def pytype(self):
|
|
return '%s.list' % BUILTINS
|
|
|
|
def getitem(self, index, context=None):
|
|
return _container_getitem(self, self.elts, index, context=context)
|
|
|
|
|
|
class Nonlocal(Statement):
|
|
"""class representing a Nonlocal node"""
|
|
_other_fields = ('names',)
|
|
|
|
def __init__(self, names, lineno=None, col_offset=None, parent=None):
|
|
self.names = names
|
|
super(Nonlocal, self).__init__(lineno, col_offset, parent)
|
|
|
|
def _infer_name(self, frame, name):
|
|
return name
|
|
|
|
|
|
class Pass(Statement):
|
|
"""class representing a Pass node"""
|
|
|
|
|
|
class Print(Statement):
|
|
"""class representing a Print node"""
|
|
_astroid_fields = ('dest', 'values',)
|
|
dest = None
|
|
values = None
|
|
|
|
def __init__(self, nl=None, lineno=None, col_offset=None, parent=None):
|
|
self.nl = nl
|
|
super(Print, self).__init__(lineno, col_offset, parent)
|
|
|
|
def postinit(self, dest=None, values=None):
|
|
self.dest = dest
|
|
self.values = values
|
|
|
|
|
|
class Raise(Statement):
|
|
"""class representing a Raise node"""
|
|
exc = None
|
|
if six.PY2:
|
|
_astroid_fields = ('exc', 'inst', 'tback')
|
|
inst = None
|
|
tback = None
|
|
|
|
def postinit(self, exc=None, inst=None, tback=None):
|
|
self.exc = exc
|
|
self.inst = inst
|
|
self.tback = tback
|
|
else:
|
|
_astroid_fields = ('exc', 'cause')
|
|
exc = None
|
|
cause = None
|
|
|
|
def postinit(self, exc=None, cause=None):
|
|
self.exc = exc
|
|
self.cause = cause
|
|
|
|
def raises_not_implemented(self):
|
|
if not self.exc:
|
|
return
|
|
for name in self.exc.nodes_of_class(Name):
|
|
if name.name == 'NotImplementedError':
|
|
return True
|
|
|
|
|
|
class Return(Statement):
|
|
"""class representing a Return node"""
|
|
_astroid_fields = ('value',)
|
|
value = None
|
|
|
|
def postinit(self, value=None):
|
|
self.value = value
|
|
|
|
|
|
class Set(_BaseContainer):
|
|
"""class representing a Set node"""
|
|
|
|
def pytype(self):
|
|
return '%s.set' % BUILTINS
|
|
|
|
|
|
class Slice(NodeNG):
|
|
"""class representing a Slice node"""
|
|
_astroid_fields = ('lower', 'upper', 'step')
|
|
lower = None
|
|
upper = None
|
|
step = None
|
|
|
|
def postinit(self, lower=None, upper=None, step=None):
|
|
self.lower = lower
|
|
self.upper = upper
|
|
self.step = step
|
|
|
|
def _wrap_attribute(self, attr):
|
|
"""Wrap the empty attributes of the Slice in a Const node."""
|
|
if not attr:
|
|
const = const_factory(attr)
|
|
const.parent = self
|
|
return const
|
|
return attr
|
|
|
|
@decorators.cachedproperty
|
|
def _proxied(self):
|
|
builtins = MANAGER.astroid_cache[BUILTINS]
|
|
return builtins.getattr('slice')[0]
|
|
|
|
def pytype(self):
|
|
return '%s.slice' % BUILTINS
|
|
|
|
def igetattr(self, attrname, context=None):
|
|
if attrname == 'start':
|
|
yield self._wrap_attribute(self.lower)
|
|
elif attrname == 'stop':
|
|
yield self._wrap_attribute(self.upper)
|
|
elif attrname == 'step':
|
|
yield self._wrap_attribute(self.step)
|
|
else:
|
|
for value in self.getattr(attrname, context=context):
|
|
yield value
|
|
|
|
def getattr(self, attrname, context=None):
|
|
return self._proxied.getattr(attrname, context)
|
|
|
|
|
|
class Starred(mixins.ParentAssignTypeMixin, NodeNG):
|
|
"""class representing a Starred node"""
|
|
_astroid_fields = ('value',)
|
|
_other_fields = ('ctx', )
|
|
value = None
|
|
|
|
def __init__(self, ctx=None, lineno=None, col_offset=None, parent=None):
|
|
self.ctx = ctx
|
|
super(Starred, self).__init__(lineno=lineno,
|
|
col_offset=col_offset, parent=parent)
|
|
|
|
def postinit(self, value=None):
|
|
self.value = value
|
|
|
|
|
|
class Subscript(NodeNG):
|
|
"""class representing a Subscript node"""
|
|
_astroid_fields = ('value', 'slice')
|
|
_other_fields = ('ctx', )
|
|
value = None
|
|
slice = None
|
|
|
|
def __init__(self, ctx=None, lineno=None, col_offset=None, parent=None):
|
|
self.ctx = ctx
|
|
super(Subscript, self).__init__(lineno=lineno,
|
|
col_offset=col_offset, parent=parent)
|
|
|
|
# pylint: disable=redefined-builtin; had to use the same name as builtin ast module.
|
|
def postinit(self, value=None, slice=None):
|
|
self.value = value
|
|
self.slice = slice
|
|
|
|
|
|
class TryExcept(mixins.BlockRangeMixIn, Statement):
|
|
"""class representing a TryExcept node"""
|
|
_astroid_fields = ('body', 'handlers', 'orelse',)
|
|
body = None
|
|
handlers = None
|
|
orelse = None
|
|
|
|
def postinit(self, body=None, handlers=None, orelse=None):
|
|
self.body = body
|
|
self.handlers = handlers
|
|
self.orelse = orelse
|
|
|
|
def _infer_name(self, frame, name):
|
|
return name
|
|
|
|
def block_range(self, lineno):
|
|
"""handle block line numbers range for try/except statements"""
|
|
last = None
|
|
for exhandler in self.handlers:
|
|
if exhandler.type and lineno == exhandler.type.fromlineno:
|
|
return lineno, lineno
|
|
if exhandler.body[0].fromlineno <= lineno <= exhandler.body[-1].tolineno:
|
|
return lineno, exhandler.body[-1].tolineno
|
|
if last is None:
|
|
last = exhandler.body[0].fromlineno - 1
|
|
return self._elsed_block_range(lineno, self.orelse, last)
|
|
|
|
|
|
class TryFinally(mixins.BlockRangeMixIn, Statement):
|
|
"""class representing a TryFinally node"""
|
|
_astroid_fields = ('body', 'finalbody',)
|
|
body = None
|
|
finalbody = None
|
|
|
|
def postinit(self, body=None, finalbody=None):
|
|
self.body = body
|
|
self.finalbody = finalbody
|
|
|
|
def block_range(self, lineno):
|
|
"""handle block line numbers range for try/finally statements"""
|
|
child = self.body[0]
|
|
# py2.5 try: except: finally:
|
|
if (isinstance(child, TryExcept) and child.fromlineno == self.fromlineno
|
|
and lineno > self.fromlineno and lineno <= child.tolineno):
|
|
return child.block_range(lineno)
|
|
return self._elsed_block_range(lineno, self.finalbody)
|
|
|
|
|
|
class Tuple(_BaseContainer):
|
|
"""class representing a Tuple node"""
|
|
|
|
_other_fields = ('ctx',)
|
|
|
|
def __init__(self, ctx=None, lineno=None,
|
|
col_offset=None, parent=None):
|
|
self.ctx = ctx
|
|
super(Tuple, self).__init__(lineno, col_offset, parent)
|
|
|
|
def pytype(self):
|
|
return '%s.tuple' % BUILTINS
|
|
|
|
def getitem(self, index, context=None):
|
|
return _container_getitem(self, self.elts, index, context=context)
|
|
|
|
|
|
class UnaryOp(NodeNG):
|
|
"""class representing an UnaryOp node"""
|
|
_astroid_fields = ('operand',)
|
|
_other_fields = ('op',)
|
|
operand = None
|
|
|
|
def __init__(self, op=None, lineno=None, col_offset=None, parent=None):
|
|
self.op = op
|
|
super(UnaryOp, self).__init__(lineno, col_offset, parent)
|
|
|
|
def postinit(self, operand=None):
|
|
self.operand = operand
|
|
|
|
# This is set by inference.py
|
|
def _infer_unaryop(self, context=None):
|
|
raise NotImplementedError
|
|
|
|
def type_errors(self, context=None):
|
|
"""Return a list of TypeErrors which can occur during inference.
|
|
|
|
Each TypeError is represented by a :class:`BadUnaryOperationMessage`,
|
|
which holds the original exception.
|
|
"""
|
|
try:
|
|
results = self._infer_unaryop(context=context)
|
|
return [result for result in results
|
|
if isinstance(result, util.BadUnaryOperationMessage)]
|
|
except exceptions.InferenceError:
|
|
return []
|
|
|
|
|
|
class While(mixins.BlockRangeMixIn, Statement):
|
|
"""class representing a While node"""
|
|
_astroid_fields = ('test', 'body', 'orelse',)
|
|
test = None
|
|
body = None
|
|
orelse = None
|
|
|
|
def postinit(self, test=None, body=None, orelse=None):
|
|
self.test = test
|
|
self.body = body
|
|
self.orelse = orelse
|
|
|
|
@decorators.cachedproperty
|
|
def blockstart_tolineno(self):
|
|
return self.test.tolineno
|
|
|
|
def block_range(self, lineno):
|
|
"""handle block line numbers range for and while statements"""
|
|
return self. _elsed_block_range(lineno, self.orelse)
|
|
|
|
|
|
class With(mixins.BlockRangeMixIn, mixins.AssignTypeMixin, Statement):
|
|
"""class representing a With node"""
|
|
_astroid_fields = ('items', 'body')
|
|
items = None
|
|
body = None
|
|
|
|
def postinit(self, items=None, body=None):
|
|
self.items = items
|
|
self.body = body
|
|
|
|
@decorators.cachedproperty
|
|
def blockstart_tolineno(self):
|
|
return self.items[-1][0].tolineno
|
|
|
|
def get_children(self):
|
|
for expr, var in self.items:
|
|
yield expr
|
|
if var:
|
|
yield var
|
|
for elt in self.body:
|
|
yield elt
|
|
|
|
|
|
class AsyncWith(With):
|
|
"""Asynchronous `with` built with the `async` keyword."""
|
|
|
|
|
|
class Yield(NodeNG):
|
|
"""class representing a Yield node"""
|
|
_astroid_fields = ('value',)
|
|
value = None
|
|
|
|
def postinit(self, value=None):
|
|
self.value = value
|
|
|
|
|
|
class YieldFrom(Yield):
|
|
""" Class representing a YieldFrom node. """
|
|
|
|
|
|
class DictUnpack(NodeNG):
|
|
"""Represents the unpacking of dicts into dicts using PEP 448."""
|
|
|
|
|
|
class FormattedValue(NodeNG):
|
|
"""Represents a PEP 498 format string."""
|
|
_astroid_fields = ('value', 'format_spec')
|
|
value = None
|
|
conversion = None
|
|
format_spec = None
|
|
|
|
def postinit(self, value, conversion=None, format_spec=None):
|
|
self.value = value
|
|
self.conversion = conversion
|
|
self.format_spec = format_spec
|
|
|
|
|
|
class JoinedStr(NodeNG):
|
|
"""Represents a list of string expressions to be joined."""
|
|
_astroid_fields = ('values',)
|
|
values = None
|
|
|
|
def postinit(self, values=None):
|
|
self.values = values
|
|
|
|
|
|
class Unknown(NodeNG):
|
|
'''This node represents a node in a constructed AST where
|
|
introspection is not possible. At the moment, it's only used in
|
|
the args attribute of FunctionDef nodes where function signature
|
|
introspection failed.
|
|
'''
|
|
def infer(self, context=None, **kwargs):
|
|
'''Inference on an Unknown node immediately terminates.'''
|
|
yield util.Uninferable
|
|
|
|
|
|
# constants ##############################################################
|
|
|
|
CONST_CLS = {
|
|
list: List,
|
|
tuple: Tuple,
|
|
dict: Dict,
|
|
set: Set,
|
|
type(None): Const,
|
|
type(NotImplemented): Const,
|
|
}
|
|
|
|
def _update_const_classes():
|
|
"""update constant classes, so the keys of CONST_CLS can be reused"""
|
|
klasses = (bool, int, float, complex, str)
|
|
if six.PY2:
|
|
# pylint: disable=undefined-variable
|
|
klasses += (unicode, long)
|
|
klasses += (bytes,)
|
|
for kls in klasses:
|
|
CONST_CLS[kls] = Const
|
|
_update_const_classes()
|
|
|
|
|
|
def _two_step_initialization(cls, value):
|
|
instance = cls()
|
|
instance.postinit(value)
|
|
return instance
|
|
|
|
|
|
def _dict_initialization(cls, value):
|
|
if isinstance(value, dict):
|
|
value = tuple(value.items())
|
|
return _two_step_initialization(cls, value)
|
|
|
|
|
|
_CONST_CLS_CONSTRUCTORS = {
|
|
List: _two_step_initialization,
|
|
Tuple: _two_step_initialization,
|
|
Dict: _dict_initialization,
|
|
Set: _two_step_initialization,
|
|
Const: lambda cls, value: cls(value)
|
|
}
|
|
|
|
|
|
def const_factory(value):
|
|
"""return an astroid node for a python value"""
|
|
# XXX we should probably be stricter here and only consider stuff in
|
|
# CONST_CLS or do better treatment: in case where value is not in CONST_CLS,
|
|
# we should rather recall the builder on this value than returning an empty
|
|
# node (another option being that const_factory shouldn't be called with something
|
|
# not in CONST_CLS)
|
|
assert not isinstance(value, NodeNG)
|
|
|
|
# Hack for ignoring elements of a sequence
|
|
# or a mapping, in order to avoid transforming
|
|
# each element to an AST. This is fixed in 2.0
|
|
# and this approach is a temporary hack.
|
|
if isinstance(value, (list, set, tuple, dict)):
|
|
elts = []
|
|
else:
|
|
elts = value
|
|
|
|
try:
|
|
initializer_cls = CONST_CLS[value.__class__]
|
|
initializer = _CONST_CLS_CONSTRUCTORS[initializer_cls]
|
|
return initializer(initializer_cls, elts)
|
|
except (KeyError, AttributeError):
|
|
node = EmptyNode()
|
|
node.object = value
|
|
return node
|
|
|
|
|
|
# Backward-compatibility aliases
|
|
|
|
Backquote = util.proxy_alias('Backquote', Repr)
|
|
Discard = util.proxy_alias('Discard', Expr)
|
|
AssName = util.proxy_alias('AssName', AssignName)
|
|
AssAttr = util.proxy_alias('AssAttr', AssignAttr)
|
|
Getattr = util.proxy_alias('Getattr', Attribute)
|
|
CallFunc = util.proxy_alias('CallFunc', Call)
|
|
From = util.proxy_alias('From', ImportFrom)
|