From 969e05b12c68df090c784a0e89fabfe69f6efe87 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Tue, 6 Oct 2015 11:58:01 -0400 Subject: [PATCH 001/312] Convert the locals to a recursive function that traverses the AST rather sets attributes on the nodes while building the AST; the handling of the mixed instance attributes/locals isn't finished. --- astroid/raw_building.py | 3 +- astroid/rebuilder.py | 20 +++--- astroid/scoped_nodes.py | 155 +++++++++++++++++++++++++++++++++------- tox.ini | 14 ++-- 4 files changed, 147 insertions(+), 45 deletions(-) diff --git a/astroid/raw_building.py b/astroid/raw_building.py index aa07791bcc..e1d18f8ce4 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -294,7 +294,8 @@ def object_build(self, node, obj): continue if member in self._done: class_node = self._done[member] - if class_node not in node.locals.get(name, ()): + if class_node not in set(node.get_children()): + # if class_node not in node.locals.get(name, ()): node.add_local_node(class_node, name) else: class_node = object_build_class(node, member, name) diff --git a/astroid/rebuilder.py b/astroid/rebuilder.py index 7ea9cf26b6..a62a64f501 100644 --- a/astroid/rebuilder.py +++ b/astroid/rebuilder.py @@ -189,10 +189,10 @@ def visit_arguments(self, node, parent, assign_ctx=None): newnode.postinit(args, defaults, kwonlyargs, kw_defaults, annotations, varargannotation, kwargannotation) # save argument names in locals: - if vararg: - newnode.parent.set_local(vararg, newnode) - if kwarg: - newnode.parent.set_local(kwarg, newnode) + # if vararg: + # newnode.parent.set_local(vararg, newnode) + # if kwarg: + # newnode.parent.set_local(kwarg, newnode) return newnode def visit_assignattr(self, node, parent, assign_ctx=None): @@ -226,7 +226,7 @@ def visit_assignname(self, node, parent, assign_ctx=None, node_name=None): # maintain consistency with the other visit functions. newnode = nodes.AssignName(node_name, getattr(node, 'lineno', None), getattr(node, 'col_offset', None), parent) - self._save_assignment(newnode) + # self._save_assignment(newnode) return newnode def visit_augassign(self, node, parent, assign_ctx=None): @@ -555,9 +555,9 @@ def visit_import(self, node, parent, assign_ctx=None): newnode = nodes.Import(names, getattr(node, 'lineno', None), getattr(node, 'col_offset', None), parent) # save import names in parent's locals: - for (name, asname) in newnode.names: - name = asname or name - parent.set_local(name.split('.')[0], newnode) + # for (name, asname) in newnode.names: + # name = asname or name + # parent.set_local(name.split('.')[0], newnode) return newnode def visit_index(self, node, parent, assign_ctx=None): @@ -613,8 +613,8 @@ def visit_name(self, node, parent, assign_ctx=None): else: newnode = nodes.Name(node.id, node.lineno, node.col_offset, parent) # XXX REMOVE me : - if assign_ctx in ('Del', 'Assign'): # 'Aug' ?? - self._save_assignment(newnode) + # if assign_ctx in ('Del', 'Assign'): # 'Aug' ?? + # self._save_assignment(newnode) return newnode def visit_str(self, node, parent, assign_ctx=None): diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index fd437520e4..f229600832 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -24,10 +24,16 @@ from __future__ import print_function +import collections import io import itertools import warnings +try: + from functools import singledispatch as _singledispatch +except ImportError: + from singledispatch import singledispatch as _singledispatch + import six import wrapt @@ -149,6 +155,10 @@ class LocalsDictNodeNG(node_classes.LookupMixIn, bases.NodeNG): # dictionary of locals with name as key and node defining the local as # value + @property + def locals(self): + return get_locals(self, collections.defaultdict(list)) + def qname(self): """return the 'qualified' name of the node, eg module.name, module.class.name ... @@ -207,7 +217,7 @@ def add_local_node(self, child_node, name=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) + # self.set_local(name or child_node.name, child_node) def __getitem__(self, item): """method from the `dict` interface returning the first node @@ -223,26 +233,26 @@ def __iter__(self): """method from the `dict` interface returning an iterator on `self.keys()` """ - return iter(self.keys()) + return iter(get_locals(self, collections.defaultdict(list)).keys()) def keys(self): """method from the `dict` interface returning a tuple containing locally defined names """ - return list(self.locals.keys()) + return get_locals(self, collections.defaultdict(list)).keys() def values(self): """method from the `dict` interface returning a tuple containing locally defined nodes which are instance of `FunctionDef` or `ClassDef` """ - return [self[key] for key in self.keys()] + return get_locals(self, collections.defaultdict(list)).values() def items(self): """method from the `dict` interface returning a list of tuple containing each locally defined name with its associated node, which is an instance of `FunctionDef` or `ClassDef` """ - return list(zip(self.keys(), self.values())) + return get_locals(self, collections.defaultdict(list)).items() def __contains__(self, name): return name in self.locals @@ -272,10 +282,10 @@ class Module(LocalsDictNodeNG): package = None # dictionary of globals with name as key and node defining the global # as value - globals = None + # globals = None # Future imports - future_imports = None + # future_imports = None # names of python special attributes (handled by getattr impl.) special_attributes = set(('__name__', '__doc__', '__file__', '__path__', @@ -285,7 +295,7 @@ class Module(LocalsDictNodeNG): _other_fields = ('name', 'doc', 'file', 'path', 'package', 'pure_python', 'future_imports') - _other_other_fields = ('locals', 'globals') + # _other_other_fields = ('locals', 'globals') def __init__(self, name, doc, file=None, path=None, package=None, parent=None, pure_python=True): @@ -296,13 +306,21 @@ def __init__(self, name, doc, file=None, path=None, package=None, self.package = package self.parent = parent self.pure_python = pure_python - self.locals = self.globals = {} + # self.locals = self.globals = {} self.body = [] - self.future_imports = set() + # self.future_imports = set() def postinit(self, body=None): self.body = body + @property + def globals(self): + return get_locals(self, collections.defaultdict(list)) + + @property + def future_imports(self): + return get_locals(self, collections.defaultdict(list))['__future__'] + def _get_stream(self): if self.file_bytes is not None: return io.BytesIO(self.file_bytes) @@ -514,12 +532,12 @@ def frame(self): class GeneratorExp(ComprehensionScope): _astroid_fields = ('elt', 'generators') - _other_other_fields = ('locals',) + # _other_other_fields = ('locals',) elt = None generators = None def __init__(self, lineno=None, col_offset=None, parent=None): - self.locals = {} + # self.locals = {} super(GeneratorExp, self).__init__(lineno, col_offset, parent) def postinit(self, elt=None, generators=None): @@ -535,13 +553,13 @@ def bool_value(self): class DictComp(ComprehensionScope): _astroid_fields = ('key', 'value', 'generators') - _other_other_fields = ('locals',) + # _other_other_fields = ('locals',) key = None value = None generators = None def __init__(self, lineno=None, col_offset=None, parent=None): - self.locals = {} + # self.locals = {} super(DictComp, self).__init__(lineno, col_offset, parent) def postinit(self, key=None, value=None, generators=None): @@ -558,12 +576,12 @@ def bool_value(self): class SetComp(ComprehensionScope): _astroid_fields = ('elt', 'generators') - _other_other_fields = ('locals',) + # _other_other_fields = ('locals',) elt = None generators = None def __init__(self, lineno=None, col_offset=None, parent=None): - self.locals = {} + # self.locals = {} super(SetComp, self).__init__(lineno, col_offset, parent) def postinit(self, elt=None, generators=None): @@ -594,10 +612,10 @@ def bool_value(self): if six.PY3: class ListComp(_ListComp, ComprehensionScope): """class representing a ListComp node""" - _other_other_fields = ('locals',) + # _other_other_fields = ('locals',) def __init__(self, lineno=None, col_offset=None, parent=None): - self.locals = {} + # self.locals = {} super(ListComp, self).__init__(lineno, col_offset, parent) else: class ListComp(_ListComp): @@ -637,7 +655,7 @@ class Lambda(mixins.FilterStmtsMixin, LocalsDictNodeNG): type = 'function' def __init__(self, lineno=None, col_offset=None, parent=None): - self.locals = {} + # self.locals = {} self.args = [] self.body = [] super(Lambda, self).__init__(lineno, col_offset, parent) @@ -701,7 +719,8 @@ class FunctionDef(bases.Statement, Lambda): is_function = True # attributes below are set by the builder module or by raw factories _other_fields = ('name', 'doc') - _other_other_fields = ('locals', '_type') + # _other_other_fields = ('locals', '_type') + _other_other_fields = ('_type') _type = None def __init__(self, name=None, doc=None, lineno=None, @@ -710,9 +729,9 @@ def __init__(self, name=None, doc=None, lineno=None, self.doc = doc self.instance_attrs = {} super(FunctionDef, self).__init__(lineno, col_offset, parent) - if parent: - frame = parent.frame() - frame.set_local(name, self) + # if parent: + # frame = parent.frame() + # frame.set_local(name, self) # pylint: disable=arguments-differ; different than Lambdas def postinit(self, args, body, decorators=None, returns=None): @@ -1074,20 +1093,21 @@ class ClassDef(mixins.FilterStmtsMixin, LocalsDictNodeNG, bases.Statement): doc="class'type, possible values are 'class' | " "'metaclass' | 'exception'") _other_fields = ('name', 'doc') - _other_other_fields = ('locals', '_newstyle') + # _other_other_fields = ('locals', '_newstyle') + _other_other_fields = ('_newstyle') _newstyle = None def __init__(self, name=None, doc=None, lineno=None, col_offset=None, parent=None): self.instance_attrs = {} - self.locals = {} + # self.locals = {} self.bases = [] self.body = [] self.name = name self.doc = doc super(ClassDef, self).__init__(lineno, col_offset, parent) - if parent is not None: - parent.frame().set_local(name, self) + # if parent is not None: + # parent.frame().set_local(name, self) def postinit(self, bases, body, decorators, newstyle=None, metaclass=None): self.bases = bases @@ -1727,6 +1747,87 @@ def bool_value(self): return True +@_singledispatch +def get_locals(node, locals_): + pass + +# pylint: disable=unused-variable; doesn't understand singledispatch +@get_locals.register(bases.NodeNG) +def locals_generic(node, locals_): + for n in node.get_children(): + get_locals(n, locals_) + return locals_ + +# pylint: disable=unused-variable; doesn't understand singledispatch +@get_locals.register(node_classes.AssignName) +@get_locals.register(node_classes.DelName) +def locals_name(node, locals_): + locals_[node.name].append(node) + +# pylint: disable=unused-variable; doesn't understand singledispatch +@get_locals.register(node_classes.Arguments) +def locals_arguments(node, locals_): + locals_[node.vararg].append(node) + locals_[node.kwarg].append(node) + +# pylint: disable=unused-variable; doesn't understand singledispatch +@get_locals.register(node_classes.Import) +def locals_import(node, locals_): + for name, asname in node.names: + name = asname or name + locals_[name.split('.')[0]].append(node) + +# pylint: disable=unused-variable; doesn't understand singledispatch +@get_locals.register(node_classes.ImportFrom) +def locals_import_from(node, locals_): + _key_func = lambda node: node.fromlineno + def sort_locals(my_list): + my_list.sort(key=_key_func) + + for name, asname in node.names: + if name == '*': + try: + imported = node.do_import_module() + except exceptions.InferenceError: + continue + for name in imported.wildcard_import_names(): + locals_[name].append(node) + sort_locals(locals_[name]) + else: + locals_[asname or name].append(node) + sort_locals(locals_[asname or name]) + +# pylint: disable=unused-variable; doesn't understand singledispatch +@get_locals.register(FunctionDef) +@get_locals.register(ClassDef) +def locals_class_function(node, locals_): + locals_[node.name].append(node) + return locals_ + +# pylint: disable=unused-variable; doesn't understand singledispatch +@get_locals.register(AssignAttr) +def locals(node, locals_): + try: + inferred = tuple() + for inferred in node.expr.infer(): + if (inferred is util.YES or isinstance(inferred, bases.Instance) + or inferred.is_function): + continue + else: + if hasattr(inferred, 'locals'): + values = inferred.locals[node.attrname] + if node in values: + continue + else: + if (frame.name == '__init__' and values and + not values[0].frame().name == '__init__'): + values.insert(0, node) + else: + values.append(node) + except exceptions.InferenceError: + pass + + # Backwards-compatibility aliases Class = node_classes.proxy_alias('Class', ClassDef) Function = node_classes.proxy_alias('Function', FunctionDef) diff --git a/tox.ini b/tox.ini index 1fe2f29d4c..e9044d6c2f 100644 --- a/tox.ini +++ b/tox.ini @@ -1,8 +1,8 @@ [tox] # official list is # envlist = py27, py33, py34, pypy, jython -envlist = py27, py33, pylint -# envlist = py27, py34 +# envlist = py27, py33, pylint +envlist = py27, py34 [testenv:pylint] deps = @@ -18,11 +18,11 @@ commands = pylint -rn --rcfile={toxinidir}/pylintrc {envsitepackagesdir}/astroid # for any defined environment even if it's not in envlist, which will # then fail on drone.io. -# [testenv:py34] -# deps = -# lazy-object-proxy -# six -# wrapt +[testenv:py34] +deps = + lazy-object-proxy + six + wrapt [testenv] deps = From 64d9868aa0aa5b24a590fa8c057572f1812ef632 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Thu, 8 Oct 2015 10:26:11 -0400 Subject: [PATCH 002/312] Example of unclear ImportError --- astroid/bases.py | 6 +++ astroid/builder.py | 16 +++--- astroid/scoped_nodes.py | 106 +++++++++++++++++++--------------------- astroid/util.py | 26 ++++++++++ tox.ini | 3 +- 5 files changed, 92 insertions(+), 65 deletions(-) diff --git a/astroid/bases.py b/astroid/bases.py index c2ea55edf1..8b64cfc781 100644 --- a/astroid/bases.py +++ b/astroid/bases.py @@ -25,6 +25,7 @@ import functools import pprint import sys +import types import warnings try: @@ -38,6 +39,7 @@ from astroid import context as contextmod from astroid import decorators as decoratorsmod from astroid import exceptions +from astroid import scoped_nodes from astroid import util @@ -274,6 +276,10 @@ def bool_value(self): def getitem(self, index, context=None): pass + @property + def instance_attrs(self): + return types.MappingProxyType(util.get_external_assignments(self.root(), self, collections.defaultdict(list))) + class UnboundMethod(Proxy): diff --git a/astroid/builder.py b/astroid/builder.py index ced86d3b86..32e099a4fa 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -151,14 +151,14 @@ def _post_build(self, module, encoding): module.file_encoding = encoding self._manager.cache_module(module) # post tree building steps after we stored the module in the cache: - for from_node in module._import_from_nodes: - if from_node.modname == '__future__': - for symbol, _ in from_node.names: - module.future_imports.add(symbol) - self.add_from_names_to_locals(from_node) - # handle delayed assattr nodes - for delayed in module._delayed_assattr: - self.delayed_assattr(delayed) + # for from_node in module._import_from_nodes: + # if from_node.modname == '__future__': + # for symbol, _ in from_node.names: + # module.future_imports.add(symbol) + # self.add_from_names_to_locals(from_node) + # # handle delayed assattr nodes + # for delayed in module._delayed_assattr: + # self.delayed_assattr(delayed) # Visit the transforms if self._apply_transforms: diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index f229600832..cdc42471d5 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -27,6 +27,7 @@ import collections import io import itertools +import types import warnings try: @@ -157,7 +158,7 @@ class LocalsDictNodeNG(node_classes.LookupMixIn, bases.NodeNG): @property def locals(self): - return get_locals(self, collections.defaultdict(list)) + return types.MappingProxyType(get_locals(self)) def qname(self): """return the 'qualified' name of the node, eg module.name, @@ -195,15 +196,18 @@ def _scope_lookup(self, node, name, offset=0): return pscope.scope_lookup(node, name) return builtin_lookup(name) # Module - def set_local(self, name, stmt): - """define in locals ( is the node defining the name) - if the node is a Module node (i.e. has globals), add the name to - globals + def set_local(self): + raise Exception('Attempted locals mutation.') - if the name is already defined, ignore it - """ - #assert not stmt in self.locals.get(name, ()), (self, stmt) - self.locals.setdefault(name, []).append(stmt) + # def set_local(self, name, stmt): + # """define in locals ( is the node defining the name) + # if the node is a Module node (i.e. has globals), add the name to + # globals + + # if the name is already defined, ignore it + # """ + # #assert not stmt in self.locals.get(name, ()), (self, stmt) + # self.locals.setdefault(name, []).append(stmt) __setitem__ = set_local @@ -233,26 +237,26 @@ def __iter__(self): """method from the `dict` interface returning an iterator on `self.keys()` """ - return iter(get_locals(self, collections.defaultdict(list)).keys()) + return iter(self.locals(self)) def keys(self): """method from the `dict` interface returning a tuple containing locally defined names """ - return get_locals(self, collections.defaultdict(list)).keys() + return self.locals.keys() def values(self): """method from the `dict` interface returning a tuple containing locally defined nodes which are instance of `FunctionDef` or `ClassDef` """ - return get_locals(self, collections.defaultdict(list)).values() + return self.locals.values() def items(self): """method from the `dict` interface returning a list of tuple containing each locally defined name with its associated node, which is an instance of `FunctionDef` or `ClassDef` """ - return get_locals(self, collections.defaultdict(list)).items() + return self.locals.items() def __contains__(self, name): return name in self.locals @@ -315,11 +319,11 @@ def postinit(self, body=None): @property def globals(self): - return get_locals(self, collections.defaultdict(list)) + return types.MappingProxyType(get_locals(self)) @property def future_imports(self): - return get_locals(self, collections.defaultdict(list))['__future__'] + return frozenset(get_locals(self)['__future__']) def _get_stream(self): if self.file_bytes is not None: @@ -664,6 +668,10 @@ def postinit(self, args, body): self.args = args self.body = body + @property + def instance_attrs(self): + return types.MappingProxyType(util.get_external_assignments(self.root(), self, collections.defaultdict(list))) + def pytype(self): if 'method' in self.type: return '%s.instancemethod' % BUILTINS @@ -727,7 +735,7 @@ def __init__(self, name=None, doc=None, lineno=None, col_offset=None, parent=None): self.name = name self.doc = doc - self.instance_attrs = {} + # self.instance_attrs = {} super(FunctionDef, self).__init__(lineno, col_offset, parent) # if parent: # frame = parent.frame() @@ -1099,7 +1107,7 @@ class ClassDef(mixins.FilterStmtsMixin, LocalsDictNodeNG, bases.Statement): def __init__(self, name=None, doc=None, lineno=None, col_offset=None, parent=None): - self.instance_attrs = {} + # self.instance_attrs = {} # self.locals = {} self.bases = [] self.body = [] @@ -1118,6 +1126,10 @@ def postinit(self, bases, body, decorators, newstyle=None, metaclass=None): if metaclass is not None: self._metaclass = metaclass + @property + def locals(self): + return types.MappingProxyType(util.get_external_assignments(self.root(), self, get_locals(self))) + def _newstyle_impl(self, context=None): if context is None: context = contextmod.InferenceContext() @@ -1747,38 +1759,50 @@ def bool_value(self): return True +def get_locals(node): + locals_ = collections.defaultdict(list) + for n in node.get_children(): + _get_locals(n, locals_) + return locals_ + @_singledispatch -def get_locals(node, locals_): +def _get_locals(node, locals_): pass # pylint: disable=unused-variable; doesn't understand singledispatch -@get_locals.register(bases.NodeNG) +@_get_locals.register(bases.NodeNG) def locals_generic(node, locals_): for n in node.get_children(): - get_locals(n, locals_) - return locals_ + _get_locals(n, locals_) + +# # pylint: disable=unused-variable; doesn't understand singledispatch +@_get_locals.register(LocalsDictNodeNG) +def locals_new_scope(node, locals_): + pass # pylint: disable=unused-variable; doesn't understand singledispatch -@get_locals.register(node_classes.AssignName) -@get_locals.register(node_classes.DelName) +@_get_locals.register(node_classes.AssignName) +@_get_locals.register(node_classes.DelName) +@_get_locals.register(FunctionDef) +@_get_locals.register(ClassDef) def locals_name(node, locals_): locals_[node.name].append(node) # pylint: disable=unused-variable; doesn't understand singledispatch -@get_locals.register(node_classes.Arguments) +@_get_locals.register(node_classes.Arguments) def locals_arguments(node, locals_): locals_[node.vararg].append(node) locals_[node.kwarg].append(node) # pylint: disable=unused-variable; doesn't understand singledispatch -@get_locals.register(node_classes.Import) +@_get_locals.register(node_classes.Import) def locals_import(node, locals_): for name, asname in node.names: name = asname or name locals_[name.split('.')[0]].append(node) # pylint: disable=unused-variable; doesn't understand singledispatch -@get_locals.register(node_classes.ImportFrom) +@_get_locals.register(node_classes.ImportFrom) def locals_import_from(node, locals_): _key_func = lambda node: node.fromlineno def sort_locals(my_list): @@ -1797,36 +1821,6 @@ def sort_locals(my_list): locals_[asname or name].append(node) sort_locals(locals_[asname or name]) -# pylint: disable=unused-variable; doesn't understand singledispatch -@get_locals.register(FunctionDef) -@get_locals.register(ClassDef) -def locals_class_function(node, locals_): - locals_[node.name].append(node) - return locals_ - -# pylint: disable=unused-variable; doesn't understand singledispatch -@get_locals.register(AssignAttr) -def locals(node, locals_): - try: - inferred = tuple() - for inferred in node.expr.infer(): - if (inferred is util.YES or isinstance(inferred, bases.Instance) - or inferred.is_function): - continue - else: - if hasattr(inferred, 'locals'): - values = inferred.locals[node.attrname] - if node in values: - continue - else: - if (frame.name == '__init__' and values and - not values[0].frame().name == '__init__'): - values.insert(0, node) - else: - values.append(node) - except exceptions.InferenceError: - pass - # Backwards-compatibility aliases Class = node_classes.proxy_alias('Class', ClassDef) diff --git a/astroid/util.py b/astroid/util.py index 96ba5fbf6e..8e96f93533 100644 --- a/astroid/util.py +++ b/astroid/util.py @@ -23,6 +23,8 @@ import six +from astroid import exceptions + def reraise(exception): '''Reraises an exception with the traceback from the current exception @@ -45,3 +47,27 @@ def __getattribute__(self, name): def __call__(self, *args, **kwargs): return self + + +def get_external_assignments(root, subject, attributes): + stack = [root] + while stack: + node = stack.pop() + stack.extend(node.get_children()) + if isinstance(node, node_classes.AssignAttr): + frame = node.frame() + try: + for inferred in (n for n in node.expr.infer() if n is subject): + values = attributes[node.attrname] + if node in values: + continue + else: + if (values and frame.is_function and + frame.name == '__init__' and not + values[0].frame().name == '__init__'): + values.insert(0, node) + else: + values.append(node) + except exceptions.InferenceError: + pass + return attributes diff --git a/tox.ini b/tox.ini index e9044d6c2f..1c74d049fc 100644 --- a/tox.ini +++ b/tox.ini @@ -2,7 +2,8 @@ # official list is # envlist = py27, py33, py34, pypy, jython # envlist = py27, py33, pylint -envlist = py27, py34 +# envlist = py27, py34 +envlist = py34 [testenv:pylint] deps = From 010929ad5c633a1c969221ea1bf0a1224ecc3cda Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Thu, 8 Oct 2015 21:17:34 -0400 Subject: [PATCH 003/312] Add lazy imports --- astroid/bases.py | 6 +++--- astroid/scoped_nodes.py | 28 ++++++++++++++++++++++++++-- astroid/util.py | 29 +++++------------------------ 3 files changed, 34 insertions(+), 29 deletions(-) diff --git a/astroid/bases.py b/astroid/bases.py index 8b64cfc781..f3e31750c1 100644 --- a/astroid/bases.py +++ b/astroid/bases.py @@ -18,7 +18,6 @@ """This module contains base classes and functions for the nodes and some inference utils. """ - from __future__ import print_function import collections @@ -39,9 +38,10 @@ from astroid import context as contextmod from astroid import decorators as decoratorsmod from astroid import exceptions -from astroid import scoped_nodes from astroid import util +scoped_nodes = util.lazy_import('scoped_nodes') + if sys.version_info >= (3, 0): BUILTINS = 'builtins' @@ -278,7 +278,7 @@ def getitem(self, index, context=None): @property def instance_attrs(self): - return types.MappingProxyType(util.get_external_assignments(self.root(), self, collections.defaultdict(list))) + return types.MappingProxyType(scoped_nodes.get_external_assignments(self.root(), self, collections.defaultdict(list))) diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index cdc42471d5..0e12af213d 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -670,7 +670,7 @@ def postinit(self, args, body): @property def instance_attrs(self): - return types.MappingProxyType(util.get_external_assignments(self.root(), self, collections.defaultdict(list))) + return types.MappingProxyType(get_external_assignments(self.root(), self, collections.defaultdict(list))) def pytype(self): if 'method' in self.type: @@ -1128,7 +1128,7 @@ def postinit(self, bases, body, decorators, newstyle=None, metaclass=None): @property def locals(self): - return types.MappingProxyType(util.get_external_assignments(self.root(), self, get_locals(self))) + return types.MappingProxyType(get_external_assignments(self.root(), self, get_locals(self))) def _newstyle_impl(self, context=None): if context is None: @@ -1822,6 +1822,30 @@ def sort_locals(my_list): sort_locals(locals_[asname or name]) +def get_external_assignments(root, subject, attributes): + stack = [root] + while stack: + node = stack.pop() + stack.extend(node.get_children()) + if isinstance(node, node_classes.AssignAttr): + frame = node.frame() + try: + for inferred in (n for n in node.expr.infer() if n is subject): + values = attributes[node.attrname] + if node in values: + continue + else: + if (values and frame.is_function and + frame.name == '__init__' and not + values[0].frame().name == '__init__'): + values.insert(0, node) + else: + values.append(node) + except exceptions.InferenceError: + pass + return attributes + + # Backwards-compatibility aliases Class = node_classes.proxy_alias('Class', ClassDef) Function = node_classes.proxy_alias('Function', FunctionDef) diff --git a/astroid/util.py b/astroid/util.py index 8e96f93533..79f14cc0bf 100644 --- a/astroid/util.py +++ b/astroid/util.py @@ -19,13 +19,18 @@ # The code in this file was originally part of logilab-common, licensed under # the same license. +import importlib import sys +import lazy_object_proxy import six from astroid import exceptions +def lazy_import(module_name): + return lazy_object_proxy.Proxy(lambda: importlib.import_module(module_name)) + def reraise(exception): '''Reraises an exception with the traceback from the current exception block.''' @@ -47,27 +52,3 @@ def __getattribute__(self, name): def __call__(self, *args, **kwargs): return self - - -def get_external_assignments(root, subject, attributes): - stack = [root] - while stack: - node = stack.pop() - stack.extend(node.get_children()) - if isinstance(node, node_classes.AssignAttr): - frame = node.frame() - try: - for inferred in (n for n in node.expr.infer() if n is subject): - values = attributes[node.attrname] - if node in values: - continue - else: - if (values and frame.is_function and - frame.name == '__init__' and not - values[0].frame().name == '__init__'): - values.insert(0, node) - else: - values.append(node) - except exceptions.InferenceError: - pass - return attributes From 1595dfbbf8dfee1841972c77accff025deda291a Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Fri, 9 Oct 2015 10:48:52 -0400 Subject: [PATCH 004/312] Fix type dispatch in bases.py and a bug in scoped_nodes.py --- astroid/bases.py | 16 ++++++++-------- astroid/scoped_nodes.py | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/astroid/bases.py b/astroid/bases.py index f3e31750c1..329c56f133 100644 --- a/astroid/bases.py +++ b/astroid/bases.py @@ -40,6 +40,7 @@ from astroid import exceptions from astroid import util +node_classes = util.lazy_import('node_classes') scoped_nodes = util.lazy_import('scoped_nodes') @@ -333,10 +334,9 @@ def _infer_type_new_call(self, caller, context): needs to be a tuple of classes and the attributes a dictionary of strings to values. """ - from astroid import node_classes # Verify the metaclass mcs = next(caller.args[0].infer(context=context)) - if mcs.__class__.__name__ != 'ClassDef': + if not isinstance(mcs, scoped_nodes.ClassDef): # Not a valid first argument. return if not mcs.is_subtype_of("%s.type" % BUILTINS): @@ -345,7 +345,7 @@ def _infer_type_new_call(self, caller, context): # Verify the name name = next(caller.args[1].infer(context=context)) - if name.__class__.__name__ != 'Const': + if not isinstance(name, node_classes.Const): # Not a valid name, needs to be a const. return if not isinstance(name.value, str): @@ -354,26 +354,26 @@ def _infer_type_new_call(self, caller, context): # Verify the bases bases = next(caller.args[2].infer(context=context)) - if bases.__class__.__name__ != 'Tuple': + if not isinstance(bases, node_classes.Tuple): # Needs to be a tuple. return inferred_bases = [next(elt.infer(context=context)) for elt in bases.elts] - if any(base.__class__.__name__ != 'ClassDef' + if not all(isinstance(base, scoped_nodes.ClassDef) for base in inferred_bases): # All the bases needs to be Classes return # Verify the attributes. attrs = next(caller.args[3].infer(context=context)) - if attrs.__class__.__name__ != 'Dict': + if not isinstance(attrs, node_classes.Dict): # Needs to be a dictionary. return cls_locals = collections.defaultdict(list) for key, value in attrs.items: key = next(key.infer(context=context)) value = next(value.infer(context=context)) - if key.__class__.__name__ != 'Const': + if not isinstance(key, node_classes.Const): # Something invalid as an attribute. return if not isinstance(key.value, str): @@ -397,7 +397,7 @@ def infer_call_result(self, caller, context=None): context = context.clone() context.boundnode = self.bound - if (self.bound.__class__.__name__ == 'ClassDef' + if (isinstance(self.bound, scoped_nodes.ClassDef) and self.bound.name == 'type' and self.name == '__new__' and len(caller.args) == 4 diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index 0e12af213d..e5bd2b88bb 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -237,7 +237,7 @@ def __iter__(self): """method from the `dict` interface returning an iterator on `self.keys()` """ - return iter(self.locals(self)) + return iter(self.locals) def keys(self): """method from the `dict` interface returning a tuple containing From 969d66666ddeb95f5260ffd722407d450bc4e5f4 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Sat, 10 Oct 2015 11:27:38 -0400 Subject: [PATCH 005/312] Handle assigning instance_attrs on ClassDefs through Instances, bug fixes --- astroid/bases.py | 2 +- astroid/brain/py2stdlib.py | 1 + astroid/builder.py | 72 +--------------------------- astroid/rebuilder.py | 98 ++++++++++++++------------------------ astroid/scoped_nodes.py | 82 ++++++++++++++++++++++++------- astroid/util.py | 3 +- 6 files changed, 108 insertions(+), 150 deletions(-) diff --git a/astroid/bases.py b/astroid/bases.py index 329c56f133..00fe66b7be 100644 --- a/astroid/bases.py +++ b/astroid/bases.py @@ -279,7 +279,7 @@ def getitem(self, index, context=None): @property def instance_attrs(self): - return types.MappingProxyType(scoped_nodes.get_external_assignments(self.root(), self, collections.defaultdict(list))) + return types.MappingProxyType(scoped_nodes.get_external_assignments(self, collections.defaultdict(list))) diff --git a/astroid/brain/py2stdlib.py b/astroid/brain/py2stdlib.py index 8971d5ca5a..98c251595e 100644 --- a/astroid/brain/py2stdlib.py +++ b/astroid/brain/py2stdlib.py @@ -333,6 +333,7 @@ def name(self): return %(name)r ''' % {'name': target.name, 'types': ', '.join(node.basenames)}) fake = AstroidBuilder(MANAGER).string_build(classdef)[target.name] + print(fake.repr_tree()) fake.parent = target.parent for method in node.mymethods(): fake.locals[method.name] = [method] diff --git a/astroid/builder.py b/astroid/builder.py index 32e099a4fa..f5b92de759 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -150,15 +150,6 @@ def _post_build(self, module, encoding): """Handles encoding and delayed nodes after a module has been built""" module.file_encoding = encoding self._manager.cache_module(module) - # post tree building steps after we stored the module in the cache: - # for from_node in module._import_from_nodes: - # if from_node.modname == '__future__': - # for symbol, _ in from_node.names: - # module.future_imports.add(symbol) - # self.add_from_names_to_locals(from_node) - # # handle delayed assattr nodes - # for delayed in module._delayed_assattr: - # self.delayed_assattr(delayed) # Visit the transforms if self._apply_transforms: @@ -187,69 +178,10 @@ def _data_build(self, data, modname, path): package = path and path.find('__init__.py') > -1 or False builder = rebuilder.TreeRebuilder(self._manager) module = builder.visit_module(node, modname, node_file, package) - module._import_from_nodes = builder._import_from_nodes - module._delayed_assattr = builder._delayed_assattr + # module._import_from_nodes = builder._import_from_nodes + # module._delayed_assattr = builder._delayed_assattr return module - def add_from_names_to_locals(self, node): - """Store imported names to the locals - - Resort the locals if coming from a delayed node - """ - _key_func = lambda node: node.fromlineno - def sort_locals(my_list): - my_list.sort(key=_key_func) - - for (name, asname) in node.names: - if name == '*': - try: - imported = node.do_import_module() - except exceptions.InferenceError: - continue - for name in imported.wildcard_import_names(): - node.parent.set_local(name, node) - sort_locals(node.parent.scope().locals[name]) - else: - node.parent.set_local(asname or name, node) - sort_locals(node.parent.scope().locals[asname or name]) - - def delayed_assattr(self, node): - """Visit a AssAttr node - - This adds name to locals and handle members definition. - """ - try: - frame = node.frame() - for inferred in node.expr.infer(): - if inferred is util.YES: - continue - try: - if inferred.__class__ is bases.Instance: - inferred = inferred._proxied - iattrs = inferred.instance_attrs - elif isinstance(inferred, bases.Instance): - # Const, Tuple, ... we may be wrong, may be not, but - # anyway we don't want to pollute builtin's namespace - continue - elif inferred.is_function: - iattrs = inferred.instance_attrs - else: - iattrs = inferred.locals - except AttributeError: - # XXX log error - continue - values = iattrs.setdefault(node.attrname, []) - if node in values: - continue - # get assign in __init__ first XXX useful ? - if (frame.name == '__init__' and values and - not values[0].frame().name == '__init__'): - values.insert(0, node) - else: - values.append(node) - except exceptions.InferenceError: - pass - def parse(code, module_name='', path=None, apply_transforms=True): """Parses a source string in order to obtain an astroid AST from it diff --git a/astroid/rebuilder.py b/astroid/rebuilder.py index a62a64f501..e4bb972f2b 100644 --- a/astroid/rebuilder.py +++ b/astroid/rebuilder.py @@ -15,11 +15,11 @@ # # You should have received a copy of the GNU Lesser General Public License along # with astroid. If not, see . -"""this module contains utilities for rebuilding a _ast tree in +"""this module contains utilities for rebuilding a ast tree in order to get a single Astroid representation """ -import _ast +import ast import sys from astroid import astpeephole @@ -27,42 +27,42 @@ -_BIN_OP_CLASSES = {_ast.Add: '+', - _ast.BitAnd: '&', - _ast.BitOr: '|', - _ast.BitXor: '^', - _ast.Div: '/', - _ast.FloorDiv: '//', - _ast.Mod: '%', - _ast.Mult: '*', - _ast.Pow: '**', - _ast.Sub: '-', - _ast.LShift: '<<', - _ast.RShift: '>>', +_BIN_OP_CLASSES = {ast.Add: '+', + ast.BitAnd: '&', + ast.BitOr: '|', + ast.BitXor: '^', + ast.Div: '/', + ast.FloorDiv: '//', + ast.Mod: '%', + ast.Mult: '*', + ast.Pow: '**', + ast.Sub: '-', + ast.LShift: '<<', + ast.RShift: '>>', } if sys.version_info >= (3, 5): - _BIN_OP_CLASSES[_ast.MatMult] = '@' + _BIN_OP_CLASSES[ast.MatMult] = '@' -_BOOL_OP_CLASSES = {_ast.And: 'and', - _ast.Or: 'or', +_BOOL_OP_CLASSES = {ast.And: 'and', + ast.Or: 'or', } -_UNARY_OP_CLASSES = {_ast.UAdd: '+', - _ast.USub: '-', - _ast.Not: 'not', - _ast.Invert: '~', +_UNARY_OP_CLASSES = {ast.UAdd: '+', + ast.USub: '-', + ast.Not: 'not', + ast.Invert: '~', } -_CMP_OP_CLASSES = {_ast.Eq: '==', - _ast.Gt: '>', - _ast.GtE: '>=', - _ast.In: 'in', - _ast.Is: 'is', - _ast.IsNot: 'is not', - _ast.Lt: '<', - _ast.LtE: '<=', - _ast.NotEq: '!=', - _ast.NotIn: 'not in', +_CMP_OP_CLASSES = {ast.Eq: '==', + ast.Gt: '>', + ast.GtE: '>=', + ast.In: 'in', + ast.Is: 'is', + ast.IsNot: 'is not', + ast.Lt: '<', + ast.LtE: '<=', + ast.NotEq: '!=', + ast.NotIn: 'not in', } CONST_NAME_TRANSFORMS = {'None': None, @@ -82,7 +82,7 @@ def _get_doc(node): try: - if isinstance(node.body[0], _ast.Expr) and isinstance(node.body[0].value, _ast.Str): + if isinstance(node.body[0], ast.Expr) and isinstance(node.body[0].value, ast.Str): doc = node.body[0].value.s node.body = node.body[1:] return node, doc @@ -104,13 +104,11 @@ def _visit_or_none(node, attr, visitor, parent, assign_ctx, visit='visit', class TreeRebuilder(object): - """Rebuilds the _ast tree to become an Astroid tree""" + """Rebuilds the ast tree to become an Astroid tree""" def __init__(self, manager): self._manager = manager self._global_names = [] - self._import_from_nodes = [] - self._delayed_assattr = [] self._visit_meths = {} self._peepholer = astpeephole.ASTPeepholeOptimizer() @@ -133,13 +131,6 @@ def visit(self, node, parent, assign_ctx=None): self._visit_meths[cls] = visit_method return visit_method(node, parent, assign_ctx) - def _save_assignment(self, node, name=None): - """save assignement situation since node.parent is not available yet""" - if self._global_names and node.name in self._global_names[-1]: - node.root().set_local(node.name, node) - else: - node.parent.set_local(node.name, node) - def visit_arguments(self, node, parent, assign_ctx=None): """visit a Arguments node by returning a fresh instance of it""" vararg, kwarg = node.vararg, node.kwarg @@ -155,7 +146,7 @@ def visit_arguments(self, node, parent, assign_ctx=None): varargannotation = None kwargannotation = None # change added in 82732 (7c5c678e4164), vararg and kwarg - # are instances of `_ast.arg`, not strings + # are instances of `ast.arg`, not strings if vararg: if PY34: if node.vararg.annotation: @@ -188,11 +179,6 @@ def visit_arguments(self, node, parent, assign_ctx=None): annotations = [] newnode.postinit(args, defaults, kwonlyargs, kw_defaults, annotations, varargannotation, kwargannotation) - # save argument names in locals: - # if vararg: - # newnode.parent.set_local(vararg, newnode) - # if kwarg: - # newnode.parent.set_local(kwarg, newnode) return newnode def visit_assignattr(self, node, parent, assign_ctx=None): @@ -226,7 +212,6 @@ def visit_assignname(self, node, parent, assign_ctx=None, node_name=None): # maintain consistency with the other visit functions. newnode = nodes.AssignName(node_name, getattr(node, 'lineno', None), getattr(node, 'col_offset', None), parent) - # self._save_assignment(newnode) return newnode def visit_augassign(self, node, parent, assign_ctx=None): @@ -245,7 +230,7 @@ def visit_repr(self, node, parent, assign_ctx=None): def visit_binop(self, node, parent, assign_ctx=None): """visit a BinOp node by returning a fresh instance of it""" - if isinstance(node.left, _ast.BinOp) and self._manager.optimize_ast: + if isinstance(node.left, ast.BinOp) and self._manager.optimize_ast: # Optimize BinOp operations in order to remove # redundant recursion. For instance, if the # following code is parsed in order to obtain @@ -312,7 +297,6 @@ def visit_call(self, node, parent, assign_ctx=None): keywords.append(new_kwargs) else: keywords = [new_kwargs] - newnode.postinit(self.visit(node.func, newnode, assign_ctx), args, keywords) return newnode @@ -370,7 +354,7 @@ def visit_comprehension(self, node, parent, assign_ctx=None): def visit_decorators(self, node, parent, assign_ctx=None): """visit a Decorators node by returning a fresh instance of it""" - # /!\ node is actually a _ast.FunctionDef node while + # /!\ node is actually a ast.FunctionDef node while # parent is a astroid.nodes.FunctionDef node newnode = nodes.Decorators(node.lineno, node.col_offset, parent) newnode.postinit([self.visit(child, newnode, assign_ctx) @@ -467,8 +451,6 @@ def visit_importfrom(self, node, parent, assign_ctx=None): newnode = nodes.ImportFrom(node.module or '', names, node.level or None, getattr(node, 'lineno', None), getattr(node, 'col_offset', None), parent) - # store From names to add them to locals after building - self._import_from_nodes.append(newnode) return newnode def _visit_functiondef(self, cls, node, parent, assign_ctx=None): @@ -515,7 +497,6 @@ def visit_attribute(self, node, parent, assign_ctx=None): # FIXME : maybe we should call visit_assignattr ? newnode = nodes.AssignAttr(node.attr, node.lineno, node.col_offset, parent) - self._delayed_assattr.append(newnode) else: newnode = nodes.Attribute(node.attr, node.lineno, node.col_offset, parent) @@ -554,10 +535,6 @@ def visit_import(self, node, parent, assign_ctx=None): names = [(alias.name, alias.asname) for alias in node.names] newnode = nodes.Import(names, getattr(node, 'lineno', None), getattr(node, 'col_offset', None), parent) - # save import names in parent's locals: - # for (name, asname) in newnode.names: - # name = asname or name - # parent.set_local(name.split('.')[0], newnode) return newnode def visit_index(self, node, parent, assign_ctx=None): @@ -612,9 +589,6 @@ def visit_name(self, node, parent, assign_ctx=None): return newnode else: newnode = nodes.Name(node.id, node.lineno, node.col_offset, parent) - # XXX REMOVE me : - # if assign_ctx in ('Del', 'Assign'): # 'Aug' ?? - # self._save_assignment(newnode) return newnode def visit_str(self, node, parent, assign_ctx=None): diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index e5bd2b88bb..fdaca981cc 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -196,7 +196,7 @@ def _scope_lookup(self, node, name, offset=0): return pscope.scope_lookup(node, name) return builtin_lookup(name) # Module - def set_local(self): + def set_local(self, name, stmt): raise Exception('Attempted locals mutation.') # def set_local(self, name, stmt): @@ -670,7 +670,7 @@ def postinit(self, args, body): @property def instance_attrs(self): - return types.MappingProxyType(get_external_assignments(self.root(), self, collections.defaultdict(list))) + return types.MappingProxyType(get_external_assignments(self, collections.defaultdict(list))) def pytype(self): if 'method' in self.type: @@ -737,9 +737,6 @@ def __init__(self, name=None, doc=None, lineno=None, self.doc = doc # self.instance_attrs = {} super(FunctionDef, self).__init__(lineno, col_offset, parent) - # if parent: - # frame = parent.frame() - # frame.set_local(name, self) # pylint: disable=arguments-differ; different than Lambdas def postinit(self, args, body, decorators=None, returns=None): @@ -1114,8 +1111,6 @@ def __init__(self, name=None, doc=None, lineno=None, self.name = name self.doc = doc super(ClassDef, self).__init__(lineno, col_offset, parent) - # if parent is not None: - # parent.frame().set_local(name, self) def postinit(self, bases, body, decorators, newstyle=None, metaclass=None): self.bases = bases @@ -1128,7 +1123,11 @@ def postinit(self, bases, body, decorators, newstyle=None, metaclass=None): @property def locals(self): - return types.MappingProxyType(get_external_assignments(self.root(), self, get_locals(self))) + return types.MappingProxyType(get_external_assignments(self, get_locals(self))) + + @property + def instance_attrs(self): + return types.MappingProxyType(get_external_assignments(self, collections.defaultdict(list))) def _newstyle_impl(self, context=None): if context is None: @@ -1760,6 +1759,24 @@ def bool_value(self): def get_locals(node): + '''Return the local variables for an appropriate node. + + For function nodes, this will be the local variables defined in + their scope, what would be returned by a locals() call in the + function body. For Modules, this will be all the global names + defined in the module, what would be returned by a locals() or + globals() call at the module level. For classes, this will be + class attributes defined in the class body, also what a locals() + call in the body would return. + + This function starts by recursing over its argument's children to + avoid incorrectly adding a class's or function's name to its local + variables. + + ''' + if not isinstance(node, LocalsDictNodeNG): + raise TypeError("This node doesn't have local variables: %s" % + type(node)) locals_ = collections.defaultdict(list) for n in node.get_children(): _get_locals(n, locals_) @@ -1767,18 +1784,19 @@ def get_locals(node): @_singledispatch def _get_locals(node, locals_): - pass + raise TypeError('Non-astroid object in an astroid AST: %s' % type(node)) # pylint: disable=unused-variable; doesn't understand singledispatch @_get_locals.register(bases.NodeNG) def locals_generic(node, locals_): + '''Generic nodes don't create name bindings or scopes.''' for n in node.get_children(): _get_locals(n, locals_) # # pylint: disable=unused-variable; doesn't understand singledispatch @_get_locals.register(LocalsDictNodeNG) def locals_new_scope(node, locals_): - pass + '''These nodes start a new scope, so terminate recursion here.''' # pylint: disable=unused-variable; doesn't understand singledispatch @_get_locals.register(node_classes.AssignName) @@ -1786,6 +1804,9 @@ def locals_new_scope(node, locals_): @_get_locals.register(FunctionDef) @_get_locals.register(ClassDef) def locals_name(node, locals_): + '''These nodes add a name to the local variables. AssignName and + DelName have no children while FunctionDef and ClassDef start a + new scope so shouldn't be recursed into.''' locals_[node.name].append(node) # pylint: disable=unused-variable; doesn't understand singledispatch @@ -1804,9 +1825,8 @@ def locals_import(node, locals_): # pylint: disable=unused-variable; doesn't understand singledispatch @_get_locals.register(node_classes.ImportFrom) def locals_import_from(node, locals_): - _key_func = lambda node: node.fromlineno def sort_locals(my_list): - my_list.sort(key=_key_func) + my_list.sort(key=lambda node: node.fromlineno) for name, asname in node.names: if name == '*': @@ -1822,20 +1842,50 @@ def sort_locals(my_list): sort_locals(locals_[asname or name]) -def get_external_assignments(root, subject, attributes): - stack = [root] +def get_external_assignments(subject, attributes): + + '''This function takes a node and returns an object representing + attribute assignments to that node. + + It searches the whole AST for AssignAttr nodes that assign to the + object represented by that node, then returns a + collections.defaultdict(list) containing all the names and nodes + that have been assigned to the original node. + + :param subject: The node that get_external_assignments() is + searching for assignments to. This should be a node whose + attributes can be assigned to. + + :param attributes: A collections.defaultdict(list) to add names + to. If a ClassDef or Module node is being assigned to, this will + be created by get_locals() and represents the attributes defined + directly on the Module or ClassDef by the AST. Otherwise, it will + represent the instance attributes assigned to a particular object. + + ''' + # As far as I know, there are no other builtin types that allow + # attribute assignment. + if not isinstance(subject, (Module, ClassDef, Lambda)): + raise TypeError("This node doesn't represent a type allowing" + "attribute assignments: %s" % type(subject)) + stack = [subject.root()] while stack: node = stack.pop() stack.extend(node.get_children()) if isinstance(node, node_classes.AssignAttr): frame = node.frame() try: - for inferred in (n for n in node.expr.infer() if n is subject): + # Here, node.expr.infer() will return either the node + # being assigned to itself, for Module, ClassDef, + # FunctionDef, or Lambda nodes, or an Instance object + # corresponding to a ClassDef node. + for inferred in (i for i in node.expr.infer() if i is subject + or getattr(i, '_proxied', None) is subject): values = attributes[node.attrname] if node in values: continue else: - if (values and frame.is_function and + if (values and isinstance(frame, FunctionDef) and frame.name == '__init__' and not values[0].frame().name == '__init__'): values.insert(0, node) diff --git a/astroid/util.py b/astroid/util.py index 79f14cc0bf..eb8148daca 100644 --- a/astroid/util.py +++ b/astroid/util.py @@ -29,7 +29,8 @@ def lazy_import(module_name): - return lazy_object_proxy.Proxy(lambda: importlib.import_module(module_name)) + return lazy_object_proxy.Proxy( + lambda: importlib.import_module('.' + module_name, 'astroid')) def reraise(exception): '''Reraises an exception with the traceback from the current exception From 9192cdeb02557638d3cac4a5ce4b1719a7127905 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Sun, 11 Oct 2015 21:44:29 -0400 Subject: [PATCH 006/312] Use namedtuple template for its inferred representation, fix some tests --- astroid/__init__.py | 7 +- astroid/bases.py | 514 ---------------------------- astroid/brain/py2stdlib.py | 91 +++-- astroid/decorators.py | 42 +++ astroid/node_classes.py | 490 +++++++++++++++++++++++++- astroid/scoped_nodes.py | 22 +- astroid/tests/unittest_brain.py | 7 +- astroid/tests/unittest_inference.py | 4 + astroid/util.py | 18 + 9 files changed, 612 insertions(+), 583 deletions(-) diff --git a/astroid/__init__.py b/astroid/__init__.py index 25f00bc4f6..0965623957 100644 --- a/astroid/__init__.py +++ b/astroid/__init__.py @@ -113,10 +113,11 @@ def transform(node, infer_function=infer_function): def register_module_extender(manager, module_name, get_extension_mod): - def transform(node): + def transform(module): extension_module = get_extension_mod() - for name, obj in extension_module.locals.items(): - node.locals[name] = obj + # for name, obj in extension_module.locals.items(): + # node.locals[name] = obj + module.body.extend(extension_module.body) manager.register_transform(Module, transform, lambda n: n.name == module_name) diff --git a/astroid/bases.py b/astroid/bases.py index 00fe66b7be..98622e7719 100644 --- a/astroid/bases.py +++ b/astroid/bases.py @@ -437,517 +437,3 @@ def __repr__(self): def __str__(self): return 'Generator(%s)' % (self._proxied.name) - - -# decorators ################################################################## - -def path_wrapper(func): - """return the given infer function wrapped to handle the path""" - # TODO: switch this to wrapt after the monkey-patching is fixed (ceridwen) - @functools.wraps(func) - def wrapped(node, context=None, _func=func, **kwargs): - """wrapper function handling context""" - if context is None: - context = contextmod.InferenceContext() - if context.push(node): - return - - yielded = set() - for res in _func(node, context, **kwargs): - # unproxy only true instance, not const, tuple, dict... - if res.__class__ is Instance: - ares = res._proxied - else: - ares = res - if ares not in yielded: - yield res - yielded.add(ares) - return wrapped - -@wrapt.decorator -def yes_if_nothing_inferred(func, instance, args, kwargs): - inferred = False - for node in func(*args, **kwargs): - inferred = True - yield node - if not inferred: - yield util.YES - -@wrapt.decorator -def raise_if_nothing_inferred(func, instance, args, kwargs): - inferred = False - for node in func(*args, **kwargs): - inferred = True - yield node - if not inferred: - raise exceptions.InferenceError() - - -# Node ###################################################################### - -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] - else: - 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 - - @decoratorsmod.cachedproperty - def fromlineno(self): - if self.lineno is None: - return self._fixed_source_line() - else: - return self.lineno - - @decoratorsmod.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 - else: - return lastchild.tolineno - - # TODO / FIXME: - assert self.fromlineno is not None, self - assert self.tolineno is not None, self - - 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(self.__class__.__name__) - - 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 instanciate_class(self): - """instanciate 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 len(node) == 0: - 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 + ' 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 len(fields) == 0: - 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 - * YES: the inference engine is uncertain of the - node's value. - """ - return util.YES - - -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] diff --git a/astroid/brain/py2stdlib.py b/astroid/brain/py2stdlib.py index 98c251595e..841512f5f8 100644 --- a/astroid/brain/py2stdlib.py +++ b/astroid/brain/py2stdlib.py @@ -6,6 +6,7 @@ * hashlib.md5 and hashlib.sha1 """ +from collections import _class_template, _repr_template, _field_template import functools import sys from textwrap import dedent @@ -22,19 +23,21 @@ PY33 = sys.version_info >= (3, 3) PY34 = sys.version_info >= (3, 4) -# general function +def infer_first(node, context): + try: + value = next(node.infer(context=context)) + if value is util.YES: + raise UseInferenceDefault() + else: + return value + except StopIteration: + raise InferenceError() def infer_func_form(node, base_type, context=None, enum=False): - """Specific inference function for namedtuple or Python 3 enum. """ - def infer_first(node): - try: - value = next(node.infer(context=context)) - if value is util.YES: - raise UseInferenceDefault() - else: - return value - except StopIteration: - raise InferenceError() + """Specific inference function for namedtuple or Python 3 enum. + + :param node: A Call node. + """ # node is a Call node, class name as first argument and generated class # attributes as second argument @@ -44,19 +47,19 @@ def infer_first(node): # namedtuple or enums list of attributes can be a list of strings or a # whitespace-separate string try: - name = infer_first(node.args[0]).value - names = infer_first(node.args[1]) + name = infer_first(node.args[0], context).value + names = infer_first(node.args[1], context) try: attributes = names.value.replace(',', ' ').split() except AttributeError: if not enum: - attributes = [infer_first(const).value for const in names.elts] + attributes = [infer_first(const, context).value for const in names.elts] else: # Enums supports either iterator of (name, value) pairs # or mappings. # TODO: support only list, tuples and mappings. if hasattr(names, 'items') and isinstance(names.items, list): - attributes = [infer_first(const[0]).value + attributes = [infer_first(const[0], context).value for const in names.items if isinstance(const[0], nodes.Const)] elif hasattr(names, 'elts'): @@ -65,11 +68,11 @@ def infer_first(node): # be mixed. if all(isinstance(const, nodes.Tuple) for const in names.elts): - attributes = [infer_first(const.elts[0]).value + attributes = [infer_first(const.elts[0], context).value for const in names.elts if isinstance(const, nodes.Tuple)] else: - attributes = [infer_first(const).value + attributes = [infer_first(const, context).value for const in names.elts] else: raise AttributeError @@ -83,6 +86,7 @@ def infer_first(node): # set base class=tuple class_node.bases.append(base_type) # XXX add __init__(*attributes) method + # print(class_node.repr_tree()) for attr in attributes: fake_node = nodes.EmptyNode() fake_node.parent = class_node @@ -268,25 +272,38 @@ def _looks_like(node, name): def infer_named_tuple(node, context=None): """Specific inference function for namedtuple Call node""" - class_node, name, attributes = infer_func_form(node, nodes.Tuple._proxied, - context=context) - fake = AstroidBuilder(MANAGER).string_build(''' -class %(name)s(tuple): - _fields = %(fields)r - def _asdict(self): - return self.__dict__ - @classmethod - def _make(cls, iterable, new=tuple.__new__, len=len): - return new(cls, iterable) - def _replace(self, **kwds): - return self - ''' % {'name': name, 'fields': attributes}) - class_node.locals['_asdict'] = fake.body[0].locals['_asdict'] - class_node.locals['_make'] = fake.body[0].locals['_make'] - class_node.locals['_replace'] = fake.body[0].locals['_replace'] - class_node.locals['_fields'] = fake.body[0].locals['_fields'] - # we use UseInferenceDefault, we can't be a generator so return an iterator - return iter([class_node]) + + # node is a Call node, class name as first argument and generated class + # attributes as second argument + if len(node.args) != 2: + # something weird here, go back to class implementation + raise UseInferenceDefault() + # namedtuple or enums list of attributes can be a list of strings or a + # whitespace-separate string + try: + type_name = infer_first(node.args[0], context).value + fields = infer_first(node.args[1], context) + try: + field_names = tuple(fields.value.replace(',', ' ').split()) + except AttributeError: + field_names = tuple(infer_first(const, context).value for const in fields.elts) + except (AttributeError, exceptions.InferenceError): + raise UseInferenceDefault() + + class_definition = _class_template.format( + typename = type_name, + field_names = field_names, + num_fields = len(field_names), + arg_list = repr(field_names).replace("'", "")[1:-1], + repr_fmt = ', '.join(_repr_template.format(name=name) + for name in field_names), + field_defs = '\n'.join(_field_template.format(index=index, name=name) + for index, name in enumerate(field_names)) + ) + + # TODO: maybe memoize this call for efficiency, if it's needed. + namedtuple_node = AstroidBuilder(MANAGER).string_build(class_definition).body[3] + return iter([namedtuple_node]) def infer_enum(node, context=None): @@ -333,7 +350,7 @@ def name(self): return %(name)r ''' % {'name': target.name, 'types': ', '.join(node.basenames)}) fake = AstroidBuilder(MANAGER).string_build(classdef)[target.name] - print(fake.repr_tree()) + # print(fake.repr_tree()) fake.parent = target.parent for method in node.mymethods(): fake.locals[method.name] = [method] diff --git a/astroid/decorators.py b/astroid/decorators.py index 2303f41e8d..b1e4b9f4f9 100644 --- a/astroid/decorators.py +++ b/astroid/decorators.py @@ -75,3 +75,45 @@ def __get__(self, inst, objtype=None): val = self.wrapped(inst) setattr(inst, self.wrapped.__name__, val) return val + + +def path_wrapper(func): + """return the given infer function wrapped to handle the path""" + # TODO: switch this to wrapt after the monkey-patching is fixed (ceridwen) + @functools.wraps(func) + def wrapped(node, context=None, _func=func, **kwargs): + """wrapper function handling context""" + if context is None: + context = contextmod.InferenceContext() + if context.push(node): + return + + yielded = set() + for res in _func(node, context, **kwargs): + # unproxy only true instance, not const, tuple, dict... + if res.__class__ is Instance: + ares = res._proxied + else: + ares = res + if ares not in yielded: + yield res + yielded.add(ares) + return wrapped + +@wrapt.decorator +def yes_if_nothing_inferred(func, instance, args, kwargs): + inferred = False + for node in func(*args, **kwargs): + inferred = True + yield node + if not inferred: + yield util.YES + +@wrapt.decorator +def raise_if_nothing_inferred(func, instance, args, kwargs): + inferred = False + for node in func(*args, **kwargs): + inferred = True + yield node + if not inferred: + raise exceptions.InferenceError() diff --git a/astroid/node_classes.py b/astroid/node_classes.py index f1c5592eda..5c23566bb6 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -124,6 +124,477 @@ def _container_getitem(instance, elts, index): return elts[index] +# Node ###################################################################### + +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] + else: + 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 + + @decoratorsmod.cachedproperty + def fromlineno(self): + if self.lineno is None: + return self._fixed_source_line() + else: + return self.lineno + + @decoratorsmod.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 + else: + return lastchild.tolineno + + # TODO / FIXME: + assert self.fromlineno is not None, self + assert self.tolineno is not None, self + + 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(self.__class__.__name__) + + 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 instanciate_class(self): + """instanciate 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 len(node) == 0: + 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('' % + (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 len(fields) == 0: + 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 + * YES: the inference engine is uncertain of the + node's value. + """ + return util.YES + + +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, bases.NodeNG, @@ -357,7 +828,7 @@ class Arguments(mixins.AssignTypeMixin, bases.NodeNG): kwargannotation = None else: _astroid_fields = ('args', 'defaults', 'kwonlyargs', 'kw_defaults') - _other_fields = ('vararg', 'kwarg') + _other_fields = ('vararg', 'kwarg') def __init__(self, vararg=None, kwarg=None, parent=None): self.vararg = vararg @@ -1376,23 +1847,6 @@ def const_factory(value): # Backward-compatibility aliases -def _instancecheck(cls, other): - wrapped = cls.__wrapped__ - other_cls = other.__class__ - is_instance_of = wrapped is other_cls or issubclass(other_cls, wrapped) - warnings.warn("%r is deprecated and slated for removal in astroid " - "2.0, use %r instead" % (cls.__class__.__name__, - wrapped.__name__), - PendingDeprecationWarning, stacklevel=2) - return is_instance_of - - -def proxy_alias(alias_name, node_type): - proxy = type(alias_name, (lazy_object_proxy.Proxy,), - {'__class__': object.__dict__['__class__'], - '__instancecheck__': _instancecheck}) - return proxy(lambda: node_type) - Backquote = proxy_alias('Backquote', Repr) Discard = proxy_alias('Discard', Expr) AssName = proxy_alias('AssName', AssignName) diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index fdaca981cc..47d77a5722 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -141,6 +141,7 @@ def builtin_lookup(name): stmts = builtin_astroid.locals[name] except KeyError: stmts = () + # print(stmts) return builtin_astroid, stmts @@ -1812,6 +1813,7 @@ def locals_name(node, locals_): # pylint: disable=unused-variable; doesn't understand singledispatch @_get_locals.register(node_classes.Arguments) def locals_arguments(node, locals_): + '''Other names assigned by functions have AssignName nodes.''' locals_[node.vararg].append(node) locals_[node.kwarg].append(node) @@ -1843,7 +1845,6 @@ def sort_locals(my_list): def get_external_assignments(subject, attributes): - '''This function takes a node and returns an object representing attribute assignments to that node. @@ -1857,17 +1858,19 @@ def get_external_assignments(subject, attributes): attributes can be assigned to. :param attributes: A collections.defaultdict(list) to add names - to. If a ClassDef or Module node is being assigned to, this will - be created by get_locals() and represents the attributes defined - directly on the Module or ClassDef by the AST. Otherwise, it will - represent the instance attributes assigned to a particular object. + to. If a ClassDef or Module node is being assigned to, this + will be created by get_locals() and represents the attributes + defined directly on the Module or ClassDef by the AST. + Otherwise, it will represent the instance attributes assigned + to a particular object and initially be empty. ''' # As far as I know, there are no other builtin types that allow - # attribute assignment. + # attribute assignment. However, Instances can also be proxies of + # nodes representing builtin types, and they're handled by simply + # returning an empty mapping. if not isinstance(subject, (Module, ClassDef, Lambda)): - raise TypeError("This node doesn't represent a type allowing" - "attribute assignments: %s" % type(subject)) + return attributes stack = [subject.root()] while stack: node = stack.pop() @@ -1885,6 +1888,9 @@ def get_external_assignments(subject, attributes): if node in values: continue else: + # I have no idea why there's a special case + # for __init__ that changes the order of the + # attributes or what that order means. if (values and isinstance(frame, FunctionDef) and frame.name == '__init__' and not values[0].frame().name == '__init__'): diff --git a/astroid/tests/unittest_brain.py b/astroid/tests/unittest_brain.py index 02c405bc5f..4bcc84a649 100644 --- a/astroid/tests/unittest_brain.py +++ b/astroid/tests/unittest_brain.py @@ -104,7 +104,8 @@ class X(namedtuple(name, fields)): for base in klass.ancestors(): if base.name == 'X': break - self.assertSetEqual({"a", "b", "c"}, set(base.instance_attrs)) + self.assertSetEqual({"a", "b", "c"}, + set(base.instanciate_class().instance_attrs)) def test_namedtuple_inference_failure(self): klass = test_utils.extract_node(""" @@ -115,8 +116,8 @@ def foo(fields): """) self.assertIs(util.YES, next(klass.infer())) - @unittest.skipIf(sys.version_info[0] > 2, - 'namedtuple inference is broken on Python 3') + # @unittest.skipIf(sys.version_info[0] > 2, + # 'namedtuple inference is broken on Python 3') def test_namedtuple_advanced_inference(self): # urlparse return an object of class ParseResult, which has a # namedtuple call and a mixin as base classes diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index 8b6d143680..91d709727e 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -17,6 +17,8 @@ # with astroid. If not, see . """tests for the astroid inference capabilities """ +from __future__ import print_function + import os import sys from functools import partial @@ -3111,6 +3113,8 @@ def test(self): getattr(self, 'test') #@ ''') + # for n in ast_nodes: + # print(n.repr_tree(), file=sys.stderr) first = next(ast_nodes[0].infer()) self.assertIsInstance(first, BoundMethod) self.assertEqual(first.bound.name, 'A') diff --git a/astroid/util.py b/astroid/util.py index eb8148daca..5fb1434005 100644 --- a/astroid/util.py +++ b/astroid/util.py @@ -53,3 +53,21 @@ def __getattribute__(self, name): def __call__(self, *args, **kwargs): return self + + +def _instancecheck(cls, other): + wrapped = cls.__wrapped__ + other_cls = other.__class__ + is_instance_of = wrapped is other_cls or issubclass(other_cls, wrapped) + warnings.warn("%r is deprecated and slated for removal in astroid " + "2.0, use %r instead" % (cls.__class__.__name__, + wrapped.__name__), + PendingDeprecationWarning, stacklevel=2) + return is_instance_of + + +def proxy_alias(alias_name, node_type): + proxy = type(alias_name, (lazy_object_proxy.Proxy,), + {'__class__': object.__dict__['__class__'], + '__instancecheck__': _instancecheck}) + return proxy(lambda: node_type) From c13955f76187efe0bfef6f95aa7fbe8f0ec39778 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Mon, 12 Oct 2015 13:09:15 -0400 Subject: [PATCH 007/312] Fix locals for the builtins --- astroid/brain/py2stdlib.py | 2 ++ astroid/node_classes.py | 4 ++-- astroid/raw_building.py | 36 +++++++++++++++++++++++++------ astroid/scoped_nodes.py | 15 +++++++++---- astroid/tests/unittest_manager.py | 2 +- 5 files changed, 45 insertions(+), 14 deletions(-) diff --git a/astroid/brain/py2stdlib.py b/astroid/brain/py2stdlib.py index 0ed641aa56..a63884cd01 100644 --- a/astroid/brain/py2stdlib.py +++ b/astroid/brain/py2stdlib.py @@ -283,6 +283,8 @@ def infer_named_tuple(node, context=None): try: type_name = infer_first(node.args[0], context).value fields = infer_first(node.args[1], context) + # TODO: this doesn't handle the case where duplicate field + # names are replaced using namedtuple's rename keyword. try: field_names = tuple(fields.value.replace(',', ' ').split()) except AttributeError: diff --git a/astroid/node_classes.py b/astroid/node_classes.py index ff3c2051ff..4fb9242cfe 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -342,14 +342,14 @@ def nearest(self, nodes): # these are lazy because they're relatively expensive to compute for every # single node, and they rarely get looked at - @decoratorsmod.cachedproperty + @decorators.cachedproperty def fromlineno(self): if self.lineno is None: return self._fixed_source_line() else: return self.lineno - @decoratorsmod.cachedproperty + @decorators.cachedproperty def tolineno(self): if not self._astroid_fields: # can't have children diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 18e46309b0..a5db080d27 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -19,6 +19,7 @@ (build_* functions) or from living object (object_build_* functions) """ +import collections import inspect import logging import os @@ -31,6 +32,7 @@ from astroid import manager from astroid import node_classes from astroid import nodes +from astroid import scoped_nodes MANAGER = manager.AstroidManager() @@ -96,9 +98,9 @@ def attach_import_node(node, modname, membername): _attach_local_node(node, from_node, membername) -def build_module(name, doc=None): +def build_module(name, doc=None, modclass=nodes.Module): """create and initialize a astroid Module node""" - node = nodes.Module(name, doc, pure_python=False) + node = modclass(name, doc, pure_python=False) node.package = False node.parent = None return node @@ -195,7 +197,7 @@ def object_build_methoddescriptor(node, member, localname): # and empty argument list func.args.args = None node.add_local_node(func, localname) - _add_dunder_class(func, member) + # _add_dunder_class(func, member) def _base_class_object_build(node, member, basenames, name=None, localname=None): @@ -258,7 +260,8 @@ def __init__(self): self._done = {} self._module = None - def inspect_build(self, module, modname=None, path=None): + def inspect_build(self, module, modname=None, modclass=nodes.Module, + path=None): """build astroid from a living module (i.e. using inspect) this is used when there is no python source code available (either because it's a built-in module or because the .py is not available) @@ -267,10 +270,10 @@ def inspect_build(self, module, modname=None, path=None): if modname is None: modname = module.__name__ try: - node = build_module(modname, module.__doc__) + node = build_module(modname, module.__doc__, modclass=modclass) except AttributeError: # in jython, java modules have no __doc__ (see #109562) - node = build_module(modname) + node = build_module(modname, modclass=modclass) node.file = node.path = path and os.path.abspath(path) or path node.name = modname MANAGER.cache_module(node) @@ -375,6 +378,9 @@ def imported_member(self, node, member, name): ### astroid bootstrapping ###################################################### Astroid_BUILDER = InspectBuilder() +class Builtins(nodes.Module): + pass + _CONST_PROXY = {} def _astroid_bootstrapping(astroid_builtin=None): """astroid boot strapping the builtins module""" @@ -382,7 +388,7 @@ def _astroid_bootstrapping(astroid_builtin=None): # inspect_build builtins, and then we can proxy Const if astroid_builtin is None: from six.moves import builtins - astroid_builtin = Astroid_BUILDER.inspect_build(builtins) + astroid_builtin = Astroid_BUILDER.inspect_build(builtins, modclass=Builtins) for cls, node_cls in node_classes.CONST_CLS.items(): if cls is type(None): @@ -400,6 +406,22 @@ def _astroid_bootstrapping(astroid_builtin=None): _astroid_bootstrapping() + +@scoped_nodes.get_locals.register(Builtins) +def scoped_node(node): + locals_ = collections.defaultdict(list) + for name in ('Ellipsis', 'False', 'None', 'NotImplemented', + 'True', '__debug__', '__package__', '__spec__', 'copyright', + 'credits', 'exit', 'help', 'license', 'quit'): + for child in (n for n in node.body if + isinstance(n, (nodes.Const, nodes.EmptyNode))): + if child.name == name: + locals_[name].append(child) + for n in node.get_children(): + scoped_nodes._get_locals(n, locals_) + return locals_ + + # TODO : find a nicer way to handle this situation; # However __proxied introduced an # infinite recursion (see https://bugs.launchpad.net/pylint/+bug/456870) diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index 6bc355b762..6dac89bc59 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -1761,6 +1761,7 @@ def bool_value(self): return True +@_singledispatch def get_locals(node): '''Return the local variables for an appropriate node. @@ -1777,20 +1778,26 @@ class attributes defined in the class body, also what a locals() variables. ''' - if not isinstance(node, LocalsDictNodeNG): - raise TypeError("This node doesn't have local variables: %s" % - type(node)) + raise TypeError("This isn't an astroid node: %s" % type(node)) + +@get_locals.register(node_classes.NodeNG) +def not_scoped_node(node): + raise TypeError("This node doesn't have local variables: %s" % type(node)) + +@get_locals.register(LocalsDictNodeNG) +def scoped_node(node): locals_ = collections.defaultdict(list) for n in node.get_children(): _get_locals(n, locals_) return locals_ + @_singledispatch def _get_locals(node, locals_): raise TypeError('Non-astroid object in an astroid AST: %s' % type(node)) # pylint: disable=unused-variable; doesn't understand singledispatch -@_get_locals.register(bases.NodeNG) +@_get_locals.register(node_classes.NodeNG) def locals_generic(node, locals_): '''Generic nodes don't create name bindings or scopes.''' for n in node.get_children(): diff --git a/astroid/tests/unittest_manager.py b/astroid/tests/unittest_manager.py index 7e4eeae7d9..f0f2d68419 100644 --- a/astroid/tests/unittest_manager.py +++ b/astroid/tests/unittest_manager.py @@ -74,7 +74,7 @@ def test_ast_from_file_name_astro_builder_exception(self): def test_do_not_expose_main(self): obj = self.manager.ast_from_module_name('__main__') self.assertEqual(obj.name, '__main__') - self.assertEqual(obj.items(), []) + self.assertEqual(obj.items(), {}.items()) def test_ast_from_module_name(self): astroid = self.manager.ast_from_module_name('unittest') From aadcb1a58230aa9aba5be221cfd75170db43c124 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Thu, 15 Oct 2015 12:11:14 -0400 Subject: [PATCH 008/312] Make instance_attrs mutable again, add external_attrs, refactor delayed assignments --- astroid/builder.py | 59 ++++++++++++++++++++++++++ astroid/scoped_nodes.py | 91 ++++++++++------------------------------- 2 files changed, 80 insertions(+), 70 deletions(-) diff --git a/astroid/builder.py b/astroid/builder.py index a390838d51..594b3892bf 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -30,6 +30,7 @@ from astroid import exceptions from astroid import manager from astroid import modutils +from astroid import nodes from astroid import raw_building from astroid import rebuilder from astroid import util @@ -149,6 +150,7 @@ def _post_build(self, module, encoding): """Handles encoding and delayed nodes after a module has been built""" module.file_encoding = encoding self._manager.cache_module(module) + delayed_assignments(module) # Visit the transforms if self._apply_transforms: @@ -182,6 +184,63 @@ def _data_build(self, data, modname, path): return module +def delayed_assignments(root): + '''This function modifies nodes according to AssignAttr nodes. + + It traverses the entire AST, and when it encounters an AssignAttr + node it modifies the instance_attrs or external_attrs of the node + respresenting that object. Because it uses inference functions + that in turn depend on instance_attrs and external_attrs, calling + it a tree that already have instance_attrs and external_attrs set + may crash or fail to modify those variables correctly. + + :param root: The root of the AST that delayed_assignments() is + searching for assignments. + + ''' + stack = [root] + while stack: + node = stack.pop() + stack.extend(node.get_children()) + if isinstance(node, nodes.AssignAttr): + # print('Method:', node.infer) + # print('Inferred:', [i for i in node.expr.infer()]) + frame = node.frame() + try: + # Here, node.expr.infer() will return either the node + # being assigned to itself, for Module, ClassDef, + # FunctionDef, or Lambda nodes, or an Instance object + # corresponding to a ClassDef node. + for inferred in node.expr.infer(): + if type(inferred) is bases.Instance: + values = inferred._proxied.instance_attrs[node.attrname] + elif isinstance(inferred, nodes.Lambda): + values = inferred.instance_attrs[node.attrname] + elif isinstance(inferred, (nodes.Module, nodes.ClassDef)): + values = inferred.external_attrs[node.attrname] + else: + continue + if node in values: + continue + else: + # print('Inferred:', repr(inferred)) + # if values: + # print('Locals or instance_attrs:', values) + # print('Frame:', repr(frame)) + # print('Assignment:', node) + + # I have no idea why there's a special case + # for __init__ that changes the order of the + # attributes or what that order means. + if (values and frame.name == '__init__' and not + values[0].frame().name == '__init__'): + values.insert(0, node) + else: + values.append(node) + except exceptions.InferenceError: + pass + + def parse(code, module_name='', path=None, apply_transforms=True): """Parses a source string in order to obtain an astroid AST from it diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index 6dac89bc59..28d0ede3ab 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -27,6 +27,7 @@ import collections import io import itertools +import pprint import types import warnings @@ -141,7 +142,7 @@ def builtin_lookup(name): stmts = builtin_astroid.locals[name] except KeyError: stmts = () - # print(stmts) + # print(repr(builtin_astroid), name, stmts) return builtin_astroid, stmts @@ -183,6 +184,10 @@ def scope(self): def _scope_lookup(self, node, name, offset=0): """XXX method for interfacing the scope lookup""" + # print(repr(self)) + # pprint.pprint(dict(self.locals)) + # print(node) + # print(name) try: stmts = node._filter_stmts(self.locals[name], self, offset) except KeyError: @@ -315,6 +320,7 @@ def __init__(self, name, doc, file=None, path=None, package=None, # self.locals = self.globals = {} self.body = [] # self.future_imports = set() + self.external_attrs = collections.defaultdict(list) def postinit(self, body=None): self.body = body @@ -661,18 +667,18 @@ class Lambda(mixins.FilterStmtsMixin, LocalsDictNodeNG): type = 'function' def __init__(self, lineno=None, col_offset=None, parent=None): - # self.locals = {} self.args = [] self.body = [] + self.instance_attrs = collections.defaultdict(list) super(Lambda, self).__init__(lineno, col_offset, parent) def postinit(self, args, body): self.args = args self.body = body - @property - def instance_attrs(self): - return types.MappingProxyType(get_external_assignments(self, collections.defaultdict(list))) + # @property + # def instance_attrs(self): + # return types.MappingProxyType(get_external_assignments(self, collections.defaultdict(list))) def pytype(self): if 'method' in self.type: @@ -1107,12 +1113,13 @@ class ClassDef(mixins.FilterStmtsMixin, LocalsDictNodeNG, def __init__(self, name=None, doc=None, lineno=None, col_offset=None, parent=None): - # self.instance_attrs = {} # self.locals = {} self.bases = [] self.body = [] self.name = name self.doc = doc + self.instance_attrs = collections.defaultdict(list) + self.external_attrs = collections.defaultdict(list) super(ClassDef, self).__init__(lineno, col_offset, parent) def postinit(self, bases, body, decorators, newstyle=None, metaclass=None): @@ -1126,11 +1133,11 @@ def postinit(self, bases, body, decorators, newstyle=None, metaclass=None): @property def locals(self): - return types.MappingProxyType(get_external_assignments(self, get_locals(self))) + return get_locals(self) - @property - def instance_attrs(self): - return types.MappingProxyType(get_external_assignments(self, collections.defaultdict(list))) + # @property + # def instance_attrs(self): + # return types.MappingProxyType(get_external_assignments(self, collections.defaultdict(list))) def _newstyle_impl(self, context=None): if context is None: @@ -1823,8 +1830,10 @@ def locals_name(node, locals_): @_get_locals.register(node_classes.Arguments) def locals_arguments(node, locals_): '''Other names assigned by functions have AssignName nodes.''' - locals_[node.vararg].append(node) - locals_[node.kwarg].append(node) + if node.vararg: + locals_[node.vararg].append(node) + if node.kwarg: + locals_[node.kwarg].append(node) # pylint: disable=unused-variable; doesn't understand singledispatch @_get_locals.register(node_classes.Import) @@ -1853,64 +1862,6 @@ def sort_locals(my_list): sort_locals(locals_[asname or name]) -def get_external_assignments(subject, attributes): - '''This function takes a node and returns an object representing - attribute assignments to that node. - - It searches the whole AST for AssignAttr nodes that assign to the - object represented by that node, then returns a - collections.defaultdict(list) containing all the names and nodes - that have been assigned to the original node. - - :param subject: The node that get_external_assignments() is - searching for assignments to. This should be a node whose - attributes can be assigned to. - - :param attributes: A collections.defaultdict(list) to add names - to. If a ClassDef or Module node is being assigned to, this - will be created by get_locals() and represents the attributes - defined directly on the Module or ClassDef by the AST. - Otherwise, it will represent the instance attributes assigned - to a particular object and initially be empty. - - ''' - # As far as I know, there are no other builtin types that allow - # attribute assignment. However, Instances can also be proxies of - # nodes representing builtin types, and they're handled by simply - # returning an empty mapping. - if not isinstance(subject, (Module, ClassDef, Lambda)): - return attributes - stack = [subject.root()] - while stack: - node = stack.pop() - stack.extend(node.get_children()) - if isinstance(node, node_classes.AssignAttr): - frame = node.frame() - try: - # Here, node.expr.infer() will return either the node - # being assigned to itself, for Module, ClassDef, - # FunctionDef, or Lambda nodes, or an Instance object - # corresponding to a ClassDef node. - for inferred in (i for i in node.expr.infer() if i is subject - or getattr(i, '_proxied', None) is subject): - values = attributes[node.attrname] - if node in values: - continue - else: - # I have no idea why there's a special case - # for __init__ that changes the order of the - # attributes or what that order means. - if (values and isinstance(frame, FunctionDef) and - frame.name == '__init__' and not - values[0].frame().name == '__init__'): - values.insert(0, node) - else: - values.append(node) - except exceptions.InferenceError: - pass - return attributes - - # Backwards-compatibility aliases Class = util.proxy_alias('Class', ClassDef) Function = util.proxy_alias('Function', FunctionDef) From f898c77ef632e84e68468c06f273116544017e8b Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Thu, 15 Oct 2015 14:57:59 -0400 Subject: [PATCH 009/312] Fix line ending --- astroid/tests/unittest_scoped_nodes.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index bf090dabb9..a68b3b8b3d 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -685,8 +685,7 @@ def test_methods(self): expected_methods = {'__init__', 'class_method', 'method', 'static_method'} klass2 = self.module['YOUPI'] methods = {m.name for m in klass2.methods()} - self.assertTrue( - methods.issuperset(expected_methods)) + self.assertTrue(methods.issuperset(expected_methods)) methods = {m.name for m in klass2.mymethods()} self.assertSetEqual(expected_methods, methods) klass2 = self.module2['Specialization'] From 1784ce236fe5654675f034b9638e6cd3ade2e418 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Fri, 23 Oct 2015 18:02:23 -0400 Subject: [PATCH 010/312] Transfer build_function and ast_from_function from modular_locals to zipper --- astroid/__pkginfo__.py | 5 +- astroid/bases.py | 12 +- astroid/builder.py | 5 +- astroid/mixins.py | 15 +- astroid/modutils.py | 5 +- astroid/node_classes.py | 75 ++--- astroid/raw_building.py | 392 ++++++++++++++++--------- astroid/rebuilder.py | 22 +- astroid/scoped_nodes.py | 133 ++++++--- astroid/tests/unittest_builder.py | 4 +- astroid/tests/unittest_manager.py | 2 +- astroid/tests/unittest_raw_building.py | 26 +- astroid/tests/unittest_scoped_nodes.py | 3 + astroid/util.py | 20 +- tox.ini | 20 +- 15 files changed, 465 insertions(+), 274 deletions(-) diff --git a/astroid/__pkginfo__.py b/astroid/__pkginfo__.py index 17aa9493e2..8a3188141b 100644 --- a/astroid/__pkginfo__.py +++ b/astroid/__pkginfo__.py @@ -27,8 +27,11 @@ if sys.version_info >= (3, 4): install_requires = ['lazy_object_proxy', 'six', 'wrapt'] -else: +elif sys.version_info == (3, 3): install_requires = ['lazy_object_proxy', 'singledispatch', 'six', 'wrapt'] +else: + install_requires = ['funcigs', 'lazy_object_proxy', 'singledispatch', 'six', + 'wrapt'] license = 'LGPL' diff --git a/astroid/bases.py b/astroid/bases.py index f5bd9a9692..2048186db7 100644 --- a/astroid/bases.py +++ b/astroid/bases.py @@ -357,7 +357,8 @@ def _infer_type_new_call(self, caller, context): if not isinstance(attrs, node_classes.Dict): # Needs to be a dictionary. return - cls_locals = collections.defaultdict(list) + # cls_locals = collections.defaultdict(list) + body = [] for key, value in attrs.items: key = next(key.infer(context=context)) value = next(value.infer(context=context)) @@ -367,7 +368,12 @@ def _infer_type_new_call(self, caller, context): if not isinstance(key.value, str): # Not a proper attribute. return - cls_locals[key.value].append(value) + assign = node_classes.Assign() + assign.postinit(targets=node_classes.AssignName(key.value), + value=value) + body.append[assign] + # cls_locals[key.value].append(value) + # print(attrs.repr_tree()) # Build the class from now. cls = mcs.__class__(name=name.value, lineno=caller.lineno, @@ -376,7 +382,7 @@ def _infer_type_new_call(self, caller, context): empty = node_classes.Pass() cls.postinit(bases=bases.elts, body=[empty], decorators=[], newstyle=True, metaclass=mcs) - cls.locals = cls_locals + # cls.locals = cls_locals return cls def infer_call_result(self, caller, context=None): diff --git a/astroid/builder.py b/astroid/builder.py index 594b3892bf..9614b03964 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -194,8 +194,9 @@ def delayed_assignments(root): it a tree that already have instance_attrs and external_attrs set may crash or fail to modify those variables correctly. - :param root: The root of the AST that delayed_assignments() is - searching for assignments. + Args: + root (node_classes.NodeNG): The root of the AST that + delayed_assignments() is searching for assignments. ''' stack = [root] diff --git a/astroid/mixins.py b/astroid/mixins.py index 9f5f9538b5..dcfade180f 100644 --- a/astroid/mixins.py +++ b/astroid/mixins.py @@ -60,10 +60,7 @@ 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) + rename_warning((type(self).__name__, type(self).__name__)) return self.assign_type() @@ -73,10 +70,7 @@ 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) + rename_warning((type(self).__name__, type(self).__name__)) return self.assign_type() def _get_filtered_stmts(self, lookup_node, node, _stmts, mystmt): @@ -96,10 +90,7 @@ def assign_type(self): return self.parent.assign_type() 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) + rename_warning((type(self).__name__, type(self).__name__)) return self.assign_type() diff --git a/astroid/modutils.py b/astroid/modutils.py index e127d845e0..4c0a9ecb00 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -40,6 +40,8 @@ except ImportError: pkg_resources = None +from astroid import util + PY_ZIPMODULE = object() if sys.platform.startswith('win'): @@ -79,7 +81,6 @@ STD_LIB_DIRS = set() EXT_LIB_DIR = get_python_lib() -IS_JYTHON = platform.python_implementation() == 'Jython' BUILTIN_MODULES = dict.fromkeys(sys.builtin_module_names, True) @@ -92,7 +93,7 @@ def _normalize_path(path): return os.path.normcase(os.path.abspath(path)) -def _path_from_filename(filename, is_jython=IS_JYTHON): +def _path_from_filename(filename, is_jython=util.JYTHON): if not is_jython: if sys.version_info > (3, 0): return filename diff --git a/astroid/node_classes.py b/astroid/node_classes.py index 4fb9242cfe..c7e83b39bc 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -420,10 +420,7 @@ def inferred(self): 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) + util.rename_warning((type(self).__name__, type(self).__name__)) return self.inferred() def instanciate_class(self): @@ -446,23 +443,19 @@ 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. + Args: + ids (bool): If true, includes the ids with the node type names. + include_linenos (bool): If true, includes the line numbers and + column offsets. + ast_state (bool): If true, includes information derived from + the whole AST like local and global variables. + indent (str): A string to use to indent the output string. + max_depth (int): If set to a positive integer, won't return + nodes deeper than max_depth in the string. + max_width (int): 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): @@ -826,8 +819,8 @@ class Arguments(mixins.AssignTypeMixin, NodeNG): # annotation, its value will be None. _astroid_fields = ('args', 'defaults', 'kwonlyargs', - 'kw_defaults', 'annotations', 'varargannotation', - 'kwargannotation') + 'kw_defaults', 'annotations', 'kwonly_annotations', + 'varargannotation', 'kwargannotation') varargannotation = None kwargannotation = None else: @@ -843,9 +836,11 @@ def __init__(self, vararg=None, kwarg=None, parent=None): self.kwonlyargs = [] self.kw_defaults = [] self.annotations = [] + self.kwonly_annotations = [] def postinit(self, args, defaults, kwonlyargs, kw_defaults, - annotations, varargannotation=None, kwargannotation=None): + annotations, kwonly_annotations, varargannotation=None, + kwargannotation=None): self.args = args self.defaults = defaults self.kwonlyargs = kwonlyargs @@ -853,6 +848,7 @@ def postinit(self, args, defaults, kwonlyargs, kw_defaults, self.annotations = annotations self.varargannotation = varargannotation self.kwargannotation = kwargannotation + self.kwonly_annotations = kwonly_annotations def _infer_name(self, frame, name): if self.parent is frame: @@ -1149,10 +1145,7 @@ 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) + util.rename_warning((type(self).__name__, type(self).__name__)) return self.assign_type() def _get_filtered_stmts(self, lookup_node, node, stmts, mystmt): @@ -1322,7 +1315,21 @@ def bool_value(self): class EmptyNode(NodeNG): - """class representing an EmptyNode node""" + '''EmptyNodes are used in manufactured ASTs that simulate features of + real ASTs for inference, usually to handle behavior implemented in + the interpreter or in C extensions. + + ''' + _other_fields = ('name', 'object') + + def __init__(self, object_, name=None, lineno=None, col_offset=None, parent=None): + if self.object is not None: + self.object = object_ + super(EmptyNode, self).__init__(lineno, col_offset, parent) + + @property + def has_underlying_object(self): + return hasattr(self, 'object') class ExceptHandler(mixins.AssignTypeMixin, Statement): @@ -1356,7 +1363,7 @@ def catch(self, exceptions): class Exec(Statement): """class representing an Exec node""" - _astroid_fields = ('expr', 'globals', 'locals',) + _astroid_fields = ('expr', 'globals', 'locals') expr = None globals = None locals = None @@ -1844,7 +1851,7 @@ def _update_const_classes(): _update_const_classes() -def const_factory(value): +def const_factory(value, parent=None): """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, @@ -1853,11 +1860,9 @@ def const_factory(value): # not in CONST_CLS) assert not isinstance(value, NodeNG) try: - return CONST_CLS[value.__class__](value) + return CONST_CLS[value.__class__](value, parent) except (KeyError, AttributeError): - node = EmptyNode() - node.object = value - return node + return EmptyNode(object_=value, parent=parent) # Backward-compatibility aliases diff --git a/astroid/raw_building.py b/astroid/raw_building.py index a5db080d27..53900df3d6 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -21,10 +21,22 @@ import collections import inspect -import logging +import itertools +import operator import os import sys import types +import warnings + +try: + from functools import singledispatch as _singledispatch +except ImportError: + from singledispatch import singledispatch as _singledispatch + +try: + from inspect import signature as _signature, Parameter as _Parameter +except ImportError: + from funcsigs import signature as _signature, Parameter as _Parameter import six @@ -33,14 +45,27 @@ from astroid import node_classes from astroid import nodes from astroid import scoped_nodes +from astroid import util + +# The repr of objects of this type describe it as "slot wrapper", but +# the repr of the type itself calls it "wrapper_descriptor". It is +# used for unbound methods implemented in C on CPython. On Jython +# it's called "method_descriptor". On PyPy the methods of both +# builtin types and Python-defined types have the same type. +WrapperDescriptorType = type(object.__getattribute__) + +# Both the reprs, for objects of this type and the type itself, refer +# to this type as "method-wrapper". It's used for bound methods +# implemented in C on CPython. On Jython, this is the same type as +# for builtin functions/methods. On PyPy both bound and unbound +# methods have the same type. +MethodWrapperType = type(object().__getattribute__) MANAGER = manager.AstroidManager() # the keys of CONST_CLS eg python builtin types _CONSTANTS = tuple(node_classes.CONST_CLS) -_JYTHON = os.name == 'java' _BUILTINS = vars(six.moves.builtins) -_LOG = logging.getLogger(__name__) def _io_discrepancy(member): @@ -67,22 +92,6 @@ def _add_dunder_class(func, member): func.instance_attrs['__class__'] = [ast_klass] -_marker = object() - - -def attach_dummy_node(node, name, object=_marker): - """create a dummy node and register it in the locals of the given - node with the specified name - """ - enode = nodes.EmptyNode() - enode.object = object - _attach_local_node(node, enode, name) - -def _has_underlying_object(self): - return hasattr(self, 'object') and self.object is not _marker - -nodes.EmptyNode.has_underlying_object = _has_underlying_object - def attach_const_node(node, name, value): """create a Const node and register it in the locals of the given node with the specified name @@ -100,43 +109,35 @@ def attach_import_node(node, modname, membername): def build_module(name, doc=None, modclass=nodes.Module): """create and initialize a astroid Module node""" - node = modclass(name, doc, pure_python=False) - node.package = False - node.parent = None - return node + util.rename_warning(('build_module', 'nodes.Module')) + return nodes.Module(name=name, doc=doc, package=False, pure_python=False) def build_class(name, basenames=(), doc=None): """create and initialize a astroid ClassDef node""" node = nodes.ClassDef(name, doc) - for base in basenames: - basenode = nodes.Name() - basenode.name = base - node.bases.append(basenode) - basenode.parent = node + node.postinit(bases=[nodes.Name(b, node) for b in basenames], + body=(), decorators=()) return node -def build_function(name, args=None, defaults=None, flag=0, doc=None): - """create and initialize a astroid FunctionDef node""" - args, defaults = args or [], defaults or [] - # first argument is now a list of decorators - func = nodes.FunctionDef(name, doc) - func.args = argsnode = nodes.Arguments() - argsnode.args = [] - for arg in args: - argsnode.args.append(nodes.Name()) - argsnode.args[-1].name = arg - argsnode.args[-1].parent = argsnode - argsnode.defaults = [] - for default in defaults: - argsnode.defaults.append(nodes.const_factory(default)) - argsnode.defaults[-1].parent = argsnode - argsnode.kwarg = None - argsnode.vararg = None - argsnode.parent = func - if args: - register_arguments(func) +Parameter = collections.namedtuple('Parameter', 'name default annotation kind') +DEFAULT_PARAMETER = Parameter(None, None, None, None) + +def build_function(name, args=(), defaults=(), annotations=(), + kwonlyargs=(), kwonly_defaults=(), + kwonly_annotations=(), vararg=None, + varargannotation=None, kwarg=None, kwargannotation=None, + returns=None, doc=None, parent=None): + """create and initialize an astroid FunctionDef node""" + func = nodes.FunctionDef(name=name, doc=doc, parent=parent) + args_node = nodes.Arguments(vararg=vararg, kwarg=kwarg, parent=func) + args = [nodes.Name(name=a.name, parent=args_node) for n in args] + kwonlyargs = [nodes.Name(name=a.name, parent=args_node) for a in kw_only] + args_node.postinit(args, defaults, kwonlyargs, kw_defaults, + annotations, kwonly_annotations, + varargannotation, kwargannotation) + func.postinit(args=args_node, body=[], returns=returns) return func @@ -144,24 +145,6 @@ def build_from_import(fromname, names): """create and initialize an astroid ImportFrom import statement""" return nodes.ImportFrom(fromname, [(name, None) for name in names]) -def register_arguments(func, args=None): - """add given arguments to local - - args is a list that may contains nested lists - (i.e. def func(a, (b, c, d)): ...) - """ - if args is None: - args = func.args.args - if func.args.vararg: - func.set_local(func.args.vararg, func.args) - if func.args.kwarg: - func.set_local(func.args.kwarg, func.args) - for arg in args: - if isinstance(arg, nodes.Name): - func.set_local(arg.name, arg) - else: - register_arguments(func, arg.elts) - def object_build_class(node, member, localname): """create astroid for a living class object""" @@ -170,17 +153,24 @@ def object_build_class(node, member, localname): localname=localname) -def object_build_function(node, member, localname): +def object_build_function(parent, func, localname): """create astroid for a living function object""" - args, varargs, varkw, defaults = inspect.getargspec(member) - if varargs is not None: - args.append(varargs) - if varkw is not None: - args.append(varkw) - func = build_function(getattr(member, '__name__', None) or localname, args, - defaults, six.get_function_code(member).co_flags, - member.__doc__) - node.add_local_node(func, localname) + signature = _signature(func) + parameters = {k: tuple(g) for k, g in + itertools.groupby(signature.parameters.values(), + operator.attrgetter('kind'))} + # This ignores POSITIONAL_ONLY args, because they only appear in + # functions implemented in C and can't be mimicked by any Python + # function. + node = build_function(getattr(func, '__name__', None) or localname, + parameters.get(_Parameter.POSITIONAL_OR_KEYWORD, ()), + parameters.get(_Parameter.KEYWORD_ONLY, ()), + parameters.get(_Parameter.VAR_POSITIONAL, None), + parameters.get(_Parameter.VAR_KEYWORD, None), + signature.return_annotation, + func.__doc__, + parent) + return node def object_build_datadescriptor(node, member, name): @@ -208,28 +198,10 @@ def _base_class_object_build(node, member, basenames, name=None, localname=None) basenames, member.__doc__) klass._newstyle = isinstance(member, type) node.add_local_node(klass, localname) - try: - # limit the instantiation trick since it's too dangerous - # (such as infinite test execution...) - # this at least resolves common case such as Exception.args, - # OSError.errno - if issubclass(member, Exception): - instdict = member().__dict__ - else: - raise TypeError - except: # pylint: disable=bare-except - pass - else: - for name, obj in instdict.items(): - valnode = nodes.EmptyNode() - valnode.object = obj - valnode.parent = klass - valnode.lineno = 1 - klass.instance_attrs[name] = [valnode] return klass -def _build_from_function(node, name, member, module): +def _build_from_function(parent, name, member, module): # verify this is not an imported function try: code = six.get_function_code(member) @@ -240,11 +212,137 @@ def _build_from_function(node, name, member, module): filename = getattr(code, 'co_filename', None) if filename is None: assert isinstance(member, object) - object_build_methoddescriptor(node, member, name) + return object_build_methoddescriptor(parent, member, name) elif filename != getattr(module, '__file__', None): - attach_dummy_node(node, name, member) + return nodes.EmptyNode(name=name, object_=member, parent=parent) else: - object_build_function(node, member, name) + return object_build_function(parent, member, name) + + +@_singledispatch +def ast_from_object(object_, built_objects=(), name=None, parent=None): + return nodes.EmptyNode(name=name, object_=object_, parent=parent) + +# pylint: disable=unused-variable; doesn't understand singledispatch +@ast_from_object.register(types.ModuleType) +def ast_from_module(module, built_objects=(), name=None, parent=None, node_class=nodes.Module): + if name is None: + name = module.__name__ + try: + source_file = inspect.getsourcefile(module) + except TypeError: + # inspect.getsourcefile raises TypeError for built-in modules. + source_file = None + # inspect.getdoc returns None for modules without docstrings like + # Jython Java modules (see #109562). + module_node = node_class(name=name, doc=inspect.getdoc(module), + source_file=source_file, + package=hasattr(module, '__path__'), + # Assume that if inspect couldn't find a Python source + # file, it's probably not implemented in pure Python. + pure_python=bool(source_file)) + MANAGER.cache_module(module_node) + built_objects = {} + module_node.postinit(body=[ast_from_object(m, built_objects, module_node) + for m in inspect.getmembers(module)]) + return module_node + +# pylint: disable=unused-variable; doesn't understand singledispatch +@ast_from_object.register(type) +@ast_from_object.register(types.GetSetDescriptorType) +@ast_from_object.register(types.MemberDescriptorType) +def ast_from_class(cls, built_objects=(), name=None, parent=None): + pass +# Old-style classes +if six.PY2: + ast_from_object.register(types.ClassType, ast_from_class) + +# pylint: disable=unused-variable; doesn't understand singledispatch + +# These two types are the same on CPython but not necessarily the same +# on other implementations. +@ast_from_object.register(types.BuiltinFunctionType) +@ast_from_object.register(types.BuiltinMethodType) +# Methods implemented in C on CPython. +@ast_from_object.register(WrapperDescriptorType) +@ast_from_object.register(MethodWrapperType) +# types defines a LambdaType but on all existing Python +# implementations it's equivalent to FunctionType. +@ast_from_object.register(types.FunctionType) +@ast_from_object.register(types.MethodType) +def ast_from_function(func, built_objects=(), name=None, parent=None): + signature = _signature(func) + parameters = {k: tuple(g) for k, g in + itertools.groupby(signature.parameters.values(), + operator.attrgetter('kind'))} + # This ignores POSITIONAL_ONLY args, because they only appear in + # functions implemented in C and can't be mimicked by any Python + # function. + def extract_args(parameters): + '''Takes an iterator over Parameter objects and returns three + sequences, arg names, default values, and annotations. + + ''' + names = [] + defaults = [] + annotations = [] + for parameter in parameters: + args.append(parameter.name) + if parameter.default is not _Parameter.empty: + defaults.append(ast_from_object(parameter.default)) + if parameter.annotation is not _Parameter.empty: + annotations.append(ast_from_object(parameter.annotation)) + else: + annotations.append(None) + return names, defaults, annotations + + def extract_vararg(parameter): + '''Takes a single-element iterator possibly containing a Parameter and + returns a name and an annotation. + + ''' + try: + vararg = next(parameter) + name = vararg.name + if vararg.annotation is not _Parameter.empty: + return name, ast_from_object(vararg.annotation) + else: + return name, None + except StopIteration: + return None, None + + names, defaults, annotations = extract_args(parameters.get(_Parameter.POSITIONAL_OR_KEYWORD, ())) + kwonlyargs, kw_defaults, kwonly_annotations = extract_args(parameters.get(_Parameter.KEYWORD_ONLY, ())) + kwarg, varargannotation = extract_vararg(parameters.get(_Parameter.VAR_POSITIONAL, ())) + kwarg, kwargannotation = parameters.get(_Parameter.VAR_KEYWORD, None), + return build_function(name or getattr(func, '__name__', None), + args, defaults, kwonlyargs, kw_defaults, + annotations, kwonly_annotations, + varargannotation, kwargannotation) + ast_from_object(signature.return_annotation), + func.__doc__, + parent) + +# pylint: disable=unused-variable; doesn't understand singledispatch +@ast_from_object.register(list) +@ast_from_object.register(dict) +@ast_from_object.register(set) +@ast_from_object.register(tuple) +def ast_from_containers(cls, built_objects=(), name=None, parent=None): + '''Handles builtin types that have their own AST nodes, like list but + not like frozenset or range.''' + +@ast_from_object.register(str) +@ast_from_object.register(bytes) +@ast_from_object.register(int) +@ast_from_object.register(float) +@ast_from_object.register(complex) +def ast_from_scalar(cls, built_objects=(), name=None, parent=None): + pass +if six.PY2: + ast_from_object.register(unicode, ast_from_scalar) + ast_from_object.register(long, ast_from_scalar) + class InspectBuilder(object): @@ -260,7 +358,7 @@ def __init__(self): self._done = {} self._module = None - def inspect_build(self, module, modname=None, modclass=nodes.Module, + def inspect_build(self, module, modname=None, node_class=nodes.Module, path=None): """build astroid from a living module (i.e. using inspect) this is used when there is no python source code available (either @@ -269,18 +367,25 @@ def inspect_build(self, module, modname=None, modclass=nodes.Module, self._module = module if modname is None: modname = module.__name__ + # In Jython, Java modules have no __doc__ (see #109562) + doc = module.__doc__ if hasattr(module, '__doc__') else None try: - node = build_module(modname, module.__doc__, modclass=modclass) - except AttributeError: - # in jython, java modules have no __doc__ (see #109562) - node = build_module(modname, modclass=modclass) - node.file = node.path = path and os.path.abspath(path) or path - node.name = modname - MANAGER.cache_module(node) - node.package = hasattr(module, '__path__') + source_file = inspect.getsourcefile(module) + except TypeError: + # inspect.getsourcefile raises TypeError for builtins. + source_file = None + module_node = node_class(name=modname, doc=doc, source_file=source_file, + package=hasattr(module + + , '__path__'), + # Assume that if inspect can't find a + # Python source file, it's probably not + # implemented in pure Python. + pure_python=bool(source_file)) + MANAGER.cache_module(module_node) self._done = {} - self.object_build(node, module) - return node + module_node.postinit(body=self.object_build(module_node, module)) + return module_node def object_build(self, node, obj): """recursive method which create a partial ast from real objects @@ -289,22 +394,17 @@ def object_build(self, node, obj): if obj in self._done: return self._done[obj] self._done[obj] = node - for name in dir(obj): - try: - member = getattr(obj, name) - except AttributeError: - # damned ExtensionClass.Base, I know you're there ! - attach_dummy_node(node, name) - continue + members = [] + for name, member in inspect.getmembers(obj): if inspect.ismethod(member): member = six.get_method_function(member) if inspect.isfunction(member): - _build_from_function(node, name, member, self._module) + members.append(_build_from_function(node, name, member, self._module)) elif inspect.isbuiltin(member): if (not _io_discrepancy(member) and self.imported_member(node, member, name)): continue - object_build_methoddescriptor(node, member, name) + members.append(object_build_methoddescriptor(node, member, name)) elif inspect.isclass(member): if self.imported_member(node, member, name): continue @@ -312,28 +412,30 @@ def object_build(self, node, obj): class_node = self._done[member] if class_node not in set(node.get_children()): # if class_node not in node.locals.get(name, ()): - node.add_local_node(class_node, name) + members.append(node.add_local_node(class_node, name)) else: class_node = object_build_class(node, member, name) # recursion - self.object_build(class_node, member) + members.append(self.object_build(class_node, member)) if name == '__class__' and class_node.parent is None: class_node.parent = self._done[self._module] elif inspect.ismethoddescriptor(member): assert isinstance(member, object) - object_build_methoddescriptor(node, member, name) + members.append(object_build_methoddescriptor(node, member, name)) elif inspect.isdatadescriptor(member): assert isinstance(member, object) - object_build_datadescriptor(node, member, name) + members.append(object_build_datadescriptor(node, member, name)) elif isinstance(member, _CONSTANTS): - attach_const_node(node, name, member) + members.append(attach_const_node(node, name, member)) elif inspect.isroutine(member): # This should be called for Jython, where some builtin # methods aren't catched by isbuiltin branch. - _build_from_function(node, name, member, self._module) + members.append(_build_from_function(node, name, member, self._module)) else: # create an empty node so that the name is actually defined - attach_dummy_node(node, name, member) + members.append(nodes.EmptyNode(parent=node, name=name, + object_=member)) + return members def imported_member(self, node, member, name): """verify this is not an imported class or handle it""" @@ -343,18 +445,18 @@ def imported_member(self, node, member, name): try: modname = getattr(member, '__module__', None) except: # pylint: disable=bare-except - _LOG.exception('unexpected error while building ' - 'astroid from living object') + warnings.warn('Unexpected error while building an AST from an ' + 'imported class.', RuntimeWarning, stacklevel=2) modname = None if modname is None: if (name in ('__new__', '__subclasshook__') - or (name in _BUILTINS and _JYTHON)): + or (name in _BUILTINS and util.JYTHON)): # Python 2.5.1 (r251:54863, Sep 1 2010, 22:03:14) # >>> print object.__new__.__module__ # None modname = six.moves.builtins.__name__ else: - attach_dummy_node(node, name, member) + nodes.EmptyNode(parent=node, name=name, object_=member) return True real_name = { @@ -378,8 +480,8 @@ def imported_member(self, node, member, name): ### astroid bootstrapping ###################################################### Astroid_BUILDER = InspectBuilder() -class Builtins(nodes.Module): - pass +# class Builtins(nodes.Module): +# pass _CONST_PROXY = {} def _astroid_bootstrapping(astroid_builtin=None): @@ -388,7 +490,7 @@ def _astroid_bootstrapping(astroid_builtin=None): # inspect_build builtins, and then we can proxy Const if astroid_builtin is None: from six.moves import builtins - astroid_builtin = Astroid_BUILDER.inspect_build(builtins, modclass=Builtins) + astroid_builtin = Astroid_BUILDER.inspect_build(builtins) #, node_class=Builtins for cls, node_cls in node_classes.CONST_CLS.items(): if cls is type(None): @@ -407,19 +509,19 @@ def _astroid_bootstrapping(astroid_builtin=None): _astroid_bootstrapping() -@scoped_nodes.get_locals.register(Builtins) -def scoped_node(node): - locals_ = collections.defaultdict(list) - for name in ('Ellipsis', 'False', 'None', 'NotImplemented', - 'True', '__debug__', '__package__', '__spec__', 'copyright', - 'credits', 'exit', 'help', 'license', 'quit'): - for child in (n for n in node.body if - isinstance(n, (nodes.Const, nodes.EmptyNode))): - if child.name == name: - locals_[name].append(child) - for n in node.get_children(): - scoped_nodes._get_locals(n, locals_) - return locals_ +# @scoped_nodes.get_locals.register(Builtins) +# def scoped_node(node): +# locals_ = collections.defaultdict(list) +# for name in ('Ellipsis', 'False', 'None', 'NotImplemented', +# 'True', '__debug__', '__package__', '__spec__', 'copyright', +# 'credits', 'exit', 'help', 'license', 'quit'): +# for child in (n for n in node.body if +# isinstance(n, (nodes.Const, nodes.EmptyNode))): +# if child.name == name: +# locals_[name].append(child) +# for n in node.get_children(): +# scoped_nodes._get_locals(n, locals_) +# return locals_ # TODO : find a nicer way to handle this situation; diff --git a/astroid/rebuilder.py b/astroid/rebuilder.py index 184614b72a..9b362d2f19 100644 --- a/astroid/rebuilder.py +++ b/astroid/rebuilder.py @@ -87,8 +87,8 @@ def _get_doc(node): node.body = node.body[1:] return node, doc except IndexError: - pass # ast built from scratch - return node, None + return node, None # ast built from scratch + def _visit_or_none(node, attr, visitor, parent, assign_ctx, visit='visit', **kws): @@ -115,8 +115,8 @@ def __init__(self, manager): def visit_module(self, node, modname, modpath, package): """visit a Module node by returning a fresh instance of it""" node, doc = _get_doc(node) - newnode = nodes.Module(name=modname, doc=doc, file=modpath, path=modpath, - package=package, parent=None) + newnode = nodes.Module(name=modname, doc=doc, package=package, + pure_python=True, source_file=modpath) newnode.postinit([self.visit(child, newnode) for child in node.body]) return newnode @@ -173,12 +173,17 @@ def visit_arguments(self, node, parent, assign_ctx=None): None for child in node.kw_defaults] annotations = [self.visit(arg.annotation, newnode, None) if arg.annotation else None for arg in node.args] + kwonly_annotations = [self.visit(arg.annotation, newnode, None) + if arg.annotation else None + for arg in node.kwonlyargs] else: kwonlyargs = [] kw_defaults = [] annotations = [] + kwonly_annotations = [] newnode.postinit(args, defaults, kwonlyargs, kw_defaults, - annotations, varargannotation, kwargannotation) + annotations, kwonly_annotations, + varargannotation, kwargannotation) return newnode def visit_assignattr(self, node, parent, assign_ctx=None): @@ -407,13 +412,6 @@ def visit_ellipsis(self, node, parent, assign_ctx=None): return nodes.Ellipsis(getattr(node, 'lineno', None), getattr(node, 'col_offset', None), parent) - - def visit_emptynode(self, node, parent, assign_ctx=None): - """visit an EmptyNode node by returning a fresh instance of it""" - return nodes.EmptyNode(getattr(node, 'lineno', None), - getattr(node, 'col_offset', None), parent) - - def visit_excepthandler(self, node, parent, assign_ctx=None): """visit an ExceptHandler node by returning a fresh instance of it""" newnode = nodes.ExceptHandler(node.lineno, node.col_offset, parent) diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index 28d0ede3ab..60c1bd5b88 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -172,7 +172,9 @@ def qname(self): return '%s.%s' % (self.parent.frame().qname(), self.name) def frame(self): - """return the first parent frame node (i.e. Module, FunctionDef or ClassDef) + """return the first parent frame node (i.e. Module, FunctionDef or + ClassDef) + """ return self @@ -228,7 +230,7 @@ def add_local_node(self, child_node, name=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) + self.set_local(name or child_node.name, child_node) def __getitem__(self, item): """method from the `dict` interface returning the first node @@ -256,14 +258,14 @@ def values(self): """method from the `dict` interface returning a tuple containing locally defined nodes which are instance of `FunctionDef` or `ClassDef` """ - return self.locals.values() + return tuple(v[0] for v in self.locals.values()) def items(self): """method from the `dict` interface returning a list of tuple containing each locally defined name with its associated node, which is an instance of `FunctionDef` or `ClassDef` """ - return self.locals.items() + return tuple((k, v[0]) for k, v in self.locals.items()) def __contains__(self, name): return name in self.locals @@ -279,9 +281,9 @@ class Module(LocalsDictNodeNG): # the file from which as been extracted the astroid representation. It may # be None if the representation has been built from a built-in module - file = None + source_file = None # Alternatively, if built from a string/bytes, this can be set - file_bytes = None + source_code = None # encoding of python source file, so we can get unicode out of it (python2 # only) file_encoding = None @@ -291,12 +293,6 @@ class Module(LocalsDictNodeNG): pure_python = None # boolean for package module package = None - # dictionary of globals with name as key and node defining the global - # as value - # globals = None - - # Future imports - # future_imports = None # names of python special attributes (handled by getattr impl.) special_attributes = set(('__name__', '__doc__', '__file__', '__path__', @@ -304,20 +300,23 @@ class Module(LocalsDictNodeNG): # names of module attributes available through the global scope scope_attrs = set(('__name__', '__doc__', '__file__', '__path__')) - _other_fields = ('name', 'doc', 'file', 'path', 'package', - 'pure_python', 'future_imports') + if six.PY2: + _other_fields = ('name', 'doc', 'file_encoding', 'path', 'package', + 'pure_python', 'source_code', 'source_file') + else: + _other_fields = ('name', 'doc', 'path', 'package', 'pure_python', + 'source_code', 'source_file') # _other_other_fields = ('locals', 'globals') - def __init__(self, name, doc, file=None, path=None, package=None, - parent=None, pure_python=True): + def __init__(self, name, doc, package=None, parent=None, + pure_python=True, source_code=None, source_file=None): self.name = name self.doc = doc - self.file = file - self.path = path self.package = package self.parent = parent self.pure_python = pure_python - # self.locals = self.globals = {} + self.source_code = source_code + self.source_file = source_file self.body = [] # self.future_imports = set() self.external_attrs = collections.defaultdict(list) @@ -325,6 +324,46 @@ def __init__(self, name, doc, file=None, path=None, package=None, def postinit(self, body=None): self.body = body + # Legacy API aliases + @property + def file(self): + rename_warning(('file', 'source_file')) + return self.source_file + @file.setter + def file(self, source_file): + rename_warning(('file', 'source_file')) + self.source_file = source_file + @file.deleter + def file(self): + rename_warning(('file', 'source_file')) + del self.source_file + + @property + def path(self): + rename_warning(('path', 'source_file')) + return self.source_file + @path.setter + def path(self, source_file): + rename_warning(('path', 'source_file')) + self.source_file = source_file + @path.deleter + def path(self): + rename_warning(('path', 'source_file')) + del self.source_file + + @property + def files_bytes(self): + rename_warning(('files_bytes', 'source_code')) + return self.source_code + @files_bytes.setter + def files_bytes(self, source_code): + rename_warning(('files_bytes', 'source_code')) + self.source_code = source_code + @files_bytes.deleter + def files_bytes(self): + rename_warning(('files_bytes', 'source_code')) + del self.source_code + @property def globals(self): return types.MappingProxyType(get_locals(self)) @@ -343,11 +382,7 @@ def _get_stream(self): @property def file_stream(self): - warnings.warn("file_stream property is deprecated and " - "it is slated for removal in astroid 1.6." - "Use the new method 'stream' instead.", - PendingDeprecationWarning, - stacklevel=2) + util.attr_to_method_warning((file_stream, type(self).__name__)) return self._get_stream() def stream(self): @@ -356,10 +391,10 @@ def stream(self): def close(self): """Close the underlying file streams.""" - warnings.warn("close method is deprecated and it is " + warnings.warn("The close method is deprecated and is " "slated for removal in astroid 1.6, along " - "with 'file_stream' property. " - "Its behaviour is replaced by managing each " + "with 'file_stream'. " + "Its behavior is replaced by managing each " "file stream returned by the 'stream' method.", PendingDeprecationWarning, stacklevel=2) @@ -495,9 +530,9 @@ def wildcard_import_names(self): # We separate the different steps of lookup in try/excepts # to avoid catching too many Exceptions default = [name for name in self.keys() if not name.startswith('_')] - try: + if '__all__' in self: all = self['__all__'] - except KeyError: + else: return default try: explicit = next(all.assigned_stmts()) @@ -725,6 +760,13 @@ def bool_value(self): class FunctionDef(node_classes.Statement, Lambda): + '''Setting FunctionDef.args to None, rather than an Arguments node, + means that the corresponding function's arguments are unknown, + probably because it represents a function implemented in C or that + is otherwise not introspectable. + + ''' + if six.PY3: _astroid_fields = ('decorators', 'args', 'body', 'returns') returns = None @@ -755,7 +797,8 @@ def postinit(self, args, body, decorators=None, returns=None): @decorators_mod.cachedproperty def extra_decorators(self): - """Get the extra decorators that this function can haves + + """Get the extra decorators that this function can haves Additional decorators are considered when they are used as assignments, as in `method = staticmethod(method)`. The property will return all the callables that are used for @@ -953,12 +996,13 @@ def infer_call_result(self, caller, context=None): result.parent = self yield result return - # This is really a gigantic hack to work around metaclass generators - # that return transient class-generating functions. Pylint's AST structure - # cannot handle a base class object that is only used for calling __new__, - # but does not contribute to the inheritance structure itself. We inject - # a fake class into the hierarchy here for several well-known metaclass - # generators, and filter it out later. + # This is really a gigantic hack to work around metaclass + # generators that return transient class-generating + # functions. Pylint's AST structure cannot handle a base class + # object that is only used for calling __new__, but does not + # contribute to the inheritance structure itself. We inject a + # fake class into the hierarchy here for several well-known + # metaclass generators, and filter it out later. if (self.name == 'with_metaclass' and len(self.args.args) == 1 and self.args.vararg is not None): @@ -1095,7 +1139,7 @@ class ClassDef(mixins.FilterStmtsMixin, LocalsDictNodeNG, # by a raw factories # a dictionary of class instances attributes - _astroid_fields = ('decorators', 'bases', 'body') # name + _astroid_fields = ('decorators', 'bases', 'body') decorators = None special_attributes = set(('__name__', '__doc__', '__dict__', '__module__', @@ -1107,13 +1151,11 @@ class ClassDef(mixins.FilterStmtsMixin, LocalsDictNodeNG, doc="class'type, possible values are 'class' | " "'metaclass' | 'exception'") _other_fields = ('name', 'doc') - # _other_other_fields = ('locals', '_newstyle') - _other_other_fields = ('_newstyle') + _other_other_fields = ('_newstyle', 'instance_attrs', 'external_attrs') _newstyle = None def __init__(self, name=None, doc=None, lineno=None, col_offset=None, parent=None): - # self.locals = {} self.bases = [] self.body = [] self.name = name @@ -1277,7 +1319,7 @@ def ancestors(self, recurs=True, context=None): """ # FIXME: should be possible to choose the resolution order # FIXME: inference make infinite loops possible here - yielded = set([self]) + yielded = {self} if context is None: context = contextmod.InferenceContext() if six.PY3: @@ -1826,6 +1868,13 @@ def locals_name(node, locals_): new scope so shouldn't be recursed into.''' locals_[node.name].append(node) +@_get_locals.register(node_classes.EmptyNode) +def locals_empty(node, locals_): + '''EmptyNodes add an object to the local variables under a specified + name.''' + if node.name: + locals_[node.name].append(node.object) + # pylint: disable=unused-variable; doesn't understand singledispatch @_get_locals.register(node_classes.Arguments) def locals_arguments(node, locals_): @@ -1833,7 +1882,7 @@ def locals_arguments(node, locals_): if node.vararg: locals_[node.vararg].append(node) if node.kwarg: - locals_[node.kwarg].append(node) + locals_[node.kwarg].append(node) # pylint: disable=unused-variable; doesn't understand singledispatch @_get_locals.register(node_classes.Import) diff --git a/astroid/tests/unittest_builder.py b/astroid/tests/unittest_builder.py index 84bf50c235..ffa7bf246b 100644 --- a/astroid/tests/unittest_builder.py +++ b/astroid/tests/unittest_builder.py @@ -456,7 +456,7 @@ def global_no_effect(): with self.assertRaises(exceptions.InferenceError): next(astroid['global_no_effect'].ilookup('CSTE2')) - @unittest.skipIf(os.name == 'java', + @unittest.skipIf(util.JYTHON, 'This test is skipped on Jython, because the ' 'socket object is patched later on with the ' 'methods we are looking for. Since we do not ' @@ -600,7 +600,7 @@ def test_module_locals(self): """test the 'locals' dictionary of a astroid module""" module = self.module _locals = module.locals - self.assertIs(_locals, module.globals) + self.assertEqual(_locals, module.globals) keys = sorted(_locals.keys()) should = ['MY_DICT', 'NameNode', 'YO', 'YOUPI', '__revision__', 'global_access', 'modutils', 'four_args', diff --git a/astroid/tests/unittest_manager.py b/astroid/tests/unittest_manager.py index f0f2d68419..1595aaf42c 100644 --- a/astroid/tests/unittest_manager.py +++ b/astroid/tests/unittest_manager.py @@ -74,7 +74,7 @@ def test_ast_from_file_name_astro_builder_exception(self): def test_do_not_expose_main(self): obj = self.manager.ast_from_module_name('__main__') self.assertEqual(obj.name, '__main__') - self.assertEqual(obj.items(), {}.items()) + self.assertEqual(obj.items(), ()) def test_ast_from_module_name(self): astroid = self.manager.ast_from_module_name('unittest') diff --git a/astroid/tests/unittest_raw_building.py b/astroid/tests/unittest_raw_building.py index 2bdaac1777..ad57c4f7e2 100644 --- a/astroid/tests/unittest_raw_building.py +++ b/astroid/tests/unittest_raw_building.py @@ -6,11 +6,12 @@ from astroid.builder import AstroidBuilder from astroid.raw_building import ( - attach_dummy_node, build_module, - build_class, build_function, build_from_import + attach_dummy_node, build_module, build_class, build_function, + build_from_import, object_build_function, Parameter ) -from astroid import test_utils from astroid import nodes +from astroid import util +from astroid import test_utils from astroid.bases import BUILTINS @@ -39,17 +40,28 @@ def test_build_function(self): self.assertEqual(node.doc, None) def test_build_function_args(self): - args = ['myArgs1', 'myArgs2'] + args = [Parameter('myArgs1', None, None, None), + Parameter('myArgs2', None, None, None)] node = build_function('MyFunction', args) self.assertEqual('myArgs1', node.args.args[0].name) self.assertEqual('myArgs2', node.args.args[1].name) self.assertEqual(2, len(node.args.args)) def test_build_function_defaults(self): - defaults = ['defaults1', 'defaults2'] - node = build_function('MyFunction', None, defaults) + args = [Parameter('myArgs1', 'defaults1', None, None), + Parameter('myArgs2', 'defaults2', None, None)] + node = build_function('MyFunction', args) self.assertEqual(2, len(node.args.defaults)) + @unittest.skipIf(sys.version_info[0] > 2, + 'Tuples are not allowed in function args in Python 3') + @unittest.expectedFailure + def test_build_function_with_tuple_args(self): + # TODO + def f(a, (b, (c, d))): + pass + object_build_function(f) + def test_build_from_import(self): names = ['exceptions, inference, inspector'] node = build_from_import('astroid', names) @@ -69,7 +81,7 @@ def test_io_is__io(self): buffered_reader = module.getattr('BufferedReader')[0] self.assertEqual(buffered_reader.root().name, 'io') - @unittest.skipUnless(os.name == 'java', 'Requires Jython') + @unittest.skipUnless(util.JYTHON, 'Requires Jython') def test_open_is_inferred_correctly(self): # Lot of Jython builtins don't have a __module__ attribute. for name, _ in inspect.getmembers(builtins, predicate=inspect.isbuiltin): diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index a68b3b8b3d..3e191328f8 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -1012,6 +1012,9 @@ def with_metaclass(meta, base=object): class ClassWithMeta(with_metaclass(type)): #@ pass """) + print(klass.repr_tree()) + print(klass.locals) + print(tuple(klass.ancestors())) self.assertEqual( ['NewBase', 'object'], [base.name for base in klass.ancestors()]) diff --git a/astroid/util.py b/astroid/util.py index 9cd1d9dce1..05d48a65a1 100644 --- a/astroid/util.py +++ b/astroid/util.py @@ -20,6 +20,7 @@ # the same license. import importlib +import platform import sys import warnings @@ -28,6 +29,8 @@ from astroid import exceptions +JYTHON = True if platform.python_implementation() == 'Jython' else False + def lazy_import(module_name): return lazy_object_proxy.Proxy( @@ -38,6 +41,18 @@ def reraise(exception): block.''' six.reraise(type(exception), exception, sys.exc_info()[2]) +def generate_warning(message, warning): + return lambda strings: warnings.warn(message % strings, warning, + stacklevel=3) + +rename_warning = generate_warning("%r is deprecated and slated for removal in " + "astroid 2.0, use %r instead", + PendingDeprecationWarning) + +attr_to_method_warning = generate_warning("%s is deprecated and slated for " + " removal in astroid 1.6, use the " + "method '%s' instead.", + PendingDeprecationWarning) @object.__new__ class YES(object): @@ -60,10 +75,7 @@ def _instancecheck(cls, other): wrapped = cls.__wrapped__ other_cls = other.__class__ is_instance_of = wrapped is other_cls or issubclass(other_cls, wrapped) - warnings.warn("%r is deprecated and slated for removal in astroid " - "2.0, use %r instead" % (cls.__class__.__name__, - wrapped.__name__), - PendingDeprecationWarning, stacklevel=2) + rename_warning((cls.__class__.__name__, wrapped.__name__)) return is_instance_of diff --git a/tox.ini b/tox.ini index 1c74d049fc..ff8eb0ff9c 100644 --- a/tox.ini +++ b/tox.ini @@ -15,20 +15,28 @@ deps = hg+https://bitbucket.org/logilab/pylint commands = pylint -rn --rcfile={toxinidir}/pylintrc {envsitepackagesdir}/astroid -# This is commented out because tox will try to load the interpreter -# for any defined environment even if it's not in envlist, which will -# then fail on drone.io. - -[testenv:py34] +[testenv:py27] deps = + funcsigs lazy-object-proxy + singledispatch six wrapt +# [testenv:py33] +# deps = +# lazy-object-proxy +# singledispatch +# six +# wrapt + +# This is commented out because tox will try to load the interpreter +# for any defined environment even if it's not in envlist, which will +# then fail on drone.io. + [testenv] deps = lazy-object-proxy - singledispatch six wrapt From f438b0425549228fdf3a8a6b3948d503aa75e43c Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Sat, 24 Oct 2015 10:46:35 -0400 Subject: [PATCH 011/312] First draft of refactoring for building from live objects mostly finished --- astroid/__init__.py | 42 +++--- astroid/raw_building.py | 317 +++++++++++++++++++++++++++++----------- astroid/scoped_nodes.py | 5 +- 3 files changed, 259 insertions(+), 105 deletions(-) diff --git a/astroid/__init__.py b/astroid/__init__.py index 997f3e9ee4..8139e185dd 100644 --- a/astroid/__init__.py +++ b/astroid/__init__.py @@ -111,24 +111,24 @@ def transform(node, infer_function=infer_function): return transform -def register_module_extender(manager, module_name, get_extension_mod): - def transform(module): - extension_module = get_extension_mod() - # for name, obj in extension_module.locals.items(): - # node.locals[name] = obj - module.body.extend(extension_module.body) - - manager.register_transform(Module, transform, lambda n: n.name == module_name) - - -# load brain plugins -from os import listdir -from os.path import join, dirname -BRAIN_MODULES_DIR = join(dirname(__file__), 'brain') -if BRAIN_MODULES_DIR not in sys.path: - # add it to the end of the list so user path take precedence - sys.path.append(BRAIN_MODULES_DIR) -# load modules in this directory -for module in listdir(BRAIN_MODULES_DIR): - if module.endswith('.py'): - __import__(module[:-3]) +# def register_module_extender(manager, module_name, get_extension_mod): +# def transform(module): +# extension_module = get_extension_mod() +# # for name, obj in extension_module.locals.items(): +# # node.locals[name] = obj +# module.body.extend(extension_module.body) + +# manager.register_transform(Module, transform, lambda n: n.name == module_name) + + +# # load brain plugins +# from os import listdir +# from os.path import join, dirname +# BRAIN_MODULES_DIR = join(dirname(__file__), 'brain') +# if BRAIN_MODULES_DIR not in sys.path: +# # add it to the end of the list so user path take precedence +# sys.path.append(BRAIN_MODULES_DIR) +# # load modules in this directory +# for module in listdir(BRAIN_MODULES_DIR): +# if module.endswith('.py'): +# __import__(module[:-3]) diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 53900df3d6..d7e24f65af 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -219,66 +219,118 @@ def _build_from_function(parent, name, member, module): return object_build_function(parent, member, name) +def ast_from_object(object_, name=None): + built_objects = {} + module = inspect.getmodule(object_) + return _ast_from_object(object_, built_objects, module, name) + +# To talk about tomorrow: + +# Cycles in the ASTs + +# Cases where e.g. a module names itself something different that the +# name its parent gives it. + +# ABCs + +# classify_class_attrs + + @_singledispatch -def ast_from_object(object_, built_objects=(), name=None, parent=None): - return nodes.EmptyNode(name=name, object_=object_, parent=parent) +def _ast_from_object(object_, built_objects, module, name=None, parent=None): + if object_ in built_objects: + return built_objects[object_] + else: + node = nodes.EmptyNode(name=name, object_=object_, parent=parent) + built_objects[object_] = node + return node + # pylint: disable=unused-variable; doesn't understand singledispatch -@ast_from_object.register(types.ModuleType) -def ast_from_module(module, built_objects=(), name=None, parent=None, node_class=nodes.Module): - if name is None: - name = module.__name__ +@_ast_from_object.register(types.ModuleType) +def ast_from_module(module, built_objects, parent_module, name=None, parent=None): + if module is not parent_module: + # This module has been imported into another. + return nodes.Import([[getattr(module, '__name__', None), name]], + parent=parent) + if module in built_objects: + return built_objects[module] try: source_file = inspect.getsourcefile(module) except TypeError: # inspect.getsourcefile raises TypeError for built-in modules. source_file = None # inspect.getdoc returns None for modules without docstrings like - # Jython Java modules (see #109562). - module_node = node_class(name=name, doc=inspect.getdoc(module), - source_file=source_file, - package=hasattr(module, '__path__'), - # Assume that if inspect couldn't find a Python source - # file, it's probably not implemented in pure Python. - pure_python=bool(source_file)) + # Jython Java modules. + module_node = node_class(name=name or module.__name__, + doc=inspect.getdoc(module), + source_file=source_file, + package=hasattr(module, '__path__'), + # Assume that if inspect couldn't find a + # Python source file, it's probably not + # implemented in pure Python. + pure_python=bool(source_file)) MANAGER.cache_module(module_node) - built_objects = {} - module_node.postinit(body=[ast_from_object(m, built_objects, module_node) - for m in inspect.getmembers(module)]) + module_node.postinit(body=[_ast_from_object(m, built_objects, module, + n, module_node) + for n, m in inspect.getmembers(module)]) + built_objects[module] = module_node return module_node + # pylint: disable=unused-variable; doesn't understand singledispatch -@ast_from_object.register(type) -@ast_from_object.register(types.GetSetDescriptorType) -@ast_from_object.register(types.MemberDescriptorType) -def ast_from_class(cls, built_objects=(), name=None, parent=None): - pass +@_ast_from_object.register(type) +@_ast_from_object.register(types.GetSetDescriptorType) +@_ast_from_object.register(types.MemberDescriptorType) +def ast_from_class(cls, built_objects, module, name=None, parent=None): + inspected_module = inspect.getmodule(cls) + if inspected_module is not module: + return ImportFrom(fromname=getattr(inspected_module, '__name__', None), + names=[[getattr(cls, '__name__', None), name]], + parent=parent) + if cls in built_objects: + return built_objects[cls] + node = nodes.ClassDef(name=name, doc=inspect.getdoc(cls)) + node.postinit(bases=[nodes.Name(b, node) for b in basenames], + body=(), decorators=()) + # TODO + built_objects[cls] = class_node + return class_node # Old-style classes if six.PY2: - ast_from_object.register(types.ClassType, ast_from_class) + _ast_from_object.register(types.ClassType, ast_from_class) + # pylint: disable=unused-variable; doesn't understand singledispatch # These two types are the same on CPython but not necessarily the same # on other implementations. -@ast_from_object.register(types.BuiltinFunctionType) -@ast_from_object.register(types.BuiltinMethodType) +@_ast_from_object.register(types.BuiltinFunctionType) +@_ast_from_object.register(types.BuiltinMethodType) # Methods implemented in C on CPython. -@ast_from_object.register(WrapperDescriptorType) -@ast_from_object.register(MethodWrapperType) +@_ast_from_object.register(WrapperDescriptorType) +@_ast_from_object.register(MethodWrapperType) # types defines a LambdaType but on all existing Python # implementations it's equivalent to FunctionType. -@ast_from_object.register(types.FunctionType) -@ast_from_object.register(types.MethodType) -def ast_from_function(func, built_objects=(), name=None, parent=None): +@_ast_from_object.register(types.FunctionType) +@_ast_from_object.register(types.MethodType) +def ast_from_function(func, built_objects, module, name=None, parent=None): + inspected_module = inspect.getmodule(func) + if inspected_module is not module: + return ImportFrom(fromname=getattr(inspected_module, '__name__', None), + names=[[getattr(func, '__name__', None), name]], + parent=parent) + if func in built_objects: + return built_objects[func] signature = _signature(func) parameters = {k: tuple(g) for k, g in itertools.groupby(signature.parameters.values(), operator.attrgetter('kind'))} - # This ignores POSITIONAL_ONLY args, because they only appear in - # functions implemented in C and can't be mimicked by any Python - # function. - def extract_args(parameters): + func_node = nodes.FunctionDef(name=name or getattr(func, '__name__', None), + doc=inspect.getdoc(func), + parent=parent) + + def extract_args(parameters, parent): '''Takes an iterator over Parameter objects and returns three sequences, arg names, default values, and annotations. @@ -287,11 +339,11 @@ def extract_args(parameters): defaults = [] annotations = [] for parameter in parameters: - args.append(parameter.name) + names.append(parameter.name) if parameter.default is not _Parameter.empty: - defaults.append(ast_from_object(parameter.default)) + defaults.append(_ast_from_object(parameter.default, parent=parent)) if parameter.annotation is not _Parameter.empty: - annotations.append(ast_from_object(parameter.annotation)) + annotations.append(_ast_from_object(parameter.annotation, parent=parent)) else: annotations.append(None) return names, defaults, annotations @@ -302,46 +354,147 @@ def extract_vararg(parameter): ''' try: - vararg = next(parameter) - name = vararg.name - if vararg.annotation is not _Parameter.empty: - return name, ast_from_object(vararg.annotation) - else: - return name, None - except StopIteration: - return None, None - - names, defaults, annotations = extract_args(parameters.get(_Parameter.POSITIONAL_OR_KEYWORD, ())) - kwonlyargs, kw_defaults, kwonly_annotations = extract_args(parameters.get(_Parameter.KEYWORD_ONLY, ())) - kwarg, varargannotation = extract_vararg(parameters.get(_Parameter.VAR_POSITIONAL, ())) - kwarg, kwargannotation = parameters.get(_Parameter.VAR_KEYWORD, None), - return build_function(name or getattr(func, '__name__', None), - args, defaults, kwonlyargs, kw_defaults, - annotations, kwonly_annotations, - varargannotation, kwargannotation) - ast_from_object(signature.return_annotation), - func.__doc__, - parent) + return parameter[0].name + except IndexError: + return None + + vararg = parameters.get(_Parameter.VAR_POSITIONAL, ()) + kwarg = parameters.get(_Parameter.VAR_KEYWORD, ()) + vararg_name = extract_vararg(vararg) + kwarg_name = extract_vararg(kwarg) + args_node = nodes.Arguments(vararg=vararg_name, kwarg=kwarg_name, parent=func_node) + + # This ignores POSITIONAL_ONLY args, because they only appear in + # functions implemented in C and can't be mimicked by any Python + # function. + names, defaults, annotations = extract_args(parameters.get(_Parameter.POSITIONAL_OR_KEYWORD, ()), args_node) + kwonlynames, kw_defaults, kwonly_annotations = extract_args(parameters.get(_Parameter.KEYWORD_ONLY, ()), args_node) + args = [nodes.Name(name=n, parent=args_node) for n in names] + kwonlyargs = [nodes.Name(name=n, parent=args_node) for n in kwonlynames] + if vararg_name and vararg[0].annotation is not _Parameter.empty: + varargannotation = vararg.annotation + else: + varargannotation = None + if kwarg_name and kwarg[0].annotation is not _Parameter.empty: + kwargannotation = kwarg.annotation + else: + kwargannotation = None + args_node.postinit(args, defaults, kwonlyargs, kw_defaults, + annotations, kwonly_annotations, + varargannotation, kwargannotation) + func_node.postinit(args=args_node, body=[], + returns=_ast_from_object(signature.return_annotation, + parent=func_node)) + built_objects[func] = func_node + return func_node + + +BUILTIN_CONTAINERS = {list: nodes.List, set: nodes.Set, frozenset: + nodes.Set, tuple: nodes.Tuple} # pylint: disable=unused-variable; doesn't understand singledispatch -@ast_from_object.register(list) -@ast_from_object.register(dict) -@ast_from_object.register(set) -@ast_from_object.register(tuple) -def ast_from_containers(cls, built_objects=(), name=None, parent=None): - '''Handles builtin types that have their own AST nodes, like list but - not like frozenset or range.''' - -@ast_from_object.register(str) -@ast_from_object.register(bytes) -@ast_from_object.register(int) -@ast_from_object.register(float) -@ast_from_object.register(complex) -def ast_from_scalar(cls, built_objects=(), name=None, parent=None): - pass +@_ast_from_object.register(list) +@_ast_from_object.register(set) +@_ast_from_object.register(frozenset) +@_ast_from_object.register(tuple) +def ast_from_builtin_container(container, built_objects, module, name=None, + parent=None): + '''Handles builtin containers that have their own AST nodes, like list + but not range. + + ''' + if (container in built_objects and + built_objects[container].targets[0].name == name) + return built_objects[container] + if name: + parent = nodes.Assign(parent=parent) + name_node = nodes.AssignName(name, parent=parent) + container_node = BUILTIN_CONTAINERS[type(container)](parent=parent) + container_node.postinit( + elts=[_ast_from_object(i, built_objects, module, parent=node) + for i in container]) + if name: + parent.postinit(targets=[name_node], value=container_node) + node = parent + else: + node = container_node + built_objects[container] = node + return node + + +# pylint: disable=unused-variable; doesn't understand singledispatch +@_ast_from_object.register(dict) +def ast_from_dict(dictionary, built_objects, module, name=None, + parent=None): + if (dictionary in built_objects and + built_objects[dictionary].targets[0].name == name) + return built_objects[dictionary] + if name: + parent = nodes.Assign(parent=parent) + name_node = nodes.AssignName(name, parent=parent) + dict_node = nodes.Dict(parent=parent) + dict_node.postinit(items=[ + _ast_from_object(k, built_objects, module, parent=node), + _ast_from_object(v, built_objects, module, parent=node), + for k, v in dictionary.items()]) + if name: + parent.postinit(targets=[name_node], value=dict_node) + node = parent + else: + node = dict_node + built_objects[dictionary] = node + return node + if six.PY2: - ast_from_object.register(unicode, ast_from_scalar) - ast_from_object.register(long, ast_from_scalar) + _ast_from_object.register(types.DictProxyType, ast_from_dict) +else: + _ast_from_object.register(types.MappingProxyType, ast_from_dict) + + +@_ast_from_object.register(str) +@_ast_from_object.register(bytes) +@_ast_from_object.register(int) +@_ast_from_object.register(float) +@_ast_from_object.register(complex) +@_ast_from_object.register(type(None)) +@_ast_from_object.register(type(NotImplemented)) +def ast_from_scalar(scalar, built_objects, module, name=None, parent=None): + if (scalar in built_objects and + built_objects[scalar].targets[0].name == name) + return built_objects[scalar] + if name: + parent = nodes.Assign(parent=parent) + name_node = nodes.AssignName(name, parent=parent) + scalar_node = Const(value=scalar, parent=parent) + if name: + parent.postinit(targets=[name_node], value=scalar_node) + node = parent + else: + node = scalar_node + built_objects[scalar] = node + return node + +if six.PY2: + _ast_from_object.register(unicode, ast_from_scalar) + _ast_from_object.register(long, ast_from_scalar) + + +@_ast_from_object.register(type(Ellipsis)) +def ast_from_ellipsis(ellipsis, built_objects, module, name=None, parent=None): + if (ellipsis in built_objects and + built_objects[ellipsis].targets[0].name == name) + return built_objects[ellipsis] + if name: + parent = nodes.Assign(parent=parent) + name_node = nodes.AssignName(name, parent=parent) + ellipsis_node = nodes.Ellipsis(parent=parent) + if name: + parent.postinit(targets=[name_node], value=ellipsis_node) + node = parent + else: + node = ellipsis_node + built_objects[ellipsis] = node + return node @@ -506,7 +659,7 @@ def _astroid_bootstrapping(astroid_builtin=None): else: _CONST_PROXY[cls] = proxy -_astroid_bootstrapping() +# _astroid_bootstrapping() # @scoped_nodes.get_locals.register(Builtins) @@ -527,11 +680,11 @@ def _astroid_bootstrapping(astroid_builtin=None): # TODO : find a nicer way to handle this situation; # However __proxied introduced an # infinite recursion (see https://bugs.launchpad.net/pylint/+bug/456870) -def _set_proxied(const): - return _CONST_PROXY[const.value.__class__] -nodes.Const._proxied = property(_set_proxied) - -_GeneratorType = nodes.ClassDef(types.GeneratorType.__name__, types.GeneratorType.__doc__) -_GeneratorType.parent = MANAGER.astroid_cache[six.moves.builtins.__name__] -bases.Generator._proxied = _GeneratorType -Astroid_BUILDER.object_build(bases.Generator._proxied, types.GeneratorType) +# def _set_proxied(const): +# return _CONST_PROXY[const.value.__class__] +# nodes.Const._proxied = property(_set_proxied) + +# _GeneratorType = nodes.ClassDef(types.GeneratorType.__name__, types.GeneratorType.__doc__) +# _GeneratorType.parent = MANAGER.astroid_cache[six.moves.builtins.__name__] +# bases.Generator._proxied = _GeneratorType +# Astroid_BUILDER.object_build(bases.Generator._proxied, types.GeneratorType) diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index 60c1bd5b88..fd6550fa59 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -797,8 +797,7 @@ def postinit(self, args, body, decorators=None, returns=None): @decorators_mod.cachedproperty def extra_decorators(self): - - """Get the extra decorators that this function can haves + """Get the extra decorators that this function can haves Additional decorators are considered when they are used as assignments, as in `method = staticmethod(method)`. The property will return all the callables that are used for @@ -1829,10 +1828,12 @@ class attributes defined in the class body, also what a locals() ''' raise TypeError("This isn't an astroid node: %s" % type(node)) +# pylint: disable=unused-variable; doesn't understand singledispatch @get_locals.register(node_classes.NodeNG) def not_scoped_node(node): raise TypeError("This node doesn't have local variables: %s" % type(node)) +# pylint: disable=unused-variable; doesn't understand singledispatch @get_locals.register(LocalsDictNodeNG) def scoped_node(node): locals_ = collections.defaultdict(list) From 08e7be6bd61d22182b1f79d7694ac88c36becf9a Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Sun, 25 Oct 2015 00:46:17 -0400 Subject: [PATCH 012/312] First draft of mock AST generation code finished --- astroid/__init__.py | 47 +++---- astroid/builder.py | 9 +- astroid/node_classes.py | 63 ++++----- astroid/nodes.py | 1 - astroid/raw_building.py | 173 ++++++++++++++----------- astroid/rebuilder.py | 5 +- astroid/scoped_nodes.py | 19 ++- astroid/tests/unittest_builder.py | 10 +- astroid/tests/unittest_raw_building.py | 96 +++++++------- 9 files changed, 216 insertions(+), 207 deletions(-) diff --git a/astroid/__init__.py b/astroid/__init__.py index 8139e185dd..ab5108e40b 100644 --- a/astroid/__init__.py +++ b/astroid/__init__.py @@ -40,8 +40,9 @@ * builder contains the class responsible to build astroid trees """ -import sys +import importlib import re +import sys from operator import attrgetter # WARNING: internal imports order matters ! @@ -111,24 +112,26 @@ def transform(node, infer_function=infer_function): return transform -# def register_module_extender(manager, module_name, get_extension_mod): -# def transform(module): -# extension_module = get_extension_mod() -# # for name, obj in extension_module.locals.items(): -# # node.locals[name] = obj -# module.body.extend(extension_module.body) - -# manager.register_transform(Module, transform, lambda n: n.name == module_name) - - -# # load brain plugins -# from os import listdir -# from os.path import join, dirname -# BRAIN_MODULES_DIR = join(dirname(__file__), 'brain') -# if BRAIN_MODULES_DIR not in sys.path: -# # add it to the end of the list so user path take precedence -# sys.path.append(BRAIN_MODULES_DIR) -# # load modules in this directory -# for module in listdir(BRAIN_MODULES_DIR): -# if module.endswith('.py'): -# __import__(module[:-3]) +def register_module_extender(manager, module_name, get_extension_mod): + def transform(module): + extension_module = get_extension_mod() + # for name, obj in extension_module.locals.items(): + # node.locals[name] = obj + module.body.extend(extension_module.body) + + manager.register_transform(Module, transform, lambda n: n.name == module_name) + + +# load brain plugins +from os import listdir +from os.path import join, dirname +BRAIN_MODULES_DIR = join(dirname(__file__), 'brain') +if BRAIN_MODULES_DIR not in sys.path: + # add it to the end of the list so user path take precedence + sys.path.append(BRAIN_MODULES_DIR) +# load modules in this directory +for module in listdir(BRAIN_MODULES_DIR): + if module.endswith('.py'): + # print(module[:-3]) + # __import__(module[:-3]) + importlib.import_module(module[:-3]) diff --git a/astroid/builder.py b/astroid/builder.py index 9614b03964..f5daee0d41 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -83,7 +83,7 @@ def open_source_file(filename): MANAGER = manager.AstroidManager() -class AstroidBuilder(raw_building.InspectBuilder): +class AstroidBuilder(object): # (raw_building.InspectBuilder): """Class for building an astroid tree from source code or from a live module. The param *manager* specifies the manager class which should be used. @@ -94,7 +94,7 @@ class AstroidBuilder(raw_building.InspectBuilder): """ def __init__(self, manager=None, apply_transforms=True): - super(AstroidBuilder, self).__init__() + # super(AstroidBuilder, self).__init__() self._manager = manager or MANAGER self._apply_transforms = apply_transforms @@ -109,7 +109,10 @@ def module_build(self, module, modname=None): if node is None: # this is a built-in module # get a partial representation by introspection - node = self.inspect_build(module, modname=modname, path=path) + # node = self.inspect_build(module, modname=modname, path=path) + node = raw_building.ast_from_object(module, name=modname) + # FIXME + node.source_file = path if self._apply_transforms: # We have to handle transformation by ourselves since the # rebuilder isn't called for builtin nodes diff --git a/astroid/node_classes.py b/astroid/node_classes.py index c7e83b39bc..c379e27912 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -37,6 +37,7 @@ from astroid import mixins from astroid import util +raw_building = util.lazy_import('raw_building') BUILTINS = six.moves.builtins.__name__ MANAGER = manager.AstroidManager() @@ -614,7 +615,8 @@ def from_constants(cls, elts=None): if elts is None: node.elts = [] else: - node.elts = [const_factory(e) for e in elts] + node.elts = [raw_building.ast_from_scalar(e, {}, None, parent=node) + for e in elts] return node def itered(self): @@ -627,6 +629,11 @@ def bool_value(self): def pytype(self): pass + @decorators.cachedproperty + def _proxied(self): + builtins = MANAGER.astroid_cache[BUILTINS] + return builtins.getattr(type(self).__name__.lower())[0] + class LookupMixIn(object): """Mixin looking up a name in the right scope @@ -1195,6 +1202,11 @@ def pytype(self): def bool_value(self): return bool(self.value) + @decorators.cachedproperty + def _proxied(self): + builtins = MANAGER.astroid_cache[BUILTINS] + return builtins.getattr(type(self.value).__name__)[0] + class Continue(Statement): """class representing a Continue node""" @@ -1253,7 +1265,10 @@ def from_constants(cls, items=None): if items is None: node.items = [] else: - node.items = [(const_factory(k), const_factory(v)) + node.items = [(raw_building.ast_from_scalar(k, {}, None, + parent=node), + raw_building.ast_from_scalar(v, {}, None, + parent=node)) for k, v in items.items()] return node @@ -1297,6 +1312,11 @@ def getitem(self, lookup_key, context=None): def bool_value(self): return bool(self.items) + @decorators.cachedproperty + def _proxied(self): + builtins = MANAGER.astroid_cache[BUILTINS] + return builtins.getattr('dict')[0] + class Expr(Statement): """class representing a Expr node""" @@ -1323,8 +1343,9 @@ class EmptyNode(NodeNG): _other_fields = ('name', 'object') def __init__(self, object_, name=None, lineno=None, col_offset=None, parent=None): - if self.object is not None: + if object_ is not None: self.object = object_ + self.name = name super(EmptyNode, self).__init__(lineno, col_offset, parent) @property @@ -1829,42 +1850,6 @@ class DictUnpack(NodeNG): """Represents the unpacking of dicts into dicts using PEP 448.""" -# 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: - klasses += (unicode, long) - klasses += (bytes,) - for kls in klasses: - CONST_CLS[kls] = Const -_update_const_classes() - - -def const_factory(value, parent=None): - """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) - try: - return CONST_CLS[value.__class__](value, parent) - except (KeyError, AttributeError): - return EmptyNode(object_=value, parent=parent) - - # Backward-compatibility aliases Backquote = util.proxy_alias('Backquote', Repr) diff --git a/astroid/nodes.py b/astroid/nodes.py index 2fd6cb6591..bf122f141d 100644 --- a/astroid/nodes.py +++ b/astroid/nodes.py @@ -44,7 +44,6 @@ ImportFrom, Attribute, Global, If, IfExp, Import, Index, Keyword, List, Name, Nonlocal, Pass, Print, Raise, Return, Set, Slice, Starred, Subscript, TryExcept, TryFinally, Tuple, UnaryOp, While, With, Yield, YieldFrom, - const_factory, AsyncFor, Await, AsyncWith, # Backwards-compatibility aliases Backquote, Discard, AssName, AssAttr, Getattr, CallFunc, From, diff --git a/astroid/raw_building.py b/astroid/raw_building.py index d7e24f65af..cedc780a13 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -28,15 +28,21 @@ import types import warnings +# This is a useful function for introspecting class attributes in +# inspect that is for some reason not exported. +from inspect import classify_class_attrs as _classify_class_attrs + try: from functools import singledispatch as _singledispatch except ImportError: from singledispatch import singledispatch as _singledispatch try: - from inspect import signature as _signature, Parameter as _Parameter + from inspect import (signature as _signature, Parameter as + _Parameter, Signature as _Signature) except ImportError: - from funcsigs import signature as _signature, Parameter as _Parameter + from funcsigs import (signature as _signature, Parameter as + _Parameter, Signature as _Signature) import six @@ -47,24 +53,26 @@ from astroid import scoped_nodes from astroid import util -# The repr of objects of this type describe it as "slot wrapper", but -# the repr of the type itself calls it "wrapper_descriptor". It is -# used for unbound methods implemented in C on CPython. On Jython -# it's called "method_descriptor". On PyPy the methods of both -# builtin types and Python-defined types have the same type. +# This is a type used for some unbound methods implemented in C on +# CPython and all unbound special methods on Jython. Bound methods +# corresponding to unbound methods of this type have the type +# types.BuiltinMethodType on CPython and Jython. On PyPy all builtin +# and Python-defined methods share the same type. +MethodDescriptorType = type(object.__format__) + +# This is another type used for some unbound methods implemented in C +# on CPython. The repr of objects of this type describe it as "slot +# wrapper", but the repr of the type itself calls it +# "wrapper_descriptor". WrapperDescriptorType = type(object.__getattribute__) # Both the reprs, for objects of this type and the type itself, refer # to this type as "method-wrapper". It's used for bound methods -# implemented in C on CPython. On Jython, this is the same type as -# for builtin functions/methods. On PyPy both bound and unbound -# methods have the same type. +# corresponding to unbound methods with the wrapper_descriptor type on +# CPython. MethodWrapperType = type(object().__getattribute__) - MANAGER = manager.AstroidManager() -# the keys of CONST_CLS eg python builtin types -_CONSTANTS = tuple(node_classes.CONST_CLS) _BUILTINS = vars(six.moves.builtins) @@ -224,25 +232,18 @@ def ast_from_object(object_, name=None): module = inspect.getmodule(object_) return _ast_from_object(object_, built_objects, module, name) -# To talk about tomorrow: - -# Cycles in the ASTs - -# Cases where e.g. a module names itself something different that the -# name its parent gives it. - -# ABCs - -# classify_class_attrs - @_singledispatch def _ast_from_object(object_, built_objects, module, name=None, parent=None): - if object_ in built_objects: - return built_objects[object_] + if name: + parent = nodes.Assign(parent=parent) + name_node = nodes.AssignName(name, parent=parent) + empty_node = nodes.EmptyNode(name=name, object_=object_, parent=parent) + if name: + parent.postinit(targets=[name_node], value=empty_node) + node = parent else: - node = nodes.EmptyNode(name=name, object_=object_, parent=parent) - built_objects[object_] = node + node = empty_node return node @@ -262,19 +263,19 @@ def ast_from_module(module, built_objects, parent_module, name=None, parent=None source_file = None # inspect.getdoc returns None for modules without docstrings like # Jython Java modules. - module_node = node_class(name=name or module.__name__, - doc=inspect.getdoc(module), - source_file=source_file, - package=hasattr(module, '__path__'), - # Assume that if inspect couldn't find a - # Python source file, it's probably not - # implemented in pure Python. - pure_python=bool(source_file)) + module_node = nodes.Module(name=name or module.__name__, + doc=inspect.getdoc(module), + source_file=source_file, + package=hasattr(module, '__path__'), + # Assume that if inspect couldn't find a + # Python source file, it's probably not + # implemented in pure Python. + pure_python=bool(source_file)) + built_objects[module] = module_node MANAGER.cache_module(module_node) module_node.postinit(body=[_ast_from_object(m, built_objects, module, n, module_node) for n, m in inspect.getmembers(module)]) - built_objects[module] = module_node return module_node @@ -282,19 +283,29 @@ def ast_from_module(module, built_objects, parent_module, name=None, parent=None @_ast_from_object.register(type) @_ast_from_object.register(types.GetSetDescriptorType) @_ast_from_object.register(types.MemberDescriptorType) -def ast_from_class(cls, built_objects, module, name=None, parent=None): +def ast_from_class(cls, built_objects, module, name=None, parent=None): + # print(cls) inspected_module = inspect.getmodule(cls) - if inspected_module is not module: - return ImportFrom(fromname=getattr(inspected_module, '__name__', None), - names=[[getattr(cls, '__name__', None), name]], - parent=parent) + if inspected_module is not None and inspected_module is not module: + return nodes.ImportFrom(fromname= + getattr(inspected_module, '__name__', None), + names=[[cls.__name__, name]], + parent=parent) if cls in built_objects: return built_objects[cls] - node = nodes.ClassDef(name=name, doc=inspect.getdoc(cls)) - node.postinit(bases=[nodes.Name(b, node) for b in basenames], - body=(), decorators=()) - # TODO + class_node = nodes.ClassDef(name=cls.__name__ or name, doc=inspect.getdoc(cls)) built_objects[cls] = class_node + try: + bases = [nodes.Name(name=b.__name__, parent=class_node) + for b in inspect.getmro(cls)[1:]] + body = [_ast_from_object(a.object, built_objects, module, a.name, parent=class_node) + for a in _classify_class_attrs(cls) if a.defining_class is cls] + except AttributeError: + bases = () + body = [_ast_from_object(m, built_objects, module, n, parent=class_node) + for n, m in inspect.getmembers(cls)] + class_node.postinit(bases=bases, body=body, decorators=(), + newstyle=isinstance(cls, type)) return class_node # Old-style classes if six.PY2: @@ -308,6 +319,7 @@ def ast_from_class(cls, built_objects, module, name=None, parent=None): @_ast_from_object.register(types.BuiltinFunctionType) @_ast_from_object.register(types.BuiltinMethodType) # Methods implemented in C on CPython. +@_ast_from_object.register(MethodDescriptorType) @_ast_from_object.register(WrapperDescriptorType) @_ast_from_object.register(MethodWrapperType) # types defines a LambdaType but on all existing Python @@ -316,19 +328,23 @@ def ast_from_class(cls, built_objects, module, name=None, parent=None): @_ast_from_object.register(types.MethodType) def ast_from_function(func, built_objects, module, name=None, parent=None): inspected_module = inspect.getmodule(func) - if inspected_module is not module: - return ImportFrom(fromname=getattr(inspected_module, '__name__', None), - names=[[getattr(func, '__name__', None), name]], - parent=parent) + if inspected_module is not None and inspected_module is not module: + return nodes.ImportFrom(fromname=getattr(inspected_module, '__name__', None), + names=[[func.__name__, name]], + parent=parent) if func in built_objects: return built_objects[func] - signature = _signature(func) + func_node = nodes.FunctionDef(name=name or func.__name__, + doc=inspect.getdoc(func), + parent=parent) + try: + signature = _signature(func) + except (ValueError, TypeError): + # FIXME: temporary hack + signature = _Signature() parameters = {k: tuple(g) for k, g in itertools.groupby(signature.parameters.values(), operator.attrgetter('kind'))} - func_node = nodes.FunctionDef(name=name or getattr(func, '__name__', None), - doc=inspect.getdoc(func), - parent=parent) def extract_args(parameters, parent): '''Takes an iterator over Parameter objects and returns three @@ -341,9 +357,9 @@ def extract_args(parameters, parent): for parameter in parameters: names.append(parameter.name) if parameter.default is not _Parameter.empty: - defaults.append(_ast_from_object(parameter.default, parent=parent)) + defaults.append(_ast_from_object(parameter.default, built_objects, module, parent=parent)) if parameter.annotation is not _Parameter.empty: - annotations.append(_ast_from_object(parameter.annotation, parent=parent)) + annotations.append(_ast_from_object(parameter.annotation, built_objects, module, parent=parent)) else: annotations.append(None) return names, defaults, annotations @@ -379,12 +395,15 @@ def extract_vararg(parameter): kwargannotation = kwarg.annotation else: kwargannotation = None + if signature.return_annotation is not _Parameter.empty: + returns=_ast_from_object(signature_return_annotation, + built_objects, + module, + parent=func_node) args_node.postinit(args, defaults, kwonlyargs, kw_defaults, annotations, kwonly_annotations, varargannotation, kwargannotation) - func_node.postinit(args=args_node, body=[], - returns=_ast_from_object(signature.return_annotation, - parent=func_node)) + func_node.postinit(args=args_node, body=[]) built_objects[func] = func_node return func_node @@ -404,21 +423,22 @@ def ast_from_builtin_container(container, built_objects, module, name=None, ''' if (container in built_objects and - built_objects[container].targets[0].name == name) + built_objects[container].targets[0].name == name): return built_objects[container] if name: parent = nodes.Assign(parent=parent) name_node = nodes.AssignName(name, parent=parent) container_node = BUILTIN_CONTAINERS[type(container)](parent=parent) - container_node.postinit( - elts=[_ast_from_object(i, built_objects, module, parent=node) - for i in container]) if name: - parent.postinit(targets=[name_node], value=container_node) node = parent else: node = container_node built_objects[container] = node + container_node.postinit( + elts=[_ast_from_object(i, built_objects, module, parent=node) + for i in container]) + if name: + parent.postinit(targets=[name_node], value=container_node) return node @@ -427,22 +447,23 @@ def ast_from_builtin_container(container, built_objects, module, name=None, def ast_from_dict(dictionary, built_objects, module, name=None, parent=None): if (dictionary in built_objects and - built_objects[dictionary].targets[0].name == name) + built_objects[dictionary].targets[0].name == name): return built_objects[dictionary] if name: parent = nodes.Assign(parent=parent) name_node = nodes.AssignName(name, parent=parent) dict_node = nodes.Dict(parent=parent) - dict_node.postinit(items=[ - _ast_from_object(k, built_objects, module, parent=node), - _ast_from_object(v, built_objects, module, parent=node), - for k, v in dictionary.items()]) if name: - parent.postinit(targets=[name_node], value=dict_node) node = parent else: node = dict_node built_objects[dictionary] = node + dict_node.postinit(items=[ + (_ast_from_object(k, built_objects, module, parent=node), + _ast_from_object(v, built_objects, module, parent=node)) + for k, v in dictionary.items()]) + if name: + parent.postinit(targets=[name_node], value=dict_node) return node if six.PY2: @@ -459,19 +480,15 @@ def ast_from_dict(dictionary, built_objects, module, name=None, @_ast_from_object.register(type(None)) @_ast_from_object.register(type(NotImplemented)) def ast_from_scalar(scalar, built_objects, module, name=None, parent=None): - if (scalar in built_objects and - built_objects[scalar].targets[0].name == name) - return built_objects[scalar] if name: parent = nodes.Assign(parent=parent) name_node = nodes.AssignName(name, parent=parent) - scalar_node = Const(value=scalar, parent=parent) + scalar_node = nodes.Const(value=scalar, parent=parent) if name: parent.postinit(targets=[name_node], value=scalar_node) node = parent else: node = scalar_node - built_objects[scalar] = node return node if six.PY2: @@ -481,9 +498,6 @@ def ast_from_scalar(scalar, built_objects, module, name=None, parent=None): @_ast_from_object.register(type(Ellipsis)) def ast_from_ellipsis(ellipsis, built_objects, module, name=None, parent=None): - if (ellipsis in built_objects and - built_objects[ellipsis].targets[0].name == name) - return built_objects[ellipsis] if name: parent = nodes.Assign(parent=parent) name_node = nodes.AssignName(name, parent=parent) @@ -493,7 +507,6 @@ def ast_from_ellipsis(ellipsis, built_objects, module, name=None, parent=None): node = parent else: node = ellipsis_node - built_objects[ellipsis] = node return node @@ -684,6 +697,8 @@ def _astroid_bootstrapping(astroid_builtin=None): # return _CONST_PROXY[const.value.__class__] # nodes.Const._proxied = property(_set_proxied) +astroid_builtin = ast_from_object(six.moves.builtins) + # _GeneratorType = nodes.ClassDef(types.GeneratorType.__name__, types.GeneratorType.__doc__) # _GeneratorType.parent = MANAGER.astroid_cache[six.moves.builtins.__name__] # bases.Generator._proxied = _GeneratorType diff --git a/astroid/rebuilder.py b/astroid/rebuilder.py index 9b362d2f19..5db8c966a4 100644 --- a/astroid/rebuilder.py +++ b/astroid/rebuilder.py @@ -26,7 +26,6 @@ from astroid import nodes - _BIN_OP_CLASSES = {ast.Add: '+', ast.BitAnd: '&', ast.BitOr: '|', @@ -86,8 +85,10 @@ def _get_doc(node): doc = node.body[0].value.s node.body = node.body[1:] return node, doc + else: + return node, None except IndexError: - return node, None # ast built from scratch + return node, None def _visit_or_none(node, attr, visitor, parent, assign_ctx, visit='visit', diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index fd6550fa59..e612a6ca44 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -327,41 +327,41 @@ def postinit(self, body=None): # Legacy API aliases @property def file(self): - rename_warning(('file', 'source_file')) + util.rename_warning(('file', 'source_file')) return self.source_file @file.setter def file(self, source_file): - rename_warning(('file', 'source_file')) + util.rename_warning(('file', 'source_file')) self.source_file = source_file @file.deleter def file(self): - rename_warning(('file', 'source_file')) + util.rename_warning(('file', 'source_file')) del self.source_file @property def path(self): - rename_warning(('path', 'source_file')) + util.rename_warning(('path', 'source_file')) return self.source_file @path.setter def path(self, source_file): - rename_warning(('path', 'source_file')) + util.rename_warning(('path', 'source_file')) self.source_file = source_file @path.deleter def path(self): - rename_warning(('path', 'source_file')) + util.rename_warning(('path', 'source_file')) del self.source_file @property def files_bytes(self): - rename_warning(('files_bytes', 'source_code')) + util.rename_warning(('files_bytes', 'source_code')) return self.source_code @files_bytes.setter def files_bytes(self, source_code): - rename_warning(('files_bytes', 'source_code')) + util.rename_warning(('files_bytes', 'source_code')) self.source_code = source_code @files_bytes.deleter def files_bytes(self): - rename_warning(('files_bytes', 'source_code')) + util.rename_warning(('files_bytes', 'source_code')) del self.source_code @property @@ -1137,7 +1137,6 @@ class ClassDef(mixins.FilterStmtsMixin, LocalsDictNodeNG, # some of the attributes below are set by the builder module or # by a raw factories - # a dictionary of class instances attributes _astroid_fields = ('decorators', 'bases', 'body') decorators = None diff --git a/astroid/tests/unittest_builder.py b/astroid/tests/unittest_builder.py index ffa7bf246b..2686fe2f87 100644 --- a/astroid/tests/unittest_builder.py +++ b/astroid/tests/unittest_builder.py @@ -27,6 +27,7 @@ from astroid import exceptions from astroid import manager from astroid import nodes +from astroid import raw_building from astroid import test_utils from astroid import util from astroid.tests import resources @@ -320,19 +321,20 @@ def test_inspect_build2(self): except ImportError: self.skipTest('test skipped: mxDateTime is not available') else: - dt_ast = self.builder.inspect_build(DateTime) + dt_ast = raw_building.ast_from_object(DateTime) # self.builder.inspect_build(DateTime) dt_ast.getattr('DateTime') # this one is failing since DateTimeType.__module__ = 'builtins' ! #dt_ast.getattr('DateTimeType') def test_inspect_build3(self): - self.builder.inspect_build(unittest) + # self.builder.inspect_build(unittest) + raw_building.ast_from_object(unittest) @test_utils.require_version(maxver='3.0') def test_inspect_build_instance(self): """test astroid tree build from a living object""" import exceptions - builtin_ast = self.builder.inspect_build(exceptions) + builtin_ast = raw_building.ast_from_object(exceptions) # self.builder.inspect_build(exceptions) fclass = builtin_ast['OSError'] # things like OSError.strerror are now (2.5) data descriptors on the # class instead of entries in the __dict__ of an instance @@ -396,7 +398,7 @@ def yiell(): #@ self.assertIsInstance(func.body[1].body[0].value, nodes.Yield) def test_object(self): - obj_ast = self.builder.inspect_build(object) + obj_ast = raw_building.ast_from_object(object) # self.builder.inspect_build(object) self.assertIn('__setattr__', obj_ast) def test_newstyle_detection(self): diff --git a/astroid/tests/unittest_raw_building.py b/astroid/tests/unittest_raw_building.py index ad57c4f7e2..3d27cd6fdc 100644 --- a/astroid/tests/unittest_raw_building.py +++ b/astroid/tests/unittest_raw_building.py @@ -5,53 +5,54 @@ from six.moves import builtins # pylint: disable=import-error from astroid.builder import AstroidBuilder -from astroid.raw_building import ( - attach_dummy_node, build_module, build_class, build_function, - build_from_import, object_build_function, Parameter -) +# from astroid.raw_building import ( +# attach_dummy_node, build_module, build_class, build_function, +# build_from_import, object_build_function, Parameter +# ) from astroid import nodes -from astroid import util +from astroid import raw_building from astroid import test_utils +from astroid import util from astroid.bases import BUILTINS class RawBuildingTC(unittest.TestCase): - def test_attach_dummy_node(self): - node = build_module('MyModule') - attach_dummy_node(node, 'DummyNode') - self.assertEqual(1, len(list(node.get_children()))) - - def test_build_module(self): - node = build_module('MyModule') - self.assertEqual(node.name, 'MyModule') - self.assertEqual(node.pure_python, False) - self.assertEqual(node.package, False) - self.assertEqual(node.parent, None) - - def test_build_class(self): - node = build_class('MyClass') - self.assertEqual(node.name, 'MyClass') - self.assertEqual(node.doc, None) - - def test_build_function(self): - node = build_function('MyFunction') - self.assertEqual(node.name, 'MyFunction') - self.assertEqual(node.doc, None) - - def test_build_function_args(self): - args = [Parameter('myArgs1', None, None, None), - Parameter('myArgs2', None, None, None)] - node = build_function('MyFunction', args) - self.assertEqual('myArgs1', node.args.args[0].name) - self.assertEqual('myArgs2', node.args.args[1].name) - self.assertEqual(2, len(node.args.args)) - - def test_build_function_defaults(self): - args = [Parameter('myArgs1', 'defaults1', None, None), - Parameter('myArgs2', 'defaults2', None, None)] - node = build_function('MyFunction', args) - self.assertEqual(2, len(node.args.defaults)) + # def test_attach_dummy_node(self): + # node = build_module('MyModule') + # attach_dummy_node(node, 'DummyNode') + # self.assertEqual(1, len(list(node.get_children()))) + + # def test_build_module(self): + # node = build_module('MyModule') + # self.assertEqual(node.name, 'MyModule') + # self.assertEqual(node.pure_python, False) + # self.assertEqual(node.package, False) + # self.assertEqual(node.parent, None) + + # def test_build_class(self): + # node = build_class('MyClass') + # self.assertEqual(node.name, 'MyClass') + # self.assertEqual(node.doc, None) + + # def test_build_function(self): + # node = build_function('MyFunction') + # self.assertEqual(node.name, 'MyFunction') + # self.assertEqual(node.doc, None) + + # def test_build_function_args(self): + # args = [Parameter('myArgs1', None, None, None), + # Parameter('myArgs2', None, None, None)] + # node = build_function('MyFunction', args) + # self.assertEqual('myArgs1', node.args.args[0].name) + # self.assertEqual('myArgs2', node.args.args[1].name) + # self.assertEqual(2, len(node.args.args)) + + # def test_build_function_defaults(self): + # args = [Parameter('myArgs1', 'defaults1', None, None), + # Parameter('myArgs2', 'defaults2', None, None)] + # node = build_function('MyFunction', args) + # self.assertEqual(2, len(node.args.defaults)) @unittest.skipIf(sys.version_info[0] > 2, 'Tuples are not allowed in function args in Python 3') @@ -60,12 +61,12 @@ def test_build_function_with_tuple_args(self): # TODO def f(a, (b, (c, d))): pass - object_build_function(f) + raw_building.ast_from_object(f) - def test_build_from_import(self): - names = ['exceptions, inference, inspector'] - node = build_from_import('astroid', names) - self.assertEqual(len(names), len(node.names)) + # def test_build_from_import(self): + # names = ['exceptions, inference, inspector'] + # node = build_from_import('astroid', names) + # self.assertEqual(len(names), len(node.names)) @test_utils.require_version(minver='3.0') def test_io_is__io(self): @@ -76,8 +77,9 @@ def test_io_is__io(self): # the true name of the module. import _io - builder = AstroidBuilder() - module = builder.inspect_build(_io) + # builder = AstroidBuilder() + # module = builder.inspect_build(_io) + module = raw_building.ast_from_object(_io, name='io') buffered_reader = module.getattr('BufferedReader')[0] self.assertEqual(buffered_reader.root().name, 'io') From 8dc2470fe3ce323760377acd82aecb0e335cdbe1 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Sun, 25 Oct 2015 10:30:08 -0400 Subject: [PATCH 013/312] Use id() for hashing in ast_from_object and fix minor bugs --- astroid/manager.py | 5 +++-- astroid/node_classes.py | 1 - astroid/raw_building.py | 31 +++++++++++++++---------------- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/astroid/manager.py b/astroid/manager.py index 07d7543cab..9fca46bc88 100644 --- a/astroid/manager.py +++ b/astroid/manager.py @@ -269,5 +269,6 @@ def clear_cache(self, astroid_builtin=None): # unittest_lookup.LookupTC.test_builtin_lookup fail depending on the # test order import astroid.raw_building - astroid.raw_building._astroid_bootstrapping( - astroid_builtin=astroid_builtin) + # astroid.raw_building._astroid_bootstrapping( + # astroid_builtin=astroid_builtin) + astroid.raw_building.ast_from_object(six.moves.builtins) diff --git a/astroid/node_classes.py b/astroid/node_classes.py index c379e27912..6e68afcf99 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -1348,7 +1348,6 @@ def __init__(self, object_, name=None, lineno=None, col_offset=None, parent=None self.name = name super(EmptyNode, self).__init__(lineno, col_offset, parent) - @property def has_underlying_object(self): return hasattr(self, 'object') diff --git a/astroid/raw_building.py b/astroid/raw_building.py index cedc780a13..e87f81d4c2 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -254,8 +254,8 @@ def ast_from_module(module, built_objects, parent_module, name=None, parent=None # This module has been imported into another. return nodes.Import([[getattr(module, '__name__', None), name]], parent=parent) - if module in built_objects: - return built_objects[module] + if id(module) in built_objects: + return built_objects[id(module)] try: source_file = inspect.getsourcefile(module) except TypeError: @@ -271,7 +271,7 @@ def ast_from_module(module, built_objects, parent_module, name=None, parent=None # Python source file, it's probably not # implemented in pure Python. pure_python=bool(source_file)) - built_objects[module] = module_node + built_objects[id(module)] = module_node MANAGER.cache_module(module_node) module_node.postinit(body=[_ast_from_object(m, built_objects, module, n, module_node) @@ -284,17 +284,16 @@ def ast_from_module(module, built_objects, parent_module, name=None, parent=None @_ast_from_object.register(types.GetSetDescriptorType) @_ast_from_object.register(types.MemberDescriptorType) def ast_from_class(cls, built_objects, module, name=None, parent=None): - # print(cls) inspected_module = inspect.getmodule(cls) if inspected_module is not None and inspected_module is not module: return nodes.ImportFrom(fromname= getattr(inspected_module, '__name__', None), names=[[cls.__name__, name]], parent=parent) - if cls in built_objects: - return built_objects[cls] + if id(cls) in built_objects: + return built_objects[id(cls)] class_node = nodes.ClassDef(name=cls.__name__ or name, doc=inspect.getdoc(cls)) - built_objects[cls] = class_node + built_objects[id(cls)] = class_node try: bases = [nodes.Name(name=b.__name__, parent=class_node) for b in inspect.getmro(cls)[1:]] @@ -332,8 +331,8 @@ def ast_from_function(func, built_objects, module, name=None, parent=None): return nodes.ImportFrom(fromname=getattr(inspected_module, '__name__', None), names=[[func.__name__, name]], parent=parent) - if func in built_objects: - return built_objects[func] + if id(func) in built_objects: + return built_objects[id(func)] func_node = nodes.FunctionDef(name=name or func.__name__, doc=inspect.getdoc(func), parent=parent) @@ -404,7 +403,7 @@ def extract_vararg(parameter): annotations, kwonly_annotations, varargannotation, kwargannotation) func_node.postinit(args=args_node, body=[]) - built_objects[func] = func_node + built_objects[id(func)] = func_node return func_node @@ -422,9 +421,9 @@ def ast_from_builtin_container(container, built_objects, module, name=None, but not range. ''' - if (container in built_objects and + if (id(container) in built_objects and built_objects[container].targets[0].name == name): - return built_objects[container] + return built_objects[id(container)] if name: parent = nodes.Assign(parent=parent) name_node = nodes.AssignName(name, parent=parent) @@ -433,7 +432,7 @@ def ast_from_builtin_container(container, built_objects, module, name=None, node = parent else: node = container_node - built_objects[container] = node + built_objects[id(container)] = node container_node.postinit( elts=[_ast_from_object(i, built_objects, module, parent=node) for i in container]) @@ -446,9 +445,9 @@ def ast_from_builtin_container(container, built_objects, module, name=None, @_ast_from_object.register(dict) def ast_from_dict(dictionary, built_objects, module, name=None, parent=None): - if (dictionary in built_objects and + if (id(dictionary) in built_objects and built_objects[dictionary].targets[0].name == name): - return built_objects[dictionary] + return built_objects[id(dictionary)] if name: parent = nodes.Assign(parent=parent) name_node = nodes.AssignName(name, parent=parent) @@ -457,7 +456,7 @@ def ast_from_dict(dictionary, built_objects, module, name=None, node = parent else: node = dict_node - built_objects[dictionary] = node + built_objects[id(dictionary)] = node dict_node.postinit(items=[ (_ast_from_object(k, built_objects, module, parent=node), _ast_from_object(v, built_objects, module, parent=node)) From 53292c9208db74b40887818d261bb8b57dffb29d Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Sun, 25 Oct 2015 10:48:25 -0400 Subject: [PATCH 014/312] Add missing id() call --- astroid/raw_building.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astroid/raw_building.py b/astroid/raw_building.py index e87f81d4c2..9420154845 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -446,7 +446,7 @@ def ast_from_builtin_container(container, built_objects, module, name=None, def ast_from_dict(dictionary, built_objects, module, name=None, parent=None): if (id(dictionary) in built_objects and - built_objects[dictionary].targets[0].name == name): + built_objects[id(dictionary)].targets[0].name == name): return built_objects[id(dictionary)] if name: parent = nodes.Assign(parent=parent) From 2b3485d652e6ff66fef20d5cbb4af02eef69670b Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Sun, 25 Oct 2015 12:14:11 -0400 Subject: [PATCH 015/312] Add another missing id() --- astroid/raw_building.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 9420154845..c8f789e8ec 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -252,8 +252,7 @@ def _ast_from_object(object_, built_objects, module, name=None, parent=None): def ast_from_module(module, built_objects, parent_module, name=None, parent=None): if module is not parent_module: # This module has been imported into another. - return nodes.Import([[getattr(module, '__name__', None), name]], - parent=parent) + return nodes.Import([[module.__name__, name]], parent=parent) if id(module) in built_objects: return built_objects[id(module)] try: @@ -261,9 +260,10 @@ def ast_from_module(module, built_objects, parent_module, name=None, parent=None except TypeError: # inspect.getsourcefile raises TypeError for built-in modules. source_file = None - # inspect.getdoc returns None for modules without docstrings like - # Jython Java modules. module_node = nodes.Module(name=name or module.__name__, + # inspect.getdoc returns None for + # modules without docstrings like + # Jython Java modules. doc=inspect.getdoc(module), source_file=source_file, package=hasattr(module, '__path__'), @@ -422,7 +422,7 @@ def ast_from_builtin_container(container, built_objects, module, name=None, ''' if (id(container) in built_objects and - built_objects[container].targets[0].name == name): + built_objects[id(container)].targets[0].name == name): return built_objects[id(container)] if name: parent = nodes.Assign(parent=parent) From bb8680d7aecf415f4c31e46ec4a44123f17db139 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Sun, 25 Oct 2015 20:03:03 -0400 Subject: [PATCH 016/312] Clean up obsolete code in raw_buliding --- astroid/raw_building.py | 267 ---------------------------------------- 1 file changed, 267 deletions(-) diff --git a/astroid/raw_building.py b/astroid/raw_building.py index c8f789e8ec..66853c7c59 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -84,11 +84,6 @@ def _io_discrepancy(member): member_self.__name__ == '_io' and member.__module__ == 'io') -def _attach_local_node(parent, node, name): - node.name = name # needed by add_local_node - parent.add_local_node(node) - - def _add_dunder_class(func, member): """Add a __class__ member to the given func node, if we can determine it.""" python_cls = member.__class__ @@ -99,36 +94,6 @@ def _add_dunder_class(func, member): ast_klass = build_class(cls_name, bases, python_cls.__doc__) func.instance_attrs['__class__'] = [ast_klass] - -def attach_const_node(node, name, value): - """create a Const node and register it in the locals of the given - node with the specified name - """ - if name not in node.special_attributes: - _attach_local_node(node, nodes.const_factory(value), name) - -def attach_import_node(node, modname, membername): - """create a ImportFrom node and register it in the locals of the given - node with the specified name - """ - from_node = nodes.ImportFrom(modname, [(membername, None)]) - _attach_local_node(node, from_node, membername) - - -def build_module(name, doc=None, modclass=nodes.Module): - """create and initialize a astroid Module node""" - util.rename_warning(('build_module', 'nodes.Module')) - return nodes.Module(name=name, doc=doc, package=False, pure_python=False) - - -def build_class(name, basenames=(), doc=None): - """create and initialize a astroid ClassDef node""" - node = nodes.ClassDef(name, doc) - node.postinit(bases=[nodes.Name(b, node) for b in basenames], - body=(), decorators=()) - return node - - Parameter = collections.namedtuple('Parameter', 'name default annotation kind') DEFAULT_PARAMETER = Parameter(None, None, None, None) @@ -148,19 +113,6 @@ def build_function(name, args=(), defaults=(), annotations=(), func.postinit(args=args_node, body=[], returns=returns) return func - -def build_from_import(fromname, names): - """create and initialize an astroid ImportFrom import statement""" - return nodes.ImportFrom(fromname, [(name, None) for name in names]) - - -def object_build_class(node, member, localname): - """create astroid for a living class object""" - basenames = [base.__name__ for base in member.__bases__] - return _base_class_object_build(node, member, basenames, - localname=localname) - - def object_build_function(parent, func, localname): """create astroid for a living function object""" signature = _signature(func) @@ -181,52 +133,6 @@ def object_build_function(parent, func, localname): return node -def object_build_datadescriptor(node, member, name): - """create astroid for a living data descriptor object""" - return _base_class_object_build(node, member, [], name) - - -def object_build_methoddescriptor(node, member, localname): - """create astroid for a living method descriptor object""" - # FIXME get arguments ? - func = build_function(getattr(member, '__name__', None) or localname, - doc=member.__doc__) - # set node's arguments to None to notice that we have no information, not - # and empty argument list - func.args.args = None - node.add_local_node(func, localname) - # _add_dunder_class(func, member) - - -def _base_class_object_build(node, member, basenames, name=None, localname=None): - """create astroid for a living class object, with a given set of base names - (e.g. ancestors) - """ - klass = build_class(name or getattr(member, '__name__', None) or localname, - basenames, member.__doc__) - klass._newstyle = isinstance(member, type) - node.add_local_node(klass, localname) - return klass - - -def _build_from_function(parent, name, member, module): - # verify this is not an imported function - try: - code = six.get_function_code(member) - except AttributeError: - # Some implementations don't provide the code object, - # such as Jython. - code = None - filename = getattr(code, 'co_filename', None) - if filename is None: - assert isinstance(member, object) - return object_build_methoddescriptor(parent, member, name) - elif filename != getattr(module, '__file__', None): - return nodes.EmptyNode(name=name, object_=member, parent=parent) - else: - return object_build_function(parent, member, name) - - def ast_from_object(object_, name=None): built_objects = {} module = inspect.getmodule(object_) @@ -509,171 +415,6 @@ def ast_from_ellipsis(ellipsis, built_objects, module, name=None, parent=None): return node - -class InspectBuilder(object): - """class for building nodes from living object - - this is actually a really minimal representation, including only Module, - FunctionDef and ClassDef nodes and some others as guessed. - """ - - # astroid from living objects ############################################### - - def __init__(self): - self._done = {} - self._module = None - - def inspect_build(self, module, modname=None, node_class=nodes.Module, - path=None): - """build astroid from a living module (i.e. using inspect) - this is used when there is no python source code available (either - because it's a built-in module or because the .py is not available) - """ - self._module = module - if modname is None: - modname = module.__name__ - # In Jython, Java modules have no __doc__ (see #109562) - doc = module.__doc__ if hasattr(module, '__doc__') else None - try: - source_file = inspect.getsourcefile(module) - except TypeError: - # inspect.getsourcefile raises TypeError for builtins. - source_file = None - module_node = node_class(name=modname, doc=doc, source_file=source_file, - package=hasattr(module - - , '__path__'), - # Assume that if inspect can't find a - # Python source file, it's probably not - # implemented in pure Python. - pure_python=bool(source_file)) - MANAGER.cache_module(module_node) - self._done = {} - module_node.postinit(body=self.object_build(module_node, module)) - return module_node - - def object_build(self, node, obj): - """recursive method which create a partial ast from real objects - (only function, class, and method are handled) - """ - if obj in self._done: - return self._done[obj] - self._done[obj] = node - members = [] - for name, member in inspect.getmembers(obj): - if inspect.ismethod(member): - member = six.get_method_function(member) - if inspect.isfunction(member): - members.append(_build_from_function(node, name, member, self._module)) - elif inspect.isbuiltin(member): - if (not _io_discrepancy(member) and - self.imported_member(node, member, name)): - continue - members.append(object_build_methoddescriptor(node, member, name)) - elif inspect.isclass(member): - if self.imported_member(node, member, name): - continue - if member in self._done: - class_node = self._done[member] - if class_node not in set(node.get_children()): - # if class_node not in node.locals.get(name, ()): - members.append(node.add_local_node(class_node, name)) - else: - class_node = object_build_class(node, member, name) - # recursion - members.append(self.object_build(class_node, member)) - if name == '__class__' and class_node.parent is None: - class_node.parent = self._done[self._module] - elif inspect.ismethoddescriptor(member): - assert isinstance(member, object) - members.append(object_build_methoddescriptor(node, member, name)) - elif inspect.isdatadescriptor(member): - assert isinstance(member, object) - members.append(object_build_datadescriptor(node, member, name)) - elif isinstance(member, _CONSTANTS): - members.append(attach_const_node(node, name, member)) - elif inspect.isroutine(member): - # This should be called for Jython, where some builtin - # methods aren't catched by isbuiltin branch. - members.append(_build_from_function(node, name, member, self._module)) - else: - # create an empty node so that the name is actually defined - members.append(nodes.EmptyNode(parent=node, name=name, - object_=member)) - return members - - def imported_member(self, node, member, name): - """verify this is not an imported class or handle it""" - # /!\ some classes like ExtensionClass doesn't have a __module__ - # attribute ! Also, this may trigger an exception on badly built module - # (see http://www.logilab.org/ticket/57299 for instance) - try: - modname = getattr(member, '__module__', None) - except: # pylint: disable=bare-except - warnings.warn('Unexpected error while building an AST from an ' - 'imported class.', RuntimeWarning, stacklevel=2) - modname = None - if modname is None: - if (name in ('__new__', '__subclasshook__') - or (name in _BUILTINS and util.JYTHON)): - # Python 2.5.1 (r251:54863, Sep 1 2010, 22:03:14) - # >>> print object.__new__.__module__ - # None - modname = six.moves.builtins.__name__ - else: - nodes.EmptyNode(parent=node, name=name, object_=member) - return True - - real_name = { - 'gtk': 'gtk_gtk', - '_io': 'io', - }.get(modname, modname) - - if real_name != self._module.__name__: - # check if it sounds valid and then add an import node, else use a - # dummy node - try: - getattr(sys.modules[modname], name) - except (KeyError, AttributeError): - attach_dummy_node(node, name, member) - else: - attach_import_node(node, modname, name) - return True - return False - - -### astroid bootstrapping ###################################################### -Astroid_BUILDER = InspectBuilder() - -# class Builtins(nodes.Module): -# pass - -_CONST_PROXY = {} -def _astroid_bootstrapping(astroid_builtin=None): - """astroid boot strapping the builtins module""" - # this boot strapping is necessary since we need the Const nodes to - # inspect_build builtins, and then we can proxy Const - if astroid_builtin is None: - from six.moves import builtins - astroid_builtin = Astroid_BUILDER.inspect_build(builtins) #, node_class=Builtins - - for cls, node_cls in node_classes.CONST_CLS.items(): - if cls is type(None): - proxy = build_class('NoneType') - proxy.parent = astroid_builtin - elif cls is type(NotImplemented): - proxy = build_class('NotImplementedType') - proxy.parent = astroid_builtin - else: - proxy = astroid_builtin.getattr(cls.__name__)[0] - if cls in (dict, list, set, tuple): - node_cls._proxied = proxy - else: - _CONST_PROXY[cls] = proxy - -# _astroid_bootstrapping() - - # @scoped_nodes.get_locals.register(Builtins) # def scoped_node(node): # locals_ = collections.defaultdict(list) @@ -688,14 +429,6 @@ def _astroid_bootstrapping(astroid_builtin=None): # scoped_nodes._get_locals(n, locals_) # return locals_ - -# TODO : find a nicer way to handle this situation; -# However __proxied introduced an -# infinite recursion (see https://bugs.launchpad.net/pylint/+bug/456870) -# def _set_proxied(const): -# return _CONST_PROXY[const.value.__class__] -# nodes.Const._proxied = property(_set_proxied) - astroid_builtin = ast_from_object(six.moves.builtins) # _GeneratorType = nodes.ClassDef(types.GeneratorType.__name__, types.GeneratorType.__doc__) From 21581aa19e48e74d2c8894c36d6d92028e8eb00f Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Sun, 25 Oct 2015 21:41:41 -0400 Subject: [PATCH 017/312] Replace recursions in constructed ASTs with Name nodes --- astroid/raw_building.py | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 66853c7c59..6340feb522 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -73,7 +73,6 @@ MethodWrapperType = type(object().__getattribute__) MANAGER = manager.AstroidManager() -_BUILTINS = vars(six.moves.builtins) def _io_discrepancy(member): @@ -160,7 +159,8 @@ def ast_from_module(module, built_objects, parent_module, name=None, parent=None # This module has been imported into another. return nodes.Import([[module.__name__, name]], parent=parent) if id(module) in built_objects: - return built_objects[id(module)] + # return built_objects[id(module)] + return nodes.Name(name=name or module.__name__, parent=parent_module) try: source_file = inspect.getsourcefile(module) except TypeError: @@ -197,8 +197,9 @@ def ast_from_class(cls, built_objects, module, name=None, parent=None): names=[[cls.__name__, name]], parent=parent) if id(cls) in built_objects: - return built_objects[id(cls)] - class_node = nodes.ClassDef(name=cls.__name__ or name, doc=inspect.getdoc(cls)) + # return built_objects[id(cls)] + return nodes.Name(name=name or cls.__name__, parent=parent) + class_node = nodes.ClassDef(name=name or cls.__name__, doc=inspect.getdoc(cls)) built_objects[id(cls)] = class_node try: bases = [nodes.Name(name=b.__name__, parent=class_node) @@ -238,10 +239,11 @@ def ast_from_function(func, built_objects, module, name=None, parent=None): names=[[func.__name__, name]], parent=parent) if id(func) in built_objects: - return built_objects[id(func)] + # return built_objects[id(func)] + return nodes.Name(name=name or func.__name__, parent=parent) func_node = nodes.FunctionDef(name=name or func.__name__, - doc=inspect.getdoc(func), - parent=parent) + doc=inspect.getdoc(func), + parent=parent) try: signature = _signature(func) except (ValueError, TypeError): @@ -329,11 +331,17 @@ def ast_from_builtin_container(container, built_objects, module, name=None, ''' if (id(container) in built_objects and built_objects[id(container)].targets[0].name == name): - return built_objects[id(container)] + # return built_objects[id(container)] + return nodes.Name(name=name, parent=parent) if name: parent = nodes.Assign(parent=parent) name_node = nodes.AssignName(name, parent=parent) - container_node = BUILTIN_CONTAINERS[type(container)](parent=parent) + try: + container_node = BUILTIN_CONTAINERS[type(container)](parent=parent) + except KeyError: + for container_type in BUILTIN_CONTAINERS: + if isinstance(container, container_type): + container_node = BUILTIN_CONTAINERS[container_type](parent=parent) if name: node = parent else: @@ -353,7 +361,8 @@ def ast_from_dict(dictionary, built_objects, module, name=None, parent=None): if (id(dictionary) in built_objects and built_objects[id(dictionary)].targets[0].name == name): - return built_objects[id(dictionary)] + # return built_objects[id(dictionary)] + return nodes.Name(name=name, parent=parent) if name: parent = nodes.Assign(parent=parent) name_node = nodes.AssignName(name, parent=parent) From ee17e72f11cd6b48d9dddbf9c45b9b47b3d9871a Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Sun, 25 Oct 2015 22:13:43 -0400 Subject: [PATCH 018/312] Add Unknown node for handling non-introspectable function signatures --- astroid/node_classes.py | 9 +++++++++ astroid/nodes.py | 6 +++--- astroid/raw_building.py | 6 ++++-- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/astroid/node_classes.py b/astroid/node_classes.py index 6e68afcf99..b0dd624130 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -957,6 +957,15 @@ def _format_args(args, defaults=None, annotations=None): return ', '.join(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. + + ''' + + class AssignAttr(mixins.ParentAssignTypeMixin, NodeNG): """class representing an AssignAttr node""" _astroid_fields = ('expr',) diff --git a/astroid/nodes.py b/astroid/nodes.py index bf122f141d..a9f187d9e4 100644 --- a/astroid/nodes.py +++ b/astroid/nodes.py @@ -40,7 +40,7 @@ Arguments, AssignAttr, Assert, Assign, AssignName, AugAssign, Repr, BinOp, BoolOp, Break, Call, Compare, Comprehension, Const, Continue, Decorators, DelAttr, DelName, Delete, - Dict, Expr, Ellipsis, EmptyNode, ExceptHandler, Exec, ExtSlice, For, + Dict, Expr, Ellipsis, ExceptHandler, Exec, ExtSlice, For, ImportFrom, Attribute, Global, If, IfExp, Import, Index, Keyword, List, Name, Nonlocal, Pass, Print, Raise, Return, Set, Slice, Starred, Subscript, TryExcept, TryFinally, Tuple, UnaryOp, While, With, Yield, YieldFrom, @@ -48,7 +48,7 @@ # Backwards-compatibility aliases Backquote, Discard, AssName, AssAttr, Getattr, CallFunc, From, # Node not present in the builtin ast module. - DictUnpack, + DictUnpack, EmptyNode, Unknown ) from astroid.scoped_nodes import ( Module, GeneratorExp, Lambda, DictComp, @@ -80,7 +80,7 @@ Raise, Return, Set, SetComp, Slice, Starred, Subscript, TryExcept, TryFinally, Tuple, - UnaryOp, + UnaryOp, Unknown, While, With, Yield, YieldFrom, ) diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 6340feb522..68c8fdff89 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -247,8 +247,10 @@ def ast_from_function(func, built_objects, module, name=None, parent=None): try: signature = _signature(func) except (ValueError, TypeError): - # FIXME: temporary hack - signature = _Signature() + # signature() raises these errors for non-introspectable + # callables. + func_node.postinit(args=nodes.Unknown(parent=func_node), body=[]) + return func_node parameters = {k: tuple(g) for k, g in itertools.groupby(signature.parameters.values(), operator.attrgetter('kind'))} From 862419b181d5c3cf25115638643235a6c8dfe49b Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Sun, 25 Oct 2015 22:25:42 -0400 Subject: [PATCH 019/312] Add stub infer() and as_string() for Unknown node --- astroid/as_string.py | 8 ++++---- astroid/node_classes.py | 3 +++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/astroid/as_string.py b/astroid/as_string.py index 0d7ba51974..7d671413cf 100644 --- a/astroid/as_string.py +++ b/astroid/as_string.py @@ -202,10 +202,6 @@ def visit_ellipsis(self, node): """return an astroid.Ellipsis node as string""" return '...' - def visit_empty(self, node): - """return an Empty node as string""" - return '' - def visit_exec(self, node): """return an astroid.Exec node as string""" if node.locals: @@ -399,6 +395,10 @@ def visit_unaryop(self, node): operator = node.op return '%s%s' % (operator, node.operand.accept(self)) + def visit_unknown(self, node): + """dummy method for visiting an Unknown node""" + return '' + def visit_while(self, node): """return an astroid.While node as string""" whiles = 'while %s:\n%s' % (node.test.accept(self), diff --git a/astroid/node_classes.py b/astroid/node_classes.py index b0dd624130..3f8c457fe1 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -964,6 +964,9 @@ class Unknown(NodeNG): introspection failed. ''' + def infer(self): + '''Inference on an Unknown node immediately terminates.''' + raise StopIteration class AssignAttr(mixins.ParentAssignTypeMixin, NodeNG): From dd8f899758ce43108bb4de929c4f2892b7804386 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Mon, 26 Oct 2015 00:48:08 -0400 Subject: [PATCH 020/312] Finish removing const_factory, fix funcsigs typo and miscellaneous small test failures --- astroid/__pkginfo__.py | 2 +- astroid/arguments.py | 2 +- astroid/inference.py | 2 +- astroid/manager.py | 2 +- astroid/node_classes.py | 6 ++---- astroid/protocols.py | 14 ++++++-------- astroid/raw_building.py | 9 ++++++++- astroid/scoped_nodes.py | 10 +++++----- astroid/tests/unittest_builder.py | 6 +++--- astroid/tests/unittest_lookup.py | 2 +- astroid/tests/unittest_manager.py | 4 ++-- astroid/tests/unittest_nodes.py | 5 +++-- astroid/tests/unittest_transforms.py | 6 +++--- 13 files changed, 37 insertions(+), 33 deletions(-) diff --git a/astroid/__pkginfo__.py b/astroid/__pkginfo__.py index 8a3188141b..ff9716ce78 100644 --- a/astroid/__pkginfo__.py +++ b/astroid/__pkginfo__.py @@ -30,7 +30,7 @@ elif sys.version_info == (3, 3): install_requires = ['lazy_object_proxy', 'singledispatch', 'six', 'wrapt'] else: - install_requires = ['funcigs', 'lazy_object_proxy', 'singledispatch', 'six', + install_requires = ['funcsigs', 'lazy_object_proxy', 'singledispatch', 'six', 'wrapt'] license = 'LGPL' diff --git a/astroid/arguments.py b/astroid/arguments.py index 5670fa863f..a5b3f860c3 100644 --- a/astroid/arguments.py +++ b/astroid/arguments.py @@ -208,7 +208,7 @@ def infer_argument(self, funcnode, name, context): kwarg = nodes.Dict(lineno=funcnode.args.lineno, col_offset=funcnode.args.col_offset, parent=funcnode.args) - kwarg.postinit([(nodes.const_factory(key), value) + kwarg.postinit([(nodes.Const(key, parent=kwarg), value) for key, value in kwargs.items()]) return iter((kwarg, )) elif funcnode.args.vararg == name: diff --git a/astroid/inference.py b/astroid/inference.py index 010607cf8a..542125946c 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -357,7 +357,7 @@ def _infer_unaryop(self, context=None): # YES, which will be returned as is. bool_value = operand.bool_value() if bool_value is not util.YES: - yield nodes.const_factory(not bool_value) + yield nodes.Const(not bool_value) else: yield util.YES else: diff --git a/astroid/manager.py b/astroid/manager.py index 9fca46bc88..b814634fd4 100644 --- a/astroid/manager.py +++ b/astroid/manager.py @@ -82,7 +82,7 @@ def ast_from_file(self, filepath, modname=None, fallback=True, source=False): modname = '.'.join(modutils.modpath_from_file(filepath)) except ImportError: modname = filepath - if modname in self.astroid_cache and self.astroid_cache[modname].file == filepath: + if modname in self.astroid_cache and self.astroid_cache[modname].source_file == filepath: return self.astroid_cache[modname] if source: from astroid.builder import AstroidBuilder diff --git a/astroid/node_classes.py b/astroid/node_classes.py index 3f8c457fe1..7636de8153 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -964,7 +964,7 @@ class Unknown(NodeNG): introspection failed. ''' - def infer(self): + def infer(self, context=None, **kwargs): '''Inference on an Unknown node immediately terminates.''' raise StopIteration @@ -1659,9 +1659,7 @@ def postinit(self, lower=None, upper=None, step=None): 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 Const(attr, parent=self) return attr @decorators.cachedproperty diff --git a/astroid/protocols.py b/astroid/protocols.py index 8bd38139c8..e01cd8f9ce 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -32,6 +32,7 @@ from astroid import decorators from astroid import node_classes from astroid import nodes +from astroid import raw_building from astroid import util @@ -83,7 +84,8 @@ def _augmented_name(name): def _infer_unary_op(obj, op): func = _UNARY_OPERATORS[op] value = func(obj) - return nodes.const_factory(value) + return raw_building.ast_from_object(value) + nodes.Tuple.infer_unary_op = lambda self, op: _infer_unary_op(tuple(self.elts), op) nodes.List.infer_unary_op = lambda self, op: _infer_unary_op(self.elts, op) @@ -121,7 +123,7 @@ def const_infer_binary_op(self, operator, other, context, _): try: impl = BIN_OP_IMPL[operator] try: - yield nodes.const_factory(impl(self.value, other.value)) + yield raw_building.ast_from_object(impl(self.value, other.value)) except TypeError: # ArithmeticError is not enough: float >> float is a TypeError yield not_implemented @@ -290,14 +292,10 @@ def _arguments_infer_argname(self, name, context): # TODO: just provide the type here, no need to have an empty Dict. if name == self.vararg: - vararg = nodes.const_factory(()) - vararg.parent = self - yield vararg + yield nodes.Tuple(parent=self) return if name == self.kwarg: - kwarg = nodes.const_factory({}) - kwarg.parent = self - yield kwarg + yield nodes.Dict(parent=self) return # if there is a default value, yield it. And then yield YES to reflect # we can't guess given argument value diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 68c8fdff89..3d6f82ec8c 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -32,6 +32,13 @@ # inspect that is for some reason not exported. from inspect import classify_class_attrs as _classify_class_attrs +# ChainMap was made available in Python 3, but a precursor lives in +# ConfigParser in 2.7. +try: + from collections import ChainMap as _ChainMap +except ImportError: + from ConfigParser import _Chainmap as _ChainMap + try: from functools import singledispatch as _singledispatch except ImportError: @@ -199,7 +206,7 @@ def ast_from_class(cls, built_objects, module, name=None, parent=None): if id(cls) in built_objects: # return built_objects[id(cls)] return nodes.Name(name=name or cls.__name__, parent=parent) - class_node = nodes.ClassDef(name=name or cls.__name__, doc=inspect.getdoc(cls)) + class_node = nodes.ClassDef(name=name or cls.__name__, doc=inspect.getdoc(cls), parent=parent) built_objects[id(cls)] = class_node try: bases = [nodes.Name(name=b.__name__, parent=class_node) diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index e612a6ca44..29f2481a75 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -121,9 +121,9 @@ def std_special_attributes(self, name, add_locals=True): else: locals = {} if name == '__name__': - return [node_classes.const_factory(self.name)] + locals.get(name, []) + return [node_classes.Const(self.name)] + locals.get(name, []) if name == '__doc__': - return [node_classes.const_factory(self.doc)] + locals.get(name, []) + return [node_classes.Const(self.doc)] + locals.get(name, []) if name == '__dict__': return [node_classes.Dict()] + locals.get(name, []) raise exceptions.NotFoundError(name) @@ -424,7 +424,7 @@ def display_type(self): def getattr(self, name, context=None, ignore_locals=False): if name in self.special_attributes: if name == '__file__': - return [node_classes.const_factory(self.file)] + self.locals.get(name, []) + return [node_classes.Const(self.source_file)] + self.locals.get(name, []) if name == '__path__' and self.package: return [node_classes.List()] + self.locals.get(name, []) return std_special_attributes(self, name) @@ -918,7 +918,7 @@ def getattr(self, name, context=None): done by an Instance proxy at inference time. """ if name == '__module__': - return [node_classes.const_factory(self.root().qname())] + return [node_classes.Const(self.root().qname())] if name in self.instance_attrs: return self.instance_attrs[name] return std_special_attributes(self, name, False) @@ -1439,7 +1439,7 @@ def getattr(self, name, context=None, class_context=True): values = self.locals.get(name, []) if name in self.special_attributes: if name == '__module__': - return [node_classes.const_factory(self.root().qname())] + values + return [node_classes.Const(self.root().qname())] + values # FIXME: do we really need the actual list of ancestors? # returning [Tuple()] + values don't break any test # this is ticket http://www.logilab.org/ticket/52785 diff --git a/astroid/tests/unittest_builder.py b/astroid/tests/unittest_builder.py index 2686fe2f87..27ba685bee 100644 --- a/astroid/tests/unittest_builder.py +++ b/astroid/tests/unittest_builder.py @@ -535,10 +535,10 @@ def func2(a={}): a.custom_attr = 0 ''' builder.parse(code) - nonetype = nodes.const_factory(None) + nonetype = nodes.Const(None) self.assertNotIn('custom_attr', nonetype.locals) self.assertNotIn('custom_attr', nonetype.instance_attrs) - nonetype = nodes.const_factory({}) + nonetype = nodes.Dict() self.assertNotIn('custom_attr', nonetype.locals) self.assertNotIn('custom_attr', nonetype.instance_attrs) @@ -591,7 +591,7 @@ def test_module_base_props(self): self.assertIsNone(module.parent) self.assertEqual(module.frame(), module) self.assertEqual(module.root(), module) - self.assertEqual(module.file, os.path.abspath(resources.find('data/module.py'))) + self.assertEqual(module.source_file, os.path.abspath(resources.find('data/module.py'))) self.assertEqual(module.pure_python, 1) self.assertEqual(module.package, 0) self.assertFalse(module.is_statement) diff --git a/astroid/tests/unittest_lookup.py b/astroid/tests/unittest_lookup.py index 805efd9e06..db6d7f3298 100644 --- a/astroid/tests/unittest_lookup.py +++ b/astroid/tests/unittest_lookup.py @@ -267,7 +267,7 @@ def test_builtin_lookup(self): self.assertEqual(len(intstmts), 1) self.assertIsInstance(intstmts[0], nodes.ClassDef) self.assertEqual(intstmts[0].name, 'int') - self.assertIs(intstmts[0], nodes.const_factory(1)._proxied) + self.assertIs(intstmts[0], nodes.Const(1)._proxied) def test_decorator_arguments_lookup(self): code = ''' diff --git a/astroid/tests/unittest_manager.py b/astroid/tests/unittest_manager.py index 1595aaf42c..feaf1be06c 100644 --- a/astroid/tests/unittest_manager.py +++ b/astroid/tests/unittest_manager.py @@ -101,8 +101,8 @@ def _test_ast_from_zip(self, archive): module = self.manager.ast_from_module_name('mypypa') self.assertEqual(module.name, 'mypypa') end = os.path.join(archive, 'mypypa') - self.assertTrue(module.file.endswith(end), - "%s doesn't endswith %s" % (module.file, end)) + self.assertTrue(module.source_file.endswith(end), + "%s doesn't endswith %s" % (module.source_file, end)) finally: # remove the module, else after importing egg, we don't get the zip if 'mypypa' in self.manager.astroid_cache: diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index ade8a92020..dbd6f5906d 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -32,6 +32,7 @@ from astroid import node_classes from astroid import nodes from astroid import parse +from astroid import raw_building from astroid import util from astroid import test_utils from astroid import transforms @@ -393,7 +394,7 @@ def test_absolute_import(self): next(astroid['message'].infer(ctx)) ctx.lookupname = 'email' m = next(astroid['email'].infer(ctx)) - self.assertFalse(m.file.startswith(os.path.join('data', 'email.py'))) + self.assertFalse(m.source_file.startswith(os.path.join('data', 'email.py'))) def test_more_absolute_import(self): astroid = resources.build_file('data/module1abs/__init__.py', 'data.module1abs') @@ -409,7 +410,7 @@ def test_as_string(self): class ConstNodeTest(unittest.TestCase): def _test(self, value): - node = nodes.const_factory(value) + node = raw_building.ast_from_object(value) self.assertIsInstance(node._proxied, nodes.ClassDef) self.assertEqual(node._proxied.name, value.__class__.__name__) self.assertIs(node.value, value) diff --git a/astroid/tests/unittest_transforms.py b/astroid/tests/unittest_transforms.py index 29a8b8dbd1..ae2691e6ec 100644 --- a/astroid/tests/unittest_transforms.py +++ b/astroid/tests/unittest_transforms.py @@ -72,7 +72,7 @@ def transform_compare(node): _, right = node.ops[0] # Assume they are Consts and they were transformed before # us. - return nodes.const_factory(node.left.value < right.value) + return nodes.Const(node.left.value < right.value) def transform_name(node): # Should be Consts @@ -97,7 +97,7 @@ def transform_function(node): name = nodes.AssignName() name.name = 'value' assign.targets = [name] - assign.value = nodes.const_factory(42) + assign.value = nodes.Const(42) node.body.append(assign) self.transformer.register_transform(nodes.FunctionDef, @@ -205,7 +205,7 @@ def transform_function(node): def test_builder_apply_transforms(self): def transform_function(node): - return nodes.const_factory(42) + return nodes.Const(42) manager = builder.MANAGER with add_transform(manager, nodes.FunctionDef, transform_function): From 98e4b7449e540999a4e05e3e76cac3c8f7168bd8 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Mon, 26 Oct 2015 02:02:06 -0400 Subject: [PATCH 021/312] Add scoping to ast_from_object using collections.ChainMap and its 2.7 equivalent in ConfigParser --- astroid/raw_building.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 3d6f82ec8c..293ef6aceb 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -45,11 +45,9 @@ from singledispatch import singledispatch as _singledispatch try: - from inspect import (signature as _signature, Parameter as - _Parameter, Signature as _Signature) + from inspect import signature as _signature, Parameter as _Parameter except ImportError: - from funcsigs import (signature as _signature, Parameter as - _Parameter, Signature as _Signature) + from funcsigs import signature as _signature, Parameter as _Parameter import six @@ -140,7 +138,7 @@ def object_build_function(parent, func, localname): def ast_from_object(object_, name=None): - built_objects = {} + built_objects = _ChainMap() module = inspect.getmodule(object_) return _ast_from_object(object_, built_objects, module, name) @@ -185,6 +183,7 @@ def ast_from_module(module, built_objects, parent_module, name=None, parent=None # implemented in pure Python. pure_python=bool(source_file)) built_objects[id(module)] = module_node + built_objects = _ChainMap({}, *built_objects.maps) MANAGER.cache_module(module_node) module_node.postinit(body=[_ast_from_object(m, built_objects, module, n, module_node) @@ -208,6 +207,7 @@ def ast_from_class(cls, built_objects, module, name=None, parent=None): return nodes.Name(name=name or cls.__name__, parent=parent) class_node = nodes.ClassDef(name=name or cls.__name__, doc=inspect.getdoc(cls), parent=parent) built_objects[id(cls)] = class_node + built_objects = _ChainMap({}, *built_objects.maps) try: bases = [nodes.Name(name=b.__name__, parent=class_node) for b in inspect.getmro(cls)[1:]] @@ -251,6 +251,8 @@ def ast_from_function(func, built_objects, module, name=None, parent=None): func_node = nodes.FunctionDef(name=name or func.__name__, doc=inspect.getdoc(func), parent=parent) + built_objects[id(func)] = func_node + built_objects = _ChainMap({}, *built_objects.maps) try: signature = _signature(func) except (ValueError, TypeError): @@ -320,7 +322,6 @@ def extract_vararg(parameter): annotations, kwonly_annotations, varargannotation, kwargannotation) func_node.postinit(args=args_node, body=[]) - built_objects[id(func)] = func_node return func_node From cb5cb364961e952771653bda8d77090fa55d88bf Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Mon, 26 Oct 2015 23:14:39 -0400 Subject: [PATCH 022/312] Fix simple test failures --- astroid/helpers.py | 5 +-- astroid/mixins.py | 6 +-- astroid/node_classes.py | 12 +++++- astroid/nodes.py | 4 +- astroid/protocols.py | 6 +-- astroid/raw_building.py | 29 +++++++++++---- astroid/scoped_nodes.py | 12 +++--- astroid/tests/resources.py | 2 +- astroid/tests/unittest_builder.py | 12 +++--- astroid/tests/unittest_nodes.py | 2 +- astroid/tests/unittest_raw_building.py | 51 -------------------------- astroid/tests/unittest_regrtest.py | 5 ++- tox.ini | 49 +++++++++++++------------ 13 files changed, 84 insertions(+), 111 deletions(-) diff --git a/astroid/helpers.py b/astroid/helpers.py index feb0985e9c..a62a5667a3 100644 --- a/astroid/helpers.py +++ b/astroid/helpers.py @@ -36,9 +36,8 @@ def _build_proxy_class(cls_name, builtins): - proxy = raw_building.build_class(cls_name) - proxy.parent = builtins - return proxy + # TODO: fix with the node constructors + return nodes.ClassDef(name=cls_name, parent=builtins) def _function_type(function, builtins): diff --git a/astroid/mixins.py b/astroid/mixins.py index dcfade180f..addcc6629e 100644 --- a/astroid/mixins.py +++ b/astroid/mixins.py @@ -60,7 +60,7 @@ def assign_type(self): return self def ass_type(self): - rename_warning((type(self).__name__, type(self).__name__)) + util.rename_warning((type(self).__name__, type(self).__name__)) return self.assign_type() @@ -70,7 +70,7 @@ def assign_type(self): return self def ass_type(self): - rename_warning((type(self).__name__, type(self).__name__)) + util.rename_warning((type(self).__name__, type(self).__name__)) return self.assign_type() def _get_filtered_stmts(self, lookup_node, node, _stmts, mystmt): @@ -90,7 +90,7 @@ def assign_type(self): return self.parent.assign_type() def ass_type(self): - rename_warning((type(self).__name__, type(self).__name__)) + util.rename_warning((type(self).__name__, type(self).__name__)) return self.assign_type() diff --git a/astroid/node_classes.py b/astroid/node_classes.py index 7636de8153..30d5f1dc26 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -615,8 +615,7 @@ def from_constants(cls, elts=None): if elts is None: node.elts = [] else: - node.elts = [raw_building.ast_from_scalar(e, {}, None, parent=node) - for e in elts] + node.elts = [raw_building.ast_from_builtin_number_text_binary(e, {}, None, parent=node) for e in elts] return node def itered(self): @@ -1220,6 +1219,15 @@ def _proxied(self): return builtins.getattr(type(self.value).__name__)[0] +class Singleton(Const): + """Represents a singleton object, at the moment None and NotImplemented.""" + + @decorators.cachedproperty + def _proxied(self): + builtins = MANAGER.astroid_cache[BUILTINS] + return builtins.getattr(str(self.value))[0] + + class Continue(Statement): """class representing a Continue node""" diff --git a/astroid/nodes.py b/astroid/nodes.py index a9f187d9e4..3573ca0753 100644 --- a/astroid/nodes.py +++ b/astroid/nodes.py @@ -48,7 +48,7 @@ # Backwards-compatibility aliases Backquote, Discard, AssName, AssAttr, Getattr, CallFunc, From, # Node not present in the builtin ast module. - DictUnpack, EmptyNode, Unknown + DictUnpack, EmptyNode, Singleton, Unknown ) from astroid.scoped_nodes import ( Module, GeneratorExp, Lambda, DictComp, @@ -78,7 +78,7 @@ Module, Pass, Print, Raise, Return, - Set, SetComp, Slice, Starred, Subscript, + Set, SetComp, Singleton, Slice, Starred, Subscript, TryExcept, TryFinally, Tuple, UnaryOp, Unknown, While, With, diff --git a/astroid/protocols.py b/astroid/protocols.py index 6fb6fac450..09b5cd3908 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -119,7 +119,7 @@ def _infer_unary_op(obj, op): @decorators.yes_if_nothing_inferred def const_infer_binary_op(self, operator, other, context, _): - not_implemented = nodes.Const(NotImplemented) + not_implemented = nodes.Singleton(NotImplemented) if isinstance(other, nodes.Const): try: impl = BIN_OP_IMPL[operator] @@ -155,7 +155,7 @@ def _multiply_seq_by_int(self, other, context): @decorators.yes_if_nothing_inferred def tl_infer_binary_op(self, operator, other, context, method): - not_implemented = nodes.Const(NotImplemented) + not_implemented = nodes.Singleton(NotImplemented) if isinstance(other, self.__class__) and operator == '+': node = self.__class__() elts = [n for elt in self.elts for n in elt.infer(context) @@ -403,7 +403,7 @@ def _infer_context_manager(self, mgr, context): if yield_point: if not yield_point.value: # TODO(cpopa): an empty yield. Should be wrapped to Const. - const = nodes.Const(None) + const = nodes.Singleton(None) const.parent = yield_point const.lineno = yield_point.lineno yield const diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 293ef6aceb..c98dd52639 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -401,23 +401,36 @@ def ast_from_dict(dictionary, built_objects, module, name=None, @_ast_from_object.register(int) @_ast_from_object.register(float) @_ast_from_object.register(complex) -@_ast_from_object.register(type(None)) -@_ast_from_object.register(type(NotImplemented)) -def ast_from_scalar(scalar, built_objects, module, name=None, parent=None): +def ast_from_builtin_number_text_binary(builtin_number_text_binary, built_objects, module, name=None, parent=None): if name: parent = nodes.Assign(parent=parent) name_node = nodes.AssignName(name, parent=parent) - scalar_node = nodes.Const(value=scalar, parent=parent) + builtin_number_text_binary_node = nodes.Const(value=builtin_number_text_binary, parent=parent) if name: - parent.postinit(targets=[name_node], value=scalar_node) + parent.postinit(targets=[name_node], value=builtin_number_text_binary_node) node = parent else: - node = scalar_node + node = builtin_number_text_binary_node return node if six.PY2: - _ast_from_object.register(unicode, ast_from_scalar) - _ast_from_object.register(long, ast_from_scalar) + _ast_from_object.register(unicode, ast_from_builtin_number_text_binary) + _ast_from_object.register(long, ast_from_builtin_number_text_binary) + + +@_ast_from_object.register(type(None)) +@_ast_from_object.register(type(NotImplemented)) +def ast_from_builtin_singleton(builtin_singleton, built_objects, module, name=None, parent=None): + if name: + parent = nodes.Assign(parent=parent) + name_node = nodes.AssignName(name, parent=parent) + builtin_singleton_node = nodes.Singleton(value=builtin_singleton, parent=parent) + if name: + parent.postinit(targets=[name_node], value=builtin_singleton_node) + node = parent + else: + node = builtin_singleton_node + return node @_ast_from_object.register(type(Ellipsis)) diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index e44c2ab1f3..e8037d8729 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -368,16 +368,16 @@ def future_imports(self): return frozenset(get_locals(self)['__future__']) def _get_stream(self): - if self.file_bytes is not None: - return io.BytesIO(self.file_bytes) - if self.file is not None: - stream = open(self.file, 'rb') + if self.source_code is not None: + return io.BytesIO(self.source_code) + if self.source_file is not None: + stream = open(self.source_file, 'rb') return stream return None @property def file_stream(self): - util.attr_to_method_warning((file_stream, type(self).__name__)) + util.attr_to_method_warning(('file_stream', type(self).__name__)) return self._get_stream() def stream(self): @@ -1013,7 +1013,7 @@ def infer_call_result(self, caller, context=None): returns = self.nodes_of_class(node_classes.Return, skip_klass=FunctionDef) for returnnode in returns: if returnnode.value is None: - yield node_classes.Const(None) + yield node_classes.Singleton(None) else: try: for inferred in returnnode.value.infer(context): diff --git a/astroid/tests/resources.py b/astroid/tests/resources.py index 7988d053dd..7e704cf2ac 100644 --- a/astroid/tests/resources.py +++ b/astroid/tests/resources.py @@ -22,7 +22,7 @@ from astroid import builder from astroid import MANAGER -from astroid.bases import BUILTINS +from astroid.bases import BUILTINS DATA_DIR = 'testdata/python{}/'.format(sys.version_info[0]) diff --git a/astroid/tests/unittest_builder.py b/astroid/tests/unittest_builder.py index 27ba685bee..87fadab32b 100644 --- a/astroid/tests/unittest_builder.py +++ b/astroid/tests/unittest_builder.py @@ -296,9 +296,9 @@ def test_inspect_build0(self): # check property has __init__ pclass = builtin_ast['property'] self.assertIn('__init__', pclass) - self.assertIsInstance(builtin_ast['None'], nodes.Const) - self.assertIsInstance(builtin_ast['True'], nodes.Const) - self.assertIsInstance(builtin_ast['False'], nodes.Const) + self.assertIsInstance(builtin_ast['None'], nodes.AssignName) + self.assertIsInstance(builtin_ast['True'], nodes.AssignName) + self.assertIsInstance(builtin_ast['False'], nodes.AssignName) if six.PY3: self.assertIsInstance(builtin_ast['Exception'], nodes.ClassDef) self.assertIsInstance(builtin_ast['NotImplementedError'], nodes.ClassDef) @@ -514,8 +514,8 @@ def A_assign_type(self): lclass = list(astroid.igetattr('A')) self.assertEqual(len(lclass), 1) lclass = lclass[0] - self.assertIn('assign_type', lclass.locals) - self.assertIn('type', lclass.locals) + self.assertIn('assign_type', lclass.external_attrs) + self.assertIn('type', lclass.external_attrs) def test_augassign_attr(self): builder.parse(""" @@ -535,7 +535,7 @@ def func2(a={}): a.custom_attr = 0 ''' builder.parse(code) - nonetype = nodes.Const(None) + nonetype = nodes.Singleton(None) self.assertNotIn('custom_attr', nonetype.locals) self.assertNotIn('custom_attr', nonetype.instance_attrs) nonetype = nodes.Dict() diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index dbd6f5906d..31002295bb 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -411,7 +411,7 @@ class ConstNodeTest(unittest.TestCase): def _test(self, value): node = raw_building.ast_from_object(value) - self.assertIsInstance(node._proxied, nodes.ClassDef) + self.assertIsInstance(node._proxied, (nodes.ClassDef, nodes.AssignName)) self.assertEqual(node._proxied.name, value.__class__.__name__) self.assertIs(node.value, value) self.assertTrue(node._proxied.parent) diff --git a/astroid/tests/unittest_raw_building.py b/astroid/tests/unittest_raw_building.py index 3d27cd6fdc..fb5690e28e 100644 --- a/astroid/tests/unittest_raw_building.py +++ b/astroid/tests/unittest_raw_building.py @@ -17,57 +17,6 @@ class RawBuildingTC(unittest.TestCase): - - # def test_attach_dummy_node(self): - # node = build_module('MyModule') - # attach_dummy_node(node, 'DummyNode') - # self.assertEqual(1, len(list(node.get_children()))) - - # def test_build_module(self): - # node = build_module('MyModule') - # self.assertEqual(node.name, 'MyModule') - # self.assertEqual(node.pure_python, False) - # self.assertEqual(node.package, False) - # self.assertEqual(node.parent, None) - - # def test_build_class(self): - # node = build_class('MyClass') - # self.assertEqual(node.name, 'MyClass') - # self.assertEqual(node.doc, None) - - # def test_build_function(self): - # node = build_function('MyFunction') - # self.assertEqual(node.name, 'MyFunction') - # self.assertEqual(node.doc, None) - - # def test_build_function_args(self): - # args = [Parameter('myArgs1', None, None, None), - # Parameter('myArgs2', None, None, None)] - # node = build_function('MyFunction', args) - # self.assertEqual('myArgs1', node.args.args[0].name) - # self.assertEqual('myArgs2', node.args.args[1].name) - # self.assertEqual(2, len(node.args.args)) - - # def test_build_function_defaults(self): - # args = [Parameter('myArgs1', 'defaults1', None, None), - # Parameter('myArgs2', 'defaults2', None, None)] - # node = build_function('MyFunction', args) - # self.assertEqual(2, len(node.args.defaults)) - - @unittest.skipIf(sys.version_info[0] > 2, - 'Tuples are not allowed in function args in Python 3') - @unittest.expectedFailure - def test_build_function_with_tuple_args(self): - # TODO - def f(a, (b, (c, d))): - pass - raw_building.ast_from_object(f) - - # def test_build_from_import(self): - # names = ['exceptions, inference, inspector'] - # node = build_from_import('astroid', names) - # self.assertEqual(len(names), len(node.names)) - @test_utils.require_version(minver='3.0') def test_io_is__io(self): # _io module calls itself io. This leads diff --git a/astroid/tests/unittest_regrtest.py b/astroid/tests/unittest_regrtest.py index 3e22152971..0a363647b8 100644 --- a/astroid/tests/unittest_regrtest.py +++ b/astroid/tests/unittest_regrtest.py @@ -25,8 +25,8 @@ from astroid.bases import BUILTINS from astroid.builder import AstroidBuilder from astroid import exceptions -from astroid.raw_building import build_module from astroid.manager import AstroidManager +from astroid import raw_building from astroid.test_utils import require_version, extract_node from astroid.tests import resources from astroid import transforms @@ -90,7 +90,8 @@ def test_living_property(self): builder = AstroidBuilder() builder._done = {} builder._module = sys.modules[__name__] - builder.object_build(build_module('module_name', ''), Whatever) + module = nodes.Module(name='module_name', doc='') + module.postinit(body=[raw_building.ast_from_object(Whatever)]) def test_new_style_class_detection(self): diff --git a/tox.ini b/tox.ini index dbf893c4c6..fb589a1512 100644 --- a/tox.ini +++ b/tox.ini @@ -1,26 +1,37 @@ [tox] -# official list is -# envlist = py27, py33, py34, pypy, jython -# envlist = py27, py33, pylint -# envlist = py27, py34 +#envlist = py27, py33, py34, py35, pypy, jython, pylint +#envlist = py27, py33, pylint envlist = py34 +skip_missing_interpreters = true -[testenv:pylint] +[testenv] deps = + py27,pypy,jython: funcsigs lazy-object-proxy - singledispatch + py27,py33,pypy,jython: singledispatch six wrapt - hg+https://bitbucket.org/logilab/pylint -commands = pylint -rn --rcfile={toxinidir}/pylintrc {envsitepackagesdir}/astroid + pylint: hg+https://bitbucket.org/logilab/pylint -[testenv:py27] -deps = - funcsigs - lazy-object-proxy - singledispatch - six - wrapt +commands = python -m unittest discover -s {envsitepackagesdir}/astroid/tests -p "unittest*.py" + +# [testenv:pylint] +# deps = +# lazy-object-proxy +# singledispatch +# six +# wrapt +# hg+https://bitbucket.org/logilab/pylint +# commands = pylint -rn --rcfile={toxinidir}/pylintrc {envsitepackagesdir}/astroid + +# [testenv:py27] +# deps = +# funcsigs +# lazy-object-proxy +# singledispatch +# six +# wrapt +# commands = python -m unittest discover -s {envsitepackagesdir}/astroid/tests -p "unittest*.py" # [testenv:py33] # deps = @@ -32,11 +43,3 @@ deps = # This is commented out because tox will try to load the interpreter # for any defined environment even if it's not in envlist, which will # then fail on drone.io. - -[testenv] -deps = - lazy-object-proxy - six - wrapt - -commands = python -m unittest discover -s {envsitepackagesdir}/astroid/tests -p "unittest*.py" From 51e5ebbb0f3180d3082dd55959b7eecdbd4fb495 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Tue, 27 Oct 2015 18:30:24 -0400 Subject: [PATCH 023/312] Refactor namedtuple, enum, nose, and multiprocessing handling in brain to generate tests --- astroid/as_string.py | 1 + astroid/brain/_nose.py | 12 ++- astroid/brain/_stdlib.py | 164 ++++++++++++++---------------- astroid/node_classes.py | 2 +- astroid/raw_building.py | 4 +- astroid/scoped_nodes.py | 4 +- astroid/tests/unittest_helpers.py | 6 +- tox.ini | 30 ------ 8 files changed, 90 insertions(+), 133 deletions(-) diff --git a/astroid/as_string.py b/astroid/as_string.py index 7d671413cf..79674bac9e 100644 --- a/astroid/as_string.py +++ b/astroid/as_string.py @@ -135,6 +135,7 @@ def visit_comprehension(self, node): def visit_const(self, node): """return an astroid.Const node as string""" return repr(node.value) + visit_singleton = visit_const def visit_continue(self, node): """return an astroid.Continue node as string""" diff --git a/astroid/brain/_nose.py b/astroid/brain/_nose.py index 9a37f4e14e..463d9eefd5 100644 --- a/astroid/brain/_nose.py +++ b/astroid/brain/_nose.py @@ -53,9 +53,11 @@ class Test(unittest.TestCase): yield 'assert_equals', astroid.BoundMethod(method, case) -def _nose_tools_transform(node): +def _nose_tools_transform(module_node): for method_name, method in _nose_tools_functions(): - node.locals[method_name] = [method] + module_node.body.append(astroid.EmptyNode(object_=method, + name=method_name, + parent=module_node)) def _nose_tools_trivial_transform(): @@ -65,13 +67,13 @@ def _nose_tools_trivial_transform(): for pep8_name, method in _nose_tools_functions(): all_entries.append(pep8_name) - stub[pep8_name] = method + stub.body.append(astroid.EmptyNode(object_=method, + name=pep8_name, parent=stub)) # Update the __all__ variable, since nose.tools # does this manually with .append. all_assign = stub['__all__'].parent - all_object = astroid.List(all_entries) - all_object.parent = all_assign + all_object = astroid.List(all_entries, parent=all_assign) all_assign.value = all_object return stub diff --git a/astroid/brain/_stdlib.py b/astroid/brain/_stdlib.py index 1ccff15715..4e1df269e5 100644 --- a/astroid/brain/_stdlib.py +++ b/astroid/brain/_stdlib.py @@ -1,9 +1,10 @@ """Astroid hooks for the Python standard library.""" +# Namedtuple template strings from collections import _class_template, _repr_template, _field_template import functools import sys -from textwrap import dedent +import textwrap from astroid import ( MANAGER, UseInferenceDefault, inference_tip, BoundMethod, @@ -25,68 +26,7 @@ def infer_first(node, context): else: return value except StopIteration: - raise InferenceError() - -def infer_func_form(node, base_type, context=None, enum=False): - """Specific inference function for namedtuple or Python 3 enum. - - :param node: A Call node. - """ - - # node is a Call node, class name as first argument and generated class - # attributes as second argument - if len(node.args) != 2: - # something weird here, go back to class implementation - raise UseInferenceDefault() - # namedtuple or enums list of attributes can be a list of strings or a - # whitespace-separate string - try: - name = infer_first(node.args[0], context).value - names = infer_first(node.args[1], context) - try: - attributes = names.value.replace(',', ' ').split() - except AttributeError: - if not enum: - attributes = [infer_first(const, context).value for const in names.elts] - else: - # Enums supports either iterator of (name, value) pairs - # or mappings. - # TODO: support only list, tuples and mappings. - if hasattr(names, 'items') and isinstance(names.items, list): - attributes = [infer_first(const[0], context).value - for const in names.items - if isinstance(const[0], nodes.Const)] - elif hasattr(names, 'elts'): - # Enums can support either ["a", "b", "c"] - # or [("a", 1), ("b", 2), ...], but they can't - # be mixed. - if all(isinstance(const, nodes.Tuple) - for const in names.elts): - attributes = [infer_first(const.elts[0], context).value - for const in names.elts - if isinstance(const, nodes.Tuple)] - else: - attributes = [infer_first(const, context).value - for const in names.elts] - else: - raise AttributeError - if not attributes: - raise AttributeError - except (AttributeError, exceptions.InferenceError): - raise UseInferenceDefault() - # we want to return a Class node instance with proper attributes set - class_node = nodes.ClassDef(name, 'docstring') - class_node.parent = node.parent - # set base class=tuple - class_node.bases.append(base_type) - # XXX add __init__(*attributes) method - # print(class_node.repr_tree()) - for attr in attributes: - fake_node = nodes.EmptyNode() - fake_node.parent = class_node - fake_node.attrname = attr - class_node.instance_attrs[attr] = [fake_node] - return class_node, name, attributes + util.reraise(InferenceError()) # module specific transformation functions ##################################### @@ -221,7 +161,7 @@ def __exit__(self, *args): pass ''' else: ctx_manager = '' - code = dedent(''' + code = textwrap.dedent(''' class Popen(object): returncode = pid = 0 @@ -250,7 +190,7 @@ def kill(self): return AstroidBuilder(MANAGER).string_build(code) -# namedtuple support ########################################################### +# namedtuple and Enum support def _looks_like(node, name): func = node.func @@ -264,28 +204,33 @@ def _looks_like(node, name): _looks_like_enum = functools.partial(_looks_like, name='Enum') -def infer_named_tuple(node, context=None): - """Specific inference function for namedtuple Call node""" - +def infer_namedtuple_enum_fields(call_node, context): # node is a Call node, class name as first argument and generated class # attributes as second argument - if len(node.args) != 2: + if len(call_node.args) != 2: # something weird here, go back to class implementation raise UseInferenceDefault() # namedtuple or enums list of attributes can be a list of strings or a # whitespace-separate string try: - type_name = infer_first(node.args[0], context).value - fields = infer_first(node.args[1], context) - # TODO: this doesn't handle the case where duplicate field - # names are replaced using namedtuple's rename keyword. - try: - field_names = tuple(fields.value.replace(',', ' ').split()) - except AttributeError: - field_names = tuple(infer_first(const, context).value for const in fields.elts) + type_name = infer_first(call_node.args[0], context).value + fields = infer_first(call_node.args[1], context) + return type_name, fields except (AttributeError, exceptions.InferenceError): raise UseInferenceDefault() + +def infer_namedtuple(namedtuple_call, context=None): + """Specific inference function for namedtuple Call node""" + + type_name, fields = infer_namedtuple_enum_fields(namedtuple_call, context) + # TODO: this doesn't handle the case where duplicate field + # names are replaced using namedtuple's rename keyword. + try: + field_names = tuple(fields.value.replace(',', ' ').split()) + except AttributeError: + field_names = tuple(infer_first(const, context).value for const in fields.elts) + class_definition = _class_template.format( typename = type_name, field_names = field_names, @@ -302,12 +247,52 @@ def infer_named_tuple(node, context=None): return iter([namedtuple_node]) -def infer_enum(node, context=None): +def infer_enum(enum_call, context=None): """ Specific inference function for enum Call node. """ - enum_meta = nodes.ClassDef("EnumMeta", 'docstring') - class_node = infer_func_form(node, enum_meta, - context=context, enum=True)[0] - return iter([class_node]) + type_name, fields = infer_namedtuple_enum_fields(enum_call, context) + try: + attributes = tuple(fields.value.replace(',', ' ').split()) + except AttributeError as exception: + # Enums supports either iterator of (name, value) pairs + # or mappings. + # TODO: support only list, tuples and mappings. + if hasattr(fields, 'items') and isinstance(fields.items, list): + attributes = [infer_first(const[0], context).value + for const in fields.items + if isinstance(const[0], nodes.Const)] + elif hasattr(fields, 'elts'): + # Enums can support either ["a", "b", "c"] + # or [("a", 1), ("b", 2), ...], but they can't + # be mixed. + if all(isinstance(const, nodes.Tuple) + for const in fields.elts): + attributes = [infer_first(const.elts[0], context).value + for const in fields.elts + if isinstance(const, nodes.Tuple)] + else: + attributes = [infer_first(const, context).value + for const in fields.elts] + else: + util.reraise(exception) + if not attributes: + util.reraise(exception) + + template = textwrap.dedent(''' + """Mock module to hold enum classes""" + class EnumMeta: + """Mock Enum metaclass""" + + class {name}(EnumMeta): + """Mock Enum class""" + def __init__(self, {attributes}): + """Fake __init__ for enums""" + ''') + code = template.format(name=type_name, attributes=', '.join(attributes)) + assignment_lines = [(' '*8 + 'self.%(a)s = %(a)s' % {'a': a}) + for a in attributes] + code += '\n'.join(assignment_lines) + module = AstroidBuilder(MANAGER).string_build(code) + return iter([module.body[0]]) def infer_enum_class(node): @@ -335,7 +320,7 @@ def infer_enum_class(node): new_targets = [] for target in targets: # Replace all the assignments with our mocked class. - classdef = dedent(''' + classdef = textwrap.dedent(''' class %(name)s(%(types)s): @property def value(self): @@ -356,7 +341,7 @@ def name(self): return node def multiprocessing_transform(): - module = AstroidBuilder(MANAGER).string_build(dedent(''' + module = AstroidBuilder(MANAGER).string_build(textwrap.dedent(''' from multiprocessing.managers import SyncManager def Manager(): return SyncManager() @@ -367,7 +352,7 @@ def Manager(): # On Python 3.4, multiprocessing uses a getattr lookup inside contexts, # in order to get the attributes they need. Since it's extremely # dynamic, we use this approach to fake it. - node = AstroidBuilder(MANAGER).string_build(dedent(''' + node = AstroidBuilder(MANAGER).string_build(textwrap.dedent(''' from multiprocessing.context import DefaultContext, BaseContext default = DefaultContext() base = BaseContext() @@ -385,14 +370,15 @@ def Manager(): value = value[0] if isinstance(value, nodes.FunctionDef): - # We need to rebound this, since otherwise + # We need to rebind this, since otherwise # it will have an extra argument (self). value = BoundMethod(value, node) - module[key] = value + module.body.append(nodes.EmptyNode(object_=value, name=key, + parent=module)) return module def multiprocessing_managers_transform(): - return AstroidBuilder(MANAGER).string_build(dedent(''' + return AstroidBuilder(MANAGER).string_build(textwrap.dedent(''' import array import threading import multiprocessing.pool as pool @@ -435,7 +421,7 @@ class SyncManager(object): ''')) -MANAGER.register_transform(nodes.Call, inference_tip(infer_named_tuple), +MANAGER.register_transform(nodes.Call, inference_tip(infer_namedtuple), _looks_like_namedtuple) MANAGER.register_transform(nodes.Call, inference_tip(infer_enum), _looks_like_enum) diff --git a/astroid/node_classes.py b/astroid/node_classes.py index 30d5f1dc26..98886b40de 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -1362,7 +1362,7 @@ class EmptyNode(NodeNG): ''' _other_fields = ('name', 'object') - def __init__(self, object_, name=None, lineno=None, col_offset=None, parent=None): + def __init__(self, object_=None, name=None, lineno=None, col_offset=None, parent=None): if object_ is not None: self.object = object_ self.name = name diff --git a/astroid/raw_building.py b/astroid/raw_building.py index c98dd52639..962e14205e 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -303,8 +303,8 @@ def extract_vararg(parameter): # function. names, defaults, annotations = extract_args(parameters.get(_Parameter.POSITIONAL_OR_KEYWORD, ()), args_node) kwonlynames, kw_defaults, kwonly_annotations = extract_args(parameters.get(_Parameter.KEYWORD_ONLY, ()), args_node) - args = [nodes.Name(name=n, parent=args_node) for n in names] - kwonlyargs = [nodes.Name(name=n, parent=args_node) for n in kwonlynames] + args = [nodes.AssignName(name=n, parent=args_node) for n in names] + kwonlyargs = [nodes.AssignName(name=n, parent=args_node) for n in kwonlynames] if vararg_name and vararg[0].annotation is not _Parameter.empty: varargannotation = vararg.annotation else: diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index e8037d8729..ae4729f0e2 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -296,10 +296,10 @@ class Module(LocalsDictNodeNG): scope_attrs = set(('__name__', '__doc__', '__file__', '__path__')) if six.PY2: - _other_fields = ('name', 'doc', 'file_encoding', 'path', 'package', + _other_fields = ('name', 'doc', 'file_encoding', 'package', 'pure_python', 'source_code', 'source_file') else: - _other_fields = ('name', 'doc', 'path', 'package', 'pure_python', + _other_fields = ('name', 'doc', 'package', 'pure_python', 'source_code', 'source_file') # _other_other_fields = ('locals', 'globals') diff --git a/astroid/tests/unittest_helpers.py b/astroid/tests/unittest_helpers.py index 62259667b0..55e8c2430a 100644 --- a/astroid/tests/unittest_helpers.py +++ b/astroid/tests/unittest_helpers.py @@ -24,7 +24,7 @@ from astroid import builder from astroid import helpers from astroid import manager -from astroid import raw_building +from astroid import nodes from astroid import test_utils from astroid import util @@ -41,9 +41,7 @@ def _extract(self, obj_name): return self.builtins.getattr(obj_name)[0] def _build_custom_builtin(self, obj_name): - proxy = raw_building.build_class(obj_name) - proxy.parent = self.builtins - return proxy + return nodes.ClassDef(name=obj_name, parent=self.builtins) def assert_classes_equal(self, cls, other): self.assertEqual(cls.name, other.name) diff --git a/tox.ini b/tox.ini index fb589a1512..f6ba87426c 100644 --- a/tox.ini +++ b/tox.ini @@ -12,34 +12,4 @@ deps = six wrapt pylint: hg+https://bitbucket.org/logilab/pylint - commands = python -m unittest discover -s {envsitepackagesdir}/astroid/tests -p "unittest*.py" - -# [testenv:pylint] -# deps = -# lazy-object-proxy -# singledispatch -# six -# wrapt -# hg+https://bitbucket.org/logilab/pylint -# commands = pylint -rn --rcfile={toxinidir}/pylintrc {envsitepackagesdir}/astroid - -# [testenv:py27] -# deps = -# funcsigs -# lazy-object-proxy -# singledispatch -# six -# wrapt -# commands = python -m unittest discover -s {envsitepackagesdir}/astroid/tests -p "unittest*.py" - -# [testenv:py33] -# deps = -# lazy-object-proxy -# singledispatch -# six -# wrapt - -# This is commented out because tox will try to load the interpreter -# for any defined environment even if it's not in envlist, which will -# then fail on drone.io. From 54cbb955ea5748a479e6a6323d65f921c5bf4d32 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Tue, 27 Oct 2015 23:59:24 -0400 Subject: [PATCH 024/312] Fix more minor test failures --- astroid/brain/_stdlib.py | 4 +- astroid/builder.py | 2 +- astroid/raw_building.py | 110 ++++++++++++------------- astroid/scoped_nodes.py | 31 +++++-- astroid/tests/unittest_brain.py | 4 +- astroid/tests/unittest_builder.py | 4 +- astroid/tests/unittest_scoped_nodes.py | 6 +- 7 files changed, 87 insertions(+), 74 deletions(-) diff --git a/astroid/brain/_stdlib.py b/astroid/brain/_stdlib.py index 4e1df269e5..fd52860a9a 100644 --- a/astroid/brain/_stdlib.py +++ b/astroid/brain/_stdlib.py @@ -279,7 +279,7 @@ def infer_enum(enum_call, context=None): template = textwrap.dedent(''' """Mock module to hold enum classes""" - class EnumMeta: + class EnumMeta(object): """Mock Enum metaclass""" class {name}(EnumMeta): @@ -292,7 +292,7 @@ def __init__(self, {attributes}): for a in attributes] code += '\n'.join(assignment_lines) module = AstroidBuilder(MANAGER).string_build(code) - return iter([module.body[0]]) + return iter([module.body[1]]) def infer_enum_class(node): diff --git a/astroid/builder.py b/astroid/builder.py index f5daee0d41..b753d74d0b 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -146,7 +146,7 @@ def file_build(self, path, modname=None): def string_build(self, data, modname='', path=None): """Build astroid from source code string.""" module = self._data_build(data, modname, path) - module.file_bytes = data.encode('utf-8') + module.source_code = data.encode('utf-8') return self._post_build(module, 'utf-8') def _post_build(self, module, encoding): diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 962e14205e..65681c6c1f 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -80,61 +80,61 @@ MANAGER = manager.AstroidManager() -def _io_discrepancy(member): - # _io module names itself `io`: http://bugs.python.org/issue18602 - member_self = getattr(member, '__self__', None) - return (member_self and - inspect.ismodule(member_self) and - member_self.__name__ == '_io' and - member.__module__ == 'io') - -def _add_dunder_class(func, member): - """Add a __class__ member to the given func node, if we can determine it.""" - python_cls = member.__class__ - cls_name = getattr(python_cls, '__name__', None) - if not cls_name: - return - bases = [ancestor.__name__ for ancestor in python_cls.__bases__] - ast_klass = build_class(cls_name, bases, python_cls.__doc__) - func.instance_attrs['__class__'] = [ast_klass] - -Parameter = collections.namedtuple('Parameter', 'name default annotation kind') -DEFAULT_PARAMETER = Parameter(None, None, None, None) - -def build_function(name, args=(), defaults=(), annotations=(), - kwonlyargs=(), kwonly_defaults=(), - kwonly_annotations=(), vararg=None, - varargannotation=None, kwarg=None, kwargannotation=None, - returns=None, doc=None, parent=None): - """create and initialize an astroid FunctionDef node""" - func = nodes.FunctionDef(name=name, doc=doc, parent=parent) - args_node = nodes.Arguments(vararg=vararg, kwarg=kwarg, parent=func) - args = [nodes.Name(name=a.name, parent=args_node) for n in args] - kwonlyargs = [nodes.Name(name=a.name, parent=args_node) for a in kw_only] - args_node.postinit(args, defaults, kwonlyargs, kw_defaults, - annotations, kwonly_annotations, - varargannotation, kwargannotation) - func.postinit(args=args_node, body=[], returns=returns) - return func - -def object_build_function(parent, func, localname): - """create astroid for a living function object""" - signature = _signature(func) - parameters = {k: tuple(g) for k, g in - itertools.groupby(signature.parameters.values(), - operator.attrgetter('kind'))} - # This ignores POSITIONAL_ONLY args, because they only appear in - # functions implemented in C and can't be mimicked by any Python - # function. - node = build_function(getattr(func, '__name__', None) or localname, - parameters.get(_Parameter.POSITIONAL_OR_KEYWORD, ()), - parameters.get(_Parameter.KEYWORD_ONLY, ()), - parameters.get(_Parameter.VAR_POSITIONAL, None), - parameters.get(_Parameter.VAR_KEYWORD, None), - signature.return_annotation, - func.__doc__, - parent) - return node +# def _io_discrepancy(member): +# # _io module names itself `io`: http://bugs.python.org/issue18602 +# member_self = getattr(member, '__self__', None) +# return (member_self and +# inspect.ismodule(member_self) and +# member_self.__name__ == '_io' and +# member.__module__ == 'io') + +# def _add_dunder_class(func, member): +# """Add a __class__ member to the given func node, if we can determine it.""" +# python_cls = member.__class__ +# cls_name = getattr(python_cls, '__name__', None) +# if not cls_name: +# return +# bases = [ancestor.__name__ for ancestor in python_cls.__bases__] +# ast_klass = build_class(cls_name, bases, python_cls.__doc__) +# func.instance_attrs['__class__'] = [ast_klass] + +# Parameter = collections.namedtuple('Parameter', 'name default annotation kind') +# DEFAULT_PARAMETER = Parameter(None, None, None, None) + +# def build_function(name, args=(), defaults=(), annotations=(), +# kwonlyargs=(), kwonly_defaults=(), +# kwonly_annotations=(), vararg=None, +# varargannotation=None, kwarg=None, kwargannotation=None, +# returns=None, doc=None, parent=None): +# """create and initialize an astroid FunctionDef node""" +# func = nodes.FunctionDef(name=name, doc=doc, parent=parent) +# args_node = nodes.Arguments(vararg=vararg, kwarg=kwarg, parent=func) +# args = [nodes.Name(name=a.name, parent=args_node) for n in args] +# kwonlyargs = [nodes.Name(name=a.name, parent=args_node) for a in kw_only] +# args_node.postinit(args, defaults, kwonlyargs, kw_defaults, +# annotations, kwonly_annotations, +# varargannotation, kwargannotation) +# func.postinit(args=args_node, body=[], returns=returns) +# return func + +# def object_build_function(parent, func, localname): +# """create astroid for a living function object""" +# signature = _signature(func) +# parameters = {k: tuple(g) for k, g in +# itertools.groupby(signature.parameters.values(), +# operator.attrgetter('kind'))} +# # This ignores POSITIONAL_ONLY args, because they only appear in +# # functions implemented in C and can't be mimicked by any Python +# # function. +# node = build_function(getattr(func, '__name__', None) or localname, +# parameters.get(_Parameter.POSITIONAL_OR_KEYWORD, ()), +# parameters.get(_Parameter.KEYWORD_ONLY, ()), +# parameters.get(_Parameter.VAR_POSITIONAL, None), +# parameters.get(_Parameter.VAR_KEYWORD, None), +# signature.return_annotation, +# func.__doc__, +# parent) +# return node def ast_from_object(object_, name=None): diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index ae4729f0e2..bf79781a10 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -220,12 +220,12 @@ def _append_node(self, child): self.body.append(child) child.parent = self - def add_local_node(self, child_node, name=None): - """append a child which should alter locals to the given node""" - 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) + # def add_local_node(self, child_node, name=None): + # """append a child which should alter locals to the given node""" + # 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) def __getitem__(self, item): """method from the `dict` interface returning the first node @@ -365,7 +365,17 @@ def globals(self): @property def future_imports(self): - return frozenset(get_locals(self)['__future__']) + index = 0 + future_imports = [] + while (index < len(self.body) and + ((isinstance(self.body[index], node_classes.ImportFrom) + and self.body[index].modname == '__future__') or + (index == 0 and isinstance(self.body[0], + node_classes.Expr)))): + future_imports.extend(n[0] for n in getattr(self.body[index], + 'names', ())) + index += 1 + return frozenset(future_imports) def _get_stream(self): if self.source_code is not None: @@ -1818,8 +1828,8 @@ class attributes defined in the class body, also what a locals() call in the body would return. This function starts by recursing over its argument's children to - avoid incorrectly adding a class's or function's name to its local - variables. + avoid incorrectly adding a class's, function's, or module's name + to its own local variables. ''' raise TypeError("This isn't an astroid node: %s" % type(node)) @@ -1891,6 +1901,9 @@ def locals_import(node, locals_): # pylint: disable=unused-variable; doesn't understand singledispatch @_get_locals.register(node_classes.ImportFrom) def locals_import_from(node, locals_): + # Don't add future imports to locals. + if node.modname == '__future__': + return def sort_locals(my_list): my_list.sort(key=lambda node: node.fromlineno) diff --git a/astroid/tests/unittest_brain.py b/astroid/tests/unittest_brain.py index c15160ae6e..1f207303d5 100644 --- a/astroid/tests/unittest_brain.py +++ b/astroid/tests/unittest_brain.py @@ -122,9 +122,9 @@ def test_namedtuple_advanced_inference(self): # urlparse return an object of class ParseResult, which has a # namedtuple call and a mixin as base classes result = test_utils.extract_node(""" - import urlparse + import six - result = __(urlparse.urlparse('gopher://')) + result = __(six.moves.urllib.parse.urlparse('gopher://')) """) instance = next(result.infer()) self.assertEqual(len(instance.getattr('scheme')), 1) diff --git a/astroid/tests/unittest_builder.py b/astroid/tests/unittest_builder.py index 87fadab32b..8c40a6272c 100644 --- a/astroid/tests/unittest_builder.py +++ b/astroid/tests/unittest_builder.py @@ -492,14 +492,14 @@ def test_no_future_imports(self): def test_future_imports(self): mod = builder.parse("from __future__ import print_function") - self.assertEqual(set(['print_function']), mod.future_imports) + self.assertEqual(frozenset(['print_function']), mod.future_imports) def test_two_future_imports(self): mod = builder.parse(""" from __future__ import print_function from __future__ import absolute_import """) - self.assertEqual(set(['print_function', 'absolute_import']), mod.future_imports) + self.assertEqual(frozenset(['print_function', 'absolute_import']), mod.future_imports) def test_inferred_build(self): code = ''' diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index a0cefd7566..2cf5828bdd 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -1035,9 +1035,9 @@ def with_metaclass(meta, base=object): class ClassWithMeta(with_metaclass(type)): #@ pass """) - print(klass.repr_tree()) - print(klass.locals) - print(tuple(klass.ancestors())) + # print(klass.repr_tree()) + # print(klass.locals) + # print(tuple(klass.ancestors())) self.assertEqual( ['NewBase', 'object'], [base.name for base in klass.ancestors()]) From 594534e7a3bf6a631fad0677d7d43cfcc8e552f2 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Wed, 28 Oct 2015 10:28:31 -0400 Subject: [PATCH 025/312] Fix Const/Singleton issues --- astroid/raw_building.py | 1 + astroid/rebuilder.py | 26 ++++++++++++++++---------- astroid/tests/unittest_builder.py | 2 +- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 65681c6c1f..79ffd69dbe 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -399,6 +399,7 @@ def ast_from_dict(dictionary, built_objects, module, name=None, @_ast_from_object.register(str) @_ast_from_object.register(bytes) @_ast_from_object.register(int) +@_ast_from_object.register(bool) @_ast_from_object.register(float) @_ast_from_object.register(complex) def ast_from_builtin_number_text_binary(builtin_number_text_binary, built_objects, module, name=None, parent=None): diff --git a/astroid/rebuilder.py b/astroid/rebuilder.py index 5db8c966a4..131ec865fc 100644 --- a/astroid/rebuilder.py +++ b/astroid/rebuilder.py @@ -64,10 +64,10 @@ ast.NotIn: 'not in', } -CONST_NAME_TRANSFORMS = {'None': None, - 'True': True, - 'False': False, - } +BUILTIN_NAMES = {'None': (nodes.Singleton, None), + 'NotImplemented': (nodes.Singleton, NotImplemented), + 'True': (nodes.Const, True), + 'False': (nodes.Const, False)} REDIRECT = {'arguments': 'Arguments', 'comprehension': 'Comprehension', @@ -592,10 +592,12 @@ def visit_name(self, node, parent, assign_ctx=None): assert assign_ctx == "Assign" newnode = nodes.AssignName(node.id, node.lineno, node.col_offset, parent) - elif node.id in CONST_NAME_TRANSFORMS: - newnode = nodes.Const(CONST_NAME_TRANSFORMS[node.id], - getattr(node, 'lineno', None), - getattr(node, 'col_offset', None), parent) + elif node.id in BUILTIN_NAMES: + newnode = + BUILTIN_NAMES[node.id][0](BUILTIN_NAMES[node.id][1], + getattr(node, 'lineno', None), + getattr(node, 'col_offset', None), + parent) return newnode else: newnode = nodes.Name(node.id, node.lineno, node.col_offset, parent) @@ -746,8 +748,12 @@ def visit_arg(self, node, parent, assign_ctx=None): def visit_nameconstant(self, node, parent, assign_ctx=None): # in Python 3.4 we have NameConstant for True / False / None - return nodes.Const(node.value, getattr(node, 'lineno', None), - getattr(node, 'col_offset', None), parent) + if isinstance(node.value, bool): + return nodes.Const(node.value, getattr(node, 'lineno', None), + getattr(node, 'col_offset', None), parent) + else: + return nodes.Singleton(node.value, getattr(node, 'lineno', None), + getattr(node, 'col_offset', None), parent) def visit_excepthandler(self, node, parent, assign_ctx=None): """visit an ExceptHandler node by returning a fresh instance of it""" diff --git a/astroid/tests/unittest_builder.py b/astroid/tests/unittest_builder.py index 8c40a6272c..004afea13c 100644 --- a/astroid/tests/unittest_builder.py +++ b/astroid/tests/unittest_builder.py @@ -309,7 +309,7 @@ def test_inspect_build0(self): def test_inspect_build1(self): time_ast = MANAGER.ast_from_module_name('time') self.assertTrue(time_ast) - self.assertEqual(time_ast['time'].args.defaults, []) + self.assertIsInstance(time_ast['time'].args, nodes.Unknown) if os.name == 'java': test_inspect_build1 = unittest.expectedFailure(test_inspect_build1) From ee8bbff45193d8d1963d37f07c4c4c9e909f9774 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Wed, 28 Oct 2015 10:42:27 -0400 Subject: [PATCH 026/312] Fix bad Emacs line breaking --- astroid/rebuilder.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/astroid/rebuilder.py b/astroid/rebuilder.py index 131ec865fc..0f6c1a12b7 100644 --- a/astroid/rebuilder.py +++ b/astroid/rebuilder.py @@ -593,11 +593,10 @@ def visit_name(self, node, parent, assign_ctx=None): newnode = nodes.AssignName(node.id, node.lineno, node.col_offset, parent) elif node.id in BUILTIN_NAMES: - newnode = - BUILTIN_NAMES[node.id][0](BUILTIN_NAMES[node.id][1], - getattr(node, 'lineno', None), - getattr(node, 'col_offset', None), - parent) + newnode = BUILTIN_NAMES[node.id][0](BUILTIN_NAMES[node.id][1], + getattr(node, 'lineno', None), + getattr(node, 'col_offset', None), + parent) return newnode else: newnode = nodes.Name(node.id, node.lineno, node.col_offset, parent) From 4bb32816e4429dc6f718551e5e1d1d11cd15332c Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Thu, 29 Oct 2015 12:18:08 -0400 Subject: [PATCH 027/312] Use inference in builtin_lookup and fix test_inferred_dont_pollute --- astroid/objects.py | 2 +- astroid/scoped_nodes.py | 10 +++++----- astroid/tests/unittest_builder.py | 23 +++++++++++------------ astroid/tests/unittest_helpers.py | 4 +++- 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/astroid/objects.py b/astroid/objects.py index 3ab0a65f26..32275b4a7d 100644 --- a/astroid/objects.py +++ b/astroid/objects.py @@ -145,7 +145,7 @@ def igetattr(self, name, context=None): found = False for cls in mro: - if name not in cls.locals: + if name not in cls.locals: # and name not in cls.external_attrs: continue found = True diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index bf79781a10..9b92d0270b 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -138,10 +138,10 @@ def builtin_lookup(name): builtin_astroid = MANAGER.ast_from_module(six.moves.builtins) if name == '__dict__': return builtin_astroid, () - try: - stmts = builtin_astroid.locals[name] - except KeyError: - stmts = () + stmts = builtin_astroid.locals.get(name, ()) + # Use inference to find what AssignName nodes point to in builtins. + stmts = [next(s.infer()) if isinstance(s, node_classes.AssignName) else s + for s in stmts] return builtin_astroid, stmts @@ -433,7 +433,7 @@ def getattr(self, name, context=None, ignore_locals=False): if name == '__path__' and self.package: return [node_classes.List()] + self.locals.get(name, []) return std_special_attributes(self, name) - if not ignore_locals and name in self.locals: + if not ignore_locals and name in self.locals: # or name in self.external_attrs) return self.locals[name] if self.package: try: diff --git a/astroid/tests/unittest_builder.py b/astroid/tests/unittest_builder.py index 004afea13c..2a5b0d4e04 100644 --- a/astroid/tests/unittest_builder.py +++ b/astroid/tests/unittest_builder.py @@ -529,18 +529,17 @@ def inc(self): def test_inferred_dont_pollute(self): code = ''' - def func(a=None): - a.custom_attr = 0 - def func2(a={}): - a.custom_attr = 0 - ''' - builder.parse(code) - nonetype = nodes.Singleton(None) - self.assertNotIn('custom_attr', nonetype.locals) - self.assertNotIn('custom_attr', nonetype.instance_attrs) - nonetype = nodes.Dict() - self.assertNotIn('custom_attr', nonetype.locals) - self.assertNotIn('custom_attr', nonetype.instance_attrs) + def func(a=None): + a.custom_attr = 0 + a #@ + def func2(a={}): + a.custom_attr = 0 + a #@ + ''' + name_nodes = test_utils.extract_node(code) + for node in name_nodes: + self.assertFalse(hasattr(node, 'locals')) + self.assertFalse(hasattr(node, 'instance_attrs')) def test_asstuple(self): code = 'a, b = range(2)' diff --git a/astroid/tests/unittest_helpers.py b/astroid/tests/unittest_helpers.py index 55e8c2430a..5df53311bf 100644 --- a/astroid/tests/unittest_helpers.py +++ b/astroid/tests/unittest_helpers.py @@ -45,6 +45,7 @@ def _build_custom_builtin(self, obj_name): def assert_classes_equal(self, cls, other): self.assertEqual(cls.name, other.name) + # self.assertEqual(cls.root(), other.root()) self.assertEqual(cls.parent, other.parent) self.assertEqual(cls.qname(), other.qname()) @@ -59,7 +60,8 @@ def test_object_type(self): ('object()', self._extract('object')), ('lambda: None', self._build_custom_builtin('function')), ('len', self._build_custom_builtin('builtin_function_or_method')), - ('None', self._build_custom_builtin('NoneType')), + # TODO + # ('None', self._build_custom_builtin('None')), ('import sys\nsys#@', self._build_custom_builtin('module')), ] for code, expected in pairs: From b93ba146a7d606b333a90ee56d6ed16583d7378d Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Thu, 29 Oct 2015 22:03:34 +0200 Subject: [PATCH 028/312] Move the inference of type.__new__ from Instance to an inference hook, in order to reduce dependencies. --- astroid/bases.py | 80 ---------------------- astroid/brain/brain_builtin_inference.py | 84 +++++++++++++++++++++++- 2 files changed, 82 insertions(+), 82 deletions(-) diff --git a/astroid/bases.py b/astroid/bases.py index 4b7f83b8ac..f11e1161aa 100644 --- a/astroid/bases.py +++ b/astroid/bases.py @@ -19,7 +19,6 @@ inference utils. """ -import collections import sys from astroid import context as contextmod @@ -307,90 +306,11 @@ def __init__(self, proxy, bound): def is_bound(self): return True - def _infer_type_new_call(self, caller, context): - """Try to infer what type.__new__(mcs, name, bases, attrs) returns. - - In order for such call to be valid, the metaclass needs to be - a subtype of ``type``, the name needs to be a string, the bases - needs to be a tuple of classes and the attributes a dictionary - of strings to values. - """ - from astroid import node_classes - # Verify the metaclass - mcs = next(caller.args[0].infer(context=context)) - if mcs.__class__.__name__ != 'ClassDef': - # Not a valid first argument. - return - if not mcs.is_subtype_of("%s.type" % BUILTINS): - # Not a valid metaclass. - return - - # Verify the name - name = next(caller.args[1].infer(context=context)) - if name.__class__.__name__ != 'Const': - # Not a valid name, needs to be a const. - return - if not isinstance(name.value, str): - # Needs to be a string. - return - - # Verify the bases - bases = next(caller.args[2].infer(context=context)) - if bases.__class__.__name__ != 'Tuple': - # Needs to be a tuple. - return - inferred_bases = [next(elt.infer(context=context)) - for elt in bases.elts] - if any(base.__class__.__name__ != 'ClassDef' - for base in inferred_bases): - # All the bases needs to be Classes - return - - # Verify the attributes. - attrs = next(caller.args[3].infer(context=context)) - if attrs.__class__.__name__ != 'Dict': - # Needs to be a dictionary. - return - cls_locals = collections.defaultdict(list) - for key, value in attrs.items: - key = next(key.infer(context=context)) - value = next(value.infer(context=context)) - if key.__class__.__name__ != 'Const': - # Something invalid as an attribute. - return - if not isinstance(key.value, str): - # Not a proper attribute. - return - cls_locals[key.value].append(value) - - # Build the class from now. - cls = mcs.__class__(name=name.value, lineno=caller.lineno, - col_offset=caller.col_offset, - parent=caller) - empty = node_classes.Pass() - cls.postinit(bases=bases.elts, body=[empty], decorators=[], - newstyle=True, metaclass=mcs) - cls.locals = cls_locals - return cls - def infer_call_result(self, caller, context=None): if context is None: context = contextmod.InferenceContext() context = context.clone() context.boundnode = self.bound - - if (self.bound.__class__.__name__ == 'ClassDef' - and self.bound.name == 'type' - and self.name == '__new__' - and len(caller.args) == 4 - # TODO(cpopa): this check shouldn't be needed. - and self._proxied.parent.frame().qname() == '%s.object' % BUILTINS): - - # Check if we have an ``type.__new__(mcs, name, bases, attrs)`` call. - new_cls = self._infer_type_new_call(caller, context) - if new_cls: - return iter((new_cls, )) - return super(BoundMethod, self).infer_call_result(caller, context) def bool_value(self): diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index 75592717e2..4e651ef875 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -1,5 +1,6 @@ """Astroid hooks for various builtins.""" +import collections from functools import partial import sys from textwrap import dedent @@ -8,6 +9,7 @@ from astroid import (MANAGER, UseInferenceDefault, NotFoundError, inference_tip, InferenceError, UnresolvableName) from astroid import arguments +from astroid import bases from astroid.builder import AstroidBuilder from astroid import helpers from astroid import nodes @@ -70,8 +72,7 @@ def split(self, *args): method.parent = class_node def extend_builtins(class_transforms): - from astroid.bases import BUILTINS - builtin_ast = MANAGER.astroid_cache[BUILTINS] + builtin_ast = MANAGER.astroid_cache[bases.BUILTINS] for class_name, transform in class_transforms.items(): transform(builtin_ast[class_name]) @@ -471,6 +472,81 @@ def infer_slice(node, context=None): return slice_node +def infer_type_dunder_new(caller, context=None): + """Try to infer what type.__new__(mcs, name, bases, attrs) returns. + + In order for such call to be valid, the metaclass needs to be + a subtype of ``type``, the name needs to be a string, the bases + needs to be a tuple of classes and the attributes a dictionary + of strings to values. + """ + if len(caller.args) != 4: + raise UseInferenceDefault + + # Verify the metaclass + mcs = next(caller.args[0].infer(context=context)) + if not isinstance(mcs, nodes.ClassDef): + # Not a valid first argument. + raise UseInferenceDefault + if not mcs.is_subtype_of("%s.type" % bases.BUILTINS): + # Not a valid metaclass. + raise UseInferenceDefault + + # Verify the name + name = next(caller.args[1].infer(context=context)) + if not isinstance(name, nodes.Const): + # Not a valid name, needs to be a const. + raise UseInferenceDefault + if not isinstance(name.value, str): + # Needs to be a string. + raise UseInferenceDefault + + # Verify the bases + cls_bases = next(caller.args[2].infer(context=context)) + if not isinstance(cls_bases, nodes.Tuple): + # Needs to be a tuple. + raise UseInferenceDefault + inferred_bases = [next(elt.infer(context=context)) + for elt in cls_bases.elts] + if any(not isinstance(base, nodes.ClassDef) for base in inferred_bases): + # All the bases needs to be Classes + raise UseInferenceDefault + + # Verify the attributes. + attrs = next(caller.args[3].infer(context=context)) + if not isinstance(attrs, nodes.Dict): + # Needs to be a dictionary. + raise UseInferenceDefault + cls_locals = collections.defaultdict(list) + for key, value in attrs.items: + key = next(key.infer(context=context)) + value = next(value.infer(context=context)) + if not isinstance(key, nodes.Const): + # Something invalid as an attribute. + raise UseInferenceDefault + if not isinstance(key.value, str): + # Not a proper attribute. + raise UseInferenceDefault + cls_locals[key.value].append(value) + + # Build the class from now. + cls = nodes.Class(name=name.value, lineno=caller.lineno, + col_offset=caller.col_offset, + parent=caller) + empty = nodes.Pass() + cls.postinit(bases=cls_bases.elts, body=[empty], decorators=[], + newstyle=True, metaclass=mcs) + cls.locals = cls_locals + return iter([cls]) + + +def _looks_like_type_dunder_new(node): + return (isinstance(node.func, nodes.Attribute) + and isinstance(node.func.expr, nodes.Name) + and node.func.expr.name == 'type' + and node.func.attrname == '__new__') + + # Builtins inference register_builtin_transform(infer_bool, 'bool') register_builtin_transform(infer_super, 'super') @@ -484,3 +560,7 @@ def infer_slice(node, context=None): register_builtin_transform(infer_frozenset, 'frozenset') register_builtin_transform(infer_type, 'type') register_builtin_transform(infer_slice, 'slice') + +# infer type.__new__ calls +MANAGER.register_transform(nodes.Call, inference_tip(infer_type_dunder_new), + _looks_like_type_dunder_new) From b674ba1173a80df94ebc968b7aaa99eff22b4dc3 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Thu, 29 Oct 2015 22:19:26 +0200 Subject: [PATCH 029/312] Move the implementation of Instance.getitem back into Instance It was previously in inference.py, since getitem used a nodes.Const inside, but that was changed a while ago (the arguments are already wrapped from the inference). Now Instance.getitem doesn't need access to Const, which means there's no dependency left between it and node_classes. --- astroid/bases.py | 20 ++++++++++++++++---- astroid/inference.py | 24 ------------------------ 2 files changed, 16 insertions(+), 28 deletions(-) diff --git a/astroid/bases.py b/astroid/bases.py index f11e1161aa..bf49202ad3 100644 --- a/astroid/bases.py +++ b/astroid/bases.py @@ -255,12 +255,24 @@ def bool_value(self): return True return result - # TODO(cpopa): this is set in inference.py - # The circular dependency hell goes deeper and deeper. - # pylint: disable=unused-argument def getitem(self, index, context=None): - pass + if context: + new_context = context.clone() + else: + context = new_context = contextmod.InferenceContext() + # Create a new callcontext for providing index as an argument. + new_context.callcontext = contextmod.CallContext(args=[index]) + new_context.boundnode = self + + method = next(self.igetattr('__getitem__', context=context)) + if not isinstance(method, BoundMethod): + raise exceptions.InferenceError + + try: + return next(method.infer_call_result(self, new_context)) + except StopIteration: + util.reraise(exceptions.InferenceError()) class UnboundMethod(Proxy): diff --git a/astroid/inference.py b/astroid/inference.py index 2943596bcd..e3e187ce76 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -699,27 +699,3 @@ def infer_empty_node(self, context=None): def infer_index(self, context=None): return self.value.infer(context) nodes.Index._infer = infer_index - -# TODO: move directly into bases.Instance when the dependency hell -# will be solved. -def instance_getitem(self, index, context=None): - # Rewrap index to Const for this case - if context: - new_context = context.clone() - else: - context = new_context = contextmod.InferenceContext() - - # Create a new callcontext for providing index as an argument. - new_context.callcontext = contextmod.CallContext(args=[index]) - new_context.boundnode = self - - method = next(self.igetattr('__getitem__', context=context)) - if not isinstance(method, bases.BoundMethod): - raise exceptions.InferenceError - - try: - return next(method.infer_call_result(self, new_context)) - except StopIteration: - util.reraise(exceptions.InferenceError()) - -bases.Instance.getitem = instance_getitem From 49e3d338a689e5e5f5ad5d8b06f817d72e1d6119 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Thu, 29 Oct 2015 22:30:07 +0200 Subject: [PATCH 030/312] Simplify the finding of type for classes. --- astroid/helpers.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/astroid/helpers.py b/astroid/helpers.py index 00f4784b3c..b3e7f1f1f6 100644 --- a/astroid/helpers.py +++ b/astroid/helpers.py @@ -67,12 +67,7 @@ def _object_type(node, context=None): for inferred in node.infer(context=context): if isinstance(inferred, scoped_nodes.ClassDef): - if inferred.newstyle: - metaclass = inferred.metaclass() - if metaclass: - yield metaclass - continue - yield builtins.getattr('type')[0] + yield inferred.metaclass() or inferred.implicit_metaclass() elif isinstance(inferred, (scoped_nodes.Lambda, bases.UnboundMethod)): yield _function_type(inferred, builtins) elif isinstance(inferred, scoped_nodes.Module): From 864305d40412a2967d06f07a125f112f40a8f2d7 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Fri, 30 Oct 2015 13:50:43 -0400 Subject: [PATCH 031/312] Rename Singleton to NameConstant, introduce ReservedName node --- astroid/as_string.py | 2 +- astroid/node_classes.py | 31 ++++++++++++++++++++++------- astroid/nodes.py | 10 ++++++---- astroid/protocols.py | 6 +++--- astroid/raw_building.py | 33 ++++++++++++++++++++++--------- astroid/rebuilder.py | 25 +++++++++++------------ astroid/scoped_nodes.py | 12 ++++++++--- astroid/tests/unittest_builder.py | 4 ++-- astroid/tests/unittest_helpers.py | 6 ++---- 9 files changed, 82 insertions(+), 47 deletions(-) diff --git a/astroid/as_string.py b/astroid/as_string.py index 79674bac9e..f907c489f4 100644 --- a/astroid/as_string.py +++ b/astroid/as_string.py @@ -135,7 +135,7 @@ def visit_comprehension(self, node): def visit_const(self, node): """return an astroid.Const node as string""" return repr(node.value) - visit_singleton = visit_const + visit_nameconstant = visit_const def visit_continue(self, node): """return an astroid.Continue node as string""" diff --git a/astroid/node_classes.py b/astroid/node_classes.py index 98886b40de..be8bd9ab30 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -1182,7 +1182,7 @@ def _get_filtered_stmts(self, lookup_node, node, stmts, mystmt): class Const(NodeNG, bases.Instance): - """represent a constant node like num, str, bool, None, bytes""" + """represent a constant node like num, str, bytes""" _other_fields = ('value',) def __init__(self, value, lineno=None, col_offset=None, parent=None): @@ -1219,13 +1219,30 @@ def _proxied(self): return builtins.getattr(type(self.value).__name__)[0] -class Singleton(Const): - """Represents a singleton object, at the moment None and NotImplemented.""" +class NameConstant(Const): + """Represents a builtin singleton, at the moment True, False, None, + and NotImplemented. - @decorators.cachedproperty - def _proxied(self): - builtins = MANAGER.astroid_cache[BUILTINS] - return builtins.getattr(str(self.value))[0] + """ + + # @decorators.cachedproperty + # def _proxied(self): + # return self + # # builtins = MANAGER.astroid_cache[BUILTINS] + # # return builtins.getattr(str(self.value))[0] + + +class ReservedName(NodeNG): + '''Used in the builtins AST to assign names to singletons.''' + _astroid_fields = ('value',) + _other_fields = ('name',) + + def __init__(self, name, lineno=None, col_offset=None, parent=None): + self.name = name + super(ReservedName, self).__init__(lineno, col_offset, parent) + + def postinit(self, value): + self.value = value class Continue(Statement): diff --git a/astroid/nodes.py b/astroid/nodes.py index 3573ca0753..7e89cb1a95 100644 --- a/astroid/nodes.py +++ b/astroid/nodes.py @@ -48,7 +48,9 @@ # Backwards-compatibility aliases Backquote, Discard, AssName, AssAttr, Getattr, CallFunc, From, # Node not present in the builtin ast module. - DictUnpack, EmptyNode, Singleton, Unknown + DictUnpack, + # Special nodes for building from live objects. + EmptyNode, NameConstant, ReservedName, Unknown ) from astroid.scoped_nodes import ( Module, GeneratorExp, Lambda, DictComp, @@ -74,11 +76,11 @@ If, IfExp, Import, Index, Keyword, Lambda, List, ListComp, - Name, Nonlocal, + Name, NameConstant, Nonlocal, Module, Pass, Print, - Raise, Return, - Set, SetComp, Singleton, Slice, Starred, Subscript, + Raise, ReservedName, Return, + Set, SetComp, Slice, Starred, Subscript, TryExcept, TryFinally, Tuple, UnaryOp, Unknown, While, With, diff --git a/astroid/protocols.py b/astroid/protocols.py index 09b5cd3908..8370bfc2f9 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -119,7 +119,7 @@ def _infer_unary_op(obj, op): @decorators.yes_if_nothing_inferred def const_infer_binary_op(self, operator, other, context, _): - not_implemented = nodes.Singleton(NotImplemented) + not_implemented = nodes.NameConstant(NotImplemented) if isinstance(other, nodes.Const): try: impl = BIN_OP_IMPL[operator] @@ -155,7 +155,7 @@ def _multiply_seq_by_int(self, other, context): @decorators.yes_if_nothing_inferred def tl_infer_binary_op(self, operator, other, context, method): - not_implemented = nodes.Singleton(NotImplemented) + not_implemented = nodes.NameConstant(NotImplemented) if isinstance(other, self.__class__) and operator == '+': node = self.__class__() elts = [n for elt in self.elts for n in elt.infer(context) @@ -403,7 +403,7 @@ def _infer_context_manager(self, mgr, context): if yield_point: if not yield_point.value: # TODO(cpopa): an empty yield. Should be wrapped to Const. - const = nodes.Singleton(None) + const = nodes.NameConstant(None) const.parent = yield_point const.lineno = yield_point.lineno yield const diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 79ffd69dbe..25e0d149bf 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -399,7 +399,6 @@ def ast_from_dict(dictionary, built_objects, module, name=None, @_ast_from_object.register(str) @_ast_from_object.register(bytes) @_ast_from_object.register(int) -@_ast_from_object.register(bool) @_ast_from_object.register(float) @_ast_from_object.register(complex) def ast_from_builtin_number_text_binary(builtin_number_text_binary, built_objects, module, name=None, parent=None): @@ -421,28 +420,44 @@ def ast_from_builtin_number_text_binary(builtin_number_text_binary, built_object @_ast_from_object.register(type(None)) @_ast_from_object.register(type(NotImplemented)) +@_ast_from_object.register(bool) def ast_from_builtin_singleton(builtin_singleton, built_objects, module, name=None, parent=None): - if name: + # A builtin singleton is assigned to a name. + if name and name != str(builtin_singleton): parent = nodes.Assign(parent=parent) - name_node = nodes.AssignName(name, parent=parent) - builtin_singleton_node = nodes.Singleton(value=builtin_singleton, parent=parent) - if name: + name_node = nodes.AssignName(name=name, parent=parent) + # This case handles the initial assignment of singletons to names + # in the builtins module. It can be triggered in other cases with + # an object that contains a builtin singleton by its own name, but + # there's never any reason to write that kind of code, and even if + # it happens it shouldn't cause any harm. + elif name and name == str(builtin_singleton): + parent = nodes.ReservedName(name=name, parent=parent) + builtin_singleton_node = nodes.NameConstant(value=builtin_singleton, parent=parent) + if name and name != str(builtin_singleton): parent.postinit(targets=[name_node], value=builtin_singleton_node) node = parent + elif name and name == str(builtin_singleton): + parent.postinit(value=builtin_singleton_node) + node = parent else: node = builtin_singleton_node return node - @_ast_from_object.register(type(Ellipsis)) def ast_from_ellipsis(ellipsis, built_objects, module, name=None, parent=None): - if name: + if name and name != str(ellipsis): parent = nodes.Assign(parent=parent) - name_node = nodes.AssignName(name, parent=parent) + name_node = nodes.AssignName(name=name, parent=parent) + elif name and name == str(ellipsis): + parent = nodes.ReservedName(name=name, parent=parent) ellipsis_node = nodes.Ellipsis(parent=parent) - if name: + if name and name != str(ellipsis): parent.postinit(targets=[name_node], value=ellipsis_node) node = parent + elif name and name == str(ellipsis): + parent.postinit(value=ellipsis_node) + node = parent else: node = ellipsis_node return node diff --git a/astroid/rebuilder.py b/astroid/rebuilder.py index 0f6c1a12b7..469f11866d 100644 --- a/astroid/rebuilder.py +++ b/astroid/rebuilder.py @@ -64,10 +64,11 @@ ast.NotIn: 'not in', } -BUILTIN_NAMES = {'None': (nodes.Singleton, None), - 'NotImplemented': (nodes.Singleton, NotImplemented), - 'True': (nodes.Const, True), - 'False': (nodes.Const, False)} +# Ellipsis is also one of these but has its own node +BUILTIN_NAMES = {'None': None, + 'NotImplemented': NotImplemented, + 'True': True, + 'False': False} REDIRECT = {'arguments': 'Arguments', 'comprehension': 'Comprehension', @@ -593,10 +594,10 @@ def visit_name(self, node, parent, assign_ctx=None): newnode = nodes.AssignName(node.id, node.lineno, node.col_offset, parent) elif node.id in BUILTIN_NAMES: - newnode = BUILTIN_NAMES[node.id][0](BUILTIN_NAMES[node.id][1], - getattr(node, 'lineno', None), - getattr(node, 'col_offset', None), - parent) + newnode = nodes.NameConstant(BUILTIN_NAMES[node.id], + getattr(node, 'lineno', None), + getattr(node, 'col_offset', None), + parent) return newnode else: newnode = nodes.Name(node.id, node.lineno, node.col_offset, parent) @@ -747,12 +748,8 @@ def visit_arg(self, node, parent, assign_ctx=None): def visit_nameconstant(self, node, parent, assign_ctx=None): # in Python 3.4 we have NameConstant for True / False / None - if isinstance(node.value, bool): - return nodes.Const(node.value, getattr(node, 'lineno', None), - getattr(node, 'col_offset', None), parent) - else: - return nodes.Singleton(node.value, getattr(node, 'lineno', None), - getattr(node, 'col_offset', None), parent) + return nodes.NameConstant(node.value, getattr(node, 'lineno', None), + getattr(node, 'col_offset', None), parent) def visit_excepthandler(self, node, parent, assign_ctx=None): """visit an ExceptHandler node by returning a fresh instance of it""" diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index 9b92d0270b..da627c72f8 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -140,8 +140,8 @@ def builtin_lookup(name): return builtin_astroid, () stmts = builtin_astroid.locals.get(name, ()) # Use inference to find what AssignName nodes point to in builtins. - stmts = [next(s.infer()) if isinstance(s, node_classes.AssignName) else s - for s in stmts] + # stmts = [next(s.infer()) if isinstance(s, node_classes.AssignName) else s + # for s in stmts] return builtin_astroid, stmts @@ -1023,7 +1023,7 @@ def infer_call_result(self, caller, context=None): returns = self.nodes_of_class(node_classes.Return, skip_klass=FunctionDef) for returnnode in returns: if returnnode.value is None: - yield node_classes.Singleton(None) + yield node_classes.NameConstant(None) else: try: for inferred in returnnode.value.infer(context): @@ -1882,6 +1882,12 @@ def locals_empty(node, locals_): if node.name: locals_[node.name].append(node.object) +@_get_locals.register(node_classes.ReservedName) +def locals_reserved_name(node, locals_): + '''EmptyNodes add an object to the local variables under a specified + name.''' + locals_[node.name].append(node.value) + # pylint: disable=unused-variable; doesn't understand singledispatch @_get_locals.register(node_classes.Arguments) def locals_arguments(node, locals_): diff --git a/astroid/tests/unittest_builder.py b/astroid/tests/unittest_builder.py index 2a5b0d4e04..66e506ce27 100644 --- a/astroid/tests/unittest_builder.py +++ b/astroid/tests/unittest_builder.py @@ -538,8 +538,8 @@ def func2(a={}): ''' name_nodes = test_utils.extract_node(code) for node in name_nodes: - self.assertFalse(hasattr(node, 'locals')) - self.assertFalse(hasattr(node, 'instance_attrs')) + self.assertFalse(hasattr(next(node.infer()), 'locals')) + self.assertFalse(hasattr(next(node.infer()), 'instance_attrs')) def test_asstuple(self): code = 'a, b = range(2)' diff --git a/astroid/tests/unittest_helpers.py b/astroid/tests/unittest_helpers.py index 5df53311bf..f6389b9436 100644 --- a/astroid/tests/unittest_helpers.py +++ b/astroid/tests/unittest_helpers.py @@ -45,8 +45,7 @@ def _build_custom_builtin(self, obj_name): def assert_classes_equal(self, cls, other): self.assertEqual(cls.name, other.name) - # self.assertEqual(cls.root(), other.root()) - self.assertEqual(cls.parent, other.parent) + self.assertEqual(cls.root(), other.root()) self.assertEqual(cls.qname(), other.qname()) def test_object_type(self): @@ -60,8 +59,7 @@ def test_object_type(self): ('object()', self._extract('object')), ('lambda: None', self._build_custom_builtin('function')), ('len', self._build_custom_builtin('builtin_function_or_method')), - # TODO - # ('None', self._build_custom_builtin('None')), + ('None', self._build_custom_builtin('None')), ('import sys\nsys#@', self._build_custom_builtin('module')), ] for code, expected in pairs: From ce39cf2b684a0eff61938ddb8e86ab34a99ade83 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sat, 31 Oct 2015 00:06:56 +0200 Subject: [PATCH 032/312] Add the concept of virtual bases This commit introduces the concept of virtual bases for the AST and runtime objects, the idea being that isinstance checks should be done against these virtual classes, while other operations need to be done on concrete objects. --- astroid/bases.py | 8 +- astroid/helpers.py | 22 ++- astroid/node_classes.py | 62 +++++++- astroid/object_bases.py | 334 ++++++++++++++++++++++++++++++++++++++++ astroid/scoped_nodes.py | 10 ++ 5 files changed, 421 insertions(+), 15 deletions(-) create mode 100644 astroid/object_bases.py diff --git a/astroid/bases.py b/astroid/bases.py index bf49202ad3..33f38760d7 100644 --- a/astroid/bases.py +++ b/astroid/bases.py @@ -23,6 +23,7 @@ from astroid import context as contextmod from astroid import exceptions +from astroid import object_bases from astroid import util @@ -122,6 +123,7 @@ def _infer_method_result_truth(instance, method_name, context): return util.YES +@object_bases.register_implementation(object_bases.Instance) class Instance(Proxy): """A special node representing a class instance.""" @@ -188,7 +190,7 @@ def _wrap_attr(self, attrs, context=None): # Unfortunately, we can't do an isinstance check here, # because of the circular dependency between astroid.bases # and astroid.scoped_nodes. - if attr.statement().scope() == self._proxied: + if isinstance(attr.statement().scope(), object_bases.ClassDef): if attr.args.args and attr.args.args[0].name == 'self': yield BoundMethod(attr, self) continue @@ -275,6 +277,7 @@ def getitem(self, index, context=None): util.reraise(exceptions.InferenceError()) +@object_bases.register_implementation(object_bases.UnboundMethod) class UnboundMethod(Proxy): """a special node representing a method not bound to an instance""" def __repr__(self): @@ -309,6 +312,7 @@ def bool_value(self): return True +@object_bases.register_implementation(object_bases.BoundMethod) class BoundMethod(UnboundMethod): """a special node representing a method bound to an instance""" def __init__(self, proxy, bound): @@ -329,6 +333,7 @@ def bool_value(self): return True +@object_bases.register_implementation(object_bases.Generator) class Generator(Instance): """a special node representing a generator. @@ -351,4 +356,3 @@ def __repr__(self): def __str__(self): return 'Generator(%s)' % (self._proxied.name) - diff --git a/astroid/helpers.py b/astroid/helpers.py index b3e7f1f1f6..3a393b6411 100644 --- a/astroid/helpers.py +++ b/astroid/helpers.py @@ -22,13 +22,11 @@ import six -from astroid import bases +from astroid import object_bases from astroid import context as contextmod from astroid import exceptions from astroid import manager -from astroid import nodes from astroid import raw_building -from astroid import scoped_nodes from astroid import util @@ -42,17 +40,17 @@ def _build_proxy_class(cls_name, builtins): def _function_type(function, builtins): - if isinstance(function, scoped_nodes.Lambda): + if isinstance(function, object_bases.Lambda): if function.root().name == BUILTINS: cls_name = 'builtin_function_or_method' else: cls_name = 'function' - elif isinstance(function, bases.BoundMethod): + elif isinstance(function, object_bases.BoundMethod): if six.PY2: cls_name = 'instancemethod' else: cls_name = 'method' - elif isinstance(function, bases.UnboundMethod): + elif isinstance(function, object_bases.UnboundMethod): if six.PY2: cls_name = 'instancemethod' else: @@ -66,11 +64,11 @@ def _object_type(node, context=None): context = context or contextmod.InferenceContext() for inferred in node.infer(context=context): - if isinstance(inferred, scoped_nodes.ClassDef): + if isinstance(inferred, object_bases.ClassDef): yield inferred.metaclass() or inferred.implicit_metaclass() - elif isinstance(inferred, (scoped_nodes.Lambda, bases.UnboundMethod)): + elif isinstance(inferred, (object_bases.Lambda, object_bases.UnboundMethod)): yield _function_type(inferred, builtins) - elif isinstance(inferred, scoped_nodes.Module): + elif isinstance(inferred, object_bases.Module): yield _build_proxy_class('module', builtins) else: yield inferred._proxied @@ -124,7 +122,7 @@ def has_known_bases(klass, context=None): for base in klass.bases: result = safe_infer(base, context=context) # TODO: check for A->B->A->B pattern in class structure too? - if (not isinstance(result, scoped_nodes.ClassDef) or + if (not isinstance(result, object_bases.ClassDef) or result is klass or not has_known_bases(result, context=context)): klass._all_bases_known = False @@ -168,11 +166,11 @@ def class_instance_as_index(node): try: for inferred in node.igetattr('__index__', context=context): - if not isinstance(inferred, bases.BoundMethod): + if not isinstance(inferred, object_bases.BoundMethod): continue for result in inferred.infer_call_result(node, context=context): - if (isinstance(result, nodes.Const) + if (isinstance(result, object_bases.Const) and isinstance(result.value, int)): return result except exceptions.InferenceError: diff --git a/astroid/node_classes.py b/astroid/node_classes.py index 7da41f596a..2ed6a7b3f6 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -29,6 +29,7 @@ import six from astroid import as_string +from astroid import object_bases from astroid import bases from astroid import context as contextmod from astroid import decorators @@ -130,6 +131,7 @@ def _container_getitem(instance, elts, index): +@object_bases.register_implementation(object_bases.NodeNG) class NodeNG(object): """Base Class for all Astroid node classes. @@ -577,6 +579,7 @@ def bool_value(self): return util.YES +@object_bases.register_implementation(object_bases.Statement) class Statement(NodeNG): """Statement node adding a few attributes""" is_statement = True @@ -598,7 +601,6 @@ def previous_sibling(self): return stmts[index -1] - @six.add_metaclass(abc.ABCMeta) class _BaseContainer(mixins.ParentAssignTypeMixin, NodeNG, bases.Instance): @@ -783,6 +785,7 @@ def _filter_stmts(self, stmts, frame, offset): # Name classes +@object_bases.register_implementation(object_bases.AssignName) class AssignName(LookupMixIn, mixins.ParentAssignTypeMixin, NodeNG): """class representing an AssignName node""" _other_fields = ('name',) @@ -792,6 +795,7 @@ def __init__(self, name=None, lineno=None, col_offset=None, parent=None): super(AssignName, self).__init__(lineno, col_offset, parent) +@object_bases.register_implementation(object_bases.DelName) class DelName(LookupMixIn, mixins.ParentAssignTypeMixin, NodeNG): """class representing a DelName node""" _other_fields = ('name',) @@ -801,6 +805,7 @@ def __init__(self, name=None, lineno=None, col_offset=None, parent=None): super(DelName, self).__init__(lineno, col_offset, parent) +@object_bases.register_implementation(object_bases.Name) class Name(LookupMixIn, NodeNG): """class representing a Name node""" _other_fields = ('name',) @@ -810,6 +815,7 @@ def __init__(self, name=None, lineno=None, col_offset=None, parent=None): super(Name, self).__init__(lineno, col_offset, parent) +@object_bases.register_implementation(object_bases.Arguments) class Arguments(mixins.AssignTypeMixin, NodeNG): """class representing an Arguments node""" if six.PY3: @@ -953,6 +959,7 @@ def _format_args(args, defaults=None, annotations=None): return ', '.join(values) +@object_bases.register_implementation(object_bases.AssignAttr) class AssignAttr(mixins.ParentAssignTypeMixin, NodeNG): """class representing an AssignAttr node""" _astroid_fields = ('expr',) @@ -967,6 +974,7 @@ def postinit(self, expr=None): self.expr = expr +@object_bases.register_implementation(object_bases.Assert) class Assert(Statement): """class representing an Assert node""" _astroid_fields = ('test', 'fail',) @@ -978,6 +986,7 @@ def postinit(self, test=None, fail=None): self.test = test +@object_bases.register_implementation(object_bases.Assign) class Assign(mixins.AssignTypeMixin, Statement): """class representing an Assign node""" _astroid_fields = ('targets', 'value',) @@ -989,6 +998,7 @@ def postinit(self, targets=None, value=None): self.value = value +@object_bases.register_implementation(object_bases.AugAssign) class AugAssign(mixins.AssignTypeMixin, Statement): """class representing an AugAssign node""" _astroid_fields = ('target', 'value') @@ -1022,6 +1032,7 @@ def type_errors(self, context=None): return [] +@object_bases.register_implementation(object_bases.Repr) class Repr(NodeNG): """class representing a Repr node""" _astroid_fields = ('value',) @@ -1031,6 +1042,7 @@ def postinit(self, value=None): self.value = value +@object_bases.register_implementation(object_bases.BinOp) class BinOp(NodeNG): """class representing a BinOp node""" _astroid_fields = ('left', 'right') @@ -1064,6 +1076,7 @@ def type_errors(self, context=None): return [] +@object_bases.register_implementation(object_bases.BoolOp) class BoolOp(NodeNG): """class representing a BoolOp node""" _astroid_fields = ('values',) @@ -1078,10 +1091,12 @@ def postinit(self, values=None): self.values = values +@object_bases.register_implementation(object_bases.Break) class Break(Statement): """class representing a Break node""" +@object_bases.register_implementation(object_bases.Call) class Call(NodeNG): """class representing a Call node""" _astroid_fields = ('func', 'args', 'keywords') @@ -1105,6 +1120,7 @@ def kwargs(self): return [keyword for keyword in keywords if keyword.arg is None] +@object_bases.register_implementation(object_bases.Compare) class Compare(NodeNG): """class representing a Compare node""" _astroid_fields = ('left', 'ops',) @@ -1128,6 +1144,7 @@ def last_child(self): #return self.left +@object_bases.register_implementation(object_bases.Comprehension) class Comprehension(NodeNG): """class representing a Comprehension node""" _astroid_fields = ('target', 'iter', 'ifs') @@ -1169,6 +1186,8 @@ def _get_filtered_stmts(self, lookup_node, node, stmts, mystmt): return stmts, False +@object_bases.register_implementation(object_bases.Const) +@object_bases.register_implementation(object_bases.Instance) class Const(NodeNG, bases.Instance): """represent a constant node like num, str, bool, None, bytes""" _other_fields = ('value',) @@ -1202,10 +1221,12 @@ def bool_value(self): return bool(self.value) +@object_bases.register_implementation(object_bases.Continue) class Continue(Statement): """class representing a Continue node""" +@object_bases.register_implementation(object_bases.Decorators) class Decorators(NodeNG): """class representing a Decorators node""" _astroid_fields = ('nodes',) @@ -1219,6 +1240,7 @@ def scope(self): return self.parent.parent.scope() +@object_bases.register_implementation(object_bases.DelAttr) class DelAttr(mixins.ParentAssignTypeMixin, NodeNG): """class representing a DelAttr node""" _astroid_fields = ('expr',) @@ -1233,6 +1255,7 @@ def postinit(self, expr=None): self.expr = expr +@object_bases.register_implementation(object_bases.Delete) class Delete(mixins.AssignTypeMixin, Statement): """class representing a Delete node""" _astroid_fields = ('targets',) @@ -1242,6 +1265,7 @@ def postinit(self, targets=None): self.targets = targets +@object_bases.register_implementation(object_bases.Dict) class Dict(NodeNG, bases.Instance): """class representing a Dict node""" _astroid_fields = ('items',) @@ -1304,6 +1328,7 @@ def bool_value(self): return bool(self.items) +@object_bases.register_implementation(object_bases.Expr) class Expr(Statement): """class representing a Expr node""" _astroid_fields = ('value',) @@ -1313,6 +1338,7 @@ def postinit(self, value=None): self.value = value +@object_bases.register_implementation(object_bases.Ellipsis) class Ellipsis(NodeNG): # pylint: disable=redefined-builtin """class representing an Ellipsis node""" @@ -1324,6 +1350,7 @@ class EmptyNode(NodeNG): """class representing an EmptyNode node""" +@object_bases.register_implementation(object_bases.ExceptHandler) class ExceptHandler(mixins.AssignTypeMixin, Statement): """class representing an ExceptHandler node""" _astroid_fields = ('type', 'name', 'body',) @@ -1353,6 +1380,7 @@ def catch(self, exceptions): return True +@object_bases.register_implementation(object_bases.Exec) class Exec(Statement): """class representing an Exec node""" _astroid_fields = ('expr', 'globals', 'locals',) @@ -1366,6 +1394,7 @@ def postinit(self, expr=None, globals=None, locals=None): self.locals = locals +@object_bases.register_implementation(object_bases.ExtSlice) class ExtSlice(NodeNG): """class representing an ExtSlice node""" _astroid_fields = ('dims',) @@ -1375,6 +1404,7 @@ def postinit(self, dims=None): self.dims = dims +@object_bases.register_implementation(object_bases.For) class For(mixins.BlockRangeMixIn, mixins.AssignTypeMixin, Statement): """class representing a For node""" _astroid_fields = ('target', 'iter', 'body', 'orelse',) @@ -1395,10 +1425,12 @@ def blockstart_tolineno(self): return self.iter.tolineno +@object_bases.register_implementation(object_bases.AsyncFor) class AsyncFor(For): """Asynchronous For built with `async` keyword.""" +@object_bases.register_implementation(object_bases.Await) class Await(NodeNG): """Await node for the `await` keyword.""" @@ -1409,6 +1441,7 @@ def postinit(self, value=None): self.value = value +@object_bases.register_implementation(object_bases.ImportFrom) class ImportFrom(mixins.ImportFromMixin, Statement): """class representing a ImportFrom node""" _other_fields = ('modname', 'names', 'level') @@ -1421,6 +1454,7 @@ def __init__(self, fromname, names, level=0, lineno=None, super(ImportFrom, self).__init__(lineno, col_offset, parent) +@object_bases.register_implementation(object_bases.Attribute) class Attribute(NodeNG): """class representing a Attribute node""" _astroid_fields = ('expr',) @@ -1435,6 +1469,7 @@ def postinit(self, expr=None): self.expr = expr +@object_bases.register_implementation(object_bases.Global) class Global(Statement): """class representing a Global node""" _other_fields = ('names',) @@ -1447,6 +1482,7 @@ def _infer_name(self, frame, name): return name +@object_bases.register_implementation(object_bases.If) class If(mixins.BlockRangeMixIn, Statement): """class representing an If node""" _astroid_fields = ('test', 'body', 'orelse') @@ -1473,6 +1509,7 @@ def block_range(self, lineno): self.body[0].fromlineno - 1) +@object_bases.register_implementation(object_bases.IfExp) class IfExp(NodeNG): """class representing an IfExp node""" _astroid_fields = ('test', 'body', 'orelse') @@ -1486,6 +1523,7 @@ def postinit(self, test=None, body=None, orelse=None): self.orelse = orelse +@object_bases.register_implementation(object_bases.Import) class Import(mixins.ImportFromMixin, Statement): """class representing an Import node""" _other_fields = ('names',) @@ -1495,6 +1533,7 @@ def __init__(self, names=None, lineno=None, col_offset=None, parent=None): super(Import, self).__init__(lineno, col_offset, parent) +@object_bases.register_implementation(object_bases.Index) class Index(NodeNG): """class representing an Index node""" _astroid_fields = ('value',) @@ -1504,6 +1543,7 @@ def postinit(self, value=None): self.value = value +@object_bases.register_implementation(object_bases.Keyword) class Keyword(NodeNG): """class representing a Keyword node""" _astroid_fields = ('value',) @@ -1518,6 +1558,7 @@ def postinit(self, value=None): self.value = value +@object_bases.register_implementation(object_bases.List) class List(_BaseContainer): """class representing a List node""" @@ -1528,6 +1569,7 @@ def getitem(self, index, context=None): return _container_getitem(self, self.elts, index) +@object_bases.register_implementation(object_bases.Nonlocal) class Nonlocal(Statement): """class representing a Nonlocal node""" _other_fields = ('names',) @@ -1540,10 +1582,12 @@ def _infer_name(self, frame, name): return name +@object_bases.register_implementation(object_bases.Pass) class Pass(Statement): """class representing a Pass node""" +@object_bases.register_implementation(object_bases.Print) class Print(Statement): """class representing a Print node""" _astroid_fields = ('dest', 'values',) @@ -1559,6 +1603,7 @@ def postinit(self, dest=None, values=None): self.values = values +@object_bases.register_implementation(object_bases.Raise) class Raise(Statement): """class representing a Raise node""" exc = None @@ -1588,6 +1633,7 @@ def raises_not_implemented(self): return True +@object_bases.register_implementation(object_bases.Return) class Return(Statement): """class representing a Return node""" _astroid_fields = ('value',) @@ -1597,6 +1643,7 @@ def postinit(self, value=None): self.value = value +@object_bases.register_implementation(object_bases.Set) class Set(_BaseContainer): """class representing a Set node""" @@ -1604,6 +1651,7 @@ def pytype(self): return '%s.set' % BUILTINS +@object_bases.register_implementation(object_bases.Slice) class Slice(NodeNG): """class representing a Slice node""" _astroid_fields = ('lower', 'upper', 'step') @@ -1647,6 +1695,7 @@ def getattr(self, attrname, context=None): return self._proxied.getattr(attrname, context) +@object_bases.register_implementation(object_bases.Starred) class Starred(mixins.ParentAssignTypeMixin, NodeNG): """class representing a Starred node""" _astroid_fields = ('value',) @@ -1656,6 +1705,7 @@ def postinit(self, value=None): self.value = value +@object_bases.register_implementation(object_bases.Subscript) class Subscript(NodeNG): """class representing a Subscript node""" _astroid_fields = ('value', 'slice') @@ -1667,6 +1717,7 @@ def postinit(self, value=None, slice=None): self.slice = slice +@object_bases.register_implementation(object_bases.TryExcept) class TryExcept(mixins.BlockRangeMixIn, Statement): """class representing a TryExcept node""" _astroid_fields = ('body', 'handlers', 'orelse',) @@ -1695,6 +1746,7 @@ def block_range(self, lineno): return self._elsed_block_range(lineno, self.orelse, last) +@object_bases.register_implementation(object_bases.TryFinally) class TryFinally(mixins.BlockRangeMixIn, Statement): """class representing a TryFinally node""" _astroid_fields = ('body', 'finalbody',) @@ -1715,6 +1767,7 @@ def block_range(self, lineno): return self._elsed_block_range(lineno, self.finalbody) +@object_bases.register_implementation(object_bases.Tuple) class Tuple(_BaseContainer): """class representing a Tuple node""" @@ -1725,6 +1778,7 @@ def getitem(self, index, context=None): return _container_getitem(self, self.elts, index) +@object_bases.register_implementation(object_bases.UnaryOp) class UnaryOp(NodeNG): """class representing an UnaryOp node""" _astroid_fields = ('operand',) @@ -1756,6 +1810,7 @@ def type_errors(self, context=None): return [] +@object_bases.register_implementation(object_bases.While) class While(mixins.BlockRangeMixIn, Statement): """class representing a While node""" _astroid_fields = ('test', 'body', 'orelse',) @@ -1777,6 +1832,7 @@ def block_range(self, lineno): return self. _elsed_block_range(lineno, self.orelse) +@object_bases.register_implementation(object_bases.With) class With(mixins.BlockRangeMixIn, mixins.AssignTypeMixin, Statement): """class representing a With node""" _astroid_fields = ('items', 'body') @@ -1800,10 +1856,12 @@ def get_children(self): yield elt +@object_bases.register_implementation(object_bases.AsyncWith) class AsyncWith(With): """Asynchronous `with` built with the `async` keyword.""" +@object_bases.register_implementation(object_bases.Yield) class Yield(NodeNG): """class representing a Yield node""" _astroid_fields = ('value',) @@ -1813,10 +1871,12 @@ def postinit(self, value=None): self.value = value +@object_bases.register_implementation(object_bases.YieldFrom) class YieldFrom(Yield): """ Class representing a YieldFrom node. """ +@object_bases.register_implementation(object_bases.DictUnpack) class DictUnpack(NodeNG): """Represents the unpacking of dicts into dicts using PEP 448.""" diff --git a/astroid/object_bases.py b/astroid/object_bases.py new file mode 100644 index 0000000000..107b69ade8 --- /dev/null +++ b/astroid/object_bases.py @@ -0,0 +1,334 @@ +# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. +# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr +# +# This file is part of astroid. +# +# astroid is free software: you can redistribute it and/or modify it +# under the terms of the GNU Lesser General Public License as published by the +# Free Software Foundation, either version 2.1 of the License, or (at your +# option) any later version. +# +# astroid is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License +# for more details. +# +# You should have received a copy of the GNU Lesser General Public License along +# with astroid. If not, see . + +import abc +import warnings + +import six + + +def register_implementation(base): + def wrapped(impl): + base.register(impl) + return impl + return wrapped + + +@six.add_metaclass(abc.ABCMeta) +class NodeNG(object): + """Base Class for all Astroid node classes. + + It represents a node of the new abstract syntax tree. + """ + is_statement = False + + def __init__(self, lineno=None, col_offset=None, parent=None): + self.lineno = lineno + self.col_offset = col_offset + self.parent = parent + + @abc.abstractmethod + def infer(self, context=None, **kwargs): + """Main interface to the interface system, return a generator on inferred values.""" + + +class Statement(NodeNG): + """Represents a Statement node.""" + is_statement = True + + +class AssignName(NodeNG): + """class representing an AssignName node""" + + +class DelName(NodeNG): + """class representing a DelName node""" + + +class Name(NodeNG): + """class representing a Name node""" + + +class Arguments(NodeNG): + """class representing an Arguments node""" + + +class AssignAttr(NodeNG): + """class representing an AssignAttr node""" + + +class Assert(Statement): + """class representing an Assert node""" + + +class Assign(Statement): + """class representing an Assign node""" + + +class AugAssign(Statement): + """class representing an AugAssign node""" + + +class Repr(NodeNG): + """class representing a Repr node""" + + +class BinOp(NodeNG): + """class representing a BinOp node""" + + +class BoolOp(NodeNG): + """class representing a BoolOp node""" + + +class Break(Statement): + """class representing a Break node""" + + +class Call(NodeNG): + """class representing a Call node""" + + +class Compare(NodeNG): + """class representing a Compare node""" + + +class Comprehension(NodeNG): + """class representing a Comprehension node""" + + +class Const(NodeNG): + """represent a constant node like num, str, bool, None, bytes""" + + +class Continue(Statement): + """class representing a Continue node""" + + +class Decorators(NodeNG): + """class representing a Decorators node""" + + +class DelAttr(NodeNG): + """class representing a DelAttr node""" + + +class Delete(Statement): + """class representing a Delete node""" + + +class Dict(NodeNG): + """class representing a Dict node""" + + +class Expr(Statement): + """class representing a Expr node""" + + +class Ellipsis(NodeNG): # pylint: disable=redefined-builtin + """class representing an Ellipsis node""" + + +class ExceptHandler(Statement): + """class representing an ExceptHandler node""" + + +class Exec(Statement): + """class representing an Exec node""" + + +class ExtSlice(NodeNG): + """class representing an ExtSlice node""" + + +class For(Statement): + """class representing a For node""" + + +class AsyncFor(For): + """Asynchronous For built with `async` keyword.""" + + +class Await(NodeNG): + """Await node for the `await` keyword.""" + + +class ImportFrom(Statement): + """class representing a ImportFrom node""" + + +class Attribute(NodeNG): + """class representing a Attribute node""" + + +class Global(Statement): + """class representing a Global node""" + + +class If(Statement): + """class representing an If node""" + + +class IfExp(NodeNG): + """class representing an IfExp node""" + + +class Import(Statement): + """class representing an Import node""" + + +class Index(NodeNG): + """class representing an Index node""" + + +class Keyword(NodeNG): + """class representing a Keyword node""" + + +class List(NodeNG): + """class representing a List node""" + + +class Nonlocal(Statement): + """class representing a Nonlocal node""" + +class Pass(Statement): + """class representing a Pass node""" + + +class Print(Statement): + """class representing a Print node""" + + +class Raise(Statement): + """class representing a Raise node""" + + +class Return(Statement): + """class representing a Return node""" + + +class Set(NodeNG): + """class representing a Set node""" + + +class Slice(NodeNG): + """class representing a Slice node""" + + +class Starred(NodeNG): + """class representing a Starred node""" + + +class Subscript(NodeNG): + """class representing a Subscript node""" + + +class TryExcept(Statement): + """class representing a TryExcept node""" + + +class TryFinally(Statement): + """class representing a TryFinally node""" + + +class Tuple(NodeNG): + """class representing a Tuple node""" + +class UnaryOp(NodeNG): + """class representing an UnaryOp node""" + + +class While(Statement): + """class representing a While node""" + +class With(Statement): + """class representing a With node""" + + +class AsyncWith(With): + """Asynchronous `with` built with the `async` keyword.""" + + +class Yield(NodeNG): + """class representing a Yield node""" + + +class YieldFrom(Yield): + """Class representing a YieldFrom node. """ + + +class DictUnpack(NodeNG): + """Represents the unpacking of dicts into dicts using PEP 448.""" + + +class Module(NodeNG): + pass + + +class GeneratorExp(NodeNG): + pass + + +class DictComp(NodeNG): + pass + + +class SetComp(NodeNG): + pass + + +class ListComp(NodeNG): + pass + + +class Lambda(NodeNG): + pass + + +class AsyncFunctionDef(NodeNG): + pass + + +class ClassDef(Statement, NodeNG): + pass + + +class FunctionDef(Statement, Lambda): + pass + + +@six.add_metaclass(abc.ABCMeta) +class RuntimeObject(object): + pass + + +class Instance(RuntimeObject): + pass + + +class UnboundMethod(RuntimeObject): + pass + + +class BoundMethod(UnboundMethod): + pass + + +class Generator(RuntimeObject): + pass diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index e6e3324aef..023a91aa02 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -29,6 +29,7 @@ import six import wrapt +from astroid import object_bases from astroid import bases from astroid import context as contextmod from astroid import exceptions @@ -247,6 +248,7 @@ def __contains__(self, name): return name in self.locals +@object_bases.register_implementation(object_bases.Module) class Module(LocalsDictNodeNG): _astroid_fields = ('body',) @@ -511,6 +513,7 @@ def frame(self): scope_lookup = LocalsDictNodeNG._scope_lookup +@object_bases.register_implementation(object_bases.GeneratorExp) class GeneratorExp(ComprehensionScope): _astroid_fields = ('elt', 'generators') _other_other_fields = ('locals',) @@ -532,6 +535,7 @@ def bool_value(self): return True +@object_bases.register_implementation(object_bases.DictComp) class DictComp(ComprehensionScope): _astroid_fields = ('key', 'value', 'generators') _other_other_fields = ('locals',) @@ -555,6 +559,7 @@ def bool_value(self): return util.YES +@object_bases.register_implementation(object_bases.SetComp) class SetComp(ComprehensionScope): _astroid_fields = ('elt', 'generators') _other_other_fields = ('locals',) @@ -576,6 +581,7 @@ def bool_value(self): return util.YES +@object_bases.register_implementation(object_bases.ListComp) class _ListComp(node_classes.NodeNG): """class representing a ListComp node""" _astroid_fields = ('elt', 'generators') @@ -627,6 +633,7 @@ def _infer_decorator_callchain(node): return 'staticmethod' +@object_bases.register_implementation(object_bases.Lambda) class Lambda(mixins.FilterStmtsMixin, LocalsDictNodeNG): _astroid_fields = ('args', 'body',) _other_other_fields = ('locals',) @@ -689,6 +696,7 @@ def bool_value(self): return True +@object_bases.register_implementation(object_bases.FunctionDef) class FunctionDef(node_classes.Statement, Lambda): if six.PY3: _astroid_fields = ('decorators', 'args', 'body', 'returns') @@ -954,6 +962,7 @@ def bool_value(self): return True +@object_bases.register_implementation(object_bases.AsyncFunctionDef) class AsyncFunctionDef(FunctionDef): """Asynchronous function created with the `async` keyword.""" @@ -1055,6 +1064,7 @@ def get_wrapping_class(node): +@object_bases.register_implementation(object_bases.ClassDef) class ClassDef(mixins.FilterStmtsMixin, LocalsDictNodeNG, node_classes.Statement): From b976aea2445a1c08197a340748bbabf8907f4a62 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sat, 31 Oct 2015 00:32:28 +0200 Subject: [PATCH 033/312] Add docstrings for the object base classes. --- astroid/object_bases.py | 156 ++++++++++++++++++++++------------------ 1 file changed, 88 insertions(+), 68 deletions(-) diff --git a/astroid/object_bases.py b/astroid/object_bases.py index 107b69ade8..0d930a8c4a 100644 --- a/astroid/object_bases.py +++ b/astroid/object_bases.py @@ -16,13 +16,25 @@ # You should have received a copy of the GNU Lesser General Public License along # with astroid. If not, see . +"""Abstract classes for nodes and other runtime objects. + +The idea is that these objects are used for isinstance checks and +any other use cases where the need for a concrete type does not exist, +while other cases, such as instantiating a node, should use the concrete +types instead. +""" + import abc -import warnings import six def register_implementation(base): + """Register an implementation for the given *base* + + The given base class is expected to have a `register` method, + similar to what `abc.ABCMeta` provides when used. + """ def wrapped(impl): base.register(impl) return impl @@ -36,7 +48,7 @@ class NodeNG(object): It represents a node of the new abstract syntax tree. """ is_statement = False - + def __init__(self, lineno=None, col_offset=None, parent=None): self.lineno = lineno self.col_offset = col_offset @@ -53,111 +65,111 @@ class Statement(NodeNG): class AssignName(NodeNG): - """class representing an AssignName node""" + """Class representing an AssignName node""" class DelName(NodeNG): - """class representing a DelName node""" + """Class representing a DelName node""" class Name(NodeNG): - """class representing a Name node""" + """Class representing a Name node""" class Arguments(NodeNG): - """class representing an Arguments node""" + """Class representing an Arguments node""" class AssignAttr(NodeNG): - """class representing an AssignAttr node""" + """Class representing an AssignAttr node""" class Assert(Statement): - """class representing an Assert node""" + """Class representing an Assert node""" class Assign(Statement): - """class representing an Assign node""" + """Class representing an Assign node""" class AugAssign(Statement): - """class representing an AugAssign node""" + """Class representing an AugAssign node""" class Repr(NodeNG): - """class representing a Repr node""" + """Class representing a Repr node""" class BinOp(NodeNG): - """class representing a BinOp node""" + """Class representing a BinOp node""" class BoolOp(NodeNG): - """class representing a BoolOp node""" + """Class representing a BoolOp node""" class Break(Statement): - """class representing a Break node""" + """Class representing a Break node""" class Call(NodeNG): - """class representing a Call node""" + """Class representing a Call node""" class Compare(NodeNG): - """class representing a Compare node""" + """Class representing a Compare node""" class Comprehension(NodeNG): - """class representing a Comprehension node""" + """Class representing a Comprehension node""" class Const(NodeNG): - """represent a constant node like num, str, bool, None, bytes""" + """Represent a constant node like num, str, bool, None, bytes""" class Continue(Statement): - """class representing a Continue node""" + """Class representing a Continue node""" class Decorators(NodeNG): - """class representing a Decorators node""" + """Class representing a Decorators node""" class DelAttr(NodeNG): - """class representing a DelAttr node""" + """Class representing a DelAttr node""" class Delete(Statement): - """class representing a Delete node""" + """Class representing a Delete node""" class Dict(NodeNG): - """class representing a Dict node""" + """Class representing a Dict node""" class Expr(Statement): - """class representing a Expr node""" + """Class representing a Expr node""" class Ellipsis(NodeNG): # pylint: disable=redefined-builtin - """class representing an Ellipsis node""" + """Class representing an Ellipsis node""" class ExceptHandler(Statement): - """class representing an ExceptHandler node""" + """Class representing an ExceptHandler node""" class Exec(Statement): - """class representing an Exec node""" + """Class representing an Exec node""" class ExtSlice(NodeNG): - """class representing an ExtSlice node""" + """Class representing an ExtSlice node""" class For(Statement): - """class representing a For node""" + """Class representing a For node""" class AsyncFor(For): @@ -169,96 +181,99 @@ class Await(NodeNG): class ImportFrom(Statement): - """class representing a ImportFrom node""" + """Class representing a ImportFrom node""" class Attribute(NodeNG): - """class representing a Attribute node""" + """Class representing a Attribute node""" class Global(Statement): - """class representing a Global node""" + """Class representing a Global node""" class If(Statement): - """class representing an If node""" + """Class representing an If node""" class IfExp(NodeNG): - """class representing an IfExp node""" + """Class representing an IfExp node""" class Import(Statement): - """class representing an Import node""" + """Class representing an Import node""" class Index(NodeNG): - """class representing an Index node""" + """Class representing an Index node""" class Keyword(NodeNG): - """class representing a Keyword node""" + """Class representing a Keyword node""" class List(NodeNG): - """class representing a List node""" + """Class representing a List node""" class Nonlocal(Statement): - """class representing a Nonlocal node""" + """Class representing a Nonlocal node""" + class Pass(Statement): - """class representing a Pass node""" + """Class representing a Pass node""" class Print(Statement): - """class representing a Print node""" + """Class representing a Print node""" class Raise(Statement): - """class representing a Raise node""" + """Class representing a Raise node""" class Return(Statement): - """class representing a Return node""" + """Class representing a Return node""" class Set(NodeNG): - """class representing a Set node""" + """Class representing a Set node""" class Slice(NodeNG): - """class representing a Slice node""" + """Class representing a Slice node""" class Starred(NodeNG): - """class representing a Starred node""" + """Class representing a Starred node""" class Subscript(NodeNG): - """class representing a Subscript node""" + """Class representing a Subscript node""" class TryExcept(Statement): - """class representing a TryExcept node""" + """Class representing a TryExcept node""" class TryFinally(Statement): - """class representing a TryFinally node""" + """Class representing a TryFinally node""" class Tuple(NodeNG): - """class representing a Tuple node""" + """Class representing a Tuple node""" + class UnaryOp(NodeNG): - """class representing an UnaryOp node""" + """Class representing an UnaryOp node""" class While(Statement): - """class representing a While node""" + """Class representing a While node""" + class With(Statement): - """class representing a With node""" + """Class representing a With node""" class AsyncWith(With): @@ -266,7 +281,7 @@ class AsyncWith(With): class Yield(NodeNG): - """class representing a Yield node""" + """Class representing a Yield node""" class YieldFrom(Yield): @@ -278,57 +293,62 @@ class DictUnpack(NodeNG): class Module(NodeNG): - pass + """Class representing a Module node.""" class GeneratorExp(NodeNG): - pass + """Class representing a GeneratorExp node.""" class DictComp(NodeNG): - pass + """Class representing a generator comprehension node.""" class SetComp(NodeNG): - pass + """Class representing a set comprehension node.""" class ListComp(NodeNG): - pass + """Class representing a list comprehension node.""" class Lambda(NodeNG): - pass + """Class representing a lambda comprehension node.""" class AsyncFunctionDef(NodeNG): - pass + """Class representing an asynchronous function node.""" class ClassDef(Statement, NodeNG): - pass + """Class representing a class definition node.""" class FunctionDef(Statement, Lambda): - pass + """Class representing a function node.""" @six.add_metaclass(abc.ABCMeta) class RuntimeObject(object): - pass + """Class representing an object that is created at runtime + + These objects aren't AST per se, being created by the action + of the interpreter when the code executes, which is a total + different step than the AST creation. + """ class Instance(RuntimeObject): - pass + """Class representing an instance.""" class UnboundMethod(RuntimeObject): - pass + """Class representing an unbound method.""" class BoundMethod(UnboundMethod): - pass + """Class representing a bound method.""" class Generator(RuntimeObject): - pass + """Class representing a Generator.""" From f12ee11c2a85b2119ac37e726c46b71ba8d3c750 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sat, 31 Oct 2015 00:40:30 +0200 Subject: [PATCH 034/312] Add base type for EmptyNode and add tests for verifying that base types are superclasses of the concrete ASTs. --- astroid/node_classes.py | 1 + astroid/object_bases.py | 4 ++++ astroid/tests/unittest_nodes.py | 15 +++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/astroid/node_classes.py b/astroid/node_classes.py index 2ed6a7b3f6..8f5d1f72cb 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -1346,6 +1346,7 @@ def bool_value(self): return True +@object_bases.register_implementation(object_bases.EmptyNode) class EmptyNode(NodeNG): """class representing an EmptyNode node""" diff --git a/astroid/object_bases.py b/astroid/object_bases.py index 0d930a8c4a..34232ce6b2 100644 --- a/astroid/object_bases.py +++ b/astroid/object_bases.py @@ -352,3 +352,7 @@ class BoundMethod(UnboundMethod): class Generator(RuntimeObject): """Class representing a Generator.""" + + +class EmptyNode(NodeNG): + pass diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index ade8a92020..09a62c5326 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -25,6 +25,7 @@ import six +from astroid import object_bases from astroid import bases from astroid import builder from astroid import context as contextmod @@ -747,5 +748,19 @@ async def function(): self._test_await_async_as_string(code) +class BaseTypesTest(unittest.TestCase): + + def test_concrete_issubclass(self): + for node in nodes.ALL_NODE_CLASSES: + name = node.__name__ + base_type = getattr(object_bases, name) + self.assertTrue(issubclass(node, base_type), (node, base_type)) + + self.assertTrue(issubclass(bases.Instance, object_bases.Instance)) + self.assertTrue(issubclass(bases.Generator, object_bases.Generator)) + self.assertTrue(issubclass(bases.BoundMethod, object_bases.BoundMethod)) + self.assertTrue(issubclass(bases.UnboundMethod, object_bases.UnboundMethod)) + + if __name__ == '__main__': unittest.main() From cb2507358ac3aa9eb71750febdc05ef4897c696f Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sat, 31 Oct 2015 01:47:49 +0200 Subject: [PATCH 035/312] Check against the base types, not concrete types. --- astroid/objects.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/astroid/objects.py b/astroid/objects.py index 3ab0a65f26..ea26ce1c26 100644 --- a/astroid/objects.py +++ b/astroid/objects.py @@ -27,6 +27,7 @@ import six +from astroid import object_bases from astroid import bases from astroid import decorators from astroid import exceptions @@ -85,17 +86,17 @@ def _infer(self, context=None): def super_mro(self): """Get the MRO which will be used to lookup attributes in this super.""" - if not isinstance(self.mro_pointer, scoped_nodes.ClassDef): + if not isinstance(self.mro_pointer, object_bases.ClassDef): raise exceptions.SuperArgumentTypeError( "The first super argument must be type.") - if isinstance(self.type, scoped_nodes.ClassDef): + if isinstance(self.type, object_bases.ClassDef): # `super(type, type)`, most likely in a class method. self._class_based = True mro_type = self.type else: mro_type = getattr(self.type, '_proxied', None) - if not isinstance(mro_type, (bases.Instance, scoped_nodes.ClassDef)): + if not isinstance(mro_type, (bases.Instance, object_bases.ClassDef)): raise exceptions.SuperArgumentTypeError( "super(type, obj): obj must be an " "instance or subtype of type") @@ -150,7 +151,7 @@ def igetattr(self, name, context=None): found = True for inferred in bases._infer_stmts([cls[name]], context, frame=self): - if not isinstance(inferred, scoped_nodes.FunctionDef): + if not isinstance(inferred, object_bases.FunctionDef): yield inferred continue From fe07be9ef3be48fc047ef1425a39ed8fdce389ee Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sat, 31 Oct 2015 21:57:23 +0200 Subject: [PATCH 036/312] Return type if a class doesn't define a metaclass when checking the object type, since implicit_metaclass() can return None for old style classes. --- astroid/helpers.py | 2 +- astroid/tests/unittest_helpers.py | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/astroid/helpers.py b/astroid/helpers.py index 3a393b6411..06c651cad6 100644 --- a/astroid/helpers.py +++ b/astroid/helpers.py @@ -65,7 +65,7 @@ def _object_type(node, context=None): for inferred in node.infer(context=context): if isinstance(inferred, object_bases.ClassDef): - yield inferred.metaclass() or inferred.implicit_metaclass() + yield inferred.metaclass() or builtins.getattr('type')[0] elif isinstance(inferred, (object_bases.Lambda, object_bases.UnboundMethod)): yield _function_type(inferred, builtins) elif isinstance(inferred, object_bases.Module): diff --git a/astroid/tests/unittest_helpers.py b/astroid/tests/unittest_helpers.py index 62259667b0..16307e1944 100644 --- a/astroid/tests/unittest_helpers.py +++ b/astroid/tests/unittest_helpers.py @@ -257,6 +257,12 @@ class A(type): #@ self.assertTrue(helpers.is_supertype(builtin_type, cls_a)) self.assertTrue(helpers.is_subtype(cls_a, builtin_type)) + @test_utils.require_version(maxver='3.0') + def test_old_style_class(self): + cls = test_utils.extract_node('''class A: pass''') + builtin_type = self._extract('type') + self.assertEqual(helpers.object_type(cls), builtin_type) + if __name__ == '__main__': unittest.main() From 4a87bb4f0d76e9a3cad6e964a1990c29d11b9149 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sat, 31 Oct 2015 22:18:05 +0200 Subject: [PATCH 037/312] Use the virtual base class for isinstance. --- astroid/objects.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astroid/objects.py b/astroid/objects.py index ea26ce1c26..c43f2f914e 100644 --- a/astroid/objects.py +++ b/astroid/objects.py @@ -96,7 +96,7 @@ def super_mro(self): mro_type = self.type else: mro_type = getattr(self.type, '_proxied', None) - if not isinstance(mro_type, (bases.Instance, object_bases.ClassDef)): + if not isinstance(mro_type, (object_bases.Instance, object_bases.ClassDef)): raise exceptions.SuperArgumentTypeError( "super(type, obj): obj must be an " "instance or subtype of type") From 23c54fd64149b03d94c0ed6ccb7ce21fb620820d Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sat, 31 Oct 2015 22:18:54 +0200 Subject: [PATCH 038/312] Remove unused import. --- astroid/objects.py | 1 - 1 file changed, 1 deletion(-) diff --git a/astroid/objects.py b/astroid/objects.py index c43f2f914e..cd62f59b6b 100644 --- a/astroid/objects.py +++ b/astroid/objects.py @@ -33,7 +33,6 @@ from astroid import exceptions from astroid import MANAGER from astroid import node_classes -from astroid import scoped_nodes from astroid import util From c58048979bccf7f2c359968f54b77cbcf3679662 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 1 Nov 2015 13:19:04 +0200 Subject: [PATCH 039/312] Start splitting AST from runtime objects There are two new directories now, runtime and tree, which are meant to separate between runtime objects, such as Instance, BoundMethod etc from AST nodes. --- astroid/bases.py | 13 +- astroid/helpers.py | 21 +- astroid/node_classes.py | 637 +++---------------- astroid/objects.py | 14 +- astroid/runtime/__init__.py | 0 astroid/runtime/runtimeabc.py | 29 + astroid/scoped_nodes.py | 25 +- astroid/tests/unittest_nodes.py | 13 +- astroid/tree/__init__.py | 0 astroid/tree/base.py | 480 ++++++++++++++ astroid/{object_bases.py => tree/treeabc.py} | 39 +- astroid/util.py | 12 + 12 files changed, 664 insertions(+), 619 deletions(-) create mode 100644 astroid/runtime/__init__.py create mode 100644 astroid/runtime/runtimeabc.py create mode 100644 astroid/tree/__init__.py create mode 100644 astroid/tree/base.py rename astroid/{object_bases.py => tree/treeabc.py} (87%) diff --git a/astroid/bases.py b/astroid/bases.py index 33f38760d7..378fbea123 100644 --- a/astroid/bases.py +++ b/astroid/bases.py @@ -23,8 +23,9 @@ from astroid import context as contextmod from astroid import exceptions -from astroid import object_bases +from astroid.runtime import runtimeabc from astroid import util +from astroid.tree import treeabc if sys.version_info >= (3, 0): @@ -123,7 +124,7 @@ def _infer_method_result_truth(instance, method_name, context): return util.YES -@object_bases.register_implementation(object_bases.Instance) +@util.register_implementation(runtimeabc.Instance) class Instance(Proxy): """A special node representing a class instance.""" @@ -190,7 +191,7 @@ def _wrap_attr(self, attrs, context=None): # Unfortunately, we can't do an isinstance check here, # because of the circular dependency between astroid.bases # and astroid.scoped_nodes. - if isinstance(attr.statement().scope(), object_bases.ClassDef): + if isinstance(attr.statement().scope(), treeabc.ClassDef): if attr.args.args and attr.args.args[0].name == 'self': yield BoundMethod(attr, self) continue @@ -277,7 +278,7 @@ def getitem(self, index, context=None): util.reraise(exceptions.InferenceError()) -@object_bases.register_implementation(object_bases.UnboundMethod) +@util.register_implementation(runtimeabc.UnboundMethod) class UnboundMethod(Proxy): """a special node representing a method not bound to an instance""" def __repr__(self): @@ -312,7 +313,7 @@ def bool_value(self): return True -@object_bases.register_implementation(object_bases.BoundMethod) +@util.register_implementation(runtimeabc.BoundMethod) class BoundMethod(UnboundMethod): """a special node representing a method bound to an instance""" def __init__(self, proxy, bound): @@ -333,7 +334,7 @@ def bool_value(self): return True -@object_bases.register_implementation(object_bases.Generator) +@util.register_implementation(runtimeabc.Generator) class Generator(Instance): """a special node representing a generator. diff --git a/astroid/helpers.py b/astroid/helpers.py index 06c651cad6..ab64594dac 100644 --- a/astroid/helpers.py +++ b/astroid/helpers.py @@ -22,7 +22,8 @@ import six -from astroid import object_bases +from astroid.tree import treeabc +from astroid.runtime import runtimeabc from astroid import context as contextmod from astroid import exceptions from astroid import manager @@ -40,17 +41,17 @@ def _build_proxy_class(cls_name, builtins): def _function_type(function, builtins): - if isinstance(function, object_bases.Lambda): + if isinstance(function, treeabc.Lambda): if function.root().name == BUILTINS: cls_name = 'builtin_function_or_method' else: cls_name = 'function' - elif isinstance(function, object_bases.BoundMethod): + elif isinstance(function, runtimeabc.BoundMethod): if six.PY2: cls_name = 'instancemethod' else: cls_name = 'method' - elif isinstance(function, object_bases.UnboundMethod): + elif isinstance(function, runtimeabc.UnboundMethod): if six.PY2: cls_name = 'instancemethod' else: @@ -64,11 +65,11 @@ def _object_type(node, context=None): context = context or contextmod.InferenceContext() for inferred in node.infer(context=context): - if isinstance(inferred, object_bases.ClassDef): + if isinstance(inferred, treeabc.ClassDef): yield inferred.metaclass() or builtins.getattr('type')[0] - elif isinstance(inferred, (object_bases.Lambda, object_bases.UnboundMethod)): + elif isinstance(inferred, (treeabc.Lambda, runtimeabc.UnboundMethod)): yield _function_type(inferred, builtins) - elif isinstance(inferred, object_bases.Module): + elif isinstance(inferred, treeabc.Module): yield _build_proxy_class('module', builtins) else: yield inferred._proxied @@ -122,7 +123,7 @@ def has_known_bases(klass, context=None): for base in klass.bases: result = safe_infer(base, context=context) # TODO: check for A->B->A->B pattern in class structure too? - if (not isinstance(result, object_bases.ClassDef) or + if (not isinstance(result, treeabc.ClassDef) or result is klass or not has_known_bases(result, context=context)): klass._all_bases_known = False @@ -166,11 +167,11 @@ def class_instance_as_index(node): try: for inferred in node.igetattr('__index__', context=context): - if not isinstance(inferred, object_bases.BoundMethod): + if not isinstance(inferred, runtimeabc.BoundMethod): continue for result in inferred.infer_call_result(node, context=context): - if (isinstance(result, object_bases.Const) + if (isinstance(result, treeabc.Const) and isinstance(result.value, int)): return result except exceptions.InferenceError: diff --git a/astroid/node_classes.py b/astroid/node_classes.py index 8f5d1f72cb..db9d3eafc8 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -28,8 +28,10 @@ import six +from astroid.tree import base +from astroid.tree import treeabc +from astroid.runtime import runtimeabc from astroid import as_string -from astroid import object_bases from astroid import bases from astroid import context as contextmod from astroid import decorators @@ -130,457 +132,8 @@ def _container_getitem(instance, elts, index): return elts[index] - -@object_bases.register_implementation(object_bases.NodeNG) -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] - else: - 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() - else: - 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 - else: - return lastchild.tolineno - - # TODO / FIXME: - assert self.fromlineno is not None, self - assert self.tolineno is not None, self - - 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(self.__class__.__name__) - - 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 instanciate_class(self): - """instanciate 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 len(node) == 0: - 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 + ' 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 len(fields) == 0: - 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 - * YES: the inference engine is uncertain of the - node's value. - """ - return util.YES - - -@object_bases.register_implementation(object_bases.Statement) -class Statement(NodeNG): +@util.register_implementation(treeabc.Statement) +class Statement(base.NodeNG): """Statement node adding a few attributes""" is_statement = True @@ -603,7 +156,7 @@ def previous_sibling(self): @six.add_metaclass(abc.ABCMeta) class _BaseContainer(mixins.ParentAssignTypeMixin, - NodeNG, bases.Instance): + base.NodeNG, bases.Instance): """Base class for Set, FrozenSet, Tuple and List.""" _astroid_fields = ('elts',) @@ -785,8 +338,8 @@ def _filter_stmts(self, stmts, frame, offset): # Name classes -@object_bases.register_implementation(object_bases.AssignName) -class AssignName(LookupMixIn, mixins.ParentAssignTypeMixin, NodeNG): +@util.register_implementation(treeabc.AssignName) +class AssignName(LookupMixIn, mixins.ParentAssignTypeMixin, base.NodeNG): """class representing an AssignName node""" _other_fields = ('name',) @@ -795,8 +348,8 @@ def __init__(self, name=None, lineno=None, col_offset=None, parent=None): super(AssignName, self).__init__(lineno, col_offset, parent) -@object_bases.register_implementation(object_bases.DelName) -class DelName(LookupMixIn, mixins.ParentAssignTypeMixin, NodeNG): +@util.register_implementation(treeabc.DelName) +class DelName(LookupMixIn, mixins.ParentAssignTypeMixin, base.NodeNG): """class representing a DelName node""" _other_fields = ('name',) @@ -805,8 +358,8 @@ def __init__(self, name=None, lineno=None, col_offset=None, parent=None): super(DelName, self).__init__(lineno, col_offset, parent) -@object_bases.register_implementation(object_bases.Name) -class Name(LookupMixIn, NodeNG): +@util.register_implementation(treeabc.Name) +class Name(LookupMixIn, base.NodeNG): """class representing a Name node""" _other_fields = ('name',) @@ -815,8 +368,8 @@ def __init__(self, name=None, lineno=None, col_offset=None, parent=None): super(Name, self).__init__(lineno, col_offset, parent) -@object_bases.register_implementation(object_bases.Arguments) -class Arguments(mixins.AssignTypeMixin, NodeNG): +@util.register_implementation(treeabc.Arguments) +class Arguments(mixins.AssignTypeMixin, base.NodeNG): """class representing an Arguments node""" if six.PY3: # Python 3.4+ uses a different approach regarding annotations, @@ -959,8 +512,8 @@ def _format_args(args, defaults=None, annotations=None): return ', '.join(values) -@object_bases.register_implementation(object_bases.AssignAttr) -class AssignAttr(mixins.ParentAssignTypeMixin, NodeNG): +@util.register_implementation(treeabc.AssignAttr) +class AssignAttr(mixins.ParentAssignTypeMixin, base.NodeNG): """class representing an AssignAttr node""" _astroid_fields = ('expr',) _other_fields = ('attrname',) @@ -974,7 +527,7 @@ def postinit(self, expr=None): self.expr = expr -@object_bases.register_implementation(object_bases.Assert) +@util.register_implementation(treeabc.Assert) class Assert(Statement): """class representing an Assert node""" _astroid_fields = ('test', 'fail',) @@ -986,7 +539,7 @@ def postinit(self, test=None, fail=None): self.test = test -@object_bases.register_implementation(object_bases.Assign) +@util.register_implementation(treeabc.Assign) class Assign(mixins.AssignTypeMixin, Statement): """class representing an Assign node""" _astroid_fields = ('targets', 'value',) @@ -998,7 +551,7 @@ def postinit(self, targets=None, value=None): self.value = value -@object_bases.register_implementation(object_bases.AugAssign) +@util.register_implementation(treeabc.AugAssign) class AugAssign(mixins.AssignTypeMixin, Statement): """class representing an AugAssign node""" _astroid_fields = ('target', 'value') @@ -1032,8 +585,8 @@ def type_errors(self, context=None): return [] -@object_bases.register_implementation(object_bases.Repr) -class Repr(NodeNG): +@util.register_implementation(treeabc.Repr) +class Repr(base.NodeNG): """class representing a Repr node""" _astroid_fields = ('value',) value = None @@ -1042,8 +595,8 @@ def postinit(self, value=None): self.value = value -@object_bases.register_implementation(object_bases.BinOp) -class BinOp(NodeNG): +@util.register_implementation(treeabc.BinOp) +class BinOp(base.NodeNG): """class representing a BinOp node""" _astroid_fields = ('left', 'right') _other_fields = ('op',) @@ -1076,8 +629,8 @@ def type_errors(self, context=None): return [] -@object_bases.register_implementation(object_bases.BoolOp) -class BoolOp(NodeNG): +@util.register_implementation(treeabc.BoolOp) +class BoolOp(base.NodeNG): """class representing a BoolOp node""" _astroid_fields = ('values',) _other_fields = ('op',) @@ -1091,13 +644,13 @@ def postinit(self, values=None): self.values = values -@object_bases.register_implementation(object_bases.Break) +@util.register_implementation(treeabc.Break) class Break(Statement): """class representing a Break node""" -@object_bases.register_implementation(object_bases.Call) -class Call(NodeNG): +@util.register_implementation(treeabc.Call) +class Call(base.NodeNG): """class representing a Call node""" _astroid_fields = ('func', 'args', 'keywords') func = None @@ -1120,8 +673,8 @@ def kwargs(self): return [keyword for keyword in keywords if keyword.arg is None] -@object_bases.register_implementation(object_bases.Compare) -class Compare(NodeNG): +@util.register_implementation(treeabc.Compare) +class Compare(base.NodeNG): """class representing a Compare node""" _astroid_fields = ('left', 'ops',) left = None @@ -1144,8 +697,8 @@ def last_child(self): #return self.left -@object_bases.register_implementation(object_bases.Comprehension) -class Comprehension(NodeNG): +@util.register_implementation(treeabc.Comprehension) +class Comprehension(base.NodeNG): """class representing a Comprehension node""" _astroid_fields = ('target', 'iter', 'ifs') target = None @@ -1186,9 +739,9 @@ def _get_filtered_stmts(self, lookup_node, node, stmts, mystmt): return stmts, False -@object_bases.register_implementation(object_bases.Const) -@object_bases.register_implementation(object_bases.Instance) -class Const(NodeNG, bases.Instance): +@util.register_implementation(treeabc.Const) +@util.register_implementation(runtimeabc.Instance) +class Const(base.NodeNG, bases.Instance): """represent a constant node like num, str, bool, None, bytes""" _other_fields = ('value',) @@ -1221,13 +774,13 @@ def bool_value(self): return bool(self.value) -@object_bases.register_implementation(object_bases.Continue) +@util.register_implementation(treeabc.Continue) class Continue(Statement): """class representing a Continue node""" -@object_bases.register_implementation(object_bases.Decorators) -class Decorators(NodeNG): +@util.register_implementation(treeabc.Decorators) +class Decorators(base.NodeNG): """class representing a Decorators node""" _astroid_fields = ('nodes',) nodes = None @@ -1240,8 +793,8 @@ def scope(self): return self.parent.parent.scope() -@object_bases.register_implementation(object_bases.DelAttr) -class DelAttr(mixins.ParentAssignTypeMixin, NodeNG): +@util.register_implementation(treeabc.DelAttr) +class DelAttr(mixins.ParentAssignTypeMixin, base.NodeNG): """class representing a DelAttr node""" _astroid_fields = ('expr',) _other_fields = ('attrname',) @@ -1255,7 +808,7 @@ def postinit(self, expr=None): self.expr = expr -@object_bases.register_implementation(object_bases.Delete) +@util.register_implementation(treeabc.Delete) class Delete(mixins.AssignTypeMixin, Statement): """class representing a Delete node""" _astroid_fields = ('targets',) @@ -1265,8 +818,8 @@ def postinit(self, targets=None): self.targets = targets -@object_bases.register_implementation(object_bases.Dict) -class Dict(NodeNG, bases.Instance): +@util.register_implementation(treeabc.Dict) +class Dict(base.NodeNG, bases.Instance): """class representing a Dict node""" _astroid_fields = ('items',) @@ -1328,7 +881,7 @@ def bool_value(self): return bool(self.items) -@object_bases.register_implementation(object_bases.Expr) +@util.register_implementation(treeabc.Expr) class Expr(Statement): """class representing a Expr node""" _astroid_fields = ('value',) @@ -1338,20 +891,20 @@ def postinit(self, value=None): self.value = value -@object_bases.register_implementation(object_bases.Ellipsis) -class Ellipsis(NodeNG): # pylint: disable=redefined-builtin +@util.register_implementation(treeabc.Ellipsis) +class Ellipsis(base.NodeNG): # pylint: disable=redefined-builtin """class representing an Ellipsis node""" def bool_value(self): return True -@object_bases.register_implementation(object_bases.EmptyNode) -class EmptyNode(NodeNG): +@util.register_implementation(treeabc.EmptyNode) +class EmptyNode(base.NodeNG): """class representing an EmptyNode node""" -@object_bases.register_implementation(object_bases.ExceptHandler) +@util.register_implementation(treeabc.ExceptHandler) class ExceptHandler(mixins.AssignTypeMixin, Statement): """class representing an ExceptHandler node""" _astroid_fields = ('type', 'name', 'body',) @@ -1381,7 +934,7 @@ def catch(self, exceptions): return True -@object_bases.register_implementation(object_bases.Exec) +@util.register_implementation(treeabc.Exec) class Exec(Statement): """class representing an Exec node""" _astroid_fields = ('expr', 'globals', 'locals',) @@ -1395,8 +948,8 @@ def postinit(self, expr=None, globals=None, locals=None): self.locals = locals -@object_bases.register_implementation(object_bases.ExtSlice) -class ExtSlice(NodeNG): +@util.register_implementation(treeabc.ExtSlice) +class ExtSlice(base.NodeNG): """class representing an ExtSlice node""" _astroid_fields = ('dims',) dims = None @@ -1405,7 +958,7 @@ def postinit(self, dims=None): self.dims = dims -@object_bases.register_implementation(object_bases.For) +@util.register_implementation(treeabc.For) class For(mixins.BlockRangeMixIn, mixins.AssignTypeMixin, Statement): """class representing a For node""" _astroid_fields = ('target', 'iter', 'body', 'orelse',) @@ -1426,13 +979,13 @@ def blockstart_tolineno(self): return self.iter.tolineno -@object_bases.register_implementation(object_bases.AsyncFor) +@util.register_implementation(treeabc.AsyncFor) class AsyncFor(For): """Asynchronous For built with `async` keyword.""" -@object_bases.register_implementation(object_bases.Await) -class Await(NodeNG): +@util.register_implementation(treeabc.Await) +class Await(base.NodeNG): """Await node for the `await` keyword.""" _astroid_fields = ('value', ) @@ -1442,7 +995,7 @@ def postinit(self, value=None): self.value = value -@object_bases.register_implementation(object_bases.ImportFrom) +@util.register_implementation(treeabc.ImportFrom) class ImportFrom(mixins.ImportFromMixin, Statement): """class representing a ImportFrom node""" _other_fields = ('modname', 'names', 'level') @@ -1455,8 +1008,8 @@ def __init__(self, fromname, names, level=0, lineno=None, super(ImportFrom, self).__init__(lineno, col_offset, parent) -@object_bases.register_implementation(object_bases.Attribute) -class Attribute(NodeNG): +@util.register_implementation(treeabc.Attribute) +class Attribute(base.NodeNG): """class representing a Attribute node""" _astroid_fields = ('expr',) _other_fields = ('attrname',) @@ -1470,7 +1023,7 @@ def postinit(self, expr=None): self.expr = expr -@object_bases.register_implementation(object_bases.Global) +@util.register_implementation(treeabc.Global) class Global(Statement): """class representing a Global node""" _other_fields = ('names',) @@ -1483,7 +1036,7 @@ def _infer_name(self, frame, name): return name -@object_bases.register_implementation(object_bases.If) +@util.register_implementation(treeabc.If) class If(mixins.BlockRangeMixIn, Statement): """class representing an If node""" _astroid_fields = ('test', 'body', 'orelse') @@ -1510,8 +1063,8 @@ def block_range(self, lineno): self.body[0].fromlineno - 1) -@object_bases.register_implementation(object_bases.IfExp) -class IfExp(NodeNG): +@util.register_implementation(treeabc.IfExp) +class IfExp(base.NodeNG): """class representing an IfExp node""" _astroid_fields = ('test', 'body', 'orelse') test = None @@ -1524,7 +1077,7 @@ def postinit(self, test=None, body=None, orelse=None): self.orelse = orelse -@object_bases.register_implementation(object_bases.Import) +@util.register_implementation(treeabc.Import) class Import(mixins.ImportFromMixin, Statement): """class representing an Import node""" _other_fields = ('names',) @@ -1534,8 +1087,8 @@ def __init__(self, names=None, lineno=None, col_offset=None, parent=None): super(Import, self).__init__(lineno, col_offset, parent) -@object_bases.register_implementation(object_bases.Index) -class Index(NodeNG): +@util.register_implementation(treeabc.Index) +class Index(base.NodeNG): """class representing an Index node""" _astroid_fields = ('value',) value = None @@ -1544,8 +1097,8 @@ def postinit(self, value=None): self.value = value -@object_bases.register_implementation(object_bases.Keyword) -class Keyword(NodeNG): +@util.register_implementation(treeabc.Keyword) +class Keyword(base.NodeNG): """class representing a Keyword node""" _astroid_fields = ('value',) _other_fields = ('arg',) @@ -1559,7 +1112,7 @@ def postinit(self, value=None): self.value = value -@object_bases.register_implementation(object_bases.List) +@util.register_implementation(treeabc.List) class List(_BaseContainer): """class representing a List node""" @@ -1570,7 +1123,7 @@ def getitem(self, index, context=None): return _container_getitem(self, self.elts, index) -@object_bases.register_implementation(object_bases.Nonlocal) +@util.register_implementation(treeabc.Nonlocal) class Nonlocal(Statement): """class representing a Nonlocal node""" _other_fields = ('names',) @@ -1583,12 +1136,12 @@ def _infer_name(self, frame, name): return name -@object_bases.register_implementation(object_bases.Pass) +@util.register_implementation(treeabc.Pass) class Pass(Statement): """class representing a Pass node""" -@object_bases.register_implementation(object_bases.Print) +@util.register_implementation(treeabc.Print) class Print(Statement): """class representing a Print node""" _astroid_fields = ('dest', 'values',) @@ -1604,7 +1157,7 @@ def postinit(self, dest=None, values=None): self.values = values -@object_bases.register_implementation(object_bases.Raise) +@util.register_implementation(treeabc.Raise) class Raise(Statement): """class representing a Raise node""" exc = None @@ -1634,7 +1187,7 @@ def raises_not_implemented(self): return True -@object_bases.register_implementation(object_bases.Return) +@util.register_implementation(treeabc.Return) class Return(Statement): """class representing a Return node""" _astroid_fields = ('value',) @@ -1644,7 +1197,7 @@ def postinit(self, value=None): self.value = value -@object_bases.register_implementation(object_bases.Set) +@util.register_implementation(treeabc.Set) class Set(_BaseContainer): """class representing a Set node""" @@ -1652,8 +1205,8 @@ def pytype(self): return '%s.set' % BUILTINS -@object_bases.register_implementation(object_bases.Slice) -class Slice(NodeNG): +@util.register_implementation(treeabc.Slice) +class Slice(base.NodeNG): """class representing a Slice node""" _astroid_fields = ('lower', 'upper', 'step') lower = None @@ -1696,8 +1249,8 @@ def getattr(self, attrname, context=None): return self._proxied.getattr(attrname, context) -@object_bases.register_implementation(object_bases.Starred) -class Starred(mixins.ParentAssignTypeMixin, NodeNG): +@util.register_implementation(treeabc.Starred) +class Starred(mixins.ParentAssignTypeMixin, base.NodeNG): """class representing a Starred node""" _astroid_fields = ('value',) value = None @@ -1706,8 +1259,8 @@ def postinit(self, value=None): self.value = value -@object_bases.register_implementation(object_bases.Subscript) -class Subscript(NodeNG): +@util.register_implementation(treeabc.Subscript) +class Subscript(base.NodeNG): """class representing a Subscript node""" _astroid_fields = ('value', 'slice') value = None @@ -1718,7 +1271,7 @@ def postinit(self, value=None, slice=None): self.slice = slice -@object_bases.register_implementation(object_bases.TryExcept) +@util.register_implementation(treeabc.TryExcept) class TryExcept(mixins.BlockRangeMixIn, Statement): """class representing a TryExcept node""" _astroid_fields = ('body', 'handlers', 'orelse',) @@ -1747,7 +1300,7 @@ def block_range(self, lineno): return self._elsed_block_range(lineno, self.orelse, last) -@object_bases.register_implementation(object_bases.TryFinally) +@util.register_implementation(treeabc.TryFinally) class TryFinally(mixins.BlockRangeMixIn, Statement): """class representing a TryFinally node""" _astroid_fields = ('body', 'finalbody',) @@ -1768,7 +1321,7 @@ def block_range(self, lineno): return self._elsed_block_range(lineno, self.finalbody) -@object_bases.register_implementation(object_bases.Tuple) +@util.register_implementation(treeabc.Tuple) class Tuple(_BaseContainer): """class representing a Tuple node""" @@ -1779,8 +1332,8 @@ def getitem(self, index, context=None): return _container_getitem(self, self.elts, index) -@object_bases.register_implementation(object_bases.UnaryOp) -class UnaryOp(NodeNG): +@util.register_implementation(treeabc.UnaryOp) +class UnaryOp(base.NodeNG): """class representing an UnaryOp node""" _astroid_fields = ('operand',) _other_fields = ('op',) @@ -1811,7 +1364,7 @@ def type_errors(self, context=None): return [] -@object_bases.register_implementation(object_bases.While) +@util.register_implementation(treeabc.While) class While(mixins.BlockRangeMixIn, Statement): """class representing a While node""" _astroid_fields = ('test', 'body', 'orelse',) @@ -1833,7 +1386,7 @@ def block_range(self, lineno): return self. _elsed_block_range(lineno, self.orelse) -@object_bases.register_implementation(object_bases.With) +@util.register_implementation(treeabc.With) class With(mixins.BlockRangeMixIn, mixins.AssignTypeMixin, Statement): """class representing a With node""" _astroid_fields = ('items', 'body') @@ -1857,13 +1410,13 @@ def get_children(self): yield elt -@object_bases.register_implementation(object_bases.AsyncWith) +@util.register_implementation(treeabc.AsyncWith) class AsyncWith(With): """Asynchronous `with` built with the `async` keyword.""" -@object_bases.register_implementation(object_bases.Yield) -class Yield(NodeNG): +@util.register_implementation(treeabc.Yield) +class Yield(base.NodeNG): """class representing a Yield node""" _astroid_fields = ('value',) value = None @@ -1872,13 +1425,13 @@ def postinit(self, value=None): self.value = value -@object_bases.register_implementation(object_bases.YieldFrom) +@util.register_implementation(treeabc.YieldFrom) class YieldFrom(Yield): """ Class representing a YieldFrom node. """ -@object_bases.register_implementation(object_bases.DictUnpack) -class DictUnpack(NodeNG): +@util.register_implementation(treeabc.DictUnpack) +class DictUnpack(base.NodeNG): """Represents the unpacking of dicts into dicts using PEP 448.""" @@ -1911,7 +1464,7 @@ def const_factory(value): # 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) + assert not isinstance(value, base.NodeNG) try: return CONST_CLS[value.__class__](value) except (KeyError, AttributeError): diff --git a/astroid/objects.py b/astroid/objects.py index cd62f59b6b..7b39b7d77f 100644 --- a/astroid/objects.py +++ b/astroid/objects.py @@ -27,7 +27,9 @@ import six -from astroid import object_bases +from astroid.tree import base +from astroid.tree import treeabc +from astroid.runtime import runtimeabc from astroid import bases from astroid import decorators from astroid import exceptions @@ -54,7 +56,7 @@ def _proxied(self): return builtins.getattr('frozenset')[0] -class Super(node_classes.NodeNG): +class Super(base.NodeNG): """Proxy class over a super call. This class offers almost the same behaviour as Python's super, @@ -85,17 +87,17 @@ def _infer(self, context=None): def super_mro(self): """Get the MRO which will be used to lookup attributes in this super.""" - if not isinstance(self.mro_pointer, object_bases.ClassDef): + if not isinstance(self.mro_pointer, treeabc.ClassDef): raise exceptions.SuperArgumentTypeError( "The first super argument must be type.") - if isinstance(self.type, object_bases.ClassDef): + if isinstance(self.type, treeabc.ClassDef): # `super(type, type)`, most likely in a class method. self._class_based = True mro_type = self.type else: mro_type = getattr(self.type, '_proxied', None) - if not isinstance(mro_type, (object_bases.Instance, object_bases.ClassDef)): + if not isinstance(mro_type, (runtimeabc.Instance, treeabc.ClassDef)): raise exceptions.SuperArgumentTypeError( "super(type, obj): obj must be an " "instance or subtype of type") @@ -150,7 +152,7 @@ def igetattr(self, name, context=None): found = True for inferred in bases._infer_stmts([cls[name]], context, frame=self): - if not isinstance(inferred, object_bases.FunctionDef): + if not isinstance(inferred, treeabc.FunctionDef): yield inferred continue diff --git a/astroid/runtime/__init__.py b/astroid/runtime/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/astroid/runtime/runtimeabc.py b/astroid/runtime/runtimeabc.py new file mode 100644 index 0000000000..40bf5c8b36 --- /dev/null +++ b/astroid/runtime/runtimeabc.py @@ -0,0 +1,29 @@ +import abc + +import six + + +@six.add_metaclass(abc.ABCMeta) +class RuntimeObject(object): + """Class representing an object that is created at runtime + + These objects aren't AST per se, being created by the action + of the interpreter when the code executes, which is a total + different step than the AST creation. + """ + + +class Instance(RuntimeObject): + """Class representing an instance.""" + + +class UnboundMethod(RuntimeObject): + """Class representing an unbound method.""" + + +class BoundMethod(UnboundMethod): + """Class representing a bound method.""" + + +class Generator(RuntimeObject): + """Class representing a Generator.""" diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index 023a91aa02..d5b9b99d11 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -29,7 +29,8 @@ import six import wrapt -from astroid import object_bases +from astroid.tree import base as treebase +from astroid.tree import treeabc from astroid import bases from astroid import context as contextmod from astroid import exceptions @@ -138,7 +139,7 @@ def builtin_lookup(name): # TODO move this Mixin to mixins.py; problem: 'FunctionDef' in _scope_lookup class LocalsDictNodeNG(node_classes.LookupMixIn, - node_classes.NodeNG): + treebase.NodeNG): """ this class provides locals handling common to Module, FunctionDef and ClassDef nodes, including a dict like interface for direct access to locals information @@ -248,7 +249,7 @@ def __contains__(self, name): return name in self.locals -@object_bases.register_implementation(object_bases.Module) +@util.register_implementation(treeabc.Module) class Module(LocalsDictNodeNG): _astroid_fields = ('body',) @@ -513,7 +514,7 @@ def frame(self): scope_lookup = LocalsDictNodeNG._scope_lookup -@object_bases.register_implementation(object_bases.GeneratorExp) +@util.register_implementation(treeabc.GeneratorExp) class GeneratorExp(ComprehensionScope): _astroid_fields = ('elt', 'generators') _other_other_fields = ('locals',) @@ -535,7 +536,7 @@ def bool_value(self): return True -@object_bases.register_implementation(object_bases.DictComp) +@util.register_implementation(treeabc.DictComp) class DictComp(ComprehensionScope): _astroid_fields = ('key', 'value', 'generators') _other_other_fields = ('locals',) @@ -559,7 +560,7 @@ def bool_value(self): return util.YES -@object_bases.register_implementation(object_bases.SetComp) +@util.register_implementation(treeabc.SetComp) class SetComp(ComprehensionScope): _astroid_fields = ('elt', 'generators') _other_other_fields = ('locals',) @@ -581,8 +582,8 @@ def bool_value(self): return util.YES -@object_bases.register_implementation(object_bases.ListComp) -class _ListComp(node_classes.NodeNG): +@util.register_implementation(treeabc.ListComp) +class _ListComp(treebase.NodeNG): """class representing a ListComp node""" _astroid_fields = ('elt', 'generators') elt = None @@ -633,7 +634,7 @@ def _infer_decorator_callchain(node): return 'staticmethod' -@object_bases.register_implementation(object_bases.Lambda) +@util.register_implementation(treeabc.Lambda) class Lambda(mixins.FilterStmtsMixin, LocalsDictNodeNG): _astroid_fields = ('args', 'body',) _other_other_fields = ('locals',) @@ -696,7 +697,7 @@ def bool_value(self): return True -@object_bases.register_implementation(object_bases.FunctionDef) +@util.register_implementation(treeabc.FunctionDef) class FunctionDef(node_classes.Statement, Lambda): if six.PY3: _astroid_fields = ('decorators', 'args', 'body', 'returns') @@ -962,7 +963,7 @@ def bool_value(self): return True -@object_bases.register_implementation(object_bases.AsyncFunctionDef) +@util.register_implementation(treeabc.AsyncFunctionDef) class AsyncFunctionDef(FunctionDef): """Asynchronous function created with the `async` keyword.""" @@ -1064,7 +1065,7 @@ def get_wrapping_class(node): -@object_bases.register_implementation(object_bases.ClassDef) +@util.register_implementation(treeabc.ClassDef) class ClassDef(mixins.FilterStmtsMixin, LocalsDictNodeNG, node_classes.Statement): diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index 09a62c5326..56813ef2bf 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -25,7 +25,6 @@ import six -from astroid import object_bases from astroid import bases from astroid import builder from astroid import context as contextmod @@ -33,10 +32,12 @@ from astroid import node_classes from astroid import nodes from astroid import parse +from astroid.runtime import runtimeabc from astroid import util from astroid import test_utils from astroid import transforms from astroid.tests import resources +from astroid.tree import treeabc abuilder = builder.AstroidBuilder() @@ -753,13 +754,13 @@ class BaseTypesTest(unittest.TestCase): def test_concrete_issubclass(self): for node in nodes.ALL_NODE_CLASSES: name = node.__name__ - base_type = getattr(object_bases, name) + base_type = getattr(treeabc, name) self.assertTrue(issubclass(node, base_type), (node, base_type)) - self.assertTrue(issubclass(bases.Instance, object_bases.Instance)) - self.assertTrue(issubclass(bases.Generator, object_bases.Generator)) - self.assertTrue(issubclass(bases.BoundMethod, object_bases.BoundMethod)) - self.assertTrue(issubclass(bases.UnboundMethod, object_bases.UnboundMethod)) + self.assertTrue(issubclass(bases.Instance, runtimeabc.Instance)) + self.assertTrue(issubclass(bases.Generator, runtimeabc.Generator)) + self.assertTrue(issubclass(bases.BoundMethod, runtimeabc.BoundMethod)) + self.assertTrue(issubclass(bases.UnboundMethod, runtimeabc.UnboundMethod)) if __name__ == '__main__': diff --git a/astroid/tree/__init__.py b/astroid/tree/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/astroid/tree/base.py b/astroid/tree/base.py new file mode 100644 index 0000000000..026dedabe1 --- /dev/null +++ b/astroid/tree/base.py @@ -0,0 +1,480 @@ +# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. +# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr +# +# This file is part of astroid. +# +# astroid is free software: you can redistribute it and/or modify it +# under the terms of the GNU Lesser General Public License as published by the +# Free Software Foundation, either version 2.1 of the License, or (at your +# option) any later version. +# +# astroid is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License +# for more details. +# +# You should have received a copy of the GNU Lesser General Public License along +# with astroid. If not, see . + +import pprint +import warnings + +try: + from functools import singledispatch as _singledispatch +except ImportError: + from singledispatch import singledispatch as _singledispatch + + +from astroid import as_string +from astroid import decorators +from astroid import exceptions +from astroid.tree import treeabc +from astroid import util + + +@util.register_implementation(treeabc.NodeNG) +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] + else: + 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() + else: + 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 + else: + return lastchild.tolineno + + # TODO / FIXME: + assert self.fromlineno is not None, self + assert self.tolineno is not None, self + + 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(self.__class__.__name__) + + 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 instanciate_class(self): + """instanciate 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 len(node) == 0: + 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 + ' 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 len(fields) == 0: + 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 + * YES: the inference engine is uncertain of the + node's value. + """ + return util.YES diff --git a/astroid/object_bases.py b/astroid/tree/treeabc.py similarity index 87% rename from astroid/object_bases.py rename to astroid/tree/treeabc.py index 34232ce6b2..0df1de8f38 100644 --- a/astroid/object_bases.py +++ b/astroid/tree/treeabc.py @@ -29,17 +29,6 @@ import six -def register_implementation(base): - """Register an implementation for the given *base* - - The given base class is expected to have a `register` method, - similar to what `abc.ABCMeta` provides when used. - """ - def wrapped(impl): - base.register(impl) - return impl - return wrapped - @six.add_metaclass(abc.ABCMeta) class NodeNG(object): @@ -59,6 +48,8 @@ def infer(self, context=None, **kwargs): """Main interface to the interface system, return a generator on inferred values.""" +# pylint: disable=abstract-method + class Statement(NodeNG): """Represents a Statement node.""" is_statement = True @@ -328,31 +319,5 @@ class FunctionDef(Statement, Lambda): """Class representing a function node.""" -@six.add_metaclass(abc.ABCMeta) -class RuntimeObject(object): - """Class representing an object that is created at runtime - - These objects aren't AST per se, being created by the action - of the interpreter when the code executes, which is a total - different step than the AST creation. - """ - - -class Instance(RuntimeObject): - """Class representing an instance.""" - - -class UnboundMethod(RuntimeObject): - """Class representing an unbound method.""" - - -class BoundMethod(UnboundMethod): - """Class representing a bound method.""" - - -class Generator(RuntimeObject): - """Class representing a Generator.""" - - class EmptyNode(NodeNG): pass diff --git a/astroid/util.py b/astroid/util.py index 20c44d72ec..7bef88091d 100644 --- a/astroid/util.py +++ b/astroid/util.py @@ -66,3 +66,15 @@ def proxy_alias(alias_name, node_type): {'__class__': object.__dict__['__class__'], '__instancecheck__': _instancecheck}) return proxy(lambda: node_type) + + +def register_implementation(base): + """Register an implementation for the given *base* + + The given base class is expected to have a `register` method, + similar to what `abc.ABCMeta` provides when used. + """ + def wrapped(impl): + base.register(impl) + return impl + return wrapped From 0a39a8f9ce0244add2028d27fc2c8b7207cd7056 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 1 Nov 2015 15:07:28 +0200 Subject: [PATCH 040/312] Move _BaseContainer in astroid.tree.base and implement from_constants with singledispatch in brain_builtin_inference, since it's used only there. --- astroid/brain/brain_builtin_inference.py | 24 ++++++++++- astroid/node_classes.py | 51 ++---------------------- astroid/objects.py | 3 +- astroid/tree/base.py | 28 ++++++++++++- 4 files changed, 54 insertions(+), 52 deletions(-) diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index 4e651ef875..1c131e847a 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -5,7 +5,12 @@ import sys from textwrap import dedent +try: + from functools import singledispatch as _singledispatch +except ImportError: + from singledispatch import singledispatch as _singledispatch import six + from astroid import (MANAGER, UseInferenceDefault, NotFoundError, inference_tip, InferenceError, UnresolvableName) from astroid import arguments @@ -131,6 +136,23 @@ def _generic_inference(node, context, node_type, transform): return transformed +@_singledispatch +def _from_constants(kls, elts): + """Get an instance of the given *kls* with the given elements set.""" + elts = [nodes.const_factory(elt) for elt in elts] + instance = kls() + instance.postinit(elts=elts) + return instance + +@_from_constants.register(nodes.Dict) +def _dict_from_constants(kls, elts): + items = [(nodes.const_factory(k), nodes.const_factory(v)) + for k, v in elts.items()] + instance = kls() + instance.postinit(items=items) + return instance + + def _generic_transform(arg, klass, iterables, build_elts): if isinstance(arg, klass): return arg @@ -151,7 +173,7 @@ def _generic_transform(arg, klass, iterables, build_elts): elts = arg.value else: return - return klass.from_constants(elts=build_elts(elts)) + return _from_constants(klass, build_elts(elts)) def _infer_builtin(node, context, diff --git a/astroid/node_classes.py b/astroid/node_classes.py index db9d3eafc8..464845de30 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -154,41 +154,6 @@ def previous_sibling(self): return stmts[index -1] -@six.add_metaclass(abc.ABCMeta) -class _BaseContainer(mixins.ParentAssignTypeMixin, - base.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): - # pylint: disable=abstract-class-instantiated; False positive on Pylint #627. - 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 """ @@ -830,16 +795,6 @@ def __init__(self, lineno=None, col_offset=None, parent=None): 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 @@ -1113,7 +1068,7 @@ def postinit(self, value=None): @util.register_implementation(treeabc.List) -class List(_BaseContainer): +class List(base.BaseContainer, bases.Instance): """class representing a List node""" def pytype(self): @@ -1198,7 +1153,7 @@ def postinit(self, value=None): @util.register_implementation(treeabc.Set) -class Set(_BaseContainer): +class Set(base.BaseContainer, bases.Instance): """class representing a Set node""" def pytype(self): @@ -1322,7 +1277,7 @@ def block_range(self, lineno): @util.register_implementation(treeabc.Tuple) -class Tuple(_BaseContainer): +class Tuple(base.BaseContainer, bases.Instance): """class representing a Tuple node""" def pytype(self): diff --git a/astroid/objects.py b/astroid/objects.py index 7b39b7d77f..60d114101d 100644 --- a/astroid/objects.py +++ b/astroid/objects.py @@ -34,14 +34,13 @@ from astroid import decorators from astroid import exceptions from astroid import MANAGER -from astroid import node_classes from astroid import util BUILTINS = six.moves.builtins.__name__ -class FrozenSet(node_classes._BaseContainer): +class FrozenSet(base.BaseContainer, bases.Instance): """class representing a FrozenSet composite node""" def pytype(self): diff --git a/astroid/tree/base.py b/astroid/tree/base.py index 026dedabe1..a8954bf2d4 100644 --- a/astroid/tree/base.py +++ b/astroid/tree/base.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU Lesser General Public License along # with astroid. If not, see . +import abc import pprint import warnings @@ -23,11 +24,12 @@ from functools import singledispatch as _singledispatch except ImportError: from singledispatch import singledispatch as _singledispatch - +import six from astroid import as_string from astroid import decorators from astroid import exceptions +from astroid import mixins from astroid.tree import treeabc from astroid import util @@ -478,3 +480,27 @@ def bool_value(self): node's value. """ return util.YES + + +@six.add_metaclass(abc.ABCMeta) +class BaseContainer(mixins.ParentAssignTypeMixin, NodeNG): + """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 + + def itered(self): + return self.elts + + def bool_value(self): + return bool(self.elts) + + @abc.abstractmethod + def pytype(self): + pass From c7a2d3a5f3e316eb7c2754d1bbe041d2c2e59f96 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 1 Nov 2015 16:54:32 +0200 Subject: [PATCH 041/312] Separate runtime objects from ASTs The runtime objects from astroid.bases and astroid.objects got merged together into astroid.runtime.objects. bases._infer_stmts was moved into a new namespace, interpreter, which should be the starting point of a the new abstract interpretation component. --- astroid/__init__.py | 12 +- astroid/arguments.py | 7 +- astroid/brain/brain_builtin_inference.py | 10 +- astroid/brain/brain_stdlib.py | 3 +- astroid/builder.py | 6 +- astroid/inference.py | 19 +-- astroid/interpreter/__init__.py | 0 astroid/interpreter/util.py | 54 +++++++ astroid/node_classes.py | 15 +- astroid/objects.py | 173 -------------------- astroid/protocols.py | 17 +- astroid/raw_building.py | 6 +- astroid/{bases.py => runtime/objects.py} | 196 ++++++++++++++++++----- astroid/runtime/runtimeabc.py | 19 +++ astroid/scoped_nodes.py | 44 ++--- astroid/tests/resources.py | 3 +- astroid/tests/unittest_brain.py | 11 +- astroid/tests/unittest_inference.py | 11 +- astroid/tests/unittest_nodes.py | 12 +- astroid/tests/unittest_objects.py | 69 ++++---- astroid/tests/unittest_raw_building.py | 4 +- astroid/tests/unittest_regrtest.py | 4 +- astroid/tests/unittest_scoped_nodes.py | 10 +- 23 files changed, 371 insertions(+), 334 deletions(-) create mode 100644 astroid/interpreter/__init__.py create mode 100644 astroid/interpreter/util.py delete mode 100644 astroid/objects.py rename astroid/{bases.py => runtime/objects.py} (69%) diff --git a/astroid/__init__.py b/astroid/__init__.py index f67bbf92b3..af020256fe 100644 --- a/astroid/__init__.py +++ b/astroid/__init__.py @@ -48,6 +48,12 @@ # pylint: disable=redefined-builtin, wildcard-import +from astroid.runtime.objects import Instance, BoundMethod, UnboundMethod +# make a manager instance (borg) accessible from astroid package +from astroid.manager import AstroidManager +MANAGER = AstroidManager() +del AstroidManager + # make all exception classes accessible from astroid package from astroid.exceptions import * @@ -57,18 +63,12 @@ # trigger extra monkey-patching from astroid import inference -# more stuff available from astroid import raw_building -from astroid.bases import Instance, BoundMethod, UnboundMethod from astroid.node_classes import are_exclusive, unpack_infer from astroid.scoped_nodes import builtin_lookup from astroid.builder import parse from astroid.util import YES -# make a manager instance (borg) accessible from astroid package -from astroid.manager import AstroidManager -MANAGER = AstroidManager() -del AstroidManager # transform utilities (filters and decorator) diff --git a/astroid/arguments.py b/astroid/arguments.py index 5670fa863f..37cea2a4be 100644 --- a/astroid/arguments.py +++ b/astroid/arguments.py @@ -16,10 +16,11 @@ # You should have received a copy of the GNU Lesser General Public License along # with astroid. If not, see . -from astroid import bases from astroid import context as contextmod from astroid import exceptions from astroid import nodes +from astroid.runtime import objects +from astroid.runtime import runtimeabc from astroid import util import six @@ -184,8 +185,8 @@ def infer_argument(self, funcnode, name, context): # XXX can do better ? boundnode = funcnode.parent.frame() if funcnode.type == 'method': - if not isinstance(boundnode, bases.Instance): - boundnode = bases.Instance(boundnode) + if not isinstance(boundnode, runtimeabc.Instance): + boundnode = objects.Instance(boundnode) return iter((boundnode,)) if funcnode.type == 'classmethod': return iter((boundnode,)) diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index 1c131e847a..c5a8903690 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -14,14 +14,16 @@ from astroid import (MANAGER, UseInferenceDefault, NotFoundError, inference_tip, InferenceError, UnresolvableName) from astroid import arguments -from astroid import bases from astroid.builder import AstroidBuilder from astroid import helpers from astroid import nodes -from astroid import objects +from astroid.runtime import objects from astroid import scoped_nodes from astroid import util + +BUILTINS = six.moves.builtins.__name__ + def _extend_str(class_node, rvalue): """function to extend builtin str/unicode class""" # TODO(cpopa): this approach will make astroid to believe @@ -77,7 +79,7 @@ def split(self, *args): method.parent = class_node def extend_builtins(class_transforms): - builtin_ast = MANAGER.astroid_cache[bases.BUILTINS] + builtin_ast = MANAGER.astroid_cache[BUILTINS] for class_name, transform in class_transforms.items(): transform(builtin_ast[class_name]) @@ -510,7 +512,7 @@ def infer_type_dunder_new(caller, context=None): if not isinstance(mcs, nodes.ClassDef): # Not a valid first argument. raise UseInferenceDefault - if not mcs.is_subtype_of("%s.type" % bases.BUILTINS): + if not mcs.is_subtype_of("%s.type" % BUILTINS): # Not a valid metaclass. raise UseInferenceDefault diff --git a/astroid/brain/brain_stdlib.py b/astroid/brain/brain_stdlib.py index a03358fa45..583a674174 100644 --- a/astroid/brain/brain_stdlib.py +++ b/astroid/brain/brain_stdlib.py @@ -5,8 +5,9 @@ from textwrap import dedent from astroid import ( - MANAGER, UseInferenceDefault, inference_tip, BoundMethod, + MANAGER, UseInferenceDefault, inference_tip, InferenceError, register_module_extender) +from astroid.runtime.objects import BoundMethod from astroid import exceptions from astroid import nodes from astroid.builder import AstroidBuilder diff --git a/astroid/builder.py b/astroid/builder.py index 9bb78b156e..bbd548aafa 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -26,12 +26,12 @@ import sys import textwrap -from astroid import bases from astroid import exceptions from astroid import manager from astroid import modutils from astroid import raw_building from astroid import rebuilder +from astroid.runtime import objects from astroid import util @@ -223,10 +223,10 @@ def delayed_assattr(self, node): if inferred is util.YES: continue try: - if inferred.__class__ is bases.Instance: + if inferred.__class__ is objects.Instance: inferred = inferred._proxied iattrs = inferred.instance_attrs - elif isinstance(inferred, bases.Instance): + elif isinstance(inferred, objects.Instance): # Const, Tuple, ... we may be wrong, may be not, but # anyway we don't want to pollute builtin's namespace continue diff --git a/astroid/inference.py b/astroid/inference.py index e3e187ce76..ba78338776 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -24,7 +24,6 @@ import itertools import operator -from astroid import bases from astroid import context as contextmod from astroid import exceptions from astroid import decorators @@ -32,6 +31,8 @@ from astroid import manager from astroid import nodes from astroid import protocols +from astroid.runtime import objects +from astroid.interpreter.util import infer_stmts from astroid import util @@ -92,7 +93,7 @@ def infer_name(self, context=None): raise exceptions.UnresolvableName(self.name) context = context.clone() context.lookupname = self.name - return bases._infer_stmts(stmts, context, frame) + return infer_stmts(stmts, context, frame) nodes.Name._infer = decorators.path_wrapper(infer_name) nodes.AssignName.infer_lhs = infer_name # won't work with a path wrapper @@ -152,7 +153,7 @@ def infer_import_from(self, context=None, asname=True): context = contextmod.copy_context(context) context.lookupname = name stmts = module.getattr(name, ignore_locals=module is self.root()) - return bases._infer_stmts(stmts, context) + return infer_stmts(stmts, context) except exceptions.NotFoundError: util.reraise(exceptions.InferenceError(name)) nodes.ImportFrom._infer = infer_import_from @@ -184,8 +185,8 @@ def infer_global(self, context=None): if context.lookupname is None: raise exceptions.InferenceError() try: - return bases._infer_stmts(self.root().getattr(context.lookupname), - context) + return infer_stmts(self.root().getattr(context.lookupname), + context) except exceptions.NotFoundError: util.reraise(exceptions.InferenceError()) nodes.Global._infer = infer_global @@ -239,7 +240,7 @@ def infer_subscript(self, context=None): yield util.YES return - if value.__class__ == bases.Instance: + if value.__class__ == objects.Instance: index_value = index else: index_value = _SLICE_SENTINEL @@ -252,7 +253,7 @@ def infer_subscript(self, context=None): step = _slice_value(index.step, context) if all(elem is not _SLICE_SENTINEL for elem in (lower, upper, step)): index_value = slice(lower, upper, step) - elif isinstance(index, bases.Instance): + elif isinstance(index, objects.Instance): index = helpers.class_instance_as_index(index) if index: index_value = index.value @@ -365,7 +366,7 @@ def _infer_unaryop(self, context=None): else: yield util.YES else: - if not isinstance(operand, bases.Instance): + if not isinstance(operand, objects.Instance): # The operation was used on something which # doesn't support it. yield exceptions.UnaryOperationError(operand, self.op, exc) @@ -675,7 +676,7 @@ def infer_assign(self, context=None): return stmt.infer(context) stmts = list(self.assigned_stmts(context=context)) - return bases._infer_stmts(stmts, context) + return infer_stmts(stmts, context) nodes.AssignName._infer = infer_assign nodes.AssignAttr._infer = infer_assign diff --git a/astroid/interpreter/__init__.py b/astroid/interpreter/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/astroid/interpreter/util.py b/astroid/interpreter/util.py new file mode 100644 index 0000000000..76077e1a0f --- /dev/null +++ b/astroid/interpreter/util.py @@ -0,0 +1,54 @@ +# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. +# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr +# +# This file is part of astroid. +# +# astroid is free software: you can redistribute it and/or modify it +# under the terms of the GNU Lesser General Public License as published by the +# Free Software Foundation, either version 2.1 of the License, or (at your +# option) any later version. +# +# astroid is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License +# for more details. +# +# You should have received a copy of the GNU Lesser General Public License along +# with astroid. If not, see . + +"""Utilities for inference.""" + +from astroid import context as contextmod +from astroid import exceptions +from astroid import util + + + +def infer_stmts(stmts, context, frame=None): + """Return an iterator on statements inferred by each statement in *stmts*.""" + stmt = None + inferred = False + if context is not None: + name = context.lookupname + context = context.clone() + else: + name = None + context = contextmod.InferenceContext() + + for stmt in stmts: + if stmt is util.YES: + yield stmt + inferred = True + continue + context.lookupname = stmt._infer_name(frame, name) + try: + for inferred in stmt.infer(context=context): + yield inferred + inferred = True + except exceptions.UnresolvableName: + continue + except exceptions.InferenceError: + yield util.YES + inferred = True + if not inferred: + raise exceptions.InferenceError(str(stmt)) diff --git a/astroid/node_classes.py b/astroid/node_classes.py index 464845de30..b2c179c3bb 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -28,11 +28,12 @@ import six +from astroid.interpreter.util import infer_stmts from astroid.tree import base from astroid.tree import treeabc from astroid.runtime import runtimeabc +from astroid.runtime import objects from astroid import as_string -from astroid import bases from astroid import context as contextmod from astroid import decorators from astroid import exceptions @@ -179,7 +180,7 @@ def ilookup(self, name): """ frame, stmts = self.lookup(name) context = contextmod.InferenceContext() - return bases._infer_stmts(stmts, context, frame) + return infer_stmts(stmts, context, frame) def _filter_stmts(self, stmts, frame, offset): """filter statements to remove ignorable statements. @@ -706,7 +707,7 @@ def _get_filtered_stmts(self, lookup_node, node, stmts, mystmt): @util.register_implementation(treeabc.Const) @util.register_implementation(runtimeabc.Instance) -class Const(base.NodeNG, bases.Instance): +class Const(base.NodeNG, objects.Instance): """represent a constant node like num, str, bool, None, bytes""" _other_fields = ('value',) @@ -784,7 +785,7 @@ def postinit(self, targets=None): @util.register_implementation(treeabc.Dict) -class Dict(base.NodeNG, bases.Instance): +class Dict(base.NodeNG, objects.Instance): """class representing a Dict node""" _astroid_fields = ('items',) @@ -1068,7 +1069,7 @@ def postinit(self, value=None): @util.register_implementation(treeabc.List) -class List(base.BaseContainer, bases.Instance): +class List(base.BaseContainer, objects.Instance): """class representing a List node""" def pytype(self): @@ -1153,7 +1154,7 @@ def postinit(self, value=None): @util.register_implementation(treeabc.Set) -class Set(base.BaseContainer, bases.Instance): +class Set(base.BaseContainer, objects.Instance): """class representing a Set node""" def pytype(self): @@ -1277,7 +1278,7 @@ def block_range(self, lineno): @util.register_implementation(treeabc.Tuple) -class Tuple(base.BaseContainer, bases.Instance): +class Tuple(base.BaseContainer, objects.Instance): """class representing a Tuple node""" def pytype(self): diff --git a/astroid/objects.py b/astroid/objects.py deleted file mode 100644 index 60d114101d..0000000000 --- a/astroid/objects.py +++ /dev/null @@ -1,173 +0,0 @@ -# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . - -""" -Inference objects are a way to represent composite AST nodes, -which are used only as inference results, so they can't be found in the -original AST tree. For instance, inferring the following frozenset use, -leads to an inferred FrozenSet: - - Call(func=Name('frozenset'), args=Tuple(...)) -""" - -import six - -from astroid.tree import base -from astroid.tree import treeabc -from astroid.runtime import runtimeabc -from astroid import bases -from astroid import decorators -from astroid import exceptions -from astroid import MANAGER -from astroid import util - - -BUILTINS = six.moves.builtins.__name__ - - -class FrozenSet(base.BaseContainer, bases.Instance): - """class representing a FrozenSet composite node""" - - def pytype(self): - return '%s.frozenset' % BUILTINS - - def _infer(self, context=None): - yield self - - @decorators.cachedproperty - def _proxied(self): - builtins = MANAGER.astroid_cache[BUILTINS] - return builtins.getattr('frozenset')[0] - - -class Super(base.NodeNG): - """Proxy class over a super call. - - This class offers almost the same behaviour as Python's super, - which is MRO lookups for retrieving attributes from the parents. - - The *mro_pointer* is the place in the MRO from where we should - start looking, not counting it. *mro_type* is the object which - provides the MRO, it can be both a type or an instance. - *self_class* is the class where the super call is, while - *scope* is the function where the super call is. - """ - - def __init__(self, mro_pointer, mro_type, self_class, scope): - self.type = mro_type - self.mro_pointer = mro_pointer - self._class_based = False - self._self_class = self_class - self._scope = scope - self._model = { - '__thisclass__': self.mro_pointer, - '__self_class__': self._self_class, - '__self__': self.type, - '__class__': self._proxied, - } - - def _infer(self, context=None): - yield self - - def super_mro(self): - """Get the MRO which will be used to lookup attributes in this super.""" - if not isinstance(self.mro_pointer, treeabc.ClassDef): - raise exceptions.SuperArgumentTypeError( - "The first super argument must be type.") - - if isinstance(self.type, treeabc.ClassDef): - # `super(type, type)`, most likely in a class method. - self._class_based = True - mro_type = self.type - else: - mro_type = getattr(self.type, '_proxied', None) - if not isinstance(mro_type, (runtimeabc.Instance, treeabc.ClassDef)): - raise exceptions.SuperArgumentTypeError( - "super(type, obj): obj must be an " - "instance or subtype of type") - - if not mro_type.newstyle: - raise exceptions.SuperError("Unable to call super on old-style classes.") - - mro = mro_type.mro() - if self.mro_pointer not in mro: - raise exceptions.SuperArgumentTypeError( - "super(type, obj): obj must be an " - "instance or subtype of type") - - index = mro.index(self.mro_pointer) - return mro[index + 1:] - - @decorators.cachedproperty - def _proxied(self): - builtins = MANAGER.astroid_cache[BUILTINS] - return builtins.getattr('super')[0] - - def pytype(self): - return '%s.super' % BUILTINS - - def display_type(self): - return 'Super of' - - @property - def name(self): - """Get the name of the MRO pointer.""" - return self.mro_pointer.name - - def igetattr(self, name, context=None): - """Retrieve the inferred values of the given attribute name.""" - - local_name = self._model.get(name) - if local_name: - yield local_name - return - - try: - mro = self.super_mro() - except (exceptions.MroError, exceptions.SuperError) as exc: - # Don't let invalid MROs or invalid super calls - # to leak out as is from this function. - util.reraise(exceptions.NotFoundError(*exc.args)) - - found = False - for cls in mro: - if name not in cls.locals: - continue - - found = True - for inferred in bases._infer_stmts([cls[name]], context, frame=self): - if not isinstance(inferred, treeabc.FunctionDef): - yield inferred - continue - - # We can obtain different descriptors from a super depending - # on what we are accessing and where the super call is. - if inferred.type == 'classmethod': - yield bases.BoundMethod(inferred, cls) - elif self._scope.type == 'classmethod' and inferred.type == 'method': - yield inferred - elif self._class_based or inferred.type == 'staticmethod': - yield inferred - else: - yield bases.BoundMethod(inferred, cls) - - if not found: - raise exceptions.NotFoundError(name) - - def getattr(self, name, context=None): - return list(self.igetattr(name, context=context)) diff --git a/astroid/protocols.py b/astroid/protocols.py index 7c9e4b917d..623e151a74 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -26,13 +26,13 @@ import six from astroid import arguments -from astroid import bases from astroid import context as contextmod from astroid import exceptions from astroid import decorators from astroid import node_classes from astroid import helpers from astroid import nodes +from astroid.runtime import objects from astroid import util @@ -167,7 +167,7 @@ def tl_infer_binary_op(self, operator, other, context, method): yield not_implemented return yield _multiply_seq_by_int(self, other, context) - elif isinstance(other, bases.Instance) and operator == '*': + elif isinstance(other, objects.Instance) and operator == '*': # Verify if the instance supports __index__. as_index = helpers.class_instance_as_index(other) if not as_index: @@ -185,7 +185,7 @@ def tl_infer_binary_op(self, operator, other, context, method): def instance_infer_binary_op(self, operator, other, context, method): return method.infer_call_result(self, context) -bases.Instance.infer_binary_op = instance_infer_binary_op +objects.Instance.infer_binary_op = instance_infer_binary_op # assignment ################################################################## @@ -281,7 +281,7 @@ def _arguments_infer_argname(self, name, context): if self.args and getattr(self.args[0], 'name', None) == name: functype = self.parent.type if functype == 'method': - yield bases.Instance(self.parent.parent.frame()) + yield objects.Instance(self.parent.parent.frame()) return if functype == 'classmethod': yield self.parent.parent.frame() @@ -373,7 +373,7 @@ def _resolve_asspart(parts, asspath, context): def excepthandler_assigned_stmts(self, node, context=None, asspath=None): for assigned in node_classes.unpack_infer(self.type): if isinstance(assigned, nodes.ClassDef): - assigned = bases.Instance(assigned) + assigned = objects.Instance(assigned) yield assigned nodes.ExceptHandler.assigned_stmts = excepthandler_assigned_stmts @@ -384,7 +384,7 @@ def _infer_context_manager(self, mgr, context): inferred = next(mgr.infer(context=context)) except exceptions.InferenceError: return - if isinstance(inferred, bases.Generator): + if isinstance(inferred, objects.Generator): # Check if it is decorated with contextlib.contextmanager. func = inferred.parent if not func.decorators: @@ -412,12 +412,12 @@ def _infer_context_manager(self, mgr, context): else: for inferred in yield_point.value.infer(context=context): yield inferred - elif isinstance(inferred, bases.Instance): + elif isinstance(inferred, objects.Instance): try: enter = next(inferred.igetattr('__enter__', context=context)) except (exceptions.InferenceError, exceptions.NotFoundError): return - if not isinstance(enter, bases.BoundMethod): + if not isinstance(enter, objects.BoundMethod): return if not context.callcontext: context.callcontext = contextmod.CallContext(args=[inferred]) @@ -444,7 +444,6 @@ def __enter__(self): # ContextManager().infer() will return ContextManager # f.infer() will return 42. """ - mgr = next(mgr for (mgr, vars) in self.items if vars == node) if asspath is None: for result in _infer_context_manager(self, mgr, context): diff --git a/astroid/raw_building.py b/astroid/raw_building.py index ee5c31e5e1..ef6c36af4f 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -27,10 +27,10 @@ import six -from astroid import bases from astroid import manager from astroid import node_classes from astroid import nodes +from astroid.runtime import objects MANAGER = manager.AstroidManager() @@ -408,5 +408,5 @@ def _set_proxied(const): _GeneratorType = nodes.ClassDef(types.GeneratorType.__name__, types.GeneratorType.__doc__) _GeneratorType.parent = MANAGER.astroid_cache[six.moves.builtins.__name__] -bases.Generator._proxied = _GeneratorType -Astroid_BUILDER.object_build(bases.Generator._proxied, types.GeneratorType) +objects.Generator._proxied = _GeneratorType +Astroid_BUILDER.object_build(objects.Generator._proxied, types.GeneratorType) diff --git a/astroid/bases.py b/astroid/runtime/objects.py similarity index 69% rename from astroid/bases.py rename to astroid/runtime/objects.py index 378fbea123..d84cc5147a 100644 --- a/astroid/bases.py +++ b/astroid/runtime/objects.py @@ -1,4 +1,4 @@ -# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. +# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr # # This file is part of astroid. @@ -15,24 +15,37 @@ # # You should have received a copy of the GNU Lesser General Public License along # with astroid. If not, see . -"""This module contains base classes and functions for the nodes and some -inference utils. + """ +Inference objects are a way to represent objects which are available +only at runtime, so they can't be found in the original AST tree. +For instance, inferring the following frozenset use, leads to an inferred +FrozenSet: + Call(func=Name('frozenset'), args=Tuple(...)) +""" import sys +import six + from astroid import context as contextmod +from astroid import decorators from astroid import exceptions +from astroid.interpreter.util import infer_stmts +from astroid import manager from astroid.runtime import runtimeabc -from astroid import util +from astroid.tree import base from astroid.tree import treeabc +from astroid import util + + +BUILTINS = six.moves.builtins.__name__ +MANAGER = manager.AstroidManager() if sys.version_info >= (3, 0): - BUILTINS = 'builtins' BOOL_SPECIAL_METHOD = '__bool__' else: - BUILTINS = '__builtin__' BOOL_SPECIAL_METHOD = '__nonzero__' PROPERTIES = {BUILTINS + '.property', 'abc.abstractproperty'} # List of possible property names. We use this list in order @@ -50,7 +63,7 @@ "LazyProperty"} -def _is_property(meth): +def is_property(meth): if PROPERTIES.intersection(meth.decoratornames()): return True stripped = {name.split(".")[-1] for name in meth.decoratornames() @@ -78,36 +91,6 @@ def infer(self, context=None): yield self -def _infer_stmts(stmts, context, frame=None): - """Return an iterator on statements inferred by each statement in *stmts*.""" - stmt = None - inferred = False - if context is not None: - name = context.lookupname - context = context.clone() - else: - name = None - context = contextmod.InferenceContext() - - for stmt in stmts: - if stmt is util.YES: - yield stmt - inferred = True - continue - context.lookupname = stmt._infer_name(frame, name) - try: - for inferred in stmt.infer(context=context): - yield inferred - inferred = True - except exceptions.UnresolvableName: - continue - except exceptions.InferenceError: - yield util.YES - inferred = True - if not inferred: - raise exceptions.InferenceError(str(stmt)) - - def _infer_method_result_truth(instance, method_name, context): # Get the method from the instance and try to infer # its return's truth value. @@ -163,8 +146,8 @@ def igetattr(self, name, context=None): # XXX frame should be self._proxied, or not ? get_attr = self.getattr(name, context, lookupclass=False) - for stmt in _infer_stmts(self._wrap_attr(get_attr, context), - context, frame=self): + for stmt in infer_stmts(self._wrap_attr(get_attr, context), + context, frame=self): yield stmt except exceptions.NotFoundError: try: @@ -180,7 +163,7 @@ def _wrap_attr(self, attrs, context=None): """wrap bound methods of attrs in a InstanceMethod proxies""" for attr in attrs: if isinstance(attr, UnboundMethod): - if _is_property(attr): + if is_property(attr): for inferred in attr.infer_call_result(self, context): yield inferred else: @@ -357,3 +340,136 @@ def __repr__(self): def __str__(self): return 'Generator(%s)' % (self._proxied.name) + + +class FrozenSet(base.BaseContainer, Instance): + """Class representing a FrozenSet composite node.""" + + def pytype(self): + return '%s.frozenset' % BUILTINS + + def _infer(self, context=None): + yield self + + @decorators.cachedproperty + def _proxied(self): + builtins = MANAGER.astroid_cache[BUILTINS] + return builtins.getattr('frozenset')[0] + + +class Super(base.NodeNG): + """Proxy class over a super call. + + This class offers almost the same behaviour as Python's super, + which is MRO lookups for retrieving attributes from the parents. + + The *mro_pointer* is the place in the MRO from where we should + start looking, not counting it. *mro_type* is the object which + provides the MRO, it can be both a type or an instance. + *self_class* is the class where the super call is, while + *scope* is the function where the super call is. + """ + + def __init__(self, mro_pointer, mro_type, self_class, scope): + self.type = mro_type + self.mro_pointer = mro_pointer + self._class_based = False + self._self_class = self_class + self._scope = scope + self._model = { + '__thisclass__': self.mro_pointer, + '__self_class__': self._self_class, + '__self__': self.type, + '__class__': self._proxied, + } + + def _infer(self, context=None): + yield self + + def super_mro(self): + """Get the MRO which will be used to lookup attributes in this super.""" + if not isinstance(self.mro_pointer, treeabc.ClassDef): + raise exceptions.SuperArgumentTypeError( + "The first super argument must be type.") + + if isinstance(self.type, treeabc.ClassDef): + # `super(type, type)`, most likely in a class method. + self._class_based = True + mro_type = self.type + else: + mro_type = getattr(self.type, '_proxied', None) + if not isinstance(mro_type, (runtimeabc.Instance, treeabc.ClassDef)): + raise exceptions.SuperArgumentTypeError( + "super(type, obj): obj must be an " + "instance or subtype of type") + + if not mro_type.newstyle: + raise exceptions.SuperError("Unable to call super on old-style classes.") + + mro = mro_type.mro() + if self.mro_pointer not in mro: + raise exceptions.SuperArgumentTypeError( + "super(type, obj): obj must be an " + "instance or subtype of type") + + index = mro.index(self.mro_pointer) + return mro[index + 1:] + + @decorators.cachedproperty + def _proxied(self): + builtins = MANAGER.astroid_cache[BUILTINS] + return builtins.getattr('super')[0] + + def pytype(self): + return '%s.super' % BUILTINS + + def display_type(self): + return 'Super of' + + @property + def name(self): + """Get the name of the MRO pointer.""" + return self.mro_pointer.name + + def igetattr(self, name, context=None): + """Retrieve the inferred values of the given attribute name.""" + + local_name = self._model.get(name) + if local_name: + yield local_name + return + + try: + mro = self.super_mro() + except (exceptions.MroError, exceptions.SuperError) as exc: + # Don't let invalid MROs or invalid super calls + # to leak out as is from this function. + util.reraise(exceptions.NotFoundError(*exc.args)) + + found = False + for cls in mro: + if name not in cls.locals: + continue + + found = True + for inferred in infer_stmts([cls[name]], context, frame=self): + if not isinstance(inferred, treeabc.FunctionDef): + yield inferred + continue + + # We can obtain different descriptors from a super depending + # on what we are accessing and where the super call is. + if inferred.type == 'classmethod': + yield BoundMethod(inferred, cls) + elif self._scope.type == 'classmethod' and inferred.type == 'method': + yield inferred + elif self._class_based or inferred.type == 'staticmethod': + yield inferred + else: + yield BoundMethod(inferred, cls) + + if not found: + raise exceptions.NotFoundError(name) + + def getattr(self, name, context=None): + return list(self.igetattr(name, context=context)) diff --git a/astroid/runtime/runtimeabc.py b/astroid/runtime/runtimeabc.py index 40bf5c8b36..612996aea3 100644 --- a/astroid/runtime/runtimeabc.py +++ b/astroid/runtime/runtimeabc.py @@ -1,3 +1,22 @@ +# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. +# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr +# +# This file is part of astroid. +# +# astroid is free software: you can redistribute it and/or modify it +# under the terms of the GNU Lesser General Public License as published by the +# Free Software Foundation, either version 2.1 of the License, or (at your +# option) any later version. +# +# astroid is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License +# for more details. +# +# You should have received a copy of the GNU Lesser General Public License along +# with astroid. If not, see . + + import abc import six diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index d5b9b99d11..a2e567e698 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -29,9 +29,10 @@ import six import wrapt +from astroid.interpreter.util import infer_stmts from astroid.tree import base as treebase from astroid.tree import treeabc -from astroid import bases +from astroid.runtime import objects from astroid import context as contextmod from astroid import exceptions from astroid import manager @@ -101,9 +102,9 @@ def decorator(func, instance, args, kwargs): def function_to_method(n, klass): if isinstance(n, FunctionDef): if n.type == 'classmethod': - return bases.BoundMethod(n, klass) + return objects.BoundMethod(n, klass) if n.type != 'staticmethod': - return bases.UnboundMethod(n) + return objects.UnboundMethod(n) return n @@ -381,8 +382,8 @@ def igetattr(self, name, context=None): context = contextmod.copy_context(context) context.lookupname = name try: - return bases._infer_stmts(self.getattr(name, context), - context, frame=self) + return infer_stmts(self.getattr(name, context), + context, frame=self) except exceptions.NotFoundError: util.reraise(exceptions.InferenceError(name)) @@ -625,7 +626,7 @@ def _infer_decorator_callchain(node): result = next(node.infer_call_result(node.parent)) except (StopIteration, exceptions.InferenceError): return - if isinstance(result, bases.Instance): + if isinstance(result, objects.Instance): result = result._proxied if isinstance(result, ClassDef): if result.is_subtype_of('%s.classmethod' % BUILTINS): @@ -860,8 +861,8 @@ def getattr(self, name, context=None): def igetattr(self, name, context=None): """Inferred getattr, which returns an iterator of inferred statements.""" try: - return bases._infer_stmts(self.getattr(name, context), - context, frame=self) + return infer_stmts(self.getattr(name, context), + context, frame=self) except exceptions.NotFoundError: util.reraise(exceptions.InferenceError(name)) @@ -925,7 +926,7 @@ def is_generator(self): def infer_call_result(self, caller, context=None): """infer what a function is returning when called""" if self.is_generator(): - result = bases.Generator() + result = objects.Generator() result.parent = self yield result return @@ -996,7 +997,7 @@ def _is_metaclass(klass, seen=None): continue else: seen.add(baseobj_name) - if isinstance(baseobj, bases.Instance): + if isinstance(baseobj, objects.Instance): # not abstract return False if baseobj is util.YES: @@ -1206,7 +1207,7 @@ def infer_call_result(self, caller, context=None): result = self._infer_type_call(caller, context) yield result else: - yield bases.Instance(self) + yield objects.Instance(self) def scope_lookup(self, node, name, offset=0): if any(node == base or base.parent_of(node) @@ -1260,7 +1261,7 @@ def ancestors(self, recurs=True, context=None): try: for baseobj in stmt.infer(context): if not isinstance(baseobj, ClassDef): - if isinstance(baseobj, bases.Instance): + if isinstance(baseobj, objects.Instance): baseobj = baseobj._proxied else: continue @@ -1349,7 +1350,7 @@ def instance_attr(self, name, context=None): def instanciate_class(self): """return Instance of ClassDef node, else return self""" - return bases.Instance(self) + return objects.Instance(self) def getattr(self, name, context=None, class_context=True): """Get an attribute from this class, using Python's attribute semantic @@ -1411,13 +1412,12 @@ def _get_attribute_from_metaclass(self, cls, name, context): except exceptions.NotFoundError: return - for attr in bases._infer_stmts(attrs, context, frame=cls): + for attr in infer_stmts(attrs, context, frame=cls): if not isinstance(attr, FunctionDef): yield attr continue - if bases._is_property(attr): - # TODO(cpopa): don't use a private API. + if objects.is_property(attr): for inferred in attr.infer_call_result(self, context): yield inferred continue @@ -1428,11 +1428,11 @@ def _get_attribute_from_metaclass(self, cls, name, context): # 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) + yield objects.BoundMethod(attr, frame) elif attr.type == 'staticmethod': yield attr else: - yield bases.BoundMethod(attr, self) + yield objects.BoundMethod(attr, self) def igetattr(self, name, context=None): """inferred getattr, need special treatment in class to handle @@ -1443,11 +1443,11 @@ def igetattr(self, name, context=None): context = contextmod.copy_context(context) context.lookupname = name try: - for inferred in bases._infer_stmts(self.getattr(name, context), - context, frame=self): + for inferred in infer_stmts(self.getattr(name, context), + context, frame=self): # yield YES object instead of descriptors when necessary if (not isinstance(inferred, node_classes.Const) - and isinstance(inferred, bases.Instance)): + and isinstance(inferred, objects.Instance)): try: inferred._proxied.getattr('__get__', context) except exceptions.NotFoundError: @@ -1696,7 +1696,7 @@ def _inferred_bases(self, context=None): baseobj = next(stmt.infer(context=context)) except exceptions.InferenceError: continue - if isinstance(baseobj, bases.Instance): + if isinstance(baseobj, objects.Instance): baseobj = baseobj._proxied if not isinstance(baseobj, ClassDef): continue diff --git a/astroid/tests/resources.py b/astroid/tests/resources.py index 7988d053dd..6a29b54ef0 100644 --- a/astroid/tests/resources.py +++ b/astroid/tests/resources.py @@ -19,13 +19,14 @@ import sys import pkg_resources +import six from astroid import builder from astroid import MANAGER -from astroid.bases import BUILTINS DATA_DIR = 'testdata/python{}/'.format(sys.version_info[0]) +BUILTINS = six.moves.builtins.__name__ def find(name): return pkg_resources.resource_filename( diff --git a/astroid/tests/unittest_brain.py b/astroid/tests/unittest_brain.py index 3520b49efb..672c115b53 100644 --- a/astroid/tests/unittest_brain.py +++ b/astroid/tests/unittest_brain.py @@ -21,14 +21,15 @@ import six from astroid import MANAGER -from astroid import bases from astroid import builder from astroid import nodes +from astroid.runtime import objects from astroid import test_utils from astroid import util import astroid +BUILTINS = six.moves.builtins.__name__ try: import nose # pylint: disable=unused-import HAS_NOSE = True @@ -338,7 +339,7 @@ def test_multiprocessing_manager(self): for attr in ('list', 'dict'): obj = next(module[attr].infer()) self.assertEqual(obj.qname(), - "{}.{}".format(bases.BUILTINS, attr)) + "{}.{}".format(BUILTINS, attr)) array = next(module['array'].infer()) self.assertEqual(array.qname(), "array.array") @@ -372,7 +373,7 @@ def mymethod(self, x): one = enum['one'] self.assertEqual(one.pytype(), '.MyEnum.one') - property_type = '{}.property'.format(bases.BUILTINS) + property_type = '{}.property'.format(BUILTINS) for propname in ('name', 'value'): prop = next(iter(one.getattr(propname))) self.assertIn(property_type, prop.decoratornames()) @@ -421,7 +422,7 @@ class MyEnum(enum.IntEnum): one = enum['one'] clazz = one.getattr('__class__')[0] - int_type = '{}.{}'.format(bases.BUILTINS, 'int') + int_type = '{}.{}'.format(BUILTINS, 'int') self.assertTrue(clazz.is_subtype_of(int_type), 'IntEnum based enums should be a subtype of int') @@ -435,7 +436,7 @@ def test_enum_func_form_is_class_not_instance(self): inferred_cls = next(cls.infer()) self.assertIsInstance(inferred_cls, nodes.ClassDef) inferred_instance = next(instance.infer()) - self.assertIsInstance(inferred_instance, bases.Instance) + self.assertIsInstance(inferred_instance, objects.Instance) @unittest.skipUnless(HAS_DATEUTIL, "This test requires the dateutil library.") diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index fa090f8527..da2ee238be 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -28,12 +28,13 @@ from astroid import InferenceError, builder, nodes from astroid.builder import parse from astroid.inference import infer_end as inference_infer_end -from astroid.bases import Instance, BoundMethod, UnboundMethod,\ - BUILTINS +from astroid.runtime.objects import ( + Instance, BoundMethod, UnboundMethod, FrozenSet +) + from astroid import arguments from astroid import decorators as decoratorsmod from astroid import helpers -from astroid import objects from astroid import test_utils from astroid import util from astroid.tests import resources @@ -44,6 +45,7 @@ def get_node_of_class(start_from, klass): builder = builder.AstroidBuilder() +BUILTINS = six.moves.builtins.__name__ if sys.version_info < (3, 0): EXC_MODULE = 'exceptions' BOOL_SPECIAL_METHOD = '__nonzero__' @@ -51,7 +53,6 @@ def get_node_of_class(start_from, klass): EXC_MODULE = BUILTINS BOOL_SPECIAL_METHOD = '__bool__' - class InferenceUtilsTest(unittest.TestCase): def test_path_wrapper(self): @@ -96,7 +97,7 @@ def assertInferDict(self, node, expected): assertInferTuple = partialmethod(_assertInferElts, nodes.Tuple) assertInferList = partialmethod(_assertInferElts, nodes.List) assertInferSet = partialmethod(_assertInferElts, nodes.Set) - assertInferFrozenSet = partialmethod(_assertInferElts, objects.FrozenSet) + assertInferFrozenSet = partialmethod(_assertInferElts, FrozenSet) CODE = ''' class C(object): diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index 56813ef2bf..f6bafbbb0b 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -25,7 +25,6 @@ import six -from astroid import bases from astroid import builder from astroid import context as contextmod from astroid import exceptions @@ -33,6 +32,7 @@ from astroid import nodes from astroid import parse from astroid.runtime import runtimeabc +from astroid.runtime import objects from astroid import util from astroid import test_utils from astroid import transforms @@ -560,7 +560,7 @@ def not_prop(self): pass self.assertEqual(inferred.value, 42, prop) inferred = next(ast['not_prop'].infer()) - self.assertIsInstance(inferred, bases.BoundMethod) + self.assertIsInstance(inferred, objects.BoundMethod) class AliasesTest(unittest.TestCase): @@ -757,10 +757,10 @@ def test_concrete_issubclass(self): base_type = getattr(treeabc, name) self.assertTrue(issubclass(node, base_type), (node, base_type)) - self.assertTrue(issubclass(bases.Instance, runtimeabc.Instance)) - self.assertTrue(issubclass(bases.Generator, runtimeabc.Generator)) - self.assertTrue(issubclass(bases.BoundMethod, runtimeabc.BoundMethod)) - self.assertTrue(issubclass(bases.UnboundMethod, runtimeabc.UnboundMethod)) + self.assertTrue(issubclass(objects.Instance, runtimeabc.Instance)) + self.assertTrue(issubclass(objects.Generator, runtimeabc.Generator)) + self.assertTrue(issubclass(objects.BoundMethod, runtimeabc.BoundMethod)) + self.assertTrue(issubclass(objects.UnboundMethod, runtimeabc.UnboundMethod)) if __name__ == '__main__': diff --git a/astroid/tests/unittest_objects.py b/astroid/tests/unittest_objects.py index e0a04d5fad..72fe8cf644 100644 --- a/astroid/tests/unittest_objects.py +++ b/astroid/tests/unittest_objects.py @@ -18,12 +18,17 @@ import unittest -from astroid import bases +import six + from astroid import exceptions from astroid import nodes -from astroid import objects +from astroid.runtime import objects from astroid import test_utils + +BUILTINS = six.moves.builtins.__name__ + + class ObjectsTest(unittest.TestCase): def test_frozenset(self): @@ -33,7 +38,7 @@ def test_frozenset(self): inferred = next(node.infer()) self.assertIsInstance(inferred, objects.FrozenSet) - self.assertEqual(inferred.pytype(), "%s.frozenset" % bases.BUILTINS) + self.assertEqual(inferred.pytype(), "%s.frozenset" % BUILTINS) itered = inferred.itered() self.assertEqual(len(itered), 2) @@ -41,7 +46,7 @@ def test_frozenset(self): self.assertEqual([const.value for const in itered], [1, 2]) proxied = inferred._proxied - self.assertEqual(inferred.qname(), "%s.frozenset" % bases.BUILTINS) + self.assertEqual(inferred.qname(), "%s.frozenset" % BUILTINS) self.assertIsInstance(proxied, nodes.ClassDef) @@ -62,16 +67,16 @@ def static(): super() #@ ''') in_static = next(ast_nodes[0].value.infer()) - self.assertIsInstance(in_static, bases.Instance) - self.assertEqual(in_static.qname(), "%s.super" % bases.BUILTINS) + self.assertIsInstance(in_static, objects.Instance) + self.assertEqual(in_static.qname(), "%s.super" % BUILTINS) module_level = next(ast_nodes[1].infer()) - self.assertIsInstance(module_level, bases.Instance) - self.assertEqual(in_static.qname(), "%s.super" % bases.BUILTINS) + self.assertIsInstance(module_level, objects.Instance) + self.assertEqual(in_static.qname(), "%s.super" % BUILTINS) no_arguments = next(ast_nodes[2].infer()) - self.assertIsInstance(no_arguments, bases.Instance) - self.assertEqual(no_arguments.qname(), "%s.super" % bases.BUILTINS) + self.assertIsInstance(no_arguments, objects.Instance) + self.assertEqual(no_arguments.qname(), "%s.super" % BUILTINS) def test_inferring_unbound_super_doesnt_work(self): node = test_utils.extract_node(''' @@ -80,8 +85,8 @@ def __init__(self): super(Test) #@ ''') unbounded = next(node.infer()) - self.assertIsInstance(unbounded, bases.Instance) - self.assertEqual(unbounded.qname(), "%s.super" % bases.BUILTINS) + self.assertIsInstance(unbounded, objects.Instance) + self.assertEqual(unbounded.qname(), "%s.super" % BUILTINS) def test_use_default_inference_on_not_inferring_args(self): ast_nodes = test_utils.extract_node(''' @@ -91,12 +96,12 @@ def __init__(self): super(Test, lala) #@ ''') first = next(ast_nodes[0].infer()) - self.assertIsInstance(first, bases.Instance) - self.assertEqual(first.qname(), "%s.super" % bases.BUILTINS) + self.assertIsInstance(first, objects.Instance) + self.assertEqual(first.qname(), "%s.super" % BUILTINS) second = next(ast_nodes[1].infer()) - self.assertIsInstance(second, bases.Instance) - self.assertEqual(second.qname(), "%s.super" % bases.BUILTINS) + self.assertIsInstance(second, objects.Instance) + self.assertEqual(second.qname(), "%s.super" % BUILTINS) @test_utils.require_version(maxver='3.0') def test_super_on_old_style_class(self): @@ -130,7 +135,7 @@ def test_classmethod(cls): ''') first = next(ast_nodes[0].infer()) self.assertIsInstance(first, objects.Super) - self.assertIsInstance(first.type, bases.Instance) + self.assertIsInstance(first.type, objects.Instance) self.assertEqual(first.type.name, 'Second') self.assertIsInstance(first.mro_pointer, nodes.ClassDef) self.assertEqual(first.mro_pointer.name, 'Second') @@ -167,7 +172,7 @@ class Fourth(Third): # super(Third, self) first = next(ast_nodes[0].infer()) self.assertIsInstance(first, objects.Super) - self.assertIsInstance(first.type, bases.Instance) + self.assertIsInstance(first.type, objects.Instance) self.assertEqual(first.type.name, 'Third') self.assertIsInstance(first.mro_pointer, nodes.ClassDef) self.assertEqual(first.mro_pointer.name, 'Third') @@ -175,7 +180,7 @@ class Fourth(Third): # super(Second, self) second = next(ast_nodes[1].infer()) self.assertIsInstance(second, objects.Super) - self.assertIsInstance(second.type, bases.Instance) + self.assertIsInstance(second.type, objects.Instance) self.assertEqual(second.type.name, 'Third') self.assertIsInstance(first.mro_pointer, nodes.ClassDef) self.assertEqual(second.mro_pointer.name, 'Second') @@ -251,7 +256,7 @@ def __init__(self): ''') inferred = next(node.infer()) proxied = inferred._proxied - self.assertEqual(proxied.qname(), "%s.super" % bases.BUILTINS) + self.assertEqual(proxied.qname(), "%s.super" % BUILTINS) self.assertIsInstance(proxied, nodes.ClassDef) def test_super_bound_model(self): @@ -282,7 +287,7 @@ def method(self): self.assertEqual(first.name, 'method') second = next(ast_nodes[1].infer()) - self.assertIsInstance(second, bases.BoundMethod) + self.assertIsInstance(second, objects.BoundMethod) self.assertEqual(second.bound.name, 'First') self.assertEqual(second.type, 'classmethod') @@ -291,19 +296,19 @@ def method(self): self.assertEqual(third.name, 'method') fourth = next(ast_nodes[3].infer()) - self.assertIsInstance(fourth, bases.BoundMethod) + self.assertIsInstance(fourth, objects.BoundMethod) self.assertEqual(fourth.bound.name, 'First') self.assertEqual(fourth.type, 'classmethod') # Super(type, obj) can lead to different attribute bindings # depending on the type of the place where super was called. fifth = next(ast_nodes[4].infer()) - self.assertIsInstance(fifth, bases.BoundMethod) + self.assertIsInstance(fifth, objects.BoundMethod) self.assertEqual(fifth.bound.name, 'First') self.assertEqual(fifth.type, 'method') sixth = next(ast_nodes[5].infer()) - self.assertIsInstance(sixth, bases.BoundMethod) + self.assertIsInstance(sixth, objects.BoundMethod) self.assertEqual(sixth.bound.name, 'First') self.assertEqual(sixth.type, 'classmethod') @@ -328,11 +333,11 @@ def __init__(self): ''') first = next(ast_nodes[0].infer()) - self.assertIsInstance(first, bases.BoundMethod) + self.assertIsInstance(first, objects.BoundMethod) self.assertEqual(first.bound.name, 'Second') second = next(ast_nodes[1].infer()) - self.assertIsInstance(second, bases.BoundMethod) + self.assertIsInstance(second, objects.BoundMethod) self.assertEqual(second.bound.name, 'First') with self.assertRaises(exceptions.InferenceError): @@ -386,13 +391,13 @@ def __init__(self): super(E, self).static #@ ''') first = next(ast_nodes[0].infer()) - self.assertIsInstance(first, bases.BoundMethod) + self.assertIsInstance(first, objects.BoundMethod) self.assertEqual(first.bound.name, 'C') second = next(ast_nodes[1].infer()) - self.assertIsInstance(second, bases.BoundMethod) + self.assertIsInstance(second, objects.BoundMethod) self.assertEqual(second.bound.name, 'B') third = next(ast_nodes[2].infer()) - self.assertIsInstance(third, bases.BoundMethod) + self.assertIsInstance(third, objects.BoundMethod) self.assertEqual(third.bound.name, 'B') fourth = next(ast_nodes[3].infer()) self.assertEqual(fourth.bound.name, 'A') @@ -417,7 +422,7 @@ def __init__(self): self.assertIsInstance(selfclass, nodes.ClassDef) self.assertEqual(selfclass.name, 'A') self_ = first.getattr('__self__')[0] - self.assertIsInstance(self_, bases.Instance) + self.assertIsInstance(self_, objects.Instance) self.assertEqual(self_.name, 'A') cls = first.getattr('__class__')[0] self.assertEqual(cls, first._proxied) @@ -477,9 +482,9 @@ def __init__(self): super(A, Missing) #@ ''') first = next(ast_nodes[0].infer()) - self.assertIsInstance(first, bases.Instance) + self.assertIsInstance(first, objects.Instance) second = next(ast_nodes[1].infer()) - self.assertIsInstance(second, bases.Instance) + self.assertIsInstance(second, objects.Instance) def test_super_invalid_types(self): node = test_utils.extract_node(''' diff --git a/astroid/tests/unittest_raw_building.py b/astroid/tests/unittest_raw_building.py index 2bdaac1777..c181451a6a 100644 --- a/astroid/tests/unittest_raw_building.py +++ b/astroid/tests/unittest_raw_building.py @@ -11,7 +11,9 @@ ) from astroid import test_utils from astroid import nodes -from astroid.bases import BUILTINS + + +BUILTINS = builtins.__name__ class RawBuildingTC(unittest.TestCase): diff --git a/astroid/tests/unittest_regrtest.py b/astroid/tests/unittest_regrtest.py index 3e22152971..9651f4738b 100644 --- a/astroid/tests/unittest_regrtest.py +++ b/astroid/tests/unittest_regrtest.py @@ -22,7 +22,6 @@ import six from astroid import MANAGER, Instance, nodes -from astroid.bases import BUILTINS from astroid.builder import AstroidBuilder from astroid import exceptions from astroid.raw_building import build_module @@ -32,6 +31,9 @@ from astroid import transforms +BUILTINS = six.moves.builtins.__name__ + + class NonRegressionTests(resources.AstroidCacheSetupMixin, unittest.TestCase): diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index 329aa6979b..293052dcae 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -24,6 +24,8 @@ import unittest import warnings +import six + from astroid import builder from astroid import nodes from astroid import scoped_nodes @@ -33,15 +35,17 @@ NoDefault, ResolveError, MroError, InconsistentMroError, DuplicateBasesError, ) -from astroid.bases import ( - BUILTINS, Instance, - BoundMethod, UnboundMethod, Generator +from astroid.runtime.objects import ( + Instance, BoundMethod, UnboundMethod, Generator ) from astroid import __pkginfo__ from astroid import test_utils from astroid.tests import resources +BUILTINS = six.moves.builtins.__name__ + + def _test_dict_interface(self, node, test_attr): self.assertIs(node[test_attr], node[test_attr]) self.assertIn(test_attr, node) From ed55a71b683ffed033aff943e5d76649bb3c03fc Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 1 Nov 2015 17:18:31 +0200 Subject: [PATCH 042/312] Move rebuilder.py into the tree namespace --- astroid/builder.py | 2 +- astroid/{ => tree}/rebuilder.py | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename astroid/{ => tree}/rebuilder.py (100%) diff --git a/astroid/builder.py b/astroid/builder.py index bbd548aafa..947befb45f 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -30,8 +30,8 @@ from astroid import manager from astroid import modutils from astroid import raw_building -from astroid import rebuilder from astroid.runtime import objects +from astroid.tree import rebuilder from astroid import util diff --git a/astroid/rebuilder.py b/astroid/tree/rebuilder.py similarity index 100% rename from astroid/rebuilder.py rename to astroid/tree/rebuilder.py From 1f09b9999e3e6b531dc94ef9b773603b2dc7bd1d Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 1 Nov 2015 17:27:32 +0200 Subject: [PATCH 043/312] Move node_classes and scoped_nodes into the tree namespace. --- astroid/__init__.py | 4 ++-- astroid/brain/brain_builtin_inference.py | 2 +- astroid/nodes.py | 4 ++-- astroid/protocols.py | 2 +- astroid/raw_building.py | 2 +- astroid/tests/testdata/python2/data/module.py | 2 +- astroid/tests/testdata/python3/data/module.py | 2 +- astroid/tests/unittest_lookup.py | 2 +- astroid/tests/unittest_modutils.py | 4 ++-- astroid/tests/unittest_nodes.py | 8 ++++---- astroid/tests/unittest_protocols.py | 2 +- astroid/tests/unittest_python3.py | 4 ++-- astroid/tests/unittest_scoped_nodes.py | 2 +- astroid/tests/unittest_utils.py | 2 +- astroid/{ => tree}/node_classes.py | 0 astroid/{ => tree}/scoped_nodes.py | 2 +- 16 files changed, 22 insertions(+), 22 deletions(-) rename astroid/{ => tree}/node_classes.py (100%) rename astroid/{ => tree}/scoped_nodes.py (99%) diff --git a/astroid/__init__.py b/astroid/__init__.py index af020256fe..2b6b7ebe0d 100644 --- a/astroid/__init__.py +++ b/astroid/__init__.py @@ -64,8 +64,8 @@ from astroid import inference from astroid import raw_building -from astroid.node_classes import are_exclusive, unpack_infer -from astroid.scoped_nodes import builtin_lookup +from astroid.tree.node_classes import are_exclusive, unpack_infer +from astroid.tree.scoped_nodes import builtin_lookup from astroid.builder import parse from astroid.util import YES diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index c5a8903690..a65f93eb64 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -18,7 +18,7 @@ from astroid import helpers from astroid import nodes from astroid.runtime import objects -from astroid import scoped_nodes +from astroid.tree import scoped_nodes from astroid import util diff --git a/astroid/nodes.py b/astroid/nodes.py index 2fd6cb6591..23f2872dd9 100644 --- a/astroid/nodes.py +++ b/astroid/nodes.py @@ -36,7 +36,7 @@ """ # pylint: disable=unused-import,redefined-builtin -from astroid.node_classes import ( +from astroid.tree.node_classes import ( Arguments, AssignAttr, Assert, Assign, AssignName, AugAssign, Repr, BinOp, BoolOp, Break, Call, Compare, Comprehension, Const, Continue, Decorators, DelAttr, DelName, Delete, @@ -51,7 +51,7 @@ # Node not present in the builtin ast module. DictUnpack, ) -from astroid.scoped_nodes import ( +from astroid.tree.scoped_nodes import ( Module, GeneratorExp, Lambda, DictComp, ListComp, SetComp, FunctionDef, ClassDef, AsyncFunctionDef, diff --git a/astroid/protocols.py b/astroid/protocols.py index 623e151a74..003d8c37b6 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -29,7 +29,7 @@ from astroid import context as contextmod from astroid import exceptions from astroid import decorators -from astroid import node_classes +from astroid.tree import node_classes from astroid import helpers from astroid import nodes from astroid.runtime import objects diff --git a/astroid/raw_building.py b/astroid/raw_building.py index ef6c36af4f..a8b0946f88 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -28,9 +28,9 @@ import six from astroid import manager -from astroid import node_classes from astroid import nodes from astroid.runtime import objects +from astroid.tree import node_classes MANAGER = manager.AstroidManager() diff --git a/astroid/tests/testdata/python2/data/module.py b/astroid/tests/testdata/python2/data/module.py index 6a67b9b684..da0381a65a 100644 --- a/astroid/tests/testdata/python2/data/module.py +++ b/astroid/tests/testdata/python2/data/module.py @@ -2,7 +2,7 @@ """ __revision__ = '$Id: module.py,v 1.2 2005-11-02 11:56:54 syt Exp $' -from astroid.node_classes import Name as NameNode +from astroid.tree.node_classes import Name as NameNode from astroid import modutils from astroid.utils import * import os.path diff --git a/astroid/tests/testdata/python3/data/module.py b/astroid/tests/testdata/python3/data/module.py index 2a5fb58cd8..abf4300f30 100644 --- a/astroid/tests/testdata/python3/data/module.py +++ b/astroid/tests/testdata/python3/data/module.py @@ -2,7 +2,7 @@ """ __revision__ = '$Id: module.py,v 1.2 2005-11-02 11:56:54 syt Exp $' -from astroid.node_classes import Name as NameNode +from astroid.tree.node_classes import Name as NameNode from astroid import modutils from astroid.utils import * import os.path diff --git a/astroid/tests/unittest_lookup.py b/astroid/tests/unittest_lookup.py index 805efd9e06..43dc9ee015 100644 --- a/astroid/tests/unittest_lookup.py +++ b/astroid/tests/unittest_lookup.py @@ -24,7 +24,7 @@ from astroid import builder from astroid import exceptions from astroid import nodes -from astroid import scoped_nodes +from astroid.tree import scoped_nodes from astroid import test_utils from astroid import util from astroid.tests import resources diff --git a/astroid/tests/unittest_modutils.py b/astroid/tests/unittest_modutils.py index 644d33f54d..3740aa2639 100644 --- a/astroid/tests/unittest_modutils.py +++ b/astroid/tests/unittest_modutils.py @@ -78,8 +78,8 @@ def test_knownValues_get_module_part_2(self): def test_knownValues_get_module_part_3(self): """relative import from given file""" - self.assertEqual(modutils.get_module_part('node_classes.AssName', - modutils.__file__), 'node_classes') + self.assertEqual(modutils.get_module_part('tree.node_classes.AssName', + modutils.__file__), 'tree.node_classes') def test_knownValues_get_compiled_module_part(self): self.assertEqual(modutils.get_module_part('math.log10'), 'math') diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index f6bafbbb0b..a4c7fb5051 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -28,7 +28,7 @@ from astroid import builder from astroid import context as contextmod from astroid import exceptions -from astroid import node_classes +from astroid.tree import node_classes from astroid import nodes from astroid import parse from astroid.runtime import runtimeabc @@ -319,8 +319,8 @@ def test_import_self_resolve(self): def test_from_self_resolve(self): namenode = next(self.module.igetattr('NameNode')) self.assertTrue(isinstance(namenode, nodes.ClassDef), namenode) - self.assertEqual(namenode.root().name, 'astroid.node_classes') - self.assertEqual(namenode.qname(), 'astroid.node_classes.Name') + self.assertEqual(namenode.root().name, 'astroid.tree.node_classes') + self.assertEqual(namenode.qname(), 'astroid.tree.node_classes.Name') self.assertEqual(namenode.pytype(), '%s.type' % BUILTINS) abspath = next(self.module2.igetattr('abspath')) self.assertTrue(isinstance(abspath, nodes.FunctionDef), abspath) @@ -345,7 +345,7 @@ def test_as_string(self): ast = self.module['modutils'] self.assertEqual(ast.as_string(), "from astroid import modutils") ast = self.module['NameNode'] - self.assertEqual(ast.as_string(), "from astroid.node_classes import Name as NameNode") + self.assertEqual(ast.as_string(), "from astroid.tree.node_classes import Name as NameNode") ast = self.module['os'] self.assertEqual(ast.as_string(), "import os.path") code = """from . import here diff --git a/astroid/tests/unittest_protocols.py b/astroid/tests/unittest_protocols.py index 9ba4f6ad1a..63cb6dce2a 100644 --- a/astroid/tests/unittest_protocols.py +++ b/astroid/tests/unittest_protocols.py @@ -22,7 +22,7 @@ from astroid import InferenceError from astroid import nodes from astroid import util -from astroid.node_classes import AssignName, Const, Name, Starred +from astroid.tree.node_classes import AssignName, Const, Name, Starred class ProtocolTests(unittest.TestCase): diff --git a/astroid/tests/unittest_python3.py b/astroid/tests/unittest_python3.py index 8701057186..1dd07c611e 100644 --- a/astroid/tests/unittest_python3.py +++ b/astroid/tests/unittest_python3.py @@ -19,9 +19,9 @@ import unittest from astroid import nodes -from astroid.node_classes import Assign, Expr, YieldFrom, Name, Const +from astroid.tree.node_classes import Assign, Expr, YieldFrom, Name, Const from astroid.builder import AstroidBuilder -from astroid.scoped_nodes import ClassDef, FunctionDef +from astroid.tree.scoped_nodes import ClassDef, FunctionDef from astroid.test_utils import require_version, extract_node diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index 293052dcae..1ef352eee2 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -28,7 +28,7 @@ from astroid import builder from astroid import nodes -from astroid import scoped_nodes +from astroid.tree import scoped_nodes from astroid import util from astroid.exceptions import ( InferenceError, NotFoundError, diff --git a/astroid/tests/unittest_utils.py b/astroid/tests/unittest_utils.py index 999f7ed174..225e67ec15 100644 --- a/astroid/tests/unittest_utils.py +++ b/astroid/tests/unittest_utils.py @@ -18,7 +18,7 @@ import unittest from astroid import builder, nodes -from astroid.node_classes import are_exclusive +from astroid.tree.node_classes import are_exclusive builder = builder.AstroidBuilder() diff --git a/astroid/node_classes.py b/astroid/tree/node_classes.py similarity index 100% rename from astroid/node_classes.py rename to astroid/tree/node_classes.py diff --git a/astroid/scoped_nodes.py b/astroid/tree/scoped_nodes.py similarity index 99% rename from astroid/scoped_nodes.py rename to astroid/tree/scoped_nodes.py index a2e567e698..4e7758e345 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -37,8 +37,8 @@ from astroid import exceptions from astroid import manager from astroid import mixins -from astroid import node_classes from astroid import decorators as decorators_mod +from astroid.tree import node_classes from astroid import util From c7d03eced84d57fff7690ec05d30a2bea03aec32 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 1 Nov 2015 17:32:48 +0200 Subject: [PATCH 044/312] Move singledispatch import into a common place, where it can be used by astroid. --- astroid/brain/brain_builtin_inference.py | 6 +----- astroid/tree/base.py | 6 +----- astroid/tree/node_classes.py | 4 ---- astroid/util.py | 5 +++++ 4 files changed, 7 insertions(+), 14 deletions(-) diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index a65f93eb64..341a010e0d 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -5,10 +5,6 @@ import sys from textwrap import dedent -try: - from functools import singledispatch as _singledispatch -except ImportError: - from singledispatch import singledispatch as _singledispatch import six from astroid import (MANAGER, UseInferenceDefault, NotFoundError, @@ -138,7 +134,7 @@ def _generic_inference(node, context, node_type, transform): return transformed -@_singledispatch +@util.singledispatch def _from_constants(kls, elts): """Get an instance of the given *kls* with the given elements set.""" elts = [nodes.const_factory(elt) for elt in elts] diff --git a/astroid/tree/base.py b/astroid/tree/base.py index a8954bf2d4..e70ddcc657 100644 --- a/astroid/tree/base.py +++ b/astroid/tree/base.py @@ -20,10 +20,6 @@ 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 @@ -368,7 +364,7 @@ def repr_tree(self, ids=False, include_linenos=False, within max_width characters, but can exceed it under some circumstances. """ - @_singledispatch + @util.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. diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index b2c179c3bb..127aaef4d3 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -21,10 +21,6 @@ import abc import pprint import warnings -try: - from functools import singledispatch as _singledispatch -except ImportError: - from singledispatch import singledispatch as _singledispatch import six diff --git a/astroid/util.py b/astroid/util.py index 7bef88091d..94818bc48d 100644 --- a/astroid/util.py +++ b/astroid/util.py @@ -25,6 +25,11 @@ import lazy_object_proxy import six +try: + from functools import singledispatch as singledispatch +except ImportError: + from singledispatch import singledispatch as singledispatch + def reraise(exception): '''Reraises an exception with the traceback from the current exception From a186a694ae06dd2c644bc623d505395bf72d8e3f Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 1 Nov 2015 18:08:14 +0200 Subject: [PATCH 045/312] Move the implementation of scopes into interpreter.scopes, which makes it more amenable to changes. --- astroid/interpreter/scope.py | 48 +++++++++++++++++++++++++++++++++ astroid/tests/unittest_nodes.py | 37 +++++++++++++++++++++++++ astroid/tree/base.py | 8 +++--- astroid/tree/node_classes.py | 4 --- astroid/tree/scoped_nodes.py | 6 ----- 5 files changed, 90 insertions(+), 13 deletions(-) create mode 100644 astroid/interpreter/scope.py diff --git a/astroid/interpreter/scope.py b/astroid/interpreter/scope.py new file mode 100644 index 0000000000..0f9f6bb7dc --- /dev/null +++ b/astroid/interpreter/scope.py @@ -0,0 +1,48 @@ +# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. +# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr +# +# This file is part of astroid. +# +# astroid is free software: you can redistribute it and/or modify it +# under the terms of the GNU Lesser General Public License as published by the +# Free Software Foundation, either version 2.1 of the License, or (at your +# option) any later version. +# +# astroid is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License +# for more details. +# +# You should have received a copy of the GNU Lesser General Public License along +# with astroid. If not, see . + +"""Implements logic for determing the scope of a node.""" +import six + +from astroid import util +from astroid.tree import treeabc + + +@util.singledispatch +def node_scope(node): + """Get the scope of the given node.""" + return node.parent.scope() + + +@node_scope.register(treeabc.Decorators) +def _decorators_scope(node): + return node.parent.parent.scope() + + +@node_scope.register(treeabc.Module) +@node_scope.register(treeabc.GeneratorExp) +@node_scope.register(treeabc.DictComp) +@node_scope.register(treeabc.SetComp) +@node_scope.register(treeabc.Lambda) +@node_scope.register(treeabc.FunctionDef) +@node_scope.register(treeabc.ClassDef) +def _scoped_nodes(node): + return node + +if six.PY3: + node_scope.register(treeabc.ListComp, _scoped_nodes) diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index a4c7fb5051..06783a1538 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -761,6 +761,43 @@ def test_concrete_issubclass(self): self.assertTrue(issubclass(objects.Generator, runtimeabc.Generator)) self.assertTrue(issubclass(objects.BoundMethod, runtimeabc.BoundMethod)) self.assertTrue(issubclass(objects.UnboundMethod, runtimeabc.UnboundMethod)) + + +class ScopeTest(unittest.TestCase): + + def test_decorators(self): + ast_node = test_utils.extract_node(''' + @test + def foo(): pass + ''') + decorators = ast_node.decorators + self.assertIsInstance(decorators.scope(), nodes.Module) + self.assertEqual(decorators.scope(), decorators.root()) + + def test_scoped_nodes(self): + module = parse(''' + def function(): + pass + genexp = (i for i in range(10)) + dictcomp = {i:i for i in range(10)} + setcomp = {i for i in range(10)} + listcomp = [i for i in range(10)] + lambd = lambda x: x + class classdef: pass + ''') + self.assertIsInstance(module.scope(), nodes.Module) + self.assertIsInstance(module['genexp'].parent.value.scope(), nodes.GeneratorExp) + self.assertIsInstance(module['dictcomp'].parent.value.scope(), nodes.DictComp) + self.assertIsInstance(module['setcomp'].parent.value.scope(), nodes.SetComp) + self.assertIsInstance(module['lambd'].parent.value.scope(), nodes.Lambda) + self.assertIsInstance(next(module['function'].infer()).scope(), nodes.FunctionDef) + self.assertIsInstance(next(module['classdef'].infer()).scope(), nodes.ClassDef) + + if six.PY3: + self.assertIsInstance(module['listcomp'].parent.value.scope(), nodes.ListComp) + else: + self.assertIsInstance(module['listcomp'].parent.value.scope(), nodes.Module) + if __name__ == '__main__': diff --git a/astroid/tree/base.py b/astroid/tree/base.py index e70ddcc657..f4ca188a71 100644 --- a/astroid/tree/base.py +++ b/astroid/tree/base.py @@ -25,6 +25,7 @@ from astroid import as_string from astroid import decorators from astroid import exceptions +from astroid.interpreter import scope from astroid import mixins from astroid.tree import treeabc from astroid import util @@ -173,11 +174,12 @@ def frame(self): return self.parent.frame() def scope(self): - """return the first node defining a new scope (i.e. Module, - FunctionDef, ClassDef, Lambda but also GenExpr) + """Get the first node defining a new scope + Scopes are introduced in Python by Module, FunctionDef, ClassDef, + Lambda, GenExpr and on Python 3, by comprehensions. """ - return self.parent.scope() + return scope.node_scope(self) def root(self): """return the root node of the tree, (i.e. a Module)""" diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index 127aaef4d3..fd8c955333 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -750,10 +750,6 @@ class Decorators(base.NodeNG): 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() - @util.register_implementation(treeabc.DelAttr) class DelAttr(mixins.ParentAssignTypeMixin, base.NodeNG): diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 4e7758e345..75406d5ed6 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -164,12 +164,6 @@ def frame(self): """ return self - def scope(self): - """return the first node defining a new scope (i.e. Module, - FunctionDef, ClassDef, Lambda but also GeneratorExp, DictComp and SetComp) - """ - return self - def _scope_lookup(self, node, name, offset=0): """XXX method for interfacing the scope lookup""" try: From 4b5b390e28de12b5335d95cf727fa7780d8d9d84 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 1 Nov 2015 18:16:55 +0200 Subject: [PATCH 046/312] Merge runtime namespace with interpreter namespace. --- astroid/__init__.py | 2 +- astroid/arguments.py | 4 ++-- astroid/brain/brain_builtin_inference.py | 2 +- astroid/brain/brain_stdlib.py | 2 +- astroid/builder.py | 2 +- astroid/helpers.py | 4 ++-- astroid/inference.py | 2 +- astroid/{runtime => interpreter}/objects.py | 2 +- astroid/{runtime => interpreter}/runtimeabc.py | 0 astroid/protocols.py | 4 ++-- astroid/raw_building.py | 2 +- astroid/runtime/__init__.py | 0 astroid/tests/unittest_brain.py | 2 +- astroid/tests/unittest_inference.py | 2 +- astroid/tests/unittest_nodes.py | 4 ++-- astroid/tests/unittest_objects.py | 2 +- astroid/tests/unittest_scoped_nodes.py | 2 +- astroid/tree/node_classes.py | 8 ++++---- astroid/tree/scoped_nodes.py | 10 +++++----- 19 files changed, 28 insertions(+), 28 deletions(-) rename astroid/{runtime => interpreter}/objects.py (99%) rename astroid/{runtime => interpreter}/runtimeabc.py (100%) delete mode 100644 astroid/runtime/__init__.py diff --git a/astroid/__init__.py b/astroid/__init__.py index 2b6b7ebe0d..2a0e3f7bbe 100644 --- a/astroid/__init__.py +++ b/astroid/__init__.py @@ -48,7 +48,7 @@ # pylint: disable=redefined-builtin, wildcard-import -from astroid.runtime.objects import Instance, BoundMethod, UnboundMethod +from astroid.interpreter.objects import Instance, BoundMethod, UnboundMethod # make a manager instance (borg) accessible from astroid package from astroid.manager import AstroidManager MANAGER = AstroidManager() diff --git a/astroid/arguments.py b/astroid/arguments.py index 37cea2a4be..e16dcd349b 100644 --- a/astroid/arguments.py +++ b/astroid/arguments.py @@ -18,9 +18,9 @@ from astroid import context as contextmod from astroid import exceptions +from astroid.interpreter import objects +from astroid.interpreter import runtimeabc from astroid import nodes -from astroid.runtime import objects -from astroid.runtime import runtimeabc from astroid import util import six diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index 341a010e0d..62e25bcad9 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -12,8 +12,8 @@ from astroid import arguments from astroid.builder import AstroidBuilder from astroid import helpers +from astroid.interpreter import objects from astroid import nodes -from astroid.runtime import objects from astroid.tree import scoped_nodes from astroid import util diff --git a/astroid/brain/brain_stdlib.py b/astroid/brain/brain_stdlib.py index 583a674174..cbaa6da531 100644 --- a/astroid/brain/brain_stdlib.py +++ b/astroid/brain/brain_stdlib.py @@ -7,8 +7,8 @@ from astroid import ( MANAGER, UseInferenceDefault, inference_tip, InferenceError, register_module_extender) -from astroid.runtime.objects import BoundMethod from astroid import exceptions +from astroid.interpreter.objects import BoundMethod from astroid import nodes from astroid.builder import AstroidBuilder from astroid import util diff --git a/astroid/builder.py b/astroid/builder.py index 947befb45f..24b994c2d7 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -27,10 +27,10 @@ import textwrap from astroid import exceptions +from astroid.interpreter import objects from astroid import manager from astroid import modutils from astroid import raw_building -from astroid.runtime import objects from astroid.tree import rebuilder from astroid import util diff --git a/astroid/helpers.py b/astroid/helpers.py index ab64594dac..b54a4269f2 100644 --- a/astroid/helpers.py +++ b/astroid/helpers.py @@ -22,12 +22,12 @@ import six -from astroid.tree import treeabc -from astroid.runtime import runtimeabc from astroid import context as contextmod from astroid import exceptions +from astroid.interpreter import runtimeabc from astroid import manager from astroid import raw_building +from astroid.tree import treeabc from astroid import util diff --git a/astroid/inference.py b/astroid/inference.py index ba78338776..245c9ac875 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -31,8 +31,8 @@ from astroid import manager from astroid import nodes from astroid import protocols -from astroid.runtime import objects from astroid.interpreter.util import infer_stmts +from astroid.interpreter import objects from astroid import util diff --git a/astroid/runtime/objects.py b/astroid/interpreter/objects.py similarity index 99% rename from astroid/runtime/objects.py rename to astroid/interpreter/objects.py index d84cc5147a..1c06287805 100644 --- a/astroid/runtime/objects.py +++ b/astroid/interpreter/objects.py @@ -32,8 +32,8 @@ from astroid import decorators from astroid import exceptions from astroid.interpreter.util import infer_stmts +from astroid.interpreter import runtimeabc from astroid import manager -from astroid.runtime import runtimeabc from astroid.tree import base from astroid.tree import treeabc from astroid import util diff --git a/astroid/runtime/runtimeabc.py b/astroid/interpreter/runtimeabc.py similarity index 100% rename from astroid/runtime/runtimeabc.py rename to astroid/interpreter/runtimeabc.py diff --git a/astroid/protocols.py b/astroid/protocols.py index 003d8c37b6..ce6d48707d 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -29,10 +29,10 @@ from astroid import context as contextmod from astroid import exceptions from astroid import decorators -from astroid.tree import node_classes +from astroid.interpreter import objects from astroid import helpers from astroid import nodes -from astroid.runtime import objects +from astroid.tree import node_classes from astroid import util diff --git a/astroid/raw_building.py b/astroid/raw_building.py index a8b0946f88..c1d1ee84f1 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -27,9 +27,9 @@ import six +from astroid.interpreter import objects from astroid import manager from astroid import nodes -from astroid.runtime import objects from astroid.tree import node_classes diff --git a/astroid/runtime/__init__.py b/astroid/runtime/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/astroid/tests/unittest_brain.py b/astroid/tests/unittest_brain.py index 672c115b53..9262391cf0 100644 --- a/astroid/tests/unittest_brain.py +++ b/astroid/tests/unittest_brain.py @@ -23,7 +23,7 @@ from astroid import MANAGER from astroid import builder from astroid import nodes -from astroid.runtime import objects +from astroid.interpreter import objects from astroid import test_utils from astroid import util import astroid diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index da2ee238be..296615ce43 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -28,7 +28,7 @@ from astroid import InferenceError, builder, nodes from astroid.builder import parse from astroid.inference import infer_end as inference_infer_end -from astroid.runtime.objects import ( +from astroid.interpreter.objects import ( Instance, BoundMethod, UnboundMethod, FrozenSet ) diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index 06783a1538..8f86cb2eb4 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -31,8 +31,8 @@ from astroid.tree import node_classes from astroid import nodes from astroid import parse -from astroid.runtime import runtimeabc -from astroid.runtime import objects +from astroid.interpreter import runtimeabc +from astroid.interpreter import objects from astroid import util from astroid import test_utils from astroid import transforms diff --git a/astroid/tests/unittest_objects.py b/astroid/tests/unittest_objects.py index 72fe8cf644..1052eb4a96 100644 --- a/astroid/tests/unittest_objects.py +++ b/astroid/tests/unittest_objects.py @@ -22,7 +22,7 @@ from astroid import exceptions from astroid import nodes -from astroid.runtime import objects +from astroid.interpreter import objects from astroid import test_utils diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index 1ef352eee2..c29c2fa671 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -35,7 +35,7 @@ NoDefault, ResolveError, MroError, InconsistentMroError, DuplicateBasesError, ) -from astroid.runtime.objects import ( +from astroid.interpreter.objects import ( Instance, BoundMethod, UnboundMethod, Generator ) from astroid import __pkginfo__ diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index fd8c955333..394bda9840 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -24,17 +24,17 @@ import six -from astroid.interpreter.util import infer_stmts from astroid.tree import base -from astroid.tree import treeabc -from astroid.runtime import runtimeabc -from astroid.runtime import objects from astroid import as_string from astroid import context as contextmod from astroid import decorators from astroid import exceptions +from astroid.interpreter.util import infer_stmts +from astroid.interpreter import runtimeabc +from astroid.interpreter import objects from astroid import manager from astroid import mixins +from astroid.tree import treeabc from astroid import util diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 75406d5ed6..c9960f0e8d 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -29,16 +29,16 @@ import six import wrapt -from astroid.interpreter.util import infer_stmts -from astroid.tree import base as treebase -from astroid.tree import treeabc -from astroid.runtime import objects from astroid import context as contextmod from astroid import exceptions +from astroid import decorators as decorators_mod +from astroid.interpreter import objects +from astroid.interpreter.util import infer_stmts from astroid import manager from astroid import mixins -from astroid import decorators as decorators_mod +from astroid.tree import base as treebase from astroid.tree import node_classes +from astroid.tree import treeabc from astroid import util From 27413464e1a65aaf49607fd6e1f6db14366cbfe9 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 2 Nov 2015 08:39:06 +0200 Subject: [PATCH 047/312] Move astpeephole in the tree namespace. --- astroid/tests/unittest_peephole.py | 2 +- astroid/{ => tree}/astpeephole.py | 0 astroid/tree/rebuilder.py | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) rename astroid/{ => tree}/astpeephole.py (100%) diff --git a/astroid/tests/unittest_peephole.py b/astroid/tests/unittest_peephole.py index 78349898cf..ec55b5d28d 100644 --- a/astroid/tests/unittest_peephole.py +++ b/astroid/tests/unittest_peephole.py @@ -23,11 +23,11 @@ import unittest import astroid -from astroid import astpeephole from astroid import builder from astroid import manager from astroid import test_utils from astroid.tests import resources +from astroid.tree import astpeephole MANAGER = manager.AstroidManager() diff --git a/astroid/astpeephole.py b/astroid/tree/astpeephole.py similarity index 100% rename from astroid/astpeephole.py rename to astroid/tree/astpeephole.py diff --git a/astroid/tree/rebuilder.py b/astroid/tree/rebuilder.py index d039adc112..6295bfa9a1 100644 --- a/astroid/tree/rebuilder.py +++ b/astroid/tree/rebuilder.py @@ -22,8 +22,8 @@ import _ast import sys -from astroid import astpeephole from astroid import nodes +from astroid.tree import astpeephole From f99953dd31ad2cf34f30f22ad6bbf218bb10e0b6 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Mon, 2 Nov 2015 09:38:38 -0500 Subject: [PATCH 048/312] Use conditional deps to simplify tox.ini --- tox.ini | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index f6ba87426c..bded6eea8d 100644 --- a/tox.ini +++ b/tox.ini @@ -1,8 +1,13 @@ [tox] +# Official list #envlist = py27, py33, py34, py35, pypy, jython, pylint + +# drone.io #envlist = py27, py33, pylint + +# For testing off drone.io---please don't delete. +# envlist py27, py34, pypy, jython, pylint envlist = py34 -skip_missing_interpreters = true [testenv] deps = From 27c3b564f3be10fdb988e64d1899fdcb632d3404 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 2 Nov 2015 17:53:28 +0200 Subject: [PATCH 049/312] Move unpack_infer and are_exclusive into the interpreter namespace. --- astroid/__init__.py | 2 +- astroid/interpreter/util.py | 77 +++++++++++++++++++++++++++++ astroid/protocols.py | 4 +- astroid/tests/unittest_nodes.py | 3 +- astroid/tests/unittest_utils.py | 2 +- astroid/tree/node_classes.py | 86 ++------------------------------- 6 files changed, 87 insertions(+), 87 deletions(-) diff --git a/astroid/__init__.py b/astroid/__init__.py index 2a0e3f7bbe..836ba47743 100644 --- a/astroid/__init__.py +++ b/astroid/__init__.py @@ -64,7 +64,7 @@ from astroid import inference from astroid import raw_building -from astroid.tree.node_classes import are_exclusive, unpack_infer +from astroid.interpreter.util import are_exclusive, unpack_infer from astroid.tree.scoped_nodes import builtin_lookup from astroid.builder import parse from astroid.util import YES diff --git a/astroid/interpreter/util.py b/astroid/interpreter/util.py index 76077e1a0f..2d3f9eb213 100644 --- a/astroid/interpreter/util.py +++ b/astroid/interpreter/util.py @@ -20,6 +20,7 @@ from astroid import context as contextmod from astroid import exceptions +from astroid.tree import treeabc from astroid import util @@ -52,3 +53,79 @@ def infer_stmts(stmts, context, frame=None): inferred = True if not inferred: raise exceptions.InferenceError(str(stmt)) + + +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, (treeabc.List, treeabc.Tuple)): + for elt in stmt.elts: + for inferred_elt in unpack_infer(elt, context): + yield inferred_elt + return + # if inferred is a final node, return it and stop + inferred = next(stmt.infer(context)) + if inferred is stmt: + yield inferred + return + # else, infer recursivly, except YES object that should be returned as is + for inferred in stmt.infer(context): + if inferred is util.YES: + yield inferred + else: + for inf_inf in unpack_infer(inferred, context): + yield inf_inf + + +def are_exclusive(stmt1, stmt2, exceptions=None): + """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, treeabc.If) and exceptions is None: + if (node.locate_child(previous)[1] + is not node.locate_child(children[node])[1]): + return True + elif isinstance(node, treeabc.TryExcept): + c2attr, c2node = node.locate_child(previous) + c1attr, c1node = node.locate_child(children[node]) + if c1node is not c2node: + if ((c2attr == 'body' + and c1attr == 'handlers' + and children[node].catch(exceptions)) or + (c2attr == 'handlers' and c1attr == 'body' and previous.catch(exceptions)) or + (c2attr == 'handlers' and c1attr == 'orelse') or + (c2attr == 'orelse' and c1attr == 'handlers')): + return True + elif c2attr == 'handlers' and c1attr == 'handlers': + return previous is not children[node] + return False + previous = node + node = node.parent + return False diff --git a/astroid/protocols.py b/astroid/protocols.py index ce6d48707d..51367da4c1 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -30,9 +30,9 @@ from astroid import exceptions from astroid import decorators from astroid.interpreter import objects +from astroid.interpreter import util as inferenceutil from astroid import helpers from astroid import nodes -from astroid.tree import node_classes from astroid import util @@ -371,7 +371,7 @@ def _resolve_asspart(parts, asspath, context): @decorators.raise_if_nothing_inferred def excepthandler_assigned_stmts(self, node, context=None, asspath=None): - for assigned in node_classes.unpack_infer(self.type): + for assigned in inferenceutil.unpack_infer(self.type): if isinstance(assigned, nodes.ClassDef): assigned = objects.Instance(assigned) yield assigned diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index 8f86cb2eb4..b056bbe277 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -33,6 +33,7 @@ from astroid import parse from astroid.interpreter import runtimeabc from astroid.interpreter import objects +from astroid.interpreter import util as inferenceutil from astroid import util from astroid import test_utils from astroid import transforms @@ -378,7 +379,7 @@ def test_bad_import_inference(self): astroid = builder.parse(code) handler_type = astroid.body[1].handlers[0].type - excs = list(node_classes.unpack_infer(handler_type)) + excs = list(inferenceutil.unpack_infer(handler_type)) # The number of returned object can differ on Python 2 # and Python 3. In one version, an additional item will # be returned, from the _pickle module, which is not diff --git a/astroid/tests/unittest_utils.py b/astroid/tests/unittest_utils.py index 225e67ec15..23c795880a 100644 --- a/astroid/tests/unittest_utils.py +++ b/astroid/tests/unittest_utils.py @@ -18,7 +18,7 @@ import unittest from astroid import builder, nodes -from astroid.tree.node_classes import are_exclusive +from astroid.interpreter.util import are_exclusive builder = builder.AstroidBuilder() diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index 394bda9840..0d789f1a1a 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -18,22 +18,20 @@ """Module for some node classes. More nodes in scoped_nodes.py """ -import abc -import pprint import warnings import six -from astroid.tree import base -from astroid import as_string from astroid import context as contextmod from astroid import decorators from astroid import exceptions from astroid.interpreter.util import infer_stmts from astroid.interpreter import runtimeabc from astroid.interpreter import objects +from astroid.interpreter import util as inferenceutil from astroid import manager from astroid import mixins +from astroid.tree import base from astroid.tree import treeabc from astroid import util @@ -42,82 +40,6 @@ MANAGER = manager.AstroidManager() -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: - for inferred_elt in unpack_infer(elt, context): - yield inferred_elt - return - # if inferred is a final node, return it and stop - inferred = next(stmt.infer(context)) - if inferred is stmt: - yield inferred - return - # else, infer recursivly, except YES object that should be returned as is - for inferred in stmt.infer(context): - if inferred is util.YES: - yield inferred - else: - for inf_inf in unpack_infer(inferred, context): - yield inf_inf - - -def are_exclusive(stmt1, stmt2, exceptions=None): - """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: - if ((c2attr == 'body' - and c1attr == 'handlers' - and children[node].catch(exceptions)) or - (c2attr == 'handlers' and c1attr == 'body' and previous.catch(exceptions)) or - (c2attr == 'handlers' and c1attr == 'orelse') or - (c2attr == 'orelse' and c1attr == 'handlers')): - return True - elif c2attr == 'handlers' and c1attr == 'handlers': - return previous is not children[node] - return False - previous = node - node = node.parent - return False - - def _container_getitem(instance, elts, index): """Get a slice or an item, using the given *index*, for the given sequence.""" if isinstance(index, slice): @@ -281,7 +203,7 @@ def _filter_stmts(self, stmts, frame, offset): # necessarily be done if the loop has no iteration, so we don't # want to clear previous assigments if any (hence the test on # optional_assign) - if not (optional_assign or are_exclusive(_stmts[pindex], node)): + if not (optional_assign or inferenceutil.are_exclusive(_stmts[pindex], node)): del _stmt_parents[pindex] del _stmts[pindex] if isinstance(node, AssignName): @@ -292,7 +214,7 @@ def _filter_stmts(self, stmts, frame, offset): _stmts = [] _stmt_parents = [] continue - if not are_exclusive(self, node): + if not inferenceutil.are_exclusive(self, node): _stmts.append(node) _stmt_parents.append(stmt.parent) return _stmts From 005e7842b73384e92b47b5556ec2e438bf4dbe99 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 2 Nov 2015 19:57:13 +0200 Subject: [PATCH 050/312] Move class_instance_as_index into interpreter.util --- astroid/helpers.py | 23 ----------------------- astroid/inference.py | 14 +++++++------- astroid/interpreter/util.py | 24 ++++++++++++++++++++++++ astroid/protocols.py | 2 +- 4 files changed, 32 insertions(+), 31 deletions(-) diff --git a/astroid/helpers.py b/astroid/helpers.py index b54a4269f2..685694538d 100644 --- a/astroid/helpers.py +++ b/astroid/helpers.py @@ -153,26 +153,3 @@ def is_subtype(type1, type2): def is_supertype(type1, type2): """Check if *type2* is a supertype of *type1*.""" return _type_check(type1, type2) - - -def class_instance_as_index(node): - """Get the value as an index for the given instance. - - If an instance provides an __index__ method, then it can - be used in some scenarios where an integer is expected, - for instance when multiplying or subscripting a list. - """ - context = contextmod.InferenceContext() - context.callcontext = contextmod.CallContext(args=[node]) - - try: - for inferred in node.igetattr('__index__', context=context): - if not isinstance(inferred, runtimeabc.BoundMethod): - continue - - for result in inferred.infer_call_result(node, context=context): - if (isinstance(result, treeabc.Const) - and isinstance(result.value, int)): - return result - except exceptions.InferenceError: - pass diff --git a/astroid/inference.py b/astroid/inference.py index 245c9ac875..733dd0ee71 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -31,7 +31,7 @@ from astroid import manager from astroid import nodes from astroid import protocols -from astroid.interpreter.util import infer_stmts +from astroid.interpreter import util as inferenceutil from astroid.interpreter import objects from astroid import util @@ -93,7 +93,7 @@ def infer_name(self, context=None): raise exceptions.UnresolvableName(self.name) context = context.clone() context.lookupname = self.name - return infer_stmts(stmts, context, frame) + return inferenceutil.infer_stmts(stmts, context, frame) nodes.Name._infer = decorators.path_wrapper(infer_name) nodes.AssignName.infer_lhs = infer_name # won't work with a path wrapper @@ -153,7 +153,7 @@ def infer_import_from(self, context=None, asname=True): context = contextmod.copy_context(context) context.lookupname = name stmts = module.getattr(name, ignore_locals=module is self.root()) - return infer_stmts(stmts, context) + return inferenceutil.infer_stmts(stmts, context) except exceptions.NotFoundError: util.reraise(exceptions.InferenceError(name)) nodes.ImportFrom._infer = infer_import_from @@ -185,8 +185,8 @@ def infer_global(self, context=None): if context.lookupname is None: raise exceptions.InferenceError() try: - return infer_stmts(self.root().getattr(context.lookupname), - context) + return inferenceutil.infer_stmts(self.root().getattr(context.lookupname), + context) except exceptions.NotFoundError: util.reraise(exceptions.InferenceError()) nodes.Global._infer = infer_global @@ -254,7 +254,7 @@ def infer_subscript(self, context=None): if all(elem is not _SLICE_SENTINEL for elem in (lower, upper, step)): index_value = slice(lower, upper, step) elif isinstance(index, objects.Instance): - index = helpers.class_instance_as_index(index) + index = inferenceutil.class_instance_as_index(index) if index: index_value = index.value else: @@ -676,7 +676,7 @@ def infer_assign(self, context=None): return stmt.infer(context) stmts = list(self.assigned_stmts(context=context)) - return infer_stmts(stmts, context) + return inferenceutil.infer_stmts(stmts, context) nodes.AssignName._infer = infer_assign nodes.AssignAttr._infer = infer_assign diff --git a/astroid/interpreter/util.py b/astroid/interpreter/util.py index 2d3f9eb213..9f07242e0b 100644 --- a/astroid/interpreter/util.py +++ b/astroid/interpreter/util.py @@ -20,6 +20,7 @@ from astroid import context as contextmod from astroid import exceptions +from astroid.interpreter import runtimeabc from astroid.tree import treeabc from astroid import util @@ -129,3 +130,26 @@ def are_exclusive(stmt1, stmt2, exceptions=None): previous = node node = node.parent return False + + +def class_instance_as_index(node): + """Get the value as an index for the given instance. + + If an instance provides an __index__ method, then it can + be used in some scenarios where an integer is expected, + for instance when multiplying or subscripting a list. + """ + context = contextmod.InferenceContext() + context.callcontext = contextmod.CallContext(args=[node]) + + try: + for inferred in node.igetattr('__index__', context=context): + if not isinstance(inferred, runtimeabc.BoundMethod): + continue + + for result in inferred.infer_call_result(node, context=context): + if (isinstance(result, treeabc.Const) + and isinstance(result.value, int)): + return result + except exceptions.InferenceError: + pass diff --git a/astroid/protocols.py b/astroid/protocols.py index 51367da4c1..1d1da6e83f 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -169,7 +169,7 @@ def tl_infer_binary_op(self, operator, other, context, method): yield _multiply_seq_by_int(self, other, context) elif isinstance(other, objects.Instance) and operator == '*': # Verify if the instance supports __index__. - as_index = helpers.class_instance_as_index(other) + as_index = inferenceutil.class_instance_as_index(other) if not as_index: yield util.YES else: From 90d07ea662fd29d06c85f0f6f05f28abf64f0f47 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 2 Nov 2015 20:04:08 +0200 Subject: [PATCH 051/312] Move has_known_bases, safe_infer, is_supertype and is_supertype into the interpreter namespace. --- astroid/brain/brain_builtin_inference.py | 3 +- astroid/helpers.py | 61 ------------------------ astroid/inference.py | 8 ++-- astroid/interpreter/util.py | 60 +++++++++++++++++++++++ astroid/protocols.py | 3 +- astroid/tests/unittest_helpers.py | 53 ++++++++++---------- astroid/tests/unittest_inference.py | 3 +- 7 files changed, 96 insertions(+), 95 deletions(-) diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index 62e25bcad9..79ec59cac9 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -13,6 +13,7 @@ from astroid.builder import AstroidBuilder from astroid import helpers from astroid.interpreter import objects +from astroid.interpreter import util as interpreterutil from astroid import nodes from astroid.tree import scoped_nodes from astroid import util @@ -472,7 +473,7 @@ def infer_slice(node, context=None): if not 0 < len(args) <= 3: raise UseInferenceDefault - args = list(map(helpers.safe_infer, args)) + args = list(map(interpreterutil.safe_infer, args)) for arg in args: if not arg or arg is util.YES: raise UseInferenceDefault diff --git a/astroid/helpers.py b/astroid/helpers.py index 685694538d..437b39de86 100644 --- a/astroid/helpers.py +++ b/astroid/helpers.py @@ -92,64 +92,3 @@ def object_type(node, context=None): if len(types) > 1 or not types: return util.YES return list(types)[0] - - -def safe_infer(node, context=None): - """Return the inferred value for the given node. - - Return None if inference failed or if there is some ambiguity (more than - one node has been inferred). - """ - try: - inferit = node.infer(context=context) - value = next(inferit) - except exceptions.InferenceError: - return - try: - next(inferit) - return # None if there is ambiguity on the inferred node - except exceptions.InferenceError: - return # there is some kind of ambiguity - except StopIteration: - return value - - -def has_known_bases(klass, context=None): - """Return true if all base classes of a class could be inferred.""" - try: - return klass._all_bases_known - except AttributeError: - pass - for base in klass.bases: - result = safe_infer(base, context=context) - # TODO: check for A->B->A->B pattern in class structure too? - if (not isinstance(result, treeabc.ClassDef) or - result is klass or - not has_known_bases(result, context=context)): - klass._all_bases_known = False - return False - klass._all_bases_known = True - return True - - -def _type_check(type1, type2): - if not all(map(has_known_bases, (type1, type2))): - return util.YES - - if not all([type1.newstyle, type2.newstyle]): - return False - try: - return type1 in type2.mro()[:-1] - except exceptions.MroError: - # The MRO is invalid. - return util.YES - - -def is_subtype(type1, type2): - """Check if *type1* is a subtype of *typ2*.""" - return _type_check(type2, type1) - - -def is_supertype(type1, type2): - """Check if *type2* is a supertype of *type1*.""" - return _type_check(type1, type2) diff --git a/astroid/inference.py b/astroid/inference.py index 733dd0ee71..46d65d660f 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -482,9 +482,9 @@ def _get_binop_flow(left, left_type, op, right, right_type, """ if _same_type(left_type, right_type): methods = [_bin_op(left, op, right, context)] - elif helpers.is_subtype(left_type, right_type): + elif inferenceutil.is_subtype(left_type, right_type): methods = [_bin_op(left, op, right, context)] - elif helpers.is_supertype(left_type, right_type): + elif inferenceutil.is_supertype(left_type, right_type): methods = [_bin_op(right, op, left, reverse_context, reverse=True), _bin_op(left, op, right, context)] else: @@ -514,10 +514,10 @@ def _get_aug_flow(left, left_type, aug_op, right, right_type, if _same_type(left_type, right_type): methods = [_aug_op(left, aug_op, right, context), _bin_op(left, op, right, context)] - elif helpers.is_subtype(left_type, right_type): + elif inferenceutil.is_subtype(left_type, right_type): methods = [_aug_op(left, aug_op, right, context), _bin_op(left, op, right, context)] - elif helpers.is_supertype(left_type, right_type): + elif inferenceutil.is_supertype(left_type, right_type): methods = [_aug_op(left, aug_op, right, context), _bin_op(right, op, left, reverse_context, reverse=True), _bin_op(left, op, right, context)] diff --git a/astroid/interpreter/util.py b/astroid/interpreter/util.py index 9f07242e0b..189ffa2c73 100644 --- a/astroid/interpreter/util.py +++ b/astroid/interpreter/util.py @@ -153,3 +153,63 @@ def class_instance_as_index(node): return result except exceptions.InferenceError: pass + +def safe_infer(node, context=None): + """Return the inferred value for the given node. + + Return None if inference failed or if there is some ambiguity (more than + one node has been inferred). + """ + try: + inferit = node.infer(context=context) + value = next(inferit) + except exceptions.InferenceError: + return + try: + next(inferit) + return # None if there is ambiguity on the inferred node + except exceptions.InferenceError: + return # there is some kind of ambiguity + except StopIteration: + return value + + +def has_known_bases(klass, context=None): + """Return true if all base classes of a class could be inferred.""" + try: + return klass._all_bases_known + except AttributeError: + pass + for base in klass.bases: + result = safe_infer(base, context=context) + # TODO: check for A->B->A->B pattern in class structure too? + if (not isinstance(result, treeabc.ClassDef) or + result is klass or + not has_known_bases(result, context=context)): + klass._all_bases_known = False + return False + klass._all_bases_known = True + return True + + +def _type_check(type1, type2): + if not all(map(has_known_bases, (type1, type2))): + return util.YES + + if not all([type1.newstyle, type2.newstyle]): + return False + try: + return type1 in type2.mro()[:-1] + except exceptions.MroError: + # The MRO is invalid. + return util.YES + + +def is_subtype(type1, type2): + """Check if *type1* is a subtype of *typ2*.""" + return _type_check(type2, type1) + + +def is_supertype(type1, type2): + """Check if *type2* is a supertype of *type1*.""" + return _type_check(type1, type2) diff --git a/astroid/protocols.py b/astroid/protocols.py index 1d1da6e83f..77e23d86d1 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -31,7 +31,6 @@ from astroid import decorators from astroid.interpreter import objects from astroid.interpreter import util as inferenceutil -from astroid import helpers from astroid import nodes from astroid import util @@ -143,7 +142,7 @@ def _multiply_seq_by_int(self, other, context): node = self.__class__() elts = [] for elt in self.elts: - infered = helpers.safe_infer(elt, context) + infered = inferenceutil.safe_infer(elt, context) if infered is None: infered = util.YES elts.append(infered) diff --git a/astroid/tests/unittest_helpers.py b/astroid/tests/unittest_helpers.py index 16307e1944..612f99428d 100644 --- a/astroid/tests/unittest_helpers.py +++ b/astroid/tests/unittest_helpers.py @@ -23,6 +23,7 @@ from astroid import builder from astroid import helpers +from astroid.interpreter import util as interpreterutil from astroid import manager from astroid import raw_building from astroid import test_utils @@ -183,15 +184,15 @@ class C(A): pass #@ int_subclass = ast_nodes[3] int_subclass = helpers.object_type(next(int_subclass.infer())) base_int = self._extract('int') - self.assertTrue(helpers.is_subtype(int_subclass, base_int)) - self.assertTrue(helpers.is_supertype(base_int, int_subclass)) + self.assertTrue(interpreterutil.is_subtype(int_subclass, base_int)) + self.assertTrue(interpreterutil.is_supertype(base_int, int_subclass)) - self.assertTrue(helpers.is_supertype(cls_a, cls_b)) - self.assertTrue(helpers.is_supertype(cls_a, cls_c)) - self.assertTrue(helpers.is_subtype(cls_b, cls_a)) - self.assertTrue(helpers.is_subtype(cls_c, cls_a)) - self.assertFalse(helpers.is_subtype(cls_a, cls_b)) - self.assertFalse(helpers.is_subtype(cls_a, cls_b)) + self.assertTrue(interpreterutil.is_supertype(cls_a, cls_b)) + self.assertTrue(interpreterutil.is_supertype(cls_a, cls_c)) + self.assertTrue(interpreterutil.is_subtype(cls_b, cls_a)) + self.assertTrue(interpreterutil.is_subtype(cls_c, cls_a)) + self.assertFalse(interpreterutil.is_subtype(cls_a, cls_b)) + self.assertFalse(interpreterutil.is_subtype(cls_a, cls_b)) @test_utils.require_version(maxver='3.0') def test_is_subtype_supertype_old_style_classes(self): @@ -201,10 +202,10 @@ class A: #@ class B(A): #@ pass ''') - self.assertFalse(helpers.is_subtype(cls_a, cls_b)) - self.assertFalse(helpers.is_subtype(cls_b, cls_a)) - self.assertFalse(helpers.is_supertype(cls_a, cls_b)) - self.assertFalse(helpers.is_supertype(cls_b, cls_a)) + self.assertFalse(interpreterutil.is_subtype(cls_a, cls_b)) + self.assertFalse(interpreterutil.is_subtype(cls_b, cls_a)) + self.assertFalse(interpreterutil.is_supertype(cls_a, cls_b)) + self.assertFalse(interpreterutil.is_supertype(cls_b, cls_a)) def test_is_subtype_supertype_mro_error(self): cls_e, cls_f = test_utils.extract_node(''' @@ -215,10 +216,10 @@ class D(B, C): pass class E(C, B): pass #@ class F(D, E): pass #@ ''') - self.assertFalse(helpers.is_subtype(cls_e, cls_f)) - self.assertEqual(helpers.is_subtype(cls_f, cls_e), util.YES) - self.assertEqual(helpers.is_supertype(cls_e, cls_f), util.YES) - self.assertFalse(helpers.is_supertype(cls_f, cls_e)) + self.assertFalse(interpreterutil.is_subtype(cls_e, cls_f)) + self.assertEqual(interpreterutil.is_subtype(cls_f, cls_e), util.YES) + self.assertEqual(interpreterutil.is_supertype(cls_e, cls_f), util.YES) + self.assertFalse(interpreterutil.is_supertype(cls_f, cls_e)) def test_is_subtype_supertype_unknown_bases(self): cls_a, cls_b = test_utils.extract_node(''' @@ -226,18 +227,18 @@ def test_is_subtype_supertype_unknown_bases(self): class A(Unknown): pass #@ class B(A): pass #@ ''') - self.assertTrue(helpers.is_subtype(cls_b, cls_a)) - self.assertTrue(helpers.is_supertype(cls_a, cls_b)) + self.assertTrue(interpreterutil.is_subtype(cls_b, cls_a)) + self.assertTrue(interpreterutil.is_supertype(cls_a, cls_b)) def test_is_subtype_supertype_unrelated_classes(self): cls_a, cls_b = test_utils.extract_node(''' class A(object): pass #@ class B(object): pass #@ ''') - self.assertFalse(helpers.is_subtype(cls_a, cls_b)) - self.assertFalse(helpers.is_subtype(cls_b, cls_a)) - self.assertFalse(helpers.is_supertype(cls_a, cls_b)) - self.assertFalse(helpers.is_supertype(cls_b, cls_a)) + self.assertFalse(interpreterutil.is_subtype(cls_a, cls_b)) + self.assertFalse(interpreterutil.is_subtype(cls_b, cls_a)) + self.assertFalse(interpreterutil.is_supertype(cls_a, cls_b)) + self.assertFalse(interpreterutil.is_supertype(cls_b, cls_a)) def test_is_subtype_supertype_classes_no_type_ancestor(self): cls_a = test_utils.extract_node(''' @@ -245,8 +246,8 @@ class A(object): #@ pass ''') builtin_type = self._extract('type') - self.assertFalse(helpers.is_supertype(builtin_type, cls_a)) - self.assertFalse(helpers.is_subtype(cls_a, builtin_type)) + self.assertFalse(interpreterutil.is_supertype(builtin_type, cls_a)) + self.assertFalse(interpreterutil.is_subtype(cls_a, builtin_type)) def test_is_subtype_supertype_classes_metaclasses(self): cls_a = test_utils.extract_node(''' @@ -254,8 +255,8 @@ class A(type): #@ pass ''') builtin_type = self._extract('type') - self.assertTrue(helpers.is_supertype(builtin_type, cls_a)) - self.assertTrue(helpers.is_subtype(cls_a, builtin_type)) + self.assertTrue(interpreterutil.is_supertype(builtin_type, cls_a)) + self.assertTrue(interpreterutil.is_subtype(cls_a, builtin_type)) @test_utils.require_version(maxver='3.0') def test_old_style_class(self): diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index 296615ce43..92fd5b2df8 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -31,6 +31,7 @@ from astroid.interpreter.objects import ( Instance, BoundMethod, UnboundMethod, FrozenSet ) +from astroid.interpreter.util import safe_infer from astroid import arguments from astroid import decorators as decoratorsmod @@ -3078,7 +3079,7 @@ def __getitem__(self, name): flow['app']['config'] = AttributeDict() flow['app']['config']['doffing'] = AttributeDict() #@ ''') - self.assertIsNone(helpers.safe_infer(ast_node.targets[0])) + self.assertIsNone(safe_infer(ast_node.targets[0])) def test_classmethod_inferred_by_context(self): ast_node = test_utils.extract_node(''' From 89d5d4b61e1d23118ba2874b0a5570c804c34ead Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 2 Nov 2015 20:34:16 +0200 Subject: [PATCH 052/312] Don't pass a callcontext to CallSite, only the arguments and the keyword arguments. --- astroid/arguments.py | 11 +---------- astroid/brain/brain_builtin_inference.py | 6 +++++- astroid/protocols.py | 5 +++-- astroid/tests/unittest_inference.py | 6 +++++- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/astroid/arguments.py b/astroid/arguments.py index e16dcd349b..87f3bc7d0b 100644 --- a/astroid/arguments.py +++ b/astroid/arguments.py @@ -36,9 +36,7 @@ class CallSite(object): and the argument name. """ - def __init__(self, callcontext): - args = callcontext.args - keywords = callcontext.keywords + def __init__(self, args, keywords): self.duplicated_keywords = set() self._unpacked_args = self._unpack_args(args) self._unpacked_kwargs = self._unpack_keywords(keywords) @@ -52,13 +50,6 @@ def __init__(self, callcontext): if value is not util.YES } - @classmethod - def from_call(cls, call_node): - """Get a CallSite object from the given Call node.""" - callcontext = contextmod.CallContext(call_node.args, - call_node.keywords) - return cls(callcontext) - def has_invalid_arguments(self): """Check if in the current CallSite were passed *invalid* arguments diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index 79ec59cac9..169a4c5110 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -254,7 +254,11 @@ def infer_dict(node, context=None): If a case can't be inferred, we'll fallback to default inference. """ - call = arguments.CallSite.from_call(node) + if node.keywords: + keywords = [(arg.arg, arg.value) for arg in node.keywords] + else: + keywords = [] + call = arguments.CallSite(node.args, keywords) if call.has_invalid_arguments() or call.has_invalid_keywords(): raise UseInferenceDefault diff --git a/astroid/protocols.py b/astroid/protocols.py index 77e23d86d1..b12535a1e9 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -287,7 +287,8 @@ def _arguments_infer_argname(self, name, context): return if context and context.callcontext: - call_site = arguments.CallSite(context.callcontext) + call_site = arguments.CallSite(context.callcontext.args, + context.callcontext.keywords) for value in call_site.infer_argument(self.parent, name, context): yield value return @@ -320,7 +321,7 @@ def arguments_assigned_stmts(self, node, context, asspath=None): callcontext = context.callcontext context = contextmod.copy_context(context) context.callcontext = None - args = arguments.CallSite(callcontext) + args = arguments.CallSite(callcontext.args, callcontext.keywords) return args.infer_argument(self.parent, node.name, context) return _arguments_infer_argname(self, node.name, context) diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index 92fd5b2df8..425dc4d709 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -3822,7 +3822,11 @@ class CallSiteTest(unittest.TestCase): @staticmethod def _call_site_from_call(call): - return arguments.CallSite.from_call(call) + if call.keywords: + keywords = [(arg.arg, arg.value) for arg in call.keywords] + else: + keywords = [] + return arguments.CallSite(call.args, keywords) def _test_call_site_pair(self, code, expected_args, expected_keywords): ast_node = test_utils.extract_node(code) From e5a6324a4050e578272f33f7d91a961c17f193f3 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Tue, 3 Nov 2015 00:18:32 -0500 Subject: [PATCH 053/312] Fix not returning function arguments correctly in get_locals and how EmptyNodes are constructed in ast_from_object --- astroid/decorators.py | 3 ++- astroid/raw_building.py | 16 ++++++++-------- astroid/scoped_nodes.py | 7 +++++-- astroid/tests/unittest_brain.py | 2 -- astroid/tests/unittest_builder.py | 8 +++++--- astroid/tests/unittest_inference.py | 12 +++++++++--- astroid/util.py | 2 -- 7 files changed, 29 insertions(+), 21 deletions(-) diff --git a/astroid/decorators.py b/astroid/decorators.py index 27ce983d36..421888d05d 100644 --- a/astroid/decorators.py +++ b/astroid/decorators.py @@ -122,12 +122,13 @@ def raise_if_nothing_inferred(func, instance, args, kwargs): propagate error information. ''' inferred = False - fields = {} + # fields = {} try: for node in func(*args, **kwargs): inferred = True yield node except exceptions.DefaultStop as e: fields = vars(e) + del fields['message'] if not inferred: raise exceptions.InferenceError(**fields) diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 25e0d149bf..a6f0e4c75a 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -145,15 +145,15 @@ def ast_from_object(object_, name=None): @_singledispatch def _ast_from_object(object_, built_objects, module, name=None, parent=None): - if name: - parent = nodes.Assign(parent=parent) - name_node = nodes.AssignName(name, parent=parent) + # if name: + # parent = nodes.Assign(parent=parent) + # name_node = nodes.AssignName(name, parent=parent) empty_node = nodes.EmptyNode(name=name, object_=object_, parent=parent) - if name: - parent.postinit(targets=[name_node], value=empty_node) - node = parent - else: - node = empty_node + # if name: + # parent.postinit(targets=[name_node], value=empty_node) + # node = parent + # else: + node = empty_node return node diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index 40a98ded32..7ac7cd93d6 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -1893,7 +1893,7 @@ def locals_empty(node, locals_): '''EmptyNodes add an object to the local variables under a specified name.''' if node.name: - locals_[node.name].append(node.object) + locals_[node.name].append(node) @_get_locals.register(node_classes.ReservedName) def locals_reserved_name(node, locals_): @@ -1904,11 +1904,14 @@ def locals_reserved_name(node, locals_): # pylint: disable=unused-variable; doesn't understand singledispatch @_get_locals.register(node_classes.Arguments) def locals_arguments(node, locals_): - '''Other names assigned by functions have AssignName nodes.''' + '''Other names assigned by functions have AssignName nodes that are + children of an Arguments node.''' if node.vararg: locals_[node.vararg].append(node) if node.kwarg: locals_[node.kwarg].append(node) + for n in node.get_children(): + _get_locals(n, locals_) # pylint: disable=unused-variable; doesn't understand singledispatch @_get_locals.register(node_classes.Import) diff --git a/astroid/tests/unittest_brain.py b/astroid/tests/unittest_brain.py index f249ef2c0c..1918ba6e91 100644 --- a/astroid/tests/unittest_brain.py +++ b/astroid/tests/unittest_brain.py @@ -122,8 +122,6 @@ def foo(fields): """) self.assertIs(util.YES, next(klass.infer())) - # @unittest.skipIf(sys.version_info[0] > 2, - # 'namedtuple inference is broken on Python 3') def test_namedtuple_advanced_inference(self): # urlparse return an object of class ParseResult, which has a # namedtuple call and a mixin as base classes diff --git a/astroid/tests/unittest_builder.py b/astroid/tests/unittest_builder.py index 72ed8181e6..fdaa6720b2 100644 --- a/astroid/tests/unittest_builder.py +++ b/astroid/tests/unittest_builder.py @@ -296,9 +296,11 @@ def test_inspect_build0(self): # check property has __init__ pclass = builtin_ast['property'] self.assertIn('__init__', pclass) - self.assertIsInstance(builtin_ast['None'], nodes.AssignName) - self.assertIsInstance(builtin_ast['True'], nodes.AssignName) - self.assertIsInstance(builtin_ast['False'], nodes.AssignName) + self.assertIsInstance(builtin_ast['None'], nodes.NameConstant) + self.assertIsInstance(builtin_ast['True'], nodes.NameConstant) + self.assertIsInstance(builtin_ast['False'], nodes.NameConstant) + self.assertIsInstance(builtin_ast['NotImplemented'], nodes.NameConstant) + self.assertIsInstance(builtin_ast['Ellipsis'], nodes.Ellipsis) if six.PY3: self.assertIsInstance(builtin_ast['Exception'], nodes.ClassDef) self.assertIsInstance(builtin_ast['NotImplementedError'], nodes.ClassDef) diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index bdc86d9daa..7943311148 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -83,7 +83,11 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): # additional assertInfer* method for builtin types def assertInferConst(self, node, expected): - inferred = next(node.infer()) + import pprint + try: + inferred = next(node.infer()) + except Exception as e: + pprint.pprint(vars(e)) self.assertIsInstance(inferred, nodes.Const) self.assertEqual(inferred.value, expected) @@ -1776,9 +1780,10 @@ def test_dict_invalid_args(self): self.assertEqual(inferred.qname(), "{}.dict".format(BUILTINS)) def test_str_methods(self): + # Compatibility issues + + # ' '.decode() #@ code = """ - ' '.decode() #@ - ' '.encode() #@ ' '.join('abcd') #@ ' '.replace('a', 'b') #@ @@ -1800,6 +1805,7 @@ def test_str_methods(self): ' '.count() #@ """ ast = test_utils.extract_node(code, __name__) + # import pdb; pdb.set_trace() self.assertInferConst(ast[0], u'') for i in range(1, 16): self.assertInferConst(ast[i], '') diff --git a/astroid/util.py b/astroid/util.py index 5b786abb42..687b2854dd 100644 --- a/astroid/util.py +++ b/astroid/util.py @@ -27,8 +27,6 @@ import lazy_object_proxy import six -from astroid import exceptions - JYTHON = True if platform.python_implementation() == 'Jython' else False From 2f5d7be968649df19f85cd688347a23463dd04db Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Tue, 3 Nov 2015 17:00:42 -0500 Subject: [PATCH 054/312] Convert _object_type to use the types from the builtins mock AST, fix a bug in BoundMethod._infer_type_new_call --- astroid/bases.py | 2 +- astroid/decorators.py | 2 +- astroid/helpers.py | 56 +++++++++++------------------ astroid/raw_building.py | 16 +++++---- astroid/tests/unittest_inference.py | 13 +++---- 5 files changed, 36 insertions(+), 53 deletions(-) diff --git a/astroid/bases.py b/astroid/bases.py index 6428c0ba5a..19d3f8bf57 100644 --- a/astroid/bases.py +++ b/astroid/bases.py @@ -378,7 +378,7 @@ def _infer_type_new_call(self, caller, context): assign = node_classes.Assign() assign.postinit(targets=node_classes.AssignName(key.value), value=value) - body.append[assign] + body.append(assign) # cls_locals[key.value].append(value) # print(attrs.repr_tree()) diff --git a/astroid/decorators.py b/astroid/decorators.py index 421888d05d..815b5407b3 100644 --- a/astroid/decorators.py +++ b/astroid/decorators.py @@ -122,7 +122,7 @@ def raise_if_nothing_inferred(func, instance, args, kwargs): propagate error information. ''' inferred = False - # fields = {} + fields = {} try: for node in func(*args, **kwargs): inferred = True diff --git a/astroid/helpers.py b/astroid/helpers.py index 897dd959f5..bc05372159 100644 --- a/astroid/helpers.py +++ b/astroid/helpers.py @@ -32,36 +32,7 @@ from astroid import util -BUILTINS = six.moves.builtins.__name__ - - -def _build_proxy_class(cls_name, builtins): - # TODO: fix with the node constructors - return nodes.ClassDef(name=cls_name, parent=builtins) - - -def _function_type(function, builtins): - if isinstance(function, scoped_nodes.Lambda): - if function.root().name == BUILTINS: - cls_name = 'builtin_function_or_method' - else: - cls_name = 'function' - elif isinstance(function, bases.BoundMethod): - if six.PY2: - cls_name = 'instancemethod' - else: - cls_name = 'method' - elif isinstance(function, bases.UnboundMethod): - if six.PY2: - cls_name = 'instancemethod' - else: - cls_name = 'function' - return _build_proxy_class(cls_name, builtins) - - def _object_type(node, context=None): - astroid_manager = manager.AstroidManager() - builtins = astroid_manager.astroid_cache[BUILTINS] context = context or contextmod.InferenceContext() for inferred in node.infer(context=context): @@ -71,11 +42,26 @@ def _object_type(node, context=None): if metaclass: yield metaclass continue - yield builtins.getattr('type')[0] + yield raw_building.astroid_builtins.getattr('type')[0] elif isinstance(inferred, (scoped_nodes.Lambda, bases.UnboundMethod)): - yield _function_type(inferred, builtins) + if isinstance(inferred, scoped_nodes.Lambda): + if inferred.root() is raw_building.astroid_builtins: + yield raw_building.astroid_builtins['BuiltinFunctionType'] + else: + yield raw_building.astroid_builtins['FunctionType'] + elif isinstance(inferred, bases.BoundMethod): + yield raw_building.astroid_builtins['MethodType'] + elif isinstance(inferred, bases.UnboundMethod): + if six.PY2: + yield raw_building.astroid_builtins['MethodType'] + else: + yield raw_building.astroid_builtins['FunctionType'] + else: + raise InferenceError('Function {func!r} inferred from {node!r} ' + 'has no identifiable type.', + node=node, func=inferred, contex=context) elif isinstance(inferred, scoped_nodes.Module): - yield _build_proxy_class('module', builtins) + yield raw_building.astroid_builtins['ModuleType'] else: yield inferred._proxied @@ -83,11 +69,9 @@ def _object_type(node, context=None): def object_type(node, context=None): """Obtain the type of the given node - This is used to implement the ``type`` builtin, which means that it's - used for inferring type calls, as well as used in a couple of other places - in the inference. The node will be inferred first, so this function can support all - sorts of objects, as long as they support inference. + sorts of objects, as long as they support inference. It will try to + retrieve the Python type, as returned by the builtin `type`. """ try: diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 1e64c6909d..263004f46b 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -475,12 +475,16 @@ def ast_from_ellipsis(ellipsis, built_objects, module, name=None, parent=None): # scoped_nodes._get_locals(n, locals_) # return locals_ -BUILTIN_TYPES = frozenset((type(None), type(NotImplemented), - types.GeneratorType, types.FunctionType, - types.MethodType)) - -# Initialize the built_objects map for the mock AST for builtins to -# ensure that the types are included as Name nodes, not explicit ASTs. +BUILTIN_TYPES = {type(None): 'NoneType', + type(NotImplemented): 'NotImplementedType', + types.GeneratorType: 'GeneratorType', + types.FunctionType: 'FunctionType', + types.MethodType: 'MethodType', + types.BuiltinFunctionType: 'BuiltinFunctionType', + types.ModuleType: 'ModuleType'} + +# Initialize the built_objects map for the builtins mock AST to ensure +# that the types are included as Name nodes, not explicit ASTs. built_objects = _ChainMap({t: True for t in BUILTIN_TYPES}) astroid_builtin = _ast_from_object(six.moves.builtins, _ChainMap({t: True for t in BUILTIN_TYPES}), diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index 7943311148..ba97cfea43 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -83,11 +83,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): # additional assertInfer* method for builtin types def assertInferConst(self, node, expected): - import pprint - try: - inferred = next(node.infer()) - except Exception as e: - pprint.pprint(vars(e)) + inferred = next(node.infer()) self.assertIsInstance(inferred, nodes.Const) self.assertEqual(inferred.value, expected) @@ -569,7 +565,7 @@ def test_qqch(self): ast = parse(code, __name__) xxx = ast['xxx'] self.assertSetEqual({n.__class__ for n in xxx.inferred()}, - {nodes.Const, util.YES.__class__}) + {nodes.NameConstant, util.YES.__class__}) def test_method_argument(self): code = ''' @@ -1780,10 +1776,9 @@ def test_dict_invalid_args(self): self.assertEqual(inferred.qname(), "{}.dict".format(BUILTINS)) def test_str_methods(self): - # Compatibility issues - - # ' '.decode() #@ code = """ + ' '.decode() #@ + ' '.encode() #@ ' '.join('abcd') #@ ' '.replace('a', 'b') #@ From 85030e7448e98d7ac099dc3c6e82dd144563afa8 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Wed, 4 Nov 2015 23:19:35 +0200 Subject: [PATCH 055/312] Add Function.called_with, which returns a CallSite with the given arguments. The returned CallSite object can be used to pair arguments with function's parameters. --- astroid/arguments.py | 232 ----------------------- astroid/brain/brain_builtin_inference.py | 3 +- astroid/protocols.py | 12 +- astroid/tests/unittest_inference.py | 82 -------- astroid/tests/unittest_scoped_nodes.py | 165 ++++++++++++++++ astroid/tree/scoped_nodes.py | 219 +++++++++++++++++++++ 6 files changed, 391 insertions(+), 322 deletions(-) delete mode 100644 astroid/arguments.py diff --git a/astroid/arguments.py b/astroid/arguments.py deleted file mode 100644 index 5611d0f9d7..0000000000 --- a/astroid/arguments.py +++ /dev/null @@ -1,232 +0,0 @@ -# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . - -from astroid import context as contextmod -from astroid import exceptions -from astroid.interpreter import objects -from astroid.interpreter import runtimeabc -from astroid import nodes -from astroid import util - -import six - - -class CallSite(object): - """Class for understanding arguments passed into a call site - - It needs a call context, which contains the arguments and the - keyword arguments that were passed into a given call site. - In order to infer what an argument represents, call - :meth:`infer_argument` with the corresponding function node - and the argument name. - """ - - def __init__(self, args, keywords): - self.duplicated_keywords = set() - self._unpacked_args = self._unpack_args(args) - self._unpacked_kwargs = self._unpack_keywords(keywords) - - self.positional_arguments = [ - arg for arg in self._unpacked_args - if arg is not util.YES - ] - self.keyword_arguments = { - key: value for key, value in self._unpacked_kwargs.items() - if value is not util.YES - } - - def has_invalid_arguments(self): - """Check if in the current CallSite were passed *invalid* arguments - - This can mean multiple things. For instance, if an unpacking - of an invalid object was passed, then this method will return True. - Other cases can be when the arguments can't be inferred by astroid, - for example, by passing objects which aren't known statically. - """ - return len(self.positional_arguments) != len(self._unpacked_args) - - def has_invalid_keywords(self): - """Check if in the current CallSite were passed *invalid* keyword arguments - - For instance, unpacking a dictionary with integer keys is invalid - (**{1:2}), because the keys must be strings, which will make this - method to return True. Other cases where this might return True if - objects which can't be inferred were passed. - """ - return len(self.keyword_arguments) != len(self._unpacked_kwargs) - - def _unpack_keywords(self, keywords): - values = {} - context = contextmod.InferenceContext() - for name, value in keywords: - if name is None: - # Then it's an unpacking operation (**) - try: - inferred = next(value.infer(context=context)) - except exceptions.InferenceError: - values[name] = util.YES - continue - - if not isinstance(inferred, nodes.Dict): - # Not something we can work with. - values[name] = util.YES - continue - - for dict_key, dict_value in inferred.items: - try: - dict_key = next(dict_key.infer(context=context)) - except exceptions.InferenceError: - values[name] = util.YES - continue - if not isinstance(dict_key, nodes.Const): - values[name] = util.YES - continue - if not isinstance(dict_key.value, six.string_types): - values[name] = util.YES - continue - if dict_key.value in values: - # The name is already in the dictionary - values[dict_key.value] = util.YES - self.duplicated_keywords.add(dict_key.value) - continue - values[dict_key.value] = dict_value - else: - values[name] = value - return values - - @staticmethod - def _unpack_args(args): - values = [] - context = contextmod.InferenceContext() - for arg in args: - if isinstance(arg, nodes.Starred): - try: - inferred = next(arg.value.infer(context=context)) - except exceptions.InferenceError: - values.append(util.YES) - continue - - if inferred is util.YES: - values.append(util.YES) - continue - if not hasattr(inferred, 'elts'): - values.append(util.YES) - continue - values.extend(inferred.elts) - else: - values.append(arg) - return values - - def infer_argument(self, funcnode, name, context): - """infer a function argument value according to the call context""" - if name in self.duplicated_keywords: - raise exceptions.InferenceError(name) - - # Look into the keywords first, maybe it's already there. - try: - return self.keyword_arguments[name].infer(context) - except KeyError: - pass - - # Too many arguments given and no variable arguments. - if len(self.positional_arguments) > len(funcnode.args.args): - if not funcnode.args.vararg: - raise exceptions.InferenceError(name) - - positional = self.positional_arguments[:len(funcnode.args.args)] - vararg = self.positional_arguments[len(funcnode.args.args):] - argindex = funcnode.args.find_argname(name)[0] - kwonlyargs = set(arg.name for arg in funcnode.args.kwonlyargs) - kwargs = { - key: value for key, value in self.keyword_arguments.items() - if key not in kwonlyargs - } - # If there are too few positionals compared to - # what the function expects to receive, check to see - # if the missing positional arguments were passed - # as keyword arguments and if so, place them into the - # positional args list. - if len(positional) < len(funcnode.args.args): - for func_arg in funcnode.args.args: - if func_arg.name in kwargs: - arg = kwargs.pop(func_arg.name) - positional.append(arg) - - if argindex is not None: - # 2. first argument of instance/class method - if argindex == 0 and funcnode.type in ('method', 'classmethod'): - if context.boundnode is not None: - boundnode = context.boundnode - else: - # XXX can do better ? - boundnode = funcnode.parent.frame() - - if isinstance(boundnode, nodes.ClassDef): - # Verify that we're accessing a method - # of the metaclass through a class, as in - # `cls.metaclass_method`. In this case, the - # first argument is always the class. - method_scope = funcnode.parent.scope() - if method_scope is boundnode.metaclass(): - return iter((boundnode, )) - - if funcnode.type == 'method': - if not isinstance(boundnode, runtimeabc.Instance): - boundnode = objects.Instance(boundnode) - return iter((boundnode,)) - if funcnode.type == 'classmethod': - return iter((boundnode,)) - # if we have a method, extract one position - # from the index, so we'll take in account - # the extra parameter represented by `self` or `cls` - if funcnode.type in ('method', 'classmethod'): - argindex -= 1 - # 2. search arg index - try: - return self.positional_arguments[argindex].infer(context) - except IndexError: - pass - - if funcnode.args.kwarg == name: - # It wants all the keywords that were passed into - # the call site. - if self.has_invalid_keywords(): - raise exceptions.InferenceError - kwarg = nodes.Dict(lineno=funcnode.args.lineno, - col_offset=funcnode.args.col_offset, - parent=funcnode.args) - kwarg.postinit([(nodes.const_factory(key), value) - for key, value in kwargs.items()]) - return iter((kwarg, )) - elif funcnode.args.vararg == name: - # It wants all the args that were passed into - # the call site. - if self.has_invalid_arguments(): - raise exceptions.InferenceError - args = nodes.Tuple(lineno=funcnode.args.lineno, - col_offset=funcnode.args.col_offset, - parent=funcnode.args) - args.postinit(vararg) - return iter((args, )) - - # Check if it's a default parameter. - try: - return funcnode.args.default_value(name).infer(context) - except exceptions.NoDefault: - pass - raise exceptions.InferenceError(name) diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index 169a4c5110..75e1bf53af 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -9,7 +9,6 @@ from astroid import (MANAGER, UseInferenceDefault, NotFoundError, inference_tip, InferenceError, UnresolvableName) -from astroid import arguments from astroid.builder import AstroidBuilder from astroid import helpers from astroid.interpreter import objects @@ -258,7 +257,7 @@ def infer_dict(node, context=None): keywords = [(arg.arg, arg.value) for arg in node.keywords] else: keywords = [] - call = arguments.CallSite(node.args, keywords) + call = scoped_nodes.CallSite(node.func, node.args, keywords) if call.has_invalid_arguments() or call.has_invalid_keywords(): raise UseInferenceDefault diff --git a/astroid/protocols.py b/astroid/protocols.py index a70c3a8b58..4a57339cd2 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -25,7 +25,6 @@ import six -from astroid import arguments from astroid import context as contextmod from astroid import exceptions from astroid import decorators @@ -291,9 +290,9 @@ def _arguments_infer_argname(self, name, context): return if context and context.callcontext: - call_site = arguments.CallSite(context.callcontext.args, - context.callcontext.keywords) - for value in call_site.infer_argument(self.parent, name, context): + call_site = self.parent.called_with(context.callcontext.args, + context.callcontext.keywords) + for value in call_site.infer_argument(name, context): yield value return @@ -325,8 +324,9 @@ def arguments_assigned_stmts(self, node, context, asspath=None): callcontext = context.callcontext context = contextmod.copy_context(context) context.callcontext = None - args = arguments.CallSite(callcontext.args, callcontext.keywords) - return args.infer_argument(self.parent, node.name, context) + call_site = self.parent.called_with(callcontext.args, + callcontext.keywords) + return call_site.infer_argument(node.name, context) return _arguments_infer_argname(self, node.name, context) nodes.Arguments.assigned_stmts = arguments_assigned_stmts diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index 2378f9f4ed..ce42f684e6 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -33,7 +33,6 @@ ) from astroid.interpreter.util import safe_infer -from astroid import arguments from astroid import decorators as decoratorsmod from astroid import helpers from astroid import test_utils @@ -3860,86 +3859,5 @@ def test_slice_type(self): self.assertEqual(inferred.name, 'slice') -class CallSiteTest(unittest.TestCase): - - @staticmethod - def _call_site_from_call(call): - if call.keywords: - keywords = [(arg.arg, arg.value) for arg in call.keywords] - else: - keywords = [] - return arguments.CallSite(call.args, keywords) - - def _test_call_site_pair(self, code, expected_args, expected_keywords): - ast_node = test_utils.extract_node(code) - call_site = self._call_site_from_call(ast_node) - self.assertEqual(len(call_site.positional_arguments), len(expected_args)) - self.assertEqual([arg.value for arg in call_site.positional_arguments], - expected_args) - self.assertEqual(len(call_site.keyword_arguments), len(expected_keywords)) - for keyword, value in expected_keywords.items(): - self.assertIn(keyword, call_site.keyword_arguments) - self.assertEqual(call_site.keyword_arguments[keyword].value, value) - - def _test_call_site(self, pairs): - for pair in pairs: - self._test_call_site_pair(*pair) - - @test_utils.require_version('3.5') - def test_call_site_starred_args(self): - pairs = [ - ( - "f(*(1, 2), *(2, 3), *(3, 4), **{'a':1}, **{'b': 2})", - [1, 2, 2, 3, 3, 4], - {'a': 1, 'b': 2} - ), - ( - "f(1, 2, *(3, 4), 5, *(6, 7), f=24, **{'c':3})", - [1, 2, 3, 4, 5, 6, 7], - {'f':24, 'c': 3}, - ), - # Too many fs passed into. - ( - "f(f=24, **{'f':24})", [], {}, - ), - ] - self._test_call_site(pairs) - - def test_call_site(self): - pairs = [ - ( - "f(1, 2)", [1, 2], {} - ), - ( - "f(1, 2, *(1, 2))", [1, 2, 1, 2], {} - ), - ( - "f(a=1, b=2, c=3)", [], {'a':1, 'b':2, 'c':3} - ) - ] - self._test_call_site(pairs) - - def _test_call_site_valid_arguments(self, values, invalid): - for value in values: - ast_node = test_utils.extract_node(value) - call_site = self._call_site_from_call(ast_node) - self.assertEqual(call_site.has_invalid_arguments(), invalid) - - def test_call_site_valid_arguments(self): - values = [ - "f(*lala)", "f(*1)", "f(*object)", - ] - self._test_call_site_valid_arguments(values, invalid=True) - values = [ - "f()", "f(*(1, ))", "f(1, 2, *(2, 3))", - ] - self._test_call_site_valid_arguments(values, invalid=False) - - def test_duplicated_keyword_arguments(self): - ast_node = test_utils.extract_node('f(f=24, **{"f": 25})') - site = self._call_site_from_call(ast_node) - self.assertIn('f', site.duplicated_keywords) - - if __name__ == '__main__': unittest.main() diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index c29c2fa671..9d502d3759 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -27,6 +27,7 @@ import six from astroid import builder +from astroid import context from astroid import nodes from astroid.tree import scoped_nodes from astroid import util @@ -559,6 +560,7 @@ def test(): self.assertEqual(inferred.value, 42) + class ClassNodeTest(ModuleLoader, unittest.TestCase): def test_dict_interface(self): @@ -1603,5 +1605,168 @@ def irelevant(self): self.assertEqual(len(parent.extra_decorators), 0) +class CallSiteTest(unittest.TestCase): + + @staticmethod + def _call_site_from_call(call, func): + if call.keywords: + keywords = [(arg.arg, arg.value) for arg in call.keywords] + else: + keywords = [] + return scoped_nodes.CallSite(func, call.args, keywords) + + def _call_site_from_code(self, code): + call = test_utils.extract_node(code) + return self._inferred_call_site_from_call(call) + + def _inferred_call_site_from_call(self, call): + inferred = next(call.func.infer()) + return self._call_site_from_call(call, inferred) + + def _test_call_site_pair(self, code, expected_args, expected_keywords): + ast_node = test_utils.extract_node(code) + call_site = self._call_site_from_call(ast_node, ast_node.func) + self.assertEqual(len(call_site.positional_arguments), len(expected_args)) + self.assertEqual([arg.value for arg in call_site.positional_arguments], + expected_args) + self.assertEqual(len(call_site.keyword_arguments), len(expected_keywords)) + for keyword, value in expected_keywords.items(): + self.assertIn(keyword, call_site.keyword_arguments) + self.assertEqual(call_site.keyword_arguments[keyword].value, value) + + def _test_call_site(self, pairs): + for pair in pairs: + self._test_call_site_pair(*pair) + + @test_utils.require_version('3.5') + def test_call_site_starred_args(self): + pairs = [ + ( + "f(*(1, 2), *(2, 3), *(3, 4), **{'a':1}, **{'b': 2})", + [1, 2, 2, 3, 3, 4], + {'a': 1, 'b': 2} + ), + ( + "f(1, 2, *(3, 4), 5, *(6, 7), f=24, **{'c':3})", + [1, 2, 3, 4, 5, 6, 7], + {'f':24, 'c': 3}, + ), + # Too many fs passed into. + ( + "f(f=24, **{'f':24})", [], {}, + ), + ] + self._test_call_site(pairs) + + def test_call_site(self): + pairs = [ + ( + "f(1, 2)", [1, 2], {} + ), + ( + "f(1, 2, *(1, 2))", [1, 2, 1, 2], {} + ), + ( + "f(a=1, b=2, c=3)", [], {'a':1, 'b':2, 'c':3} + ) + ] + self._test_call_site(pairs) + + def _test_call_site_valid_arguments(self, values, invalid): + for value in values: + ast_node = test_utils.extract_node(value) + call_site = self._call_site_from_call(ast_node, ast_node.func) + self.assertEqual(call_site.has_invalid_arguments(), invalid) + + def test_call_site_valid_arguments(self): + values = [ + "f(*lala)", "f(*1)", "f(*object)", + ] + self._test_call_site_valid_arguments(values, invalid=True) + values = [ + "f()", "f(*(1, ))", "f(1, 2, *(2, 3))", + ] + self._test_call_site_valid_arguments(values, invalid=False) + + def test_duplicated_keyword_arguments(self): + ast_node = test_utils.extract_node('f(f=24, **{"f": 25})') + site = self._call_site_from_call(ast_node, ast_node.func) + self.assertIn('f', site.duplicated_keywords) + + def test_duplicate_keywords(self): + call_site = self._call_site_from_code(''' + def test(a): pass + test(a=1, **{'a': 2}) #@ + ''') + with self.assertRaises(InferenceError): + call_site.infer_argument('a', context.InferenceContext()) + + def test_keyword_argument(self): + call_site = self._call_site_from_code(''' + def test(a): pass + test(a=1) #@ + ''') + inferred = next(call_site.infer_argument('a', context.InferenceContext())) + self.assertIsInstance(inferred, nodes.Const) + self.assertEqual(inferred.value, 1) + + def test_too_many_positional_arguments(self): + call_site = self._call_site_from_code(''' + def test(a): pass + test(1, 2) #@ + ''') + with self.assertRaises(InferenceError): + call_site.infer_argument('a', context.InferenceContext()) + + def test_first_argument_is_instance(self): + call_site = self._call_site_from_code(''' + class A: + def test(self): return self + A().test() #@ + ''') + inferred = next(call_site.infer_argument('self', context.InferenceContext())) + self.assertIsInstance(inferred, Instance) + self.assertEqual(inferred.name, 'A') + + def test_first_argument_is_class(self): + module = builder.parse(''' + class A(type): + def something(cls): return cls + import six + @six.add_metaclass(A) + class B(object): pass + B.something() #@ + ''') + bound = module['B'] + call_context = context.InferenceContext() + call_context.boundnode = bound + call = module.body[-1].value + call_site = self._inferred_call_site_from_call(call) + inferred = next(call_site.infer_argument('cls', call_context)) + + self.assertIsInstance(inferred, nodes.ClassDef) + self.assertEqual(inferred.name, 'B') + + def test_first_argument_is_class_for_classmethods(self): + call_site = self._call_site_from_code(''' + class A: + @classmethod + def test(cls): cls + A.test() #@ + ''') + inferred = next(call_site.infer_argument('cls', context.InferenceContext())) + self.assertIsInstance(inferred, nodes.ClassDef) + self.assertEqual(inferred.name, 'A') + + def test_positional_arguments(self): + call_site = self._call_site_from_code(''' + def test(a): pass + test(2) #@ + ''') + inferred = next(call_site.infer_argument('a', context.InferenceContext())) + self.assertIsInstance(inferred, nodes.Const) + self.assertEqual(inferred.value, 2) + + if __name__ == '__main__': unittest.main() diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index c9960f0e8d..a59fcab7ad 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -33,6 +33,7 @@ from astroid import exceptions from astroid import decorators as decorators_mod from astroid.interpreter import objects +from astroid.interpreter import runtimeabc from astroid.interpreter.util import infer_stmts from astroid import manager from astroid import mixins @@ -628,6 +629,212 @@ def _infer_decorator_callchain(node): if result.is_subtype_of('%s.staticmethod' % BUILTINS): return 'staticmethod' +class CallSite(object): + """Class for understanding arguments passed into a call site + + It needs a call context, which contains the arguments and the + keyword arguments that were passed into a given call site. + In order to infer what an argument represents, call + :meth:`infer_argument` with the corresponding function node + and the argument name. + """ + + def __init__(self, funcnode, args, keywords): + self._funcnode = funcnode + self.duplicated_keywords = set() + self._unpacked_args = self._unpack_args(args) + self._unpacked_kwargs = self._unpack_keywords(keywords) + + self.positional_arguments = [ + arg for arg in self._unpacked_args + if arg is not util.YES + ] + self.keyword_arguments = { + key: value for key, value in self._unpacked_kwargs.items() + if value is not util.YES + } + + def has_invalid_arguments(self): + """Check if in the current CallSite were passed *invalid* arguments + + This can mean multiple things. For instance, if an unpacking + of an invalid object was passed, then this method will return True. + Other cases can be when the arguments can't be inferred by astroid, + for example, by passing objects which aren't known statically. + """ + return len(self.positional_arguments) != len(self._unpacked_args) + + def has_invalid_keywords(self): + """Check if in the current CallSite were passed *invalid* keyword arguments + + For instance, unpacking a dictionary with integer keys is invalid + (**{1:2}), because the keys must be strings, which will make this + method to return True. Other cases where this might return True if + objects which can't be inferred were passed. + """ + return len(self.keyword_arguments) != len(self._unpacked_kwargs) + + def _unpack_keywords(self, keywords): + values = {} + context = contextmod.InferenceContext() + for name, value in keywords: + if name is None: + # Then it's an unpacking operation (**) + try: + inferred = next(value.infer(context=context)) + except exceptions.InferenceError: + values[name] = util.YES + continue + + if not isinstance(inferred, treeabc.Dict): + # Not something we can work with. + values[name] = util.YES + continue + + for dict_key, dict_value in inferred.items: + try: + dict_key = next(dict_key.infer(context=context)) + except exceptions.InferenceError: + values[name] = util.YES + continue + if not isinstance(dict_key, treeabc.Const): + values[name] = util.YES + continue + if not isinstance(dict_key.value, six.string_types): + values[name] = util.YES + continue + if dict_key.value in values: + # The name is already in the dictionary + values[dict_key.value] = util.YES + self.duplicated_keywords.add(dict_key.value) + continue + values[dict_key.value] = dict_value + else: + values[name] = value + return values + + @staticmethod + def _unpack_args(args): + values = [] + context = contextmod.InferenceContext() + for arg in args: + if isinstance(arg, treeabc.Starred): + try: + inferred = next(arg.value.infer(context=context)) + except exceptions.InferenceError: + values.append(util.YES) + continue + + if inferred is util.YES: + values.append(util.YES) + continue + if not hasattr(inferred, 'elts'): + values.append(util.YES) + continue + values.extend(inferred.elts) + else: + values.append(arg) + return values + + def infer_argument(self, name, context): + """infer a function argument value according to the call context""" + if name in self.duplicated_keywords: + raise exceptions.InferenceError(name) + + # Look into the keywords first, maybe it's already there. + try: + return self.keyword_arguments[name].infer(context) + except KeyError: + pass + + # Too many arguments given and no variable arguments. + if len(self.positional_arguments) > len(self._funcnode.args.args): + if not self._funcnode.args.vararg: + raise exceptions.InferenceError(name) + + positional = self.positional_arguments[:len(self._funcnode.args.args)] + vararg = self.positional_arguments[len(self._funcnode.args.args):] + argindex = self._funcnode.args.find_argname(name)[0] + kwonlyargs = set(arg.name for arg in self._funcnode.args.kwonlyargs) + kwargs = { + key: value for key, value in self.keyword_arguments.items() + if key not in kwonlyargs + } + # If there are too few positionals compared to + # what the function expects to receive, check to see + # if the missing positional arguments were passed + # as keyword arguments and if so, place them into the + # positional args list. + if len(positional) < len(self._funcnode.args.args): + for func_arg in self._funcnode.args.args: + if func_arg.name in kwargs: + arg = kwargs.pop(func_arg.name) + positional.append(arg) + + if argindex is not None: + # 2. first argument of instance/class method + if argindex == 0 and self._funcnode.type in ('method', 'classmethod'): + if context.boundnode is not None: + boundnode = context.boundnode + else: + # XXX can do better ? + boundnode = self._funcnode.parent.frame() + + if isinstance(boundnode, ClassDef): + # Verify that we're accessing a method + # of the metaclass through a class, as in + # `cls.metaclass_method`. In this case, the + # first argument is always the class. + method_scope = self._funcnode.parent.scope() + if method_scope is boundnode.metaclass(): + return iter((boundnode, )) + + if self._funcnode.type == 'method': + if not isinstance(boundnode, runtimeabc.Instance): + boundnode = objects.Instance(boundnode) + return iter((boundnode,)) + if self._funcnode.type == 'classmethod': + return iter((boundnode,)) + # if we have a method, extract one position + # from the index, so we'll take in account + # the extra parameter represented by `self` or `cls` + if self._funcnode.type in ('method', 'classmethod'): + argindex -= 1 + # 2. search arg index + try: + return self.positional_arguments[argindex].infer(context) + except IndexError: + pass + + if self._funcnode.args.kwarg == name: + # It wants all the keywords that were passed into + # the call site. + if self.has_invalid_keywords(): + raise exceptions.InferenceError + kwarg = node_classes.Dict(lineno=self._funcnode.args.lineno, + col_offset=self._funcnode.args.col_offset, + parent=self._funcnode.args) + kwarg.postinit([(node_classes.const_factory(key), value) + for key, value in kwargs.items()]) + return iter((kwarg, )) + elif self._funcnode.args.vararg == name: + # It wants all the args that were passed into + # the call site. + if self.has_invalid_arguments(): + raise exceptions.InferenceError + args = node_classes.Tuple(lineno=self._funcnode.args.lineno, + col_offset=self._funcnode.args.col_offset, + parent=self._funcnode.args) + args.postinit(vararg) + return iter((args, )) + + # Check if it's a default parameter. + try: + return self._funcnode.args.default_value(name).infer(context) + except exceptions.NoDefault: + pass + raise exceptions.InferenceError(name) + @util.register_implementation(treeabc.Lambda) class Lambda(mixins.FilterStmtsMixin, LocalsDictNodeNG): @@ -691,6 +898,18 @@ def scope_lookup(self, node, name, offset=0): def bool_value(self): return True + def called_with(self, args, keywords): + """Get a CallSite object with the given arguments + + Given these arguments, this will return an object + which considers them as being passed into the current function, + which can then be used to infer their values. + `args` needs to be a list of arguments, while `keywords` + needs to be a list of tuples, where each tuple is formed + by a keyword name and a keyword value. + """ + return CallSite(self, args, keywords) + @util.register_implementation(treeabc.FunctionDef) class FunctionDef(node_classes.Statement, Lambda): From 2d081b1d0bf8023d6f916929b560c91729eda409 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Wed, 4 Nov 2015 23:25:22 +0200 Subject: [PATCH 056/312] Use virtual checks instead of concrete object checks. --- astroid/protocols.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/astroid/protocols.py b/astroid/protocols.py index 4a57339cd2..c9ad98c12b 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -31,6 +31,7 @@ from astroid.interpreter import objects from astroid.interpreter import util as inferenceutil from astroid import nodes +from astroid.tree import treeabc from astroid import util @@ -116,7 +117,7 @@ def _infer_unary_op(obj, op): @decorators.yes_if_nothing_inferred def const_infer_binary_op(self, operator, other, context, _): not_implemented = nodes.Const(NotImplemented) - if isinstance(other, nodes.Const): + if isinstance(other, treeabc.Const): try: impl = BIN_OP_IMPL[operator] try: @@ -160,7 +161,7 @@ def tl_infer_binary_op(self, operator, other, context, method): if not n is util.YES] node.elts = elts yield node - elif isinstance(other, nodes.Const) and operator == '*': + elif isinstance(other, treeabc.Const) and operator == '*': if not isinstance(other.value, int): yield not_implemented return @@ -241,7 +242,7 @@ def _resolve_looppart(parts, asspath, context): def for_assigned_stmts(self, node, context=None, asspath=None): if asspath is None: for lst in self.iter.infer(context): - if isinstance(lst, (nodes.Tuple, nodes.List)): + if isinstance(lst, (treeabc.Tuple, treeabc.List)): for item in lst.elts: yield item else: @@ -279,7 +280,7 @@ def _arguments_infer_argname(self, name, context): if self.args and getattr(self.args[0], 'name', None) == name: functype = self.parent.type cls = self.parent.parent.scope() - is_metaclass = isinstance(cls, nodes.ClassDef) and cls.type == 'metaclass' + is_metaclass = isinstance(cls, treeabc.ClassDef) and cls.type == 'metaclass' # If this is a metaclass, then the first argument will always # be the class, not an instance. if is_metaclass or functype == 'classmethod': @@ -376,7 +377,7 @@ def _resolve_asspart(parts, asspath, context): @decorators.raise_if_nothing_inferred def excepthandler_assigned_stmts(self, node, context=None, asspath=None): for assigned in inferenceutil.unpack_infer(self.type): - if isinstance(assigned, nodes.ClassDef): + if isinstance(assigned, treeabc.ClassDef): assigned = objects.Instance(assigned) yield assigned @@ -395,7 +396,7 @@ def _infer_context_manager(self, mgr, context): return for decorator_node in func.decorators.nodes: decorator = next(decorator_node.infer(context)) - if isinstance(decorator, nodes.FunctionDef): + if isinstance(decorator, treeabc.FunctionDef): if decorator.qname() == _CONTEXTLIB_MGR: break else: @@ -405,7 +406,7 @@ def _infer_context_manager(self, mgr, context): # Get the first yield point. If it has multiple yields, # then a RuntimeError will be raised. # TODO(cpopa): Handle flows. - yield_point = next(func.nodes_of_class(nodes.Yield), None) + yield_point = next(func.nodes_of_class(treeabc.Yield), None) if yield_point: if not yield_point.value: # TODO(cpopa): an empty yield. Should be wrapped to Const. @@ -472,14 +473,14 @@ def __enter__(self): @decorators.yes_if_nothing_inferred def starred_assigned_stmts(self, node=None, context=None, asspath=None): stmt = self.statement() - if not isinstance(stmt, (nodes.Assign, nodes.For)): + if not isinstance(stmt, (treeabc.Assign, treeabc.For)): raise exceptions.InferenceError() - if isinstance(stmt, nodes.Assign): + if isinstance(stmt, treeabc.Assign): value = stmt.value lhs = stmt.targets[0] - if sum(1 for node in lhs.nodes_of_class(nodes.Starred)) > 1: + if sum(1 for node in lhs.nodes_of_class(treeabc.Starred)) > 1: # Too many starred arguments in the expression. raise exceptions.InferenceError() @@ -508,12 +509,12 @@ def starred_assigned_stmts(self, node=None, context=None, asspath=None): # to remvoe anything after the starred node. for index, node in enumerate(lhs.elts): - if not isinstance(node, nodes.Starred): + if not isinstance(node, treeabc.Starred): elts.popleft() continue lhs_elts = collections.deque(reversed(lhs.elts[index:])) for node in lhs_elts: - if not isinstance(node, nodes.Starred): + if not isinstance(node, treeabc.Starred): elts.pop() continue # We're done From 282ee076e73e0683752cafeba9c64156074b5e88 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Wed, 4 Nov 2015 23:45:31 +0200 Subject: [PATCH 057/312] Remove the circular dependency between protocols.py and node_classes.py This is accomplished with two types of modifications: * implementing dispatch functions on virtual base classes, which removes entirely the need of monkey-patching of node classes's methods. * since some of the functions in protocols.py needed to create new nodes, we injected the node_classes module into the dispatched function when calling it, which is a nice use case of dependency injection. This commit also includes new exceptions, which might be raised whenever trying to access a capability, with a dispatch function, on a node which does not support it. --- astroid/exceptions.py | 18 +++++ astroid/inference.py | 14 ++-- astroid/protocols.py | 151 +++++++++++++++++++---------------- astroid/tree/node_classes.py | 51 +++++++++--- 4 files changed, 145 insertions(+), 89 deletions(-) diff --git a/astroid/exceptions.py b/astroid/exceptions.py index b308258779..7fd906dd6c 100644 --- a/astroid/exceptions.py +++ b/astroid/exceptions.py @@ -68,6 +68,24 @@ class NoDefault(AstroidError): """ +class NotSupportedError(AstroidError): + """Exception raised whenever a capability is accessed on a node + which doesn't provide it. + """ + + +class UnaryOperationNotSupportedError(NotSupportedError): + """Internal exception raised by the inference when an object does not + suport unary operands. + """ + + +class BinaryOperationNotSupportedError(NotSupportedError): + """Internal exception raised by the inference when an object does + not support binary operations. + """ + + class OperationError(object): """Object which describes a TypeError occurred somewhere in the inference chain diff --git a/astroid/inference.py b/astroid/inference.py index 46d65d660f..871af194d5 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -350,11 +350,11 @@ def _infer_unaryop(self, context=None): """Infer what an UnaryOp should return when evaluated.""" for operand in self.operand.infer(context): try: - yield operand.infer_unary_op(self.op) + yield protocols.infer_unary_op(operand, self.op, nodes) except TypeError as exc: # The operand doesn't support this operation. yield exceptions.UnaryOperationError(operand, self.op, exc) - except AttributeError as exc: + except exceptions.UnaryOperationNotSupportedError as exc: meth = protocols.UNARY_OP_METHOD[self.op] if meth is None: # `not node`. Determine node's boolean @@ -414,7 +414,7 @@ def _invoke_binop_inference(instance, op, other, context, method_name): """Invoke binary operation inference on the given instance.""" method = instance.getattr(method_name)[0] inferred = next(method.infer(context=context)) - return instance.infer_binary_op(op, other, context, inferred) + return protocols.infer_binary_op(instance, op, other, context, inferred, nodes) def _aug_op(instance, op, other, context, reverse=False): @@ -543,9 +543,9 @@ def _infer_binary_operation(left, right, op, context, flow_factory): for method in methods: try: results = list(method()) - except AttributeError: - continue - except exceptions.NotFoundError: + except exceptions.BinaryOperationNotSupportedError: + continue + except (AttributeError, exceptions.NotFoundError): continue except exceptions.InferenceError: yield util.YES @@ -662,7 +662,7 @@ def infer_arguments(self, context=None): name = context.lookupname if name is None: raise exceptions.InferenceError() - return protocols._arguments_infer_argname(self, name, context) + return protocols._arguments_infer_argname(self, name, context, nodes) nodes.Arguments._infer = infer_arguments diff --git a/astroid/protocols.py b/astroid/protocols.py index c9ad98c12b..2ef247167e 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -26,11 +26,11 @@ import six from astroid import context as contextmod -from astroid import exceptions from astroid import decorators +from astroid import exceptions from astroid.interpreter import objects +from astroid.interpreter import runtimeabc from astroid.interpreter import util as inferenceutil -from astroid import nodes from astroid.tree import treeabc from astroid import util @@ -80,16 +80,41 @@ def _augmented_name(name): } -def _infer_unary_op(obj, op): +def _infer_unary_op(obj, op, nodes): func = _UNARY_OPERATORS[op] value = func(obj) return nodes.const_factory(value) -nodes.Tuple.infer_unary_op = lambda self, op: _infer_unary_op(tuple(self.elts), op) -nodes.List.infer_unary_op = lambda self, op: _infer_unary_op(self.elts, op) -nodes.Set.infer_unary_op = lambda self, op: _infer_unary_op(set(self.elts), op) -nodes.Const.infer_unary_op = lambda self, op: _infer_unary_op(self.value, op) -nodes.Dict.infer_unary_op = lambda self, op: _infer_unary_op(dict(self.items), op) + +@util.singledispatch +def infer_unary_op(self, op, nodes): + raise exceptions.UnaryOperationNotSupportedError + + +@infer_unary_op.register(treeabc.Tuple) +def _infer_unary_op_tuple(self, op, nodes): + return _infer_unary_op(tuple(self.elts), op, nodes) + + +@infer_unary_op.register(treeabc.List) +def _infer_unary_op_list(self, op, nodes): + return _infer_unary_op(self.elts, op, nodes) + + +@infer_unary_op.register(treeabc.Set) +def _infer_unary_op_set(self, op, nodes): + return _infer_unary_op(set(self.elts), op, nodes) + + +@infer_unary_op.register(treeabc.Const) +def _infer_unary_op_const(self, op, nodes): + return _infer_unary_op(self.value, op, nodes) + + +@infer_unary_op.register(treeabc.Dict) +def _infer_unary_op_dict(self, op, nodes): + return _infer_unary_op(dict(self.items), op, nodes) + # Binary operations @@ -114,9 +139,15 @@ def _infer_unary_op(obj, op): BIN_OP_IMPL[_KEY + '='] = _IMPL +@util.singledispatch +def infer_binary_op(self, operator, other, context, method, nodes): + raise exceptions.BinaryOperationNotSupportedError + + +@infer_binary_op.register(treeabc.Const) @decorators.yes_if_nothing_inferred -def const_infer_binary_op(self, operator, other, context, _): - not_implemented = nodes.Const(NotImplemented) +def const_infer_binary_op(self, operator, other, context, _, nodes): + not_implemented = nodes.const_factory(NotImplemented) if isinstance(other, treeabc.Const): try: impl = BIN_OP_IMPL[operator] @@ -135,8 +166,6 @@ def const_infer_binary_op(self, operator, other, context, _): else: yield not_implemented -nodes.Const.infer_binary_op = const_infer_binary_op - def _multiply_seq_by_int(self, other, context): node = self.__class__() @@ -150,9 +179,11 @@ def _multiply_seq_by_int(self, other, context): return node +@infer_binary_op.register(treeabc.Tuple) +@infer_binary_op.register(treeabc.List) @decorators.yes_if_nothing_inferred -def tl_infer_binary_op(self, operator, other, context, method): - not_implemented = nodes.Const(NotImplemented) +def tl_infer_binary_op(self, operator, other, context, method, nodes): + not_implemented = nodes.const_factory(NotImplemented) if isinstance(other, self.__class__) and operator == '+': node = self.__class__() elts = [n for elt in self.elts for n in elt.infer(context) @@ -166,7 +197,7 @@ def tl_infer_binary_op(self, operator, other, context, method): yield not_implemented return yield _multiply_seq_by_int(self, other, context) - elif isinstance(other, objects.Instance) and operator == '*': + elif isinstance(other, runtimeabc.Instance) and operator == '*': # Verify if the instance supports __index__. as_index = inferenceutil.class_instance_as_index(other) if not as_index: @@ -176,29 +207,17 @@ def tl_infer_binary_op(self, operator, other, context, method): else: yield not_implemented -nodes.Tuple.infer_binary_op = tl_infer_binary_op -nodes.List.infer_binary_op = tl_infer_binary_op - +@infer_binary_op.register(runtimeabc.Instance) @decorators.yes_if_nothing_inferred -def instance_infer_binary_op(self, operator, other, context, method): +def instance_infer_binary_op(self, operator, other, context, method, nodes): return method.infer_call_result(self, context) -objects.Instance.infer_binary_op = instance_infer_binary_op - -# assignment ################################################################## +@util.singledispatch +def assigned_stmts(self, nodes, node=None, context=None, asspath=None): + raise exceptions.NotSupportedError -"""the assigned_stmts method is responsible to return the assigned statement -(e.g. not inferred) according to the assignment type. - -The `asspath` argument is used to record the lhs path of the original node. -For instance if we want assigned statements for 'c' in 'a, (b,c)', asspath -will be [1, 1] once arrived to the Assign node. - -The `context` argument is the current inference context which should be given -to any intermediary inference necessary. -""" def _resolve_looppart(parts, asspath, context): """recursive function to resolve multiple assignments on loops""" @@ -238,8 +257,10 @@ def _resolve_looppart(parts, asspath, context): break +@assigned_stmts.register(treeabc.For) +@assigned_stmts.register(treeabc.Comprehension) @decorators.raise_if_nothing_inferred -def for_assigned_stmts(self, node, context=None, asspath=None): +def for_assigned_stmts(self, nodes, node=None, context=None, asspath=None): if asspath is None: for lst in self.iter.infer(context): if isinstance(lst, (treeabc.Tuple, treeabc.List)): @@ -250,27 +271,23 @@ def for_assigned_stmts(self, node, context=None, asspath=None): asspath, context): yield inferred -nodes.For.assigned_stmts = for_assigned_stmts -nodes.Comprehension.assigned_stmts = for_assigned_stmts - -def mulass_assigned_stmts(self, node, context=None, asspath=None): +@assigned_stmts.register(treeabc.Tuple) +@assigned_stmts.register(treeabc.List) +def mulass_assigned_stmts(self, nodes, node=None, context=None, asspath=None): if asspath is None: asspath = [] asspath.insert(0, self.elts.index(node)) return self.parent.assigned_stmts(self, context, asspath) -nodes.Tuple.assigned_stmts = mulass_assigned_stmts -nodes.List.assigned_stmts = mulass_assigned_stmts - -def assend_assigned_stmts(self, context=None): +@assigned_stmts.register(treeabc.AssignName) +@assigned_stmts.register(treeabc.AssignAttr) +def assend_assigned_stmts(self, nodes, node=None, context=None, asspath=None): return self.parent.assigned_stmts(self, context=context) -nodes.AssignName.assigned_stmts = assend_assigned_stmts -nodes.AssignAttr.assigned_stmts = assend_assigned_stmts -def _arguments_infer_argname(self, name, context): +def _arguments_infer_argname(self, name, context, nodes): # arguments information may be missing, in which case we can't do anything # more if not (self.args or self.vararg or self.kwarg): @@ -319,7 +336,8 @@ def _arguments_infer_argname(self, name, context): yield util.YES -def arguments_assigned_stmts(self, node, context, asspath=None): +@assigned_stmts.register(treeabc.Arguments) +def arguments_assigned_stmts(self, nodes, node=None, context=None, asspath=None): if context.callcontext: # reset call context/name callcontext = context.callcontext @@ -328,22 +346,19 @@ def arguments_assigned_stmts(self, node, context, asspath=None): call_site = self.parent.called_with(callcontext.args, callcontext.keywords) return call_site.infer_argument(node.name, context) - return _arguments_infer_argname(self, node.name, context) - -nodes.Arguments.assigned_stmts = arguments_assigned_stmts + return _arguments_infer_argname(self, node.name, context, nodes) +@assigned_stmts.register(treeabc.Assign) +@assigned_stmts.register(treeabc.AugAssign) @decorators.raise_if_nothing_inferred -def assign_assigned_stmts(self, node, context=None, asspath=None): +def assign_assigned_stmts(self, nodes, node=None, context=None, asspath=None): if not asspath: yield self.value return for inferred in _resolve_asspart(self.value.infer(context), asspath, context): yield inferred -nodes.Assign.assigned_stmts = assign_assigned_stmts -nodes.AugAssign.assigned_stmts = assign_assigned_stmts - def _resolve_asspart(parts, asspath, context): """recursive function to resolve multiple assignments""" @@ -374,22 +389,21 @@ def _resolve_asspart(parts, asspath, context): return +@assigned_stmts.register(treeabc.ExceptHandler) @decorators.raise_if_nothing_inferred -def excepthandler_assigned_stmts(self, node, context=None, asspath=None): +def excepthandler_assigned_stmts(self, nodes, node=None, context=None, asspath=None): for assigned in inferenceutil.unpack_infer(self.type): if isinstance(assigned, treeabc.ClassDef): assigned = objects.Instance(assigned) yield assigned -nodes.ExceptHandler.assigned_stmts = excepthandler_assigned_stmts - -def _infer_context_manager(self, mgr, context): +def _infer_context_manager(self, mgr, context, nodes): try: inferred = next(mgr.infer(context=context)) except exceptions.InferenceError: return - if isinstance(inferred, objects.Generator): + if isinstance(inferred, runtimeabc.Generator): # Check if it is decorated with contextlib.contextmanager. func = inferred.parent if not func.decorators: @@ -410,19 +424,19 @@ def _infer_context_manager(self, mgr, context): if yield_point: if not yield_point.value: # TODO(cpopa): an empty yield. Should be wrapped to Const. - const = nodes.Const(None) + const = nodes.const_factory(None) const.parent = yield_point const.lineno = yield_point.lineno yield const else: for inferred in yield_point.value.infer(context=context): yield inferred - elif isinstance(inferred, objects.Instance): + elif isinstance(inferred, runtimeabc.Instance): try: enter = next(inferred.igetattr('__enter__', context=context)) except (exceptions.InferenceError, exceptions.NotFoundError): return - if not isinstance(enter, objects.BoundMethod): + if not isinstance(enter, runtimeabc.BoundMethod): return if not context.callcontext: context.callcontext = contextmod.CallContext(args=[inferred]) @@ -430,8 +444,9 @@ def _infer_context_manager(self, mgr, context): yield result +@assigned_stmts.register(treeabc.With) @decorators.raise_if_nothing_inferred -def with_assigned_stmts(self, node, context=None, asspath=None): +def with_assigned_stmts(self, nodes, node=None, context=None, asspath=None): """Infer names and other nodes from a *with* statement. This enables only inference for name binding in a *with* statement. @@ -451,10 +466,10 @@ def __enter__(self): """ mgr = next(mgr for (mgr, vars) in self.items if vars == node) if asspath is None: - for result in _infer_context_manager(self, mgr, context): + for result in _infer_context_manager(self, mgr, context, nodes): yield result else: - for result in _infer_context_manager(self, mgr, context): + for result in _infer_context_manager(self, mgr, context, nodes): # Walk the asspath and get the item at the final index. obj = result for index in asspath: @@ -467,11 +482,9 @@ def __enter__(self): yield obj -nodes.With.assigned_stmts = with_assigned_stmts - - +@assigned_stmts.register(treeabc.Starred) @decorators.yes_if_nothing_inferred -def starred_assigned_stmts(self, node=None, context=None, asspath=None): +def starred_assigned_stmts(self, nodes, node=None, context=None, asspath=None): stmt = self.statement() if not isinstance(stmt, (treeabc.Assign, treeabc.For)): raise exceptions.InferenceError() @@ -506,7 +519,7 @@ def starred_assigned_stmts(self, node=None, context=None, asspath=None): # be the list of values which the Starred node will represent # This is done in two steps, from left to right to remove # anything before the starred node and from right to left - # to remvoe anything after the starred node. + # to remove anything after the starred node. for index, node in enumerate(lhs.elts): if not isinstance(node, treeabc.Starred): @@ -523,5 +536,3 @@ def starred_assigned_stmts(self, node=None, context=None, asspath=None): packed.parent = self yield packed break - -nodes.Starred.assigned_stmts = starred_assigned_stmts diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index 0d789f1a1a..9963e2f5ca 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -19,6 +19,7 @@ """ import warnings +import sys import six @@ -31,6 +32,7 @@ from astroid.interpreter import util as inferenceutil from astroid import manager from astroid import mixins +from astroid import protocols from astroid.tree import base from astroid.tree import treeabc from astroid import util @@ -73,6 +75,27 @@ def previous_sibling(self): return stmts[index -1] +class AssignedStmtsMixin(object): + """Provide an `assigned_stmts` method to classes which inherits it.""" + + def assigned_stmts(self, node=None, context=None, asspath=None): + """Responsible to return the assigned statement + (e.g. not inferred) according to the assignment type. + + The `asspath` parameter is used to record the lhs path of the original node. + For instance if we want assigned statements for 'c' in 'a, (b,c)', asspath + will be [1, 1] once arrived to the Assign node. + + The `context` parameter is the current inference context which should be given + to any intermediary inference necessary. + """ + # Inject the current module into assigned_stmts, in order to avoid + # circular dependencies between these modules. + return protocols.assigned_stmts(self, sys.modules[__name__], + node=node, context=context, + asspath=asspath) + + class LookupMixIn(object): """Mixin looking up a name in the right scope """ @@ -223,7 +246,8 @@ def _filter_stmts(self, stmts, frame, offset): # Name classes @util.register_implementation(treeabc.AssignName) -class AssignName(LookupMixIn, mixins.ParentAssignTypeMixin, base.NodeNG): +class AssignName(LookupMixIn, mixins.ParentAssignTypeMixin, + AssignedStmtsMixin, base.NodeNG): """class representing an AssignName node""" _other_fields = ('name',) @@ -253,7 +277,7 @@ def __init__(self, name=None, lineno=None, col_offset=None, parent=None): @util.register_implementation(treeabc.Arguments) -class Arguments(mixins.AssignTypeMixin, base.NodeNG): +class Arguments(mixins.AssignTypeMixin, AssignedStmtsMixin, base.NodeNG): """class representing an Arguments node""" if six.PY3: # Python 3.4+ uses a different approach regarding annotations, @@ -397,7 +421,8 @@ def _format_args(args, defaults=None, annotations=None): @util.register_implementation(treeabc.AssignAttr) -class AssignAttr(mixins.ParentAssignTypeMixin, base.NodeNG): +class AssignAttr(mixins.ParentAssignTypeMixin, + AssignedStmtsMixin, base.NodeNG): """class representing an AssignAttr node""" _astroid_fields = ('expr',) _other_fields = ('attrname',) @@ -424,7 +449,7 @@ def postinit(self, test=None, fail=None): @util.register_implementation(treeabc.Assign) -class Assign(mixins.AssignTypeMixin, Statement): +class Assign(mixins.AssignTypeMixin, AssignedStmtsMixin, Statement): """class representing an Assign node""" _astroid_fields = ('targets', 'value',) targets = None @@ -436,7 +461,7 @@ def postinit(self, targets=None, value=None): @util.register_implementation(treeabc.AugAssign) -class AugAssign(mixins.AssignTypeMixin, Statement): +class AugAssign(mixins.AssignTypeMixin, AssignedStmtsMixin, Statement): """class representing an AugAssign node""" _astroid_fields = ('target', 'value') _other_fields = ('op',) @@ -582,7 +607,7 @@ def last_child(self): @util.register_implementation(treeabc.Comprehension) -class Comprehension(base.NodeNG): +class Comprehension(AssignedStmtsMixin, base.NodeNG): """class representing a Comprehension node""" _astroid_fields = ('target', 'iter', 'ifs') target = None @@ -775,7 +800,7 @@ class EmptyNode(base.NodeNG): @util.register_implementation(treeabc.ExceptHandler) -class ExceptHandler(mixins.AssignTypeMixin, Statement): +class ExceptHandler(mixins.AssignTypeMixin, AssignedStmtsMixin, Statement): """class representing an ExceptHandler node""" _astroid_fields = ('type', 'name', 'body',) type = None @@ -829,7 +854,8 @@ def postinit(self, dims=None): @util.register_implementation(treeabc.For) -class For(mixins.BlockRangeMixIn, mixins.AssignTypeMixin, Statement): +class For(mixins.BlockRangeMixIn, mixins.AssignTypeMixin, + AssignedStmtsMixin, Statement): """class representing a For node""" _astroid_fields = ('target', 'iter', 'body', 'orelse',) target = None @@ -983,7 +1009,7 @@ def postinit(self, value=None): @util.register_implementation(treeabc.List) -class List(base.BaseContainer, objects.Instance): +class List(base.BaseContainer, AssignedStmtsMixin, objects.Instance): """class representing a List node""" def pytype(self): @@ -1120,7 +1146,7 @@ def getattr(self, attrname, context=None): @util.register_implementation(treeabc.Starred) -class Starred(mixins.ParentAssignTypeMixin, base.NodeNG): +class Starred(mixins.ParentAssignTypeMixin, AssignedStmtsMixin, base.NodeNG): """class representing a Starred node""" _astroid_fields = ('value',) value = None @@ -1192,7 +1218,7 @@ def block_range(self, lineno): @util.register_implementation(treeabc.Tuple) -class Tuple(base.BaseContainer, objects.Instance): +class Tuple(base.BaseContainer, AssignedStmtsMixin, objects.Instance): """class representing a Tuple node""" def pytype(self): @@ -1257,7 +1283,8 @@ def block_range(self, lineno): @util.register_implementation(treeabc.With) -class With(mixins.BlockRangeMixIn, mixins.AssignTypeMixin, Statement): +class With(mixins.BlockRangeMixIn, mixins.AssignTypeMixin, + AssignedStmtsMixin, Statement): """class representing a With node""" _astroid_fields = ('items', 'body') items = None From 8c15192f2607d35ae19730d7779ad7b15fe60f6d Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Thu, 5 Nov 2015 12:18:16 +0200 Subject: [PATCH 058/312] Use virtual checks instead of concrete object checks. --- astroid/inference.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/astroid/inference.py b/astroid/inference.py index 871af194d5..a8516a22a3 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -33,6 +33,7 @@ from astroid import protocols from astroid.interpreter import util as inferenceutil from astroid.interpreter import objects +from astroid.tree import treeabc from astroid import util @@ -73,7 +74,7 @@ def _higher_function_scope(node): which encloses the given node. """ current = node - while current.parent and not isinstance(current.parent, nodes.FunctionDef): + while current.parent and not isinstance(current.parent, treeabc.FunctionDef): current = current.parent if current and current.parent: return current.parent @@ -196,7 +197,7 @@ def infer_global(self, context=None): def _slice_value(index, context=None): """Get the value of the given slice index.""" - if isinstance(index, nodes.Const): + if isinstance(index, treeabc.Const): if isinstance(index.value, (int, type(None))): return index.value elif index is None: @@ -210,7 +211,7 @@ def _slice_value(index, context=None): except exceptions.InferenceError: pass else: - if isinstance(inferred, nodes.Const): + if isinstance(inferred, treeabc.Const): if isinstance(inferred.value, (int, type(None))): return inferred.value @@ -244,9 +245,9 @@ def infer_subscript(self, context=None): index_value = index else: index_value = _SLICE_SENTINEL - if isinstance(index, nodes.Const): + if isinstance(index, treeabc.Const): index_value = index.value - elif isinstance(index, nodes.Slice): + elif isinstance(index, treeabc.Slice): # Infer slices from the original object. lower = _slice_value(index.lower, context) upper = _slice_value(index.upper, context) @@ -407,7 +408,7 @@ def infer_unaryop(self, context=None): def _is_not_implemented(const): """Check if the given const node is NotImplemented.""" - return isinstance(const, nodes.Const) and const.value is NotImplemented + return isinstance(const, treeabc.Const) and const.value is NotImplemented def _invoke_binop_inference(instance, op, other, context, method_name): @@ -672,7 +673,7 @@ def infer_assign(self, context=None): assign node """ stmt = self.statement() - if isinstance(stmt, nodes.AugAssign): + if isinstance(stmt, treeabc.AugAssign): return stmt.infer(context) stmts = list(self.assigned_stmts(context=context)) From ef2e48a4842c0d30a3d8379c0570731aab8243ef Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Thu, 5 Nov 2015 15:44:59 +0200 Subject: [PATCH 059/312] The inference functions are implemented as dispatch-functions on virtual base classes, instead of monkey-patching nodes This has the nice side effect that circular the dependency between inference.py and node_classes is finally removed. --- astroid/helpers.py | 3 +- astroid/inference.py | 188 ++++++++++++++---------------- astroid/interpreter/objects.py | 8 +- astroid/interpreter/runtimeabc.py | 8 ++ astroid/protocols.py | 5 +- astroid/tree/base.py | 5 +- astroid/tree/node_classes.py | 50 ++++++-- 7 files changed, 147 insertions(+), 120 deletions(-) diff --git a/astroid/helpers.py b/astroid/helpers.py index 437b39de86..a0ba6c18e6 100644 --- a/astroid/helpers.py +++ b/astroid/helpers.py @@ -26,7 +26,6 @@ from astroid import exceptions from astroid.interpreter import runtimeabc from astroid import manager -from astroid import raw_building from astroid.tree import treeabc from astroid import util @@ -35,6 +34,8 @@ def _build_proxy_class(cls_name, builtins): + # TODO(cpopa): remove this when merging with modular-locals. + from astroid import raw_building proxy = raw_building.build_class(cls_name) proxy.parent = builtins return proxy diff --git a/astroid/inference.py b/astroid/inference.py index 871af194d5..20efeae74e 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -25,39 +25,39 @@ import operator from astroid import context as contextmod -from astroid import exceptions from astroid import decorators +from astroid import exceptions from astroid import helpers +from astroid.interpreter import runtimeabc +from astroid.interpreter import util as inferenceutil from astroid import manager -from astroid import nodes from astroid import protocols -from astroid.interpreter import util as inferenceutil -from astroid.interpreter import objects +from astroid.tree import treeabc from astroid import util MANAGER = manager.AstroidManager() -# .infer method ############################################################### +@util.singledispatch +def infer(self, context=None): + raise exceptions.InferenceError +@infer.register(treeabc.Module) +@infer.register(treeabc.ClassDef) +@infer.register(treeabc.FunctionDef) +@infer.register(treeabc.Lambda) +@infer.register(treeabc.Const) +@infer.register(treeabc.List) +@infer.register(treeabc.Tuple) +@infer.register(treeabc.Dict) +@infer.register(treeabc.Set) +@infer.register(treeabc.Slice) +@infer.register(runtimeabc.Super) +@infer.register(runtimeabc.FrozenSet) def infer_end(self, context=None): - """inference's end for node such as Module, ClassDef, FunctionDef, - Const... - - """ yield self -nodes.Module._infer = infer_end -nodes.ClassDef._infer = infer_end -nodes.FunctionDef._infer = infer_end -nodes.Lambda._infer = infer_end -nodes.Const._infer = infer_end -nodes.List._infer = infer_end -nodes.Tuple._infer = infer_end -nodes.Dict._infer = infer_end -nodes.Set._infer = infer_end -nodes.Slice._infer = infer_end def _higher_function_scope(node): @@ -73,11 +73,12 @@ def _higher_function_scope(node): which encloses the given node. """ current = node - while current.parent and not isinstance(current.parent, nodes.FunctionDef): + while current.parent and not isinstance(current.parent, treeabc.FunctionDef): current = current.parent if current and current.parent: return current.parent + def infer_name(self, context=None): """infer a Name: use name lookup rules""" frame, stmts = self.lookup(self.name) @@ -94,10 +95,11 @@ def infer_name(self, context=None): context = context.clone() context.lookupname = self.name return inferenceutil.infer_stmts(stmts, context, frame) -nodes.Name._infer = decorators.path_wrapper(infer_name) -nodes.AssignName.infer_lhs = infer_name # won't work with a path wrapper + +infer.register(treeabc.Name, decorators.path_wrapper(infer_name)) +@infer.register(treeabc.Call) @decorators.raise_if_nothing_inferred @decorators.path_wrapper def infer_call(self, context=None): @@ -117,9 +119,9 @@ def infer_call(self, context=None): except exceptions.InferenceError: ## XXX log error ? continue -nodes.Call._infer = infer_call +@infer.register(treeabc.Import) @decorators.path_wrapper def infer_import(self, context=None, asname=True): """infer an Import node: return the imported module/object""" @@ -130,16 +132,9 @@ def infer_import(self, context=None, asname=True): yield self.do_import_module(self.real_name(name)) else: yield self.do_import_module(name) -nodes.Import._infer = infer_import - - -def infer_name_module(self, name): - context = contextmod.InferenceContext() - context.lookupname = name - return self.infer(context, asname=False) -nodes.Import.infer_name_module = infer_name_module +@infer.register(treeabc.ImportFrom) @decorators.path_wrapper def infer_import_from(self, context=None, asname=True): """infer a ImportFrom node: return the imported module/object""" @@ -156,7 +151,6 @@ def infer_import_from(self, context=None, asname=True): return inferenceutil.infer_stmts(stmts, context) except exceptions.NotFoundError: util.reraise(exceptions.InferenceError(name)) -nodes.ImportFrom._infer = infer_import_from @decorators.raise_if_nothing_inferred @@ -176,10 +170,11 @@ def infer_attribute(self, context=None): except AttributeError: # XXX method / function context.boundnode = None -nodes.Attribute._infer = decorators.path_wrapper(infer_attribute) -nodes.AssignAttr.infer_lhs = infer_attribute # # won't work with a path wrapper +infer.register(treeabc.Attribute, decorators.path_wrapper(infer_attribute)) + +@infer.register(treeabc.Global) @decorators.path_wrapper def infer_global(self, context=None): if context.lookupname is None: @@ -189,14 +184,13 @@ def infer_global(self, context=None): context) except exceptions.NotFoundError: util.reraise(exceptions.InferenceError()) -nodes.Global._infer = infer_global _SLICE_SENTINEL = object() def _slice_value(index, context=None): """Get the value of the given slice index.""" - if isinstance(index, nodes.Const): + if isinstance(index, treeabc.Const): if isinstance(index.value, (int, type(None))): return index.value elif index is None: @@ -210,7 +204,7 @@ def _slice_value(index, context=None): except exceptions.InferenceError: pass else: - if isinstance(inferred, nodes.Const): + if isinstance(inferred, treeabc.Const): if isinstance(inferred.value, (int, type(None))): return inferred.value @@ -240,20 +234,20 @@ def infer_subscript(self, context=None): yield util.YES return - if value.__class__ == objects.Instance: + if value.__class__.__name__ == 'Instance': index_value = index else: index_value = _SLICE_SENTINEL - if isinstance(index, nodes.Const): + if isinstance(index, treeabc.Const): index_value = index.value - elif isinstance(index, nodes.Slice): + elif isinstance(index, treeabc.Slice): # Infer slices from the original object. lower = _slice_value(index.lower, context) upper = _slice_value(index.upper, context) step = _slice_value(index.step, context) if all(elem is not _SLICE_SENTINEL for elem in (lower, upper, step)): index_value = slice(lower, upper, step) - elif isinstance(index, objects.Instance): + elif isinstance(index, runtimeabc.Instance): index = inferenceutil.class_instance_as_index(index) if index: index_value = index.value @@ -276,10 +270,10 @@ def infer_subscript(self, context=None): for inferred in assigned.infer(context): yield inferred -nodes.Subscript._infer = decorators.path_wrapper(infer_subscript) -nodes.Subscript.infer_lhs = infer_subscript +infer.register(treeabc.Subscript, decorators.path_wrapper(infer_subscript)) +@infer.register(treeabc.BoolOp) @decorators.raise_if_nothing_inferred @decorators.path_wrapper def _infer_boolop(self, context=None): @@ -330,8 +324,6 @@ def _infer_boolop(self, context=None): else: yield value -nodes.BoolOp._infer = _infer_boolop - # UnaryOp, BinOp and AugAssign inferences @@ -346,7 +338,7 @@ def _filter_operation_errors(self, infer_callable, context, error): yield result -def _infer_unaryop(self, context=None): +def infer_unaryop(self, context=None, nodes=None): """Infer what an UnaryOp should return when evaluated.""" for operand in self.operand.infer(context): try: @@ -366,7 +358,7 @@ def _infer_unaryop(self, context=None): else: yield util.YES else: - if not isinstance(operand, objects.Instance): + if not isinstance(operand, runtimeabc.Instance): # The operation was used on something which # doesn't support it. yield exceptions.UnaryOperationError(operand, self.op, exc) @@ -396,38 +388,37 @@ def _infer_unaryop(self, context=None): @decorators.raise_if_nothing_inferred @decorators.path_wrapper -def infer_unaryop(self, context=None): +def filtered_infer_unaryop(self, context=None, nodes=None): """Infer what an UnaryOp should return when evaluated.""" - return _filter_operation_errors(self, _infer_unaryop, context, + with_nodes_func = functools.partial(infer_unaryop, nodes=nodes) + return _filter_operation_errors(self, with_nodes_func, context, exceptions.UnaryOperationError) -nodes.UnaryOp._infer_unaryop = _infer_unaryop -nodes.UnaryOp._infer = infer_unaryop - def _is_not_implemented(const): """Check if the given const node is NotImplemented.""" - return isinstance(const, nodes.Const) and const.value is NotImplemented + return isinstance(const, treeabc.Const) and const.value is NotImplemented -def _invoke_binop_inference(instance, op, other, context, method_name): +def _invoke_binop_inference(instance, op, other, context, method_name, nodes): """Invoke binary operation inference on the given instance.""" method = instance.getattr(method_name)[0] inferred = next(method.infer(context=context)) return protocols.infer_binary_op(instance, op, other, context, inferred, nodes) -def _aug_op(instance, op, other, context, reverse=False): +def _aug_op(instance, op, other, context, nodes, reverse=False): """Get an inference callable for an augmented binary operation.""" method_name = protocols.AUGMENTED_OP_METHOD[op] return functools.partial(_invoke_binop_inference, instance=instance, op=op, other=other, context=context, - method_name=method_name) + method_name=method_name, + nodes=nodes) -def _bin_op(instance, op, other, context, reverse=False): +def _bin_op(instance, op, other, context, nodes, reverse=False): """Get an inference callable for a normal binary operation. If *reverse* is True, then the reflected method will be used instead. @@ -440,7 +431,8 @@ def _bin_op(instance, op, other, context, reverse=False): instance=instance, op=op, other=other, context=context, - method_name=method_name) + method_name=method_name, + nodes=nodes) def _get_binop_contexts(context, left, right): @@ -465,7 +457,7 @@ def _same_type(type1, type2): def _get_binop_flow(left, left_type, op, right, right_type, - context, reverse_context): + context, reverse_context, nodes): """Get the flow for binary operations. The rules are a bit messy: @@ -481,20 +473,20 @@ def _get_binop_flow(left, left_type, op, right, right_type, is first tried and then left.__op__(right) """ if _same_type(left_type, right_type): - methods = [_bin_op(left, op, right, context)] + methods = [_bin_op(left, op, right, context, nodes)] elif inferenceutil.is_subtype(left_type, right_type): - methods = [_bin_op(left, op, right, context)] + methods = [_bin_op(left, op, right, context, nodes)] elif inferenceutil.is_supertype(left_type, right_type): - methods = [_bin_op(right, op, left, reverse_context, reverse=True), - _bin_op(left, op, right, context)] + methods = [_bin_op(right, op, left, reverse_context, nodes, reverse=True), + _bin_op(left, op, right, context, nodes)] else: - methods = [_bin_op(left, op, right, context), - _bin_op(right, op, left, reverse_context, reverse=True)] + methods = [_bin_op(left, op, right, context, nodes), + _bin_op(right, op, left, reverse_context, nodes, reverse=True)] return methods def _get_aug_flow(left, left_type, aug_op, right, right_type, - context, reverse_context): + context, reverse_context, nodes): """Get the flow for augmented binary operations. The rules are a bit messy: @@ -512,23 +504,23 @@ def _get_aug_flow(left, left_type, aug_op, right, right_type, """ op = aug_op.strip("=") if _same_type(left_type, right_type): - methods = [_aug_op(left, aug_op, right, context), - _bin_op(left, op, right, context)] + methods = [_aug_op(left, aug_op, right, context, nodes), + _bin_op(left, op, right, context, nodes)] elif inferenceutil.is_subtype(left_type, right_type): - methods = [_aug_op(left, aug_op, right, context), - _bin_op(left, op, right, context)] + methods = [_aug_op(left, aug_op, right, context, nodes), + _bin_op(left, op, right, context, nodes)] elif inferenceutil.is_supertype(left_type, right_type): - methods = [_aug_op(left, aug_op, right, context), - _bin_op(right, op, left, reverse_context, reverse=True), - _bin_op(left, op, right, context)] + methods = [_aug_op(left, aug_op, right, context, nodes), + _bin_op(right, op, left, reverse_context, nodes, reverse=True), + _bin_op(left, op, right, context, nodes)] else: - methods = [_aug_op(left, aug_op, right, context), - _bin_op(left, op, right, context), - _bin_op(right, op, left, reverse_context, reverse=True)] + methods = [_aug_op(left, aug_op, right, context, nodes), + _bin_op(left, op, right, context, nodes), + _bin_op(right, op, left, reverse_context, nodes, reverse=True)] return methods -def _infer_binary_operation(left, right, op, context, flow_factory): +def _infer_binary_operation(left, right, op, context, flow_factory, nodes): """Infer a binary operation between a left operand and a right operand This is used by both normal binary operations and augmented binary @@ -539,12 +531,12 @@ def _infer_binary_operation(left, right, op, context, flow_factory): left_type = helpers.object_type(left) right_type = helpers.object_type(right) methods = flow_factory(left, left_type, op, right, right_type, - context, reverse_context) + context, reverse_context, nodes) for method in methods: try: results = list(method()) except exceptions.BinaryOperationNotSupportedError: - continue + continue except (AttributeError, exceptions.NotFoundError): continue except exceptions.InferenceError: @@ -575,7 +567,7 @@ def _infer_binary_operation(left, right, op, context, flow_factory): yield exceptions.BinaryOperationError(left_type, op, right_type) -def _infer_binop(self, context): +def infer_binop(self, context, nodes): """Binary operation inferrence logic.""" if context is None: context = contextmod.InferenceContext() @@ -601,23 +593,21 @@ def _infer_binop(self, context): yield util.YES return - results = _infer_binary_operation(lhs, rhs, op, - context, _get_binop_flow) + results = _infer_binary_operation(lhs, rhs, op, context, + _get_binop_flow, nodes) for result in results: yield result @decorators.yes_if_nothing_inferred @decorators.path_wrapper -def infer_binop(self, context=None): - return _filter_operation_errors(self, _infer_binop, context, +def filtered_infer_binop(self, context=None, nodes=None): + with_nodes_func = functools.partial(infer_binop, nodes=nodes) + return _filter_operation_errors(self, with_nodes_func, context, exceptions.BinaryOperationError) -nodes.BinOp._infer_binop = _infer_binop -nodes.BinOp._infer = infer_binop - -def _infer_augassign(self, context=None): +def infer_augassign(self, context=None, nodes=None): """Inferrence logic for augmented binary operations.""" if context is None: context = contextmod.InferenceContext() @@ -642,47 +632,46 @@ def _infer_augassign(self, context=None): return results = _infer_binary_operation(lhs, rhs, op, - context, _get_aug_flow) + context, _get_aug_flow, nodes) for result in results: yield result @decorators.path_wrapper -def infer_augassign(self, context=None): - return _filter_operation_errors(self, _infer_augassign, context, +def filtered_infer_augassign(self, context=None, nodes=None): + with_nodes_func = functools.partial(infer_augassign, nodes=nodes) + return _filter_operation_errors(self, with_nodes_func, context, exceptions.BinaryOperationError) -nodes.AugAssign._infer_augassign = _infer_augassign -nodes.AugAssign._infer = infer_augassign # End of binary operation inference. -def infer_arguments(self, context=None): +def infer_arguments(self, context=None, nodes=None): name = context.lookupname if name is None: raise exceptions.InferenceError() return protocols._arguments_infer_argname(self, name, context, nodes) -nodes.Arguments._infer = infer_arguments +@infer.register(treeabc.AssignName) +@infer.register(treeabc.AssignAttr) @decorators.path_wrapper def infer_assign(self, context=None): """infer a AssignName/AssignAttr: need to inspect the RHS part of the assign node """ stmt = self.statement() - if isinstance(stmt, nodes.AugAssign): + if isinstance(stmt, treeabc.AugAssign): return stmt.infer(context) stmts = list(self.assigned_stmts(context=context)) return inferenceutil.infer_stmts(stmts, context) -nodes.AssignName._infer = infer_assign -nodes.AssignAttr._infer = infer_assign # no infer method on DelName and DelAttr (expected InferenceError) +@infer.register(treeabc.EmptyNode) @decorators.path_wrapper def infer_empty_node(self, context=None): if not self.has_underlying_object(): @@ -694,9 +683,8 @@ def infer_empty_node(self, context=None): yield inferred except exceptions.AstroidError: yield util.YES -nodes.EmptyNode._infer = infer_empty_node +@infer.register(treeabc.Index) def infer_index(self, context=None): return self.value.infer(context) -nodes.Index._infer = infer_index diff --git a/astroid/interpreter/objects.py b/astroid/interpreter/objects.py index 1c06287805..504b9f9c52 100644 --- a/astroid/interpreter/objects.py +++ b/astroid/interpreter/objects.py @@ -342,21 +342,20 @@ def __str__(self): return 'Generator(%s)' % (self._proxied.name) +@util.register_implementation(runtimeabc.FrozenSet) class FrozenSet(base.BaseContainer, Instance): """Class representing a FrozenSet composite node.""" def pytype(self): return '%s.frozenset' % BUILTINS - def _infer(self, context=None): - yield self - @decorators.cachedproperty def _proxied(self): builtins = MANAGER.astroid_cache[BUILTINS] return builtins.getattr('frozenset')[0] +@util.register_implementation(runtimeabc.Super) class Super(base.NodeNG): """Proxy class over a super call. @@ -383,9 +382,6 @@ def __init__(self, mro_pointer, mro_type, self_class, scope): '__class__': self._proxied, } - def _infer(self, context=None): - yield self - def super_mro(self): """Get the MRO which will be used to lookup attributes in this super.""" if not isinstance(self.mro_pointer, treeabc.ClassDef): diff --git a/astroid/interpreter/runtimeabc.py b/astroid/interpreter/runtimeabc.py index 612996aea3..ad873ce963 100644 --- a/astroid/interpreter/runtimeabc.py +++ b/astroid/interpreter/runtimeabc.py @@ -46,3 +46,11 @@ class BoundMethod(UnboundMethod): class Generator(RuntimeObject): """Class representing a Generator.""" + + +class Super(RuntimeObject): + """Class representing a super proxy.""" + + +class FrozenSet(RuntimeObject): + """Class representing a frozenset.""" diff --git a/astroid/protocols.py b/astroid/protocols.py index 2ef247167e..7eb8a1b18b 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -28,7 +28,6 @@ from astroid import context as contextmod from astroid import decorators from astroid import exceptions -from astroid.interpreter import objects from astroid.interpreter import runtimeabc from astroid.interpreter import util as inferenceutil from astroid.tree import treeabc @@ -304,7 +303,7 @@ def _arguments_infer_argname(self, name, context, nodes): yield cls return if functype == 'method': - yield objects.Instance(self.parent.parent.frame()) + yield self.parent.parent.frame().instanciate_class() return if context and context.callcontext: @@ -394,7 +393,7 @@ def _resolve_asspart(parts, asspath, context): def excepthandler_assigned_stmts(self, nodes, node=None, context=None, asspath=None): for assigned in inferenceutil.unpack_infer(self.type): if isinstance(assigned, treeabc.ClassDef): - assigned = objects.Instance(assigned) + assigned = assigned.instanciate_class() yield assigned diff --git a/astroid/tree/base.py b/astroid/tree/base.py index f4ca188a71..f5b598a2e3 100644 --- a/astroid/tree/base.py +++ b/astroid/tree/base.py @@ -25,6 +25,7 @@ from astroid import as_string from astroid import decorators from astroid import exceptions +from astroid import inference from astroid.interpreter import scope from astroid import mixins from astroid.tree import treeabc @@ -75,14 +76,14 @@ def infer(self, context=None, **kwargs): pass if not context: - return self._infer(context, **kwargs) + return inference.infer(self, 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)) + return context.cache_generator(key, inference.infer(self, context, **kwargs)) def _repr_name(self): """return self.name or self.attrname or '' for nice representation""" diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index 9963e2f5ca..479d877265 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -18,6 +18,7 @@ """Module for some node classes. More nodes in scoped_nodes.py """ +import functools import warnings import sys @@ -26,6 +27,7 @@ from astroid import context as contextmod from astroid import decorators from astroid import exceptions +from astroid import inference from astroid.interpreter.util import infer_stmts from astroid.interpreter import runtimeabc from astroid.interpreter import objects @@ -255,6 +257,8 @@ def __init__(self, name=None, lineno=None, col_offset=None, parent=None): self.name = name super(AssignName, self).__init__(lineno, col_offset, parent) + infer_lhs = inference.infer_name + @util.register_implementation(treeabc.DelName) class DelName(LookupMixIn, mixins.ParentAssignTypeMixin, base.NodeNG): @@ -435,6 +439,8 @@ def __init__(self, attrname=None, lineno=None, col_offset=None, parent=None): def postinit(self, expr=None): self.expr = expr + infer_lhs = inference.infer_attribute + @util.register_implementation(treeabc.Assert) class Assert(Statement): @@ -476,9 +482,9 @@ 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 _infer_augassign(self, context): + return inference.infer_augassign(self, nodes=sys.modules[__name__], + context=context) def type_errors(self, context=None): """Return a list of TypeErrors which can occur during inference. @@ -520,9 +526,9 @@ 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 _infer_binop(self, context): + return inference.infer_binop(self, nodes=sys.modules[__name__], + context=context) def type_errors(self, context=None): """Return a list of TypeErrors which can occur during inference. @@ -982,6 +988,11 @@ def __init__(self, names=None, lineno=None, col_offset=None, parent=None): self.names = names super(Import, self).__init__(lineno, col_offset, parent) + def infer_name_module(self, name): + context = contextmod.InferenceContext() + context.lookupname = name + return self.infer(context, asname=False) + @util.register_implementation(treeabc.Index) class Index(base.NodeNG): @@ -1166,6 +1177,8 @@ def postinit(self, value=None, slice=None): self.value = value self.slice = slice + infer_lhs = inference.infer_subscript + @util.register_implementation(treeabc.TryExcept) class TryExcept(mixins.BlockRangeMixIn, Statement): @@ -1242,9 +1255,9 @@ def __init__(self, op=None, lineno=None, col_offset=None, parent=None): def postinit(self, operand=None): self.operand = operand - # This is set by inference.py def _infer_unaryop(self, context=None): - raise NotImplementedError + return inference.infer_unaryop(self, nodes=sys.modules[__name__], + context=context) def type_errors(self, context=None): """Return a list of TypeErrors which can occur during inference. @@ -1379,3 +1392,24 @@ def const_factory(value): Getattr = util.proxy_alias('Getattr', Attribute) CallFunc = util.proxy_alias('CallFunc', Call) From = util.proxy_alias('From', ImportFrom) + + +# Register additional inference dispatched functions. We do +# this here, since we need to pass this module as an argument +# to these functions, in order to avoid circular dependencies +# between inference and node_classes. + +_module = sys.modules[__name__] +inference.infer.register(treeabc.UnaryOp, + functools.partial(inference.filtered_infer_unaryop, + nodes=_module)) +inference.infer.register(treeabc.Arguments, + functools.partial(inference.infer_arguments, + nodes=_module)) +inference.infer.register(treeabc.BinOp, + functools.partial(inference.filtered_infer_binop, + nodes=_module)) +inference.infer.register(treeabc.AugAssign, + functools.partial(inference.filtered_infer_augassign, + nodes=_module)) +del _module From 8603238d7669d1b87bbfb573049929b8fe2716ca Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sat, 7 Nov 2015 14:42:25 +0200 Subject: [PATCH 060/312] Separate class instances and builtin instances into two concepts One problem with handling both concepts with a single class leads to type-testing using type identity rather than using isinstance, since builtin instances uses the same base class as class instances (even though this is fairly fuzzy). With two classes instead, we can easily distinguish between these cases. The commit includes two new virtual base classes, Instance and BuiltinInstance and separates the Instance class into BaseInstance and Instance, also changing in some places the use of type-identity testing with isinstance testing. Closes issue #214. --- astroid/builder.py | 6 +++--- astroid/decorators.py | 3 ++- astroid/inference.py | 2 +- astroid/interpreter/objects.py | 20 +++++++++++++------- astroid/interpreter/runtimeabc.py | 4 ++++ astroid/tests/unittest_inference.py | 27 +++++++++++++++++---------- astroid/tree/node_classes.py | 16 ++++++++++------ 7 files changed, 50 insertions(+), 28 deletions(-) diff --git a/astroid/builder.py b/astroid/builder.py index 24b994c2d7..51044eac20 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -27,7 +27,7 @@ import textwrap from astroid import exceptions -from astroid.interpreter import objects +from astroid.interpreter import runtimeabc from astroid import manager from astroid import modutils from astroid import raw_building @@ -223,10 +223,10 @@ def delayed_assattr(self, node): if inferred is util.YES: continue try: - if inferred.__class__ is objects.Instance: + if isinstance(inferred, runtimeabc.Instance): inferred = inferred._proxied iattrs = inferred.instance_attrs - elif isinstance(inferred, objects.Instance): + elif isinstance(inferred, runtimeabc.BuiltinInstance): # Const, Tuple, ... we may be wrong, may be not, but # anyway we don't want to pollute builtin's namespace continue diff --git a/astroid/decorators.py b/astroid/decorators.py index 0709744831..d612fa9ada 100644 --- a/astroid/decorators.py +++ b/astroid/decorators.py @@ -27,6 +27,7 @@ from astroid import context as contextmod from astroid import exceptions +from astroid.interpreter import runtimeabc from astroid import util @@ -95,7 +96,7 @@ def wrapped(node, context=None, _func=func, **kwargs): yielded = set() for res in _func(node, context, **kwargs): # unproxy only true instance, not const, tuple, dict... - if res.__class__.__name__ == 'Instance': + if isinstance(res, runtimeabc.Instance): ares = res._proxied else: ares = res diff --git a/astroid/inference.py b/astroid/inference.py index 20efeae74e..67ff4d8978 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -234,7 +234,7 @@ def infer_subscript(self, context=None): yield util.YES return - if value.__class__.__name__ == 'Instance': + if isinstance(value, runtimeabc.Instance): index_value = index else: index_value = _SLICE_SENTINEL diff --git a/astroid/interpreter/objects.py b/astroid/interpreter/objects.py index 504b9f9c52..3e0bc60d67 100644 --- a/astroid/interpreter/objects.py +++ b/astroid/interpreter/objects.py @@ -107,9 +107,12 @@ def _infer_method_result_truth(instance, method_name, context): return util.YES -@util.register_implementation(runtimeabc.Instance) -class Instance(Proxy): - """A special node representing a class instance.""" + +class BaseInstance(Proxy): + """An instance base class, which provides lookup methods for potential instances.""" + + def display_type(self): + return 'Instance of' def getattr(self, name, context=None, lookupclass=True): try: @@ -194,6 +197,12 @@ def infer_call_result(self, caller, context=None): if not inferred: raise exceptions.InferenceError() + +@util.register_implementation(runtimeabc.Instance) +class Instance(BaseInstance): + """A special node representing a class instance.""" + + def __repr__(self): return '' % (self._proxied.root().name, self._proxied.name, @@ -212,9 +221,6 @@ def callable(self): def pytype(self): return self._proxied.qname() - def display_type(self): - return 'Instance of' - def bool_value(self): """Infer the truth value for an Instance @@ -318,7 +324,7 @@ def bool_value(self): @util.register_implementation(runtimeabc.Generator) -class Generator(Instance): +class Generator(BaseInstance): """a special node representing a generator. Proxied class is set once for all in raw_building. diff --git a/astroid/interpreter/runtimeabc.py b/astroid/interpreter/runtimeabc.py index ad873ce963..4fc1a15db8 100644 --- a/astroid/interpreter/runtimeabc.py +++ b/astroid/interpreter/runtimeabc.py @@ -36,6 +36,10 @@ class Instance(RuntimeObject): """Class representing an instance.""" +class BuiltinInstance(RuntimeObject): + """Represents an instance of a builtin.""" + + class UnboundMethod(RuntimeObject): """Class representing an unbound method.""" diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index ce42f684e6..4c49b45ece 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -31,6 +31,7 @@ from astroid.interpreter.objects import ( Instance, BoundMethod, UnboundMethod, FrozenSet ) +from astroid.interpreter import runtimeabc from astroid.interpreter.util import safe_infer from astroid import decorators as decoratorsmod @@ -472,7 +473,7 @@ def test_builtin_types(self): n = ast['l'] inferred = next(n.infer()) self.assertIsInstance(inferred, nodes.List) - self.assertIsInstance(inferred, Instance) + self.assertIsInstance(inferred, runtimeabc.BuiltinInstance) self.assertEqual(inferred.getitem(0).value, 1) self.assertIsInstance(inferred._proxied, nodes.ClassDef) self.assertEqual(inferred._proxied.name, 'list') @@ -480,21 +481,21 @@ def test_builtin_types(self): n = ast['t'] inferred = next(n.infer()) self.assertIsInstance(inferred, nodes.Tuple) - self.assertIsInstance(inferred, Instance) + self.assertIsInstance(inferred, runtimeabc.BuiltinInstance) self.assertEqual(inferred.getitem(0).value, 2) self.assertIsInstance(inferred._proxied, nodes.ClassDef) self.assertEqual(inferred._proxied.name, 'tuple') n = ast['d'] inferred = next(n.infer()) self.assertIsInstance(inferred, nodes.Dict) - self.assertIsInstance(inferred, Instance) + self.assertIsInstance(inferred, runtimeabc.BuiltinInstance) self.assertIsInstance(inferred._proxied, nodes.ClassDef) self.assertEqual(inferred._proxied.name, 'dict') self.assertIn('get', inferred._proxied.locals) n = ast['s'] inferred = next(n.infer()) self.assertIsInstance(inferred, nodes.Const) - self.assertIsInstance(inferred, Instance) + self.assertIsInstance(inferred, runtimeabc.BuiltinInstance) self.assertEqual(inferred.name, 'str') self.assertIn('lower', inferred._proxied.locals) n = ast['s2'] @@ -506,7 +507,7 @@ def test_builtin_types(self): n = ast['s'] inferred = next(n.infer()) self.assertIsInstance(inferred, nodes.Set) - self.assertIsInstance(inferred, Instance) + self.assertIsInstance(inferred, runtimeabc.BuiltinInstance) self.assertEqual(inferred.name, 'set') self.assertIn('remove', inferred._proxied.locals) @@ -517,7 +518,7 @@ def test_unicode_type(self): n = ast['u'] inferred = next(n.infer()) self.assertIsInstance(inferred, nodes.Const) - self.assertIsInstance(inferred, Instance) + self.assertIsInstance(inferred, runtimeabc.BuiltinInstance) self.assertEqual(inferred.name, 'unicode') self.assertIn('lower', inferred._proxied.locals) @@ -1727,9 +1728,6 @@ def func(): dict([(1, 2), ([4, 5], 2)]) #@ dict([None, None]) #@ - def using_unknown_kwargs(**kwargs): - return dict(**kwargs) - using_unknown_kwargs(a=1, b=2) #@ """ ast = test_utils.extract_node(code, __name__) self.assertInferDict(ast[0], {}) @@ -1744,9 +1742,18 @@ def using_unknown_kwargs(**kwargs): for node in ast[10:]: inferred = next(node.infer()) - self.assertIsInstance(inferred, Instance) + self.assertIsInstance(inferred, runtimeabc.Instance) self.assertEqual(inferred.qname(), "{}.dict".format(BUILTINS)) + @unittest.expectedFailure + def test_dict_inference_call_context_not_propagated(self): + ast_node = test_utils.extract_node(''' + def using_unknown_kwargs(**kwargs): + return dict(**kwargs) + using_unknown_kwargs(a=1, b=2) #@ + ''') + self.assertInferDict(ast_node, {'a': 1, 'b': 2}) + def test_dict_inference_kwargs(self): ast_node = test_utils.extract_node('''dict(a=1, b=2, **{'c': 3})''') self.assertInferDict(ast_node, {'a': 1, 'b': 2, 'c': 3}) diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index 479d877265..52ca1f766f 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -655,8 +655,8 @@ def _get_filtered_stmts(self, lookup_node, node, stmts, mystmt): @util.register_implementation(treeabc.Const) -@util.register_implementation(runtimeabc.Instance) -class Const(base.NodeNG, objects.Instance): +@util.register_implementation(runtimeabc.BuiltinInstance) +class Const(base.NodeNG, objects.BaseInstance): """represent a constant node like num, str, bool, None, bytes""" _other_fields = ('value',) @@ -730,7 +730,8 @@ def postinit(self, targets=None): @util.register_implementation(treeabc.Dict) -class Dict(base.NodeNG, objects.Instance): +@util.register_implementation(runtimeabc.BuiltinInstance) +class Dict(base.NodeNG, objects.BaseInstance): """class representing a Dict node""" _astroid_fields = ('items',) @@ -1020,7 +1021,8 @@ def postinit(self, value=None): @util.register_implementation(treeabc.List) -class List(base.BaseContainer, AssignedStmtsMixin, objects.Instance): +@util.register_implementation(runtimeabc.BuiltinInstance) +class List(base.BaseContainer, AssignedStmtsMixin, objects.BaseInstance): """class representing a List node""" def pytype(self): @@ -1105,7 +1107,8 @@ def postinit(self, value=None): @util.register_implementation(treeabc.Set) -class Set(base.BaseContainer, objects.Instance): +@util.register_implementation(runtimeabc.BuiltinInstance) +class Set(base.BaseContainer, objects.BaseInstance): """class representing a Set node""" def pytype(self): @@ -1231,7 +1234,8 @@ def block_range(self, lineno): @util.register_implementation(treeabc.Tuple) -class Tuple(base.BaseContainer, AssignedStmtsMixin, objects.Instance): +@util.register_implementation(runtimeabc.BuiltinInstance) +class Tuple(base.BaseContainer, AssignedStmtsMixin, objects.BaseInstance): """class representing a Tuple node""" def pytype(self): From 17c6056be90a6e716cdba5e70cd37515b2688f40 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sat, 7 Nov 2015 14:55:58 +0200 Subject: [PATCH 061/312] Move LookupMixin into mixins, since we can it doesn't need to depend on Name nodes anymore. --- astroid/mixins.py | 149 +++++++++++++++++++++++++++++++++ astroid/tree/node_classes.py | 154 +---------------------------------- astroid/tree/scoped_nodes.py | 2 +- 3 files changed, 153 insertions(+), 152 deletions(-) diff --git a/astroid/mixins.py b/astroid/mixins.py index 9f5f9538b5..93784a517b 100644 --- a/astroid/mixins.py +++ b/astroid/mixins.py @@ -20,8 +20,11 @@ import warnings +from astroid import context from astroid import decorators from astroid import exceptions +from astroid.interpreter import util as interpreterutil +from astroid.tree import treeabc from astroid import util @@ -146,3 +149,149 @@ def real_name(self, asname): if asname == _asname: return name raise exceptions.NotFoundError(asname) + + +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) + return interpreterutil.infer_stmts(stmts, context.InferenceContext(), 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() + if not myframe is frame or self is frame: + return stmts + 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 assigment is hidding previous + # assigment + _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 + # assignement 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 assigments if any (hence the test on + # optional_assign) + if not (optional_assign or interpreterutil.are_exclusive(_stmts[pindex], node)): + del _stmt_parents[pindex] + del _stmts[pindex] + if isinstance(node, treeabc.AssignName): + if not optional_assign and stmt.parent is mystmt.parent: + _stmts = [] + _stmt_parents = [] + elif isinstance(node, treeabc.DelName): + _stmts = [] + _stmt_parents = [] + continue + if not interpreterutil.are_exclusive(self, node): + _stmts.append(node) + _stmt_parents.append(stmt.parent) + return _stmts diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index 52ca1f766f..e8ba256b46 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -31,7 +31,6 @@ from astroid.interpreter.util import infer_stmts from astroid.interpreter import runtimeabc from astroid.interpreter import objects -from astroid.interpreter import util as inferenceutil from astroid import manager from astroid import mixins from astroid import protocols @@ -98,157 +97,10 @@ def assigned_stmts(self, node=None, context=None, asspath=None): asspath=asspath) -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 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() - if not myframe is frame or self is frame: - return stmts - 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 assigment is hidding previous - # assigment - _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 - # assignement 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 assigments if any (hence the test on - # optional_assign) - if not (optional_assign or inferenceutil.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 inferenceutil.are_exclusive(self, node): - _stmts.append(node) - _stmt_parents.append(stmt.parent) - return _stmts - - # Name classes @util.register_implementation(treeabc.AssignName) -class AssignName(LookupMixIn, mixins.ParentAssignTypeMixin, +class AssignName(mixins.LookupMixIn, mixins.ParentAssignTypeMixin, AssignedStmtsMixin, base.NodeNG): """class representing an AssignName node""" _other_fields = ('name',) @@ -261,7 +113,7 @@ def __init__(self, name=None, lineno=None, col_offset=None, parent=None): @util.register_implementation(treeabc.DelName) -class DelName(LookupMixIn, mixins.ParentAssignTypeMixin, base.NodeNG): +class DelName(mixins.LookupMixIn, mixins.ParentAssignTypeMixin, base.NodeNG): """class representing a DelName node""" _other_fields = ('name',) @@ -271,7 +123,7 @@ def __init__(self, name=None, lineno=None, col_offset=None, parent=None): @util.register_implementation(treeabc.Name) -class Name(LookupMixIn, base.NodeNG): +class Name(mixins.LookupMixIn, base.NodeNG): """class representing a Name node""" _other_fields = ('name',) diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index a59fcab7ad..4e67b55421 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -140,7 +140,7 @@ def builtin_lookup(name): # TODO move this Mixin to mixins.py; problem: 'FunctionDef' in _scope_lookup -class LocalsDictNodeNG(node_classes.LookupMixIn, +class LocalsDictNodeNG(mixins.LookupMixIn, treebase.NodeNG): """ this class provides locals handling common to Module, FunctionDef and ClassDef nodes, including a dict like interface for direct access From 039a7c8e0ea0747a49c7fff165e405e505a1a09e Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 10 Nov 2015 19:06:04 +0200 Subject: [PATCH 062/312] .scope() returns the proper scope for Arguments's default values, function annotations and comprehensions. Closes issue #211. --- ChangeLog | 3 ++ astroid/interpreter/scope.py | 73 ++++++++++++++++++++++++-- astroid/tests/unittest_nodes.py | 91 ++++++++++++++++++++++++++++++++- 3 files changed, 162 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6b93e834b0..1742e86658 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,9 @@ Change log for the astroid package (used to be astng) -- + * .scope() returns the proper scope for Arguments's default values, + function annotations and comprehensions. Closes issue #211. + * Class.getattr('__mro__') returns the actual MRO. Closes issue #128. * The logilab-common dependency is not needed anymore as the needed code diff --git a/astroid/interpreter/scope.py b/astroid/interpreter/scope.py index 0f9f6bb7dc..a4ddf41cfc 100644 --- a/astroid/interpreter/scope.py +++ b/astroid/interpreter/scope.py @@ -13,20 +13,85 @@ # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License # for more details. # -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# You should have received a copy of the GNU Lesser General Public License +# along with astroid. If not, see . """Implements logic for determing the scope of a node.""" + +import itertools + import six -from astroid import util from astroid.tree import treeabc +from astroid import util + + +@util.singledispatch +def _scope_by_parent(parent, node): + """Detect the scope of the *node* by parent's rules. + + The scope for certain kind of nodes depends on the + parent, as it is the case for default values of arguments + and function annotation, where the scope is not the scope of + the parent, but the parent scope of the parent. + """ + # This is separated in multiple dispatch methods on parents, + # in order to decouple the implementation for the normal cases. + + +@_scope_by_parent.register(treeabc.Arguments) +def _scope_by_argument_parent(parent, node): + args = parent + if node in itertools.chain(args.defaults, args.kw_defaults): + return args.parent.parent.scope() + if six.PY3: + look_for = itertools.chain( + (args.kwargannotation, ), + (args.varargannotation, ), + args.annotations) + if node in look_for: + return args.parent.parent.scope() + + +@_scope_by_parent.register(treeabc.FunctionDef) +def _scope_by_function_parent(parent, node): + # Verify if the node is the return annotation of a function, + # in which case the scope is the parent scope of the function. + if six.PY3 and node is parent.returns: + return parent.parent.scope() + + +@_scope_by_parent.register(treeabc.Comprehension) +def _scope_by_comprehension_parent(parent, node): + # Get the scope of a node which has a comprehension + # as a parent. The rules are a bit hairy, but in essence + # it is simple enough: list comprehensions leaks variables + # on Python 2, so they have the parent scope of the list comprehension + # itself. The iter part of the comprehension has almost always + # another scope than the comprehension itself, but only for the + # first generator (the outer one). Other comprehensions don't leak + # variables on Python 2 and 3. + + comprehension = parent_scope = parent.parent + generators = comprehension.generators + + # The first outer generator always has a different scope + first_iter = generators[0].iter + if node is first_iter: + return parent_scope.parent.scope() + + # This might not be correct for all the cases, but it + # should be enough for most of them. + if six.PY2 and isinstance(parent_scope, treeabc.ListComp): + return parent_scope.parent.scope() + return parent.scope() @util.singledispatch def node_scope(node): """Get the scope of the given node.""" - return node.parent.scope() + scope = _scope_by_parent(node.parent, node) + return scope or node.parent.scope() @node_scope.register(treeabc.Decorators) diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index b056bbe277..f4bb897fb6 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -798,7 +798,96 @@ class classdef: pass self.assertIsInstance(module['listcomp'].parent.value.scope(), nodes.ListComp) else: self.assertIsInstance(module['listcomp'].parent.value.scope(), nodes.Module) - + + def test_scope_of_default_argument_value(self): + node = test_utils.extract_node(''' + def test(a=__(b)): + pass + ''') + scope = node.scope() + self.assertIsInstance(scope, nodes.Module) + + @test_utils.require_version(minver='3.0') + def test_scope_of_default_keyword_argument_value(self): + node = test_utils.extract_node(''' + def test(*, b=__(c)): + pass + ''') + scope = node.scope() + self.assertIsInstance(scope, nodes.Module) + + @test_utils.require_version(minver='3.0') + def test_scope_of_annotations(self): + ast_nodes = test_utils.extract_node(''' + def test(a: __(b), *args:__(f), c:__(d)=4, **kwargs: _(l))->__(x): + pass + ''') + for node in ast_nodes: + scope = node.scope() + self.assertIsInstance(scope, nodes.Module) + + def test_scope_of_list_comprehension_target_composite_nodes(self): + ast_node = test_utils.extract_node(''' + [i for data in __([DATA1, DATA2]) for i in data] + ''') + node = ast_node.elts[0] + scope = node.scope() + self.assertIsInstance(scope, nodes.Module) + + def test_scope_of_nested_list_comprehensions(self): + ast_node = test_utils.extract_node(''' + [1 for data in DATA for x in __(data)] + ''') + scope = ast_node.scope() + if six.PY2: + self.assertIsInstance(scope, nodes.Module) + else: + self.assertIsInstance(scope, nodes.ListComp) + + def test_scope_of_list_comprehension_targets(self): + ast_node = test_utils.extract_node(''' + [1 for data in DATA] + ''') + # target is `data` from the list comprehension + target = ast_node.generators[0].target + scope = target.scope() + if six.PY2: + self.assertIsInstance(scope, nodes.Module) + else: + self.assertIsInstance(scope, nodes.ListComp) + + def test_scope_of_list_comprehension_value(self): + ast_node = test_utils.extract_node(''' + [__(i) for i in DATA] + ''') + scope = ast_node.scope() + if six.PY3: + self.assertIsInstance(scope, nodes.ListComp) + else: + self.assertIsInstance(scope, nodes.Module) + + def test_scope_of_dict_comprehension(self): + ast_nodes = test_utils.extract_node(''' + {i: __(j) for (i, j) in DATA} + {i:j for (i, j) in __(DATA)} + ''') + elt_scope = ast_nodes[0].scope() + self.assertIsInstance(elt_scope, nodes.DictComp) + iter_scope = ast_nodes[1].scope() + self.assertIsInstance(iter_scope, nodes.Module) + + ast_node = test_utils.extract_node(''' + {i:1 for i in DATA}''') + target = ast_node.generators[0].target + target_scope = target.scope() + self.assertIsInstance(target_scope, nodes.DictComp) + + def test_scope_elt_of_generator_exp(self): + ast_node = test_utils.extract_node(''' + list(__(i) for i in range(10)) + ''') + scope = ast_node.scope() + self.assertIsInstance(scope, nodes.GeneratorExp) if __name__ == '__main__': From 316281446ef79d71bf9338c3ce3dfca6ef964c5f Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Wed, 11 Nov 2015 01:32:50 +0200 Subject: [PATCH 063/312] Fix a bug in the inference of Starred nodes This bug occurs when the left hand side of an assignment has an extra element comparing with the right hand side, which makes the Starred node to not be inferred correctly as an empty List. Closes issue #239. --- ChangeLog | 4 ++++ astroid/protocols.py | 19 +++++++++++-------- astroid/tests/unittest_protocols.py | 6 ++++-- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1742e86658..0ed445e2dc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,10 @@ Change log for the astroid package (used to be astng) -- + * Fix a bug in the inference of Starred nodes, when the left hand + side of an assignment has an extra element comparing with the + right hand side. Closes issue #239. + * .scope() returns the proper scope for Arguments's default values, function annotations and comprehensions. Closes issue #211. diff --git a/astroid/protocols.py b/astroid/protocols.py index 7eb8a1b18b..c31b22088f 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -508,10 +508,14 @@ def starred_assigned_stmts(self, nodes, node=None, context=None, asspath=None): yield util.YES return + if len(lhs.elts) > len(rhs.elts) + 1: + # Since there is only one Starred node on the + # left hand side, the lhs number of elements + # can be at most N + 1, where N is the number of elements + # of rhs. + raise exceptions.InferenceError + elts = collections.deque(rhs.elts[:]) - if len(lhs.elts) > len(rhs.elts): - # a, *b, c = (1, 2) - raise exceptions.InferenceError() # Unpack iteratively the values from the rhs of the assignment, # until the find the starred node. What will remain will @@ -519,7 +523,6 @@ def starred_assigned_stmts(self, nodes, node=None, context=None, asspath=None): # This is done in two steps, from left to right to remove # anything before the starred node and from right to left # to remove anything after the starred node. - for index, node in enumerate(lhs.elts): if not isinstance(node, treeabc.Starred): elts.popleft() @@ -529,9 +532,9 @@ def starred_assigned_stmts(self, nodes, node=None, context=None, asspath=None): if not isinstance(node, treeabc.Starred): elts.pop() continue - # We're done - packed = nodes.List() - packed.elts = elts - packed.parent = self + # We're done. + packed = nodes.List(parent=self) + packed.postinit(elts=elts) yield packed break + break diff --git a/astroid/tests/unittest_protocols.py b/astroid/tests/unittest_protocols.py index 63cb6dce2a..6c6cfc7fd9 100644 --- a/astroid/tests/unittest_protocols.py +++ b/astroid/tests/unittest_protocols.py @@ -104,6 +104,8 @@ def test_assigned_stmts_starred_assnames(self): "*b, a = (1, 2) #@", [1]) self._helper_starred_expected_const( "[*b] = (1, 2) #@", [1, 2]) + self._helper_starred_expected_const( + "a, *b, c = (1, 2) #@", []) @require_version(minver='3.0') def test_assigned_stmts_starred_yes(self): @@ -123,12 +125,12 @@ def test(arg): def test_assign_stmts_starred_fails(self): # Too many starred self._helper_starred_inference_error("a, *b, *c = (1, 2, 3) #@") - # Too many lhs values - self._helper_starred_inference_error("a, *b, c = (1, 2) #@") # This could be solved properly, but it complicates needlessly the # code for assigned_stmts, without oferring real benefit. self._helper_starred_inference_error( "(*a, b), (c, *d) = (1, 2, 3), (4, 5, 6) #@") + self._helper_starred_inference_error( + "a, *b, c, d = 1, 2") def test_assigned_stmts_assignments(self): assign_stmts = extract_node(""" From d5fe52b69e747916a2aee419fa505d6cce825838 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Thu, 12 Nov 2015 17:12:00 +0200 Subject: [PATCH 064/312] Remove the astpeephole It was initially added in order to remove a recursion error in rebuilder, which happened when a lot of strings were added together, leading to an actual recursion error when executing the code. The peepholer was more of a constant folding algorithm rather than a real peepholer. Since it's purpose is limited and the original problem is not worth fixing in astroid, but in the user code, this is removed. Closes issue #210 --- astroid/builder.py | 2 +- astroid/manager.py | 1 - .../testdata/python2/data/joined_strings.py | 1051 ----------------- .../testdata/python3/data/joined_strings.py | 1051 ----------------- astroid/tests/unittest_peephole.py | 121 -- astroid/tree/astpeephole.py | 86 -- astroid/tree/rebuilder.py | 22 +- 7 files changed, 2 insertions(+), 2332 deletions(-) delete mode 100644 astroid/tests/testdata/python2/data/joined_strings.py delete mode 100644 astroid/tests/testdata/python3/data/joined_strings.py delete mode 100644 astroid/tests/unittest_peephole.py delete mode 100644 astroid/tree/astpeephole.py diff --git a/astroid/builder.py b/astroid/builder.py index 51044eac20..7252603fbb 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -184,7 +184,7 @@ def _data_build(self, data, modname, path): package = True else: package = path and path.find('__init__.py') > -1 or False - builder = rebuilder.TreeRebuilder(self._manager) + builder = rebuilder.TreeRebuilder() module = builder.visit_module(node, modname, node_file, package) module._import_from_nodes = builder._import_from_nodes module._delayed_assattr = builder._delayed_assattr diff --git a/astroid/manager.py b/astroid/manager.py index 07d7543cab..962d5b16ad 100644 --- a/astroid/manager.py +++ b/astroid/manager.py @@ -58,7 +58,6 @@ def __init__(self): self._mod_file_cache = {} self._failed_import_hooks = [] self.always_load_extensions = False - self.optimize_ast = False self.extension_package_whitelist = set() self._transform = transforms.TransformVisitor() diff --git a/astroid/tests/testdata/python2/data/joined_strings.py b/astroid/tests/testdata/python2/data/joined_strings.py deleted file mode 100644 index ca1af58baa..0000000000 --- a/astroid/tests/testdata/python2/data/joined_strings.py +++ /dev/null @@ -1,1051 +0,0 @@ -x = ('R0lGODlhigJnAef/AAABAAEEAAkCAAMGAg0GBAYJBQoMCBMODQ4QDRITEBkS' - +'CxsSEhkWDhYYFQ0aJhkaGBweGyccGh8hHiIkIiMmGTEiHhQoPSYoJSkqKDcp' - +'Ii0uLDAxLzI0Mh44U0gxMDI5JkM0JjU3NDY6Kjc5Njo7OUE8Ozw+Oz89QTxA' - +'F1akOFFiRIgPHTZksKBAgMCLGTdGNIAAQgKfDAcgZbj0odOnUA8GBAA7' - +'CxsSEhkWDhYYFQ0aJhkaGBweGyccGh8hHiIkIiMmGTEiHhQoPSYoJSkqKDcp' - +'Ii0uLDAxLzI0Mh44U0gxMDI5JkM0JjU3NDY6Kjc5Njo7OUE8Ozw+Oz89QTxA' - +'F1akOFFiRIgPHTZksKBAgMCLGTdGNIAAQgKfDAcgZbj0odOnUA8GBAA7' - +'CxsSEhkWDhYYFQ0aJhkaGBweGyccGh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ccGh8hHiIkIiMmGTEiHhQoPSYoJSkqKDcp' - +'Ii0uLDAxLzI0Mh44U0gxMDI5JkM0JjU3NDY6Kjc5Njo7OUE8Ozw+Oz89QTxA' - +'F1akOFFiRIgPHTZksKBAgMCLGTdGNIAAQgKfDAcgZbj0odOnUA8GBAA7' - +'CxsSEhkWDhYYFQ0aJhkaGBweGyccGh8hHiIkIiMmGTEiHhQoPSYoJSkqKDcp' - +'Ii0uLDAxLzI0Mh44U0gxMDI5JkM0JjU3NDY6Kjc5Njo7OUE8Ozw+Oz89QTxA' - +'F1akOFFiRIgPHTZksKBAgMCLGTdGNIAAQgKfDAcgZbj0odOnUA8GBAA7' - +'CxsSEhkWDhYYFQ0aJhkaGBweGyccGh8hHiIkIiMmGTEiHhQoPSYoJSkqKDcp' - +'Ii0uLDAxLzI0Mh44U0gxMDI5JkM0JjU3NDY6Kjc5Njo7OUE8Ozw+Oz89QTxA' - +'F1akOFFiRIgPHTZksKBAgMCLGTdGNIAAQgKfDAcgZbj0odOnUA8GBAA7') \ No newline at end of file diff --git a/astroid/tests/testdata/python3/data/joined_strings.py b/astroid/tests/testdata/python3/data/joined_strings.py deleted file mode 100644 index ca1af58baa..0000000000 --- a/astroid/tests/testdata/python3/data/joined_strings.py +++ /dev/null @@ -1,1051 +0,0 @@ -x = ('R0lGODlhigJnAef/AAABAAEEAAkCAAMGAg0GBAYJBQoMCBMODQ4QDRITEBkS' - +'CxsSEhkWDhYYFQ0aJhkaGBweGyccGh8hHiIkIiMmGTEiHhQoPSYoJSkqKDcp' - +'Ii0uLDAxLzI0Mh44U0gxMDI5JkM0JjU3NDY6Kjc5Njo7OUE8Ozw+Oz89QTxA' - +'F1akOFFiRIgPHTZksKBAgMCLGTdGNIAAQgKfDAcgZbj0odOnUA8GBAA7' - +'CxsSEhkWDhYYFQ0aJhkaGBweGyccGh8hHiIkIiMmGTEiHhQoPSYoJSkqKDcp' - +'Ii0uLDAxLzI0Mh44U0gxMDI5JkM0JjU3NDY6K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6Kjc5Njo7OUE8Ozw+Oz89QTxA' - +'F1akOFFiRIgPHTZksKBAgMCLGTdGNIAAQgKfDAcgZbj0odOnUA8GBAA7' - +'CxsSEhkWDhYYFQ0aJhkaGBweGyccGh8hHiIkIiMmGTEiHhQoPSYoJSkqKDcp' - +'Ii0uLDAxLzI0Mh44U0gxMDI5JkM0JjU3NDY6Kjc5Njo7OUE8Ozw+Oz89QTxA' - +'F1akOFFiRIgPHTZksKBAgMCLGTdGNIAAQgKfDAcgZbj0odOnUA8GBAA7' - +'CxsSEhkWDhYYFQ0aJhkaGBweGyccGh8hHiIkIiMmGTEiHhQoPSYoJSkqKDcp' - +'Ii0uLDAxLzI0Mh44U0gxMDI5JkM0JjU3NDY6Kjc5Njo7OUE8Ozw+Oz89QTxA' - +'F1akOFFiRIgPHTZksKBAgMCLGTdGNIAAQgKfDAcgZbj0odOnUA8GBAA7' - +'CxsSEhkWDhYYFQ0aJhkaGBweGyccGh8hHiIkIiMmGTEiHhQoPSYoJSkqKDcp' - +'Ii0uLDAxLzI0Mh44U0gxMDI5JkM0JjU3NDY6Kjc5Njo7OUE8Ozw+Oz89QTxA' - +'F1akOFFiRIgPHTZksKBAgMCLGTdGNIAAQgKfDAcgZbj0odOnUA8GBAA7') \ No newline at end of file diff --git a/astroid/tests/unittest_peephole.py b/astroid/tests/unittest_peephole.py deleted file mode 100644 index ec55b5d28d..0000000000 --- a/astroid/tests/unittest_peephole.py +++ /dev/null @@ -1,121 +0,0 @@ -# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . - -"""Tests for the astroid AST peephole optimizer.""" - -import ast -import textwrap -import unittest - -import astroid -from astroid import builder -from astroid import manager -from astroid import test_utils -from astroid.tests import resources -from astroid.tree import astpeephole - - -MANAGER = manager.AstroidManager() - - -class PeepholeOptimizer(unittest.TestCase): - @classmethod - def setUpClass(cls): - MANAGER.optimize_ast = True - - @classmethod - def tearDownClass(cls): - MANAGER.optimize_ast = False - - def setUp(self): - self._optimizer = astpeephole.ASTPeepholeOptimizer() - - @staticmethod - def _get_binops(code): - module = ast.parse(textwrap.dedent(code)) - return [node.value for node in module.body - if isinstance(node, ast.Expr)] - - @test_utils.require_version(maxver='3.0') - def test_optimize_binop_unicode(self): - nodes = self._get_binops(""" - u"a" + u"b" + u"c" - - u"a" + "c" + "b" - u"a" + b"c" - """) - - result = self._optimizer.optimize_binop(nodes[0]) - self.assertIsInstance(result, astroid.Const) - self.assertEqual(result.value, u"abc") - - self.assertIsNone(self._optimizer.optimize_binop(nodes[1])) - self.assertIsNone(self._optimizer.optimize_binop(nodes[2])) - - def test_optimize_binop(self): - nodes = self._get_binops(""" - "a" + "b" + "c" + "d" - b"a" + b"b" + b"c" + b"d" - "a" + "b" - - "a" + "b" + 1 + object - var = 4 - "a" + "b" + var + "c" - "a" + "b" + "c" - "4" - "a" + "b" + "c" + "d".format() - "a" - "b" - "a" - 1 + 4 + 5 + 6 - """) - - result = self._optimizer.optimize_binop(nodes[0]) - self.assertIsInstance(result, astroid.Const) - self.assertEqual(result.value, "abcd") - - result = self._optimizer.optimize_binop(nodes[1]) - self.assertIsInstance(result, astroid.Const) - self.assertEqual(result.value, b"abcd") - - for node in nodes[2:]: - self.assertIsNone(self._optimizer.optimize_binop(node)) - - def test_big_binop_crash(self): - # Test that we don't fail on a lot of joined strings - # through the addition operator. - module = resources.build_file('data/joined_strings.py') - element = next(module['x'].infer()) - self.assertIsInstance(element, astroid.Const) - self.assertEqual(len(element.value), 61660) - - def test_optimisation_disabled(self): - try: - MANAGER.optimize_ast = False - module = builder.parse(""" - '1' + '2' + '3' - """) - self.assertIsInstance(module.body[0], astroid.Expr) - self.assertIsInstance(module.body[0].value, astroid.BinOp) - self.assertIsInstance(module.body[0].value.left, astroid.BinOp) - self.assertIsInstance(module.body[0].value.left.left, - astroid.Const) - finally: - MANAGER.optimize_ast = True - - -if __name__ == '__main__': - unittest.main() diff --git a/astroid/tree/astpeephole.py b/astroid/tree/astpeephole.py deleted file mode 100644 index c66dd5a719..0000000000 --- a/astroid/tree/astpeephole.py +++ /dev/null @@ -1,86 +0,0 @@ -# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . -"""Small AST optimizations.""" - -import _ast - -from astroid import nodes - - -__all__ = ('ASTPeepholeOptimizer', ) - - -try: - _TYPES = (_ast.Str, _ast.Bytes) -except AttributeError: - _TYPES = (_ast.Str, ) - - -class ASTPeepholeOptimizer(object): - """Class for applying small optimizations to generate new AST.""" - - def optimize_binop(self, node, parent=None): - """Optimize BinOps with string Const nodes on the lhs. - - This fixes an infinite recursion crash, where multiple - strings are joined using the addition operator. With a - sufficient number of such strings, astroid will fail - with a maximum recursion limit exceeded. The - function will return a Const node with all the strings - already joined. - Return ``None`` if no AST node can be obtained - through optimization. - """ - ast_nodes = [] - current = node - while isinstance(current, _ast.BinOp): - # lhs must be a BinOp with the addition operand. - if not isinstance(current.left, _ast.BinOp): - return - if (not isinstance(current.left.op, _ast.Add) - or not isinstance(current.op, _ast.Add)): - return - - # rhs must a str / bytes. - if not isinstance(current.right, _TYPES): - return - - ast_nodes.append(current.right.s) - current = current.left - - if (isinstance(current, _ast.BinOp) - and isinstance(current.left, _TYPES) - and isinstance(current.right, _TYPES)): - # Stop early if we are at the last BinOp in - # the operation - ast_nodes.append(current.right.s) - ast_nodes.append(current.left.s) - break - - if not ast_nodes: - return - - # If we have inconsistent types, bail out. - known = type(ast_nodes[0]) - if any(not isinstance(element, known) - for element in ast_nodes[1:]): - return - - value = known().join(reversed(ast_nodes)) - newnode = nodes.Const(value, node.lineno, node.col_offset, parent) - return newnode diff --git a/astroid/tree/rebuilder.py b/astroid/tree/rebuilder.py index 6295bfa9a1..6bae7d7162 100644 --- a/astroid/tree/rebuilder.py +++ b/astroid/tree/rebuilder.py @@ -23,8 +23,6 @@ import sys from astroid import nodes -from astroid.tree import astpeephole - _BIN_OP_CLASSES = {_ast.Add: '+', @@ -106,8 +104,7 @@ def _visit_or_none(node, attr, visitor, parent, assign_ctx, visit='visit', class TreeRebuilder(object): """Rebuilds the _ast tree to become an Astroid tree""" - def __init__(self, manager): - self._manager = manager + def __init__(self): self._global_names = [] self._import_from_nodes = [] self._delayed_assattr = [] @@ -245,23 +242,6 @@ def visit_repr(self, node, parent, assign_ctx=None): def visit_binop(self, node, parent, assign_ctx=None): """visit a BinOp node by returning a fresh instance of it""" - if isinstance(node.left, _ast.BinOp) and self._manager.optimize_ast: - # Optimize BinOp operations in order to remove - # redundant recursion. For instance, if the - # following code is parsed in order to obtain - # its ast, then the rebuilder will fail with an - # infinite recursion, the same will happen with the - # inference engine as well. There's no need to hold - # so many objects for the BinOp if they can be reduced - # to something else (also, the optimization - # might handle only Const binops, which isn't a big - # problem for the correctness of the program). - # - # ("a" + "b" + # one thousand more + "c") - newnode = self._peepholer.optimize_binop(node, parent) - if newnode: - return newnode - newnode = nodes.BinOp(_BIN_OP_CLASSES[type(node.op)], node.lineno, node.col_offset, parent) newnode.postinit(self.visit(node.left, newnode, assign_ctx), From 93c90866c330a26a538443fd7b299bad89adc5f5 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Thu, 12 Nov 2015 22:20:24 +0200 Subject: [PATCH 065/312] Remove useless line. --- astroid/tree/rebuilder.py | 1 - 1 file changed, 1 deletion(-) diff --git a/astroid/tree/rebuilder.py b/astroid/tree/rebuilder.py index 6bae7d7162..de068f64ff 100644 --- a/astroid/tree/rebuilder.py +++ b/astroid/tree/rebuilder.py @@ -109,7 +109,6 @@ def __init__(self): self._import_from_nodes = [] self._delayed_assattr = [] self._visit_meths = {} - self._peepholer = astpeephole.ASTPeepholeOptimizer() def visit_module(self, node, modname, modpath, package): """visit a Module node by returning a fresh instance of it""" From b2d4d603830833fc1211cc538bdc0eec378b4b1d Mon Sep 17 00:00:00 2001 From: Dmitry Pribysh Date: Wed, 4 Nov 2015 13:53:03 +0300 Subject: [PATCH 066/312] Add __getitem__ method to collections.deque inferface description. --- astroid/brain/brain_stdlib.py | 1 + 1 file changed, 1 insertion(+) diff --git a/astroid/brain/brain_stdlib.py b/astroid/brain/brain_stdlib.py index cbaa6da531..68f3936bec 100644 --- a/astroid/brain/brain_stdlib.py +++ b/astroid/brain/brain_stdlib.py @@ -141,6 +141,7 @@ def reverse(self): pass def rotate(self, n): pass def __iter__(self): return self def __reversed__(self): return self.iterable[::-1] + def __getitem__(self, index): pass ''') From 3765bd2efa76657dca5914c2f77539ebf44eba9f Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Wed, 4 Nov 2015 11:00:34 -0500 Subject: [PATCH 067/312] Use introspection for _object_type --- astroid/helpers.py | 13 +++++++------ astroid/raw_building.py | 20 +++++++++++++------- astroid/tests/unittest_builder.py | 4 ++-- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/astroid/helpers.py b/astroid/helpers.py index bc05372159..2467a46772 100644 --- a/astroid/helpers.py +++ b/astroid/helpers.py @@ -19,6 +19,7 @@ """ Various helper utilities. """ +import types import six @@ -46,22 +47,22 @@ def _object_type(node, context=None): elif isinstance(inferred, (scoped_nodes.Lambda, bases.UnboundMethod)): if isinstance(inferred, scoped_nodes.Lambda): if inferred.root() is raw_building.astroid_builtins: - yield raw_building.astroid_builtins['BuiltinFunctionType'] + yield raw_building.astroid_builtins[types.BuiltinFunctionType.__name__] else: - yield raw_building.astroid_builtins['FunctionType'] + yield raw_building.astroid_builtins[types.FunctionType.__name__] elif isinstance(inferred, bases.BoundMethod): - yield raw_building.astroid_builtins['MethodType'] + yield raw_building.astroid_builtins[types.MethodType.__name__] elif isinstance(inferred, bases.UnboundMethod): if six.PY2: - yield raw_building.astroid_builtins['MethodType'] + yield raw_building.astroid_builtins[types.MethodType.__name__] else: - yield raw_building.astroid_builtins['FunctionType'] + yield raw_building.astroid_builtins[types.FunctionType.__name__] else: raise InferenceError('Function {func!r} inferred from {node!r} ' 'has no identifiable type.', node=node, func=inferred, contex=context) elif isinstance(inferred, scoped_nodes.Module): - yield raw_building.astroid_builtins['ModuleType'] + yield raw_building.astroid_builtins[types.ModuleType.__name__] else: yield inferred._proxied diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 263004f46b..009f1ec71a 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -475,13 +475,19 @@ def ast_from_ellipsis(ellipsis, built_objects, module, name=None, parent=None): # scoped_nodes._get_locals(n, locals_) # return locals_ -BUILTIN_TYPES = {type(None): 'NoneType', - type(NotImplemented): 'NotImplementedType', - types.GeneratorType: 'GeneratorType', - types.FunctionType: 'FunctionType', - types.MethodType: 'MethodType', - types.BuiltinFunctionType: 'BuiltinFunctionType', - types.ModuleType: 'ModuleType'} +BUILTIN_TYPES = frozenset((type(None), type(NotImplemented), + types.GeneratorType, types.FunctionType, + types.MethodType, + types.BuiltinFunctionType, + types.ModuleType)) + +# BUILTIN_TYPES = {type(None): 'NoneType', +# type(NotImplemented): 'NotImplementedType', +# types.GeneratorType: 'GeneratorType', +# types.FunctionType: 'FunctionType', +# types.MethodType: 'MethodType', +# types.BuiltinFunctionType: 'BuiltinFunctionType', +# types.ModuleType: 'ModuleType'} # Initialize the built_objects map for the builtins mock AST to ensure # that the types are included as Name nodes, not explicit ASTs. diff --git a/astroid/tests/unittest_builder.py b/astroid/tests/unittest_builder.py index fdaa6720b2..4fa2ff3ca7 100644 --- a/astroid/tests/unittest_builder.py +++ b/astroid/tests/unittest_builder.py @@ -540,8 +540,8 @@ def func2(a={}): ''' name_nodes = test_utils.extract_node(code) for node in name_nodes: - self.assertFalse(hasattr(next(node.infer()), 'locals')) - self.assertFalse(hasattr(next(node.infer()), 'instance_attrs')) + self.assertNotIn('custom_attr', next(node.infer()).locals) + self.assertNotIn('custom_attr', next(node.infer()).instance_attrs) def test_asstuple(self): code = 'a, b = range(2)' From d4e20de8e08a419203024e849f52503318df1664 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Wed, 4 Nov 2015 18:28:06 -0500 Subject: [PATCH 068/312] Fix generator issues and object_type tests --- astroid/raw_building.py | 2 ++ astroid/tests/unittest_helpers.py | 47 +++++++++++++------------------ 2 files changed, 22 insertions(+), 27 deletions(-) diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 009f1ec71a..0297af83fd 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -505,3 +505,5 @@ def ast_from_ellipsis(ellipsis, built_objects, module, name=None, parent=None): six.moves.builtins) astroid_builtins.body.append(class_node) class_node.parent = astroid_builtins + +bases.Generator._proxied = astroid_builtins.getattr(types.GeneratorType.__name__)[0] diff --git a/astroid/tests/unittest_helpers.py b/astroid/tests/unittest_helpers.py index f6389b9436..fede32adb3 100644 --- a/astroid/tests/unittest_helpers.py +++ b/astroid/tests/unittest_helpers.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU Lesser General Public License along # with astroid. If not, see . +import types import unittest import six @@ -40,14 +41,6 @@ def setUp(self): def _extract(self, obj_name): return self.builtins.getattr(obj_name)[0] - def _build_custom_builtin(self, obj_name): - return nodes.ClassDef(name=obj_name, parent=self.builtins) - - def assert_classes_equal(self, cls, other): - self.assertEqual(cls.name, other.name) - self.assertEqual(cls.root(), other.root()) - self.assertEqual(cls.qname(), other.qname()) - def test_object_type(self): pairs = [ ('1', self._extract('int')), @@ -57,15 +50,15 @@ def test_object_type(self): ('type', self._extract('type')), ('object', self._extract('type')), ('object()', self._extract('object')), - ('lambda: None', self._build_custom_builtin('function')), - ('len', self._build_custom_builtin('builtin_function_or_method')), - ('None', self._build_custom_builtin('None')), - ('import sys\nsys#@', self._build_custom_builtin('module')), + ('lambda: None', self._extract(types.FunctionType.__name__)), + ('len', self._extract(types.BuiltinFunctionType.__name__)), + ('None', self._extract(type(None).__name__)), + ('import sys\nsys#@', self._extract(types.ModuleType.__name__)), ] for code, expected in pairs: node = test_utils.extract_node(code) objtype = helpers.object_type(node) - self.assert_classes_equal(objtype, expected) + self.assertIs(objtype, expected) def test_object_type_classes_and_functions(self): ast_nodes = test_utils.extract_node(''' @@ -91,28 +84,28 @@ def static_method(): pass ''') from_self = helpers.object_type(ast_nodes[0]) cls = next(ast_nodes[1].infer()) - self.assert_classes_equal(from_self, cls) + self.assertIs(from_self, cls) cls_type = helpers.object_type(ast_nodes[1]) - self.assert_classes_equal(cls_type, self._extract('type')) + self.assertIs(cls_type, self._extract('type')) instance_type = helpers.object_type(ast_nodes[2]) cls = next(ast_nodes[2].infer())._proxied - self.assert_classes_equal(instance_type, cls) + self.assertIs(instance_type, cls) expected_method_types = [ - (ast_nodes[3], 'instancemethod' if six.PY2 else 'function'), - (ast_nodes[4], 'instancemethod' if six.PY2 else 'method'), - (ast_nodes[5], 'instancemethod' if six.PY2 else 'method'), - (ast_nodes[6], 'instancemethod' if six.PY2 else 'method'), - (ast_nodes[7], 'function'), - (ast_nodes[8], 'function'), - (ast_nodes[9], 'generator'), + (ast_nodes[3], types.MethodType.__name__ if six.PY2 else types.FunctionType.__name__), + (ast_nodes[4], types.MethodType.__name__), + (ast_nodes[5], types.MethodType.__name__), + (ast_nodes[6], types.MethodType.__name__), + (ast_nodes[7], types.FunctionType.__name__), + (ast_nodes[8], types.FunctionType.__name__), + (ast_nodes[9], types.GeneratorType.__name__), ] for node, expected in expected_method_types: node_type = helpers.object_type(node) - expected_type = self._build_custom_builtin(expected) - self.assert_classes_equal(node_type, expected_type) + expected_type = self._extract(expected) + self.assertIs(node_type, expected_type) @test_utils.require_version(minver='3.0') def test_object_type_metaclasses(self): @@ -123,11 +116,11 @@ class Meta(metaclass=abc.ABCMeta): meta_instance = Meta() ''') meta_type = helpers.object_type(module['Meta']) - self.assert_classes_equal(meta_type, module['Meta'].metaclass()) + self.assertIs(meta_type, module['Meta'].metaclass()) meta_instance = next(module['meta_instance'].infer()) instance_type = helpers.object_type(meta_instance) - self.assert_classes_equal(instance_type, module['Meta']) + self.assertIs(instance_type, module['Meta']) @test_utils.require_version(minver='3.0') def test_object_type_most_derived(self): From f538d4be57450053843b34dc1d7d549d293a9b13 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Thu, 5 Nov 2015 00:39:14 -0500 Subject: [PATCH 069/312] Fix inference on type() generating classes and getattr handling. * Fix ClassDef._infer_type_call and BoundMethod._infer_type_new_call to build proper ASTs. * Fix corresponding type() tests to use inferred values rather than directly looking in locals. * Add external_attrs handling to Module.getattr and ClassDef.getattr. --- astroid/bases.py | 19 ++++++++----------- astroid/scoped_nodes.py | 22 +++++++++++++++------- astroid/tests/unittest_inference.py | 5 +++-- astroid/tests/unittest_scoped_nodes.py | 13 ++++++++----- 4 files changed, 34 insertions(+), 25 deletions(-) diff --git a/astroid/bases.py b/astroid/bases.py index 19d3f8bf57..a0d94e4ad5 100644 --- a/astroid/bases.py +++ b/astroid/bases.py @@ -359,12 +359,15 @@ def _infer_type_new_call(self, caller, context): # All the bases needs to be Classes return + cls = mcs.__class__(name=name.value, lineno=caller.lineno, + col_offset=caller.col_offset, + parent=caller) + # Verify the attributes. attrs = next(caller.args[3].infer(context=context)) if not isinstance(attrs, node_classes.Dict): # Needs to be a dictionary. return - # cls_locals = collections.defaultdict(list) body = [] for key, value in attrs.items: key = next(key.infer(context=context)) @@ -375,21 +378,15 @@ def _infer_type_new_call(self, caller, context): if not isinstance(key.value, str): # Not a proper attribute. return - assign = node_classes.Assign() - assign.postinit(targets=node_classes.AssignName(key.value), + assign = node_classes.Assign(parent=cls) + assign.postinit(targets=node_classes.AssignName(key.value, + parent=assign), value=value) body.append(assign) - # cls_locals[key.value].append(value) - # print(attrs.repr_tree()) # Build the class from now. - cls = mcs.__class__(name=name.value, lineno=caller.lineno, - col_offset=caller.col_offset, - parent=caller) - empty = node_classes.Pass() - cls.postinit(bases=bases.elts, body=[empty], decorators=[], + cls.postinit(bases=bases.elts, body=body, decorators=[], newstyle=True, metaclass=mcs) - # cls.locals = cls_locals return cls def infer_call_result(self, caller, context=None): diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index 9f25405a1c..6995d5b69c 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -438,8 +438,11 @@ def getattr(self, name, context=None, ignore_locals=False): if name == '__path__' and self.package: return [node_classes.List()] + self.locals.get(name, []) return std_special_attributes(self, name) - if not ignore_locals and name in self.locals: # or name in self.external_attrs) + if not ignore_locals and name in self.locals: return self.locals[name] + # TODO: should ignore_locals also affect external_attrs? + if name in self.external_attrs: + return self.external_attrs[name] if self.package: try: return [self.import_module(name, relative_only=True)] @@ -1257,12 +1260,12 @@ def _infer_type_call(self, caller, context): else: return util.YES - result = ClassDef(name, None) + result = ClassDef(name, None, parent=caller.parent) # Get the bases of the class. class_bases = next(caller.args[1].infer(context)) if isinstance(class_bases, (node_classes.Tuple, node_classes.List)): - result.bases = class_bases.itered() + bases = class_bases.itered() else: # There is currently no AST node that can represent an 'unknown' # node (YES is not an AST node), therefore we simply return YES here @@ -1275,13 +1278,18 @@ def _infer_type_call(self, caller, context): except exceptions.InferenceError: members = None + body = [] if members and isinstance(members, node_classes.Dict): for attr, value in members.items: if (isinstance(attr, node_classes.Const) and isinstance(attr.value, six.string_types)): - result.locals[attr.value] = [value] + assign = node_classes.Assign(parent=result) + assign.postinit(targets=node_classes.AssignName(attr.value, + parent=assign), + value=value) + body.append(assign) - result.parent = caller.parent + result.postinit(bases=bases, body=body, decorators=[], newstyle=True) return result def infer_call_result(self, caller, context=None): @@ -1453,7 +1461,7 @@ def getattr(self, name, context=None, class_context=True): metaclass will be done. """ - values = self.locals.get(name, []) + values = self.locals.get(name, []) + self.external_attrs.get(name, []) if name in self.special_attributes: if name == '__module__': return [node_classes.Const(self.root().qname())] + values @@ -1471,7 +1479,7 @@ def getattr(self, name, context=None, class_context=True): # don't modify the list in self.locals! values = list(values) for classnode in self.ancestors(recurs=True, context=context): - values += classnode.locals.get(name, []) + values += classnode.locals.get(name, []) + classnode.external_attrs.get(name, []) if class_context: values += self._metaclass_lookup_attribute(name, context) diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index ba97cfea43..cc780e7b9c 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -2939,8 +2939,9 @@ class Entity(object): self.assertEqual(ancestors[0], inferred.root()['Entity']) attributes = inferred.getattr('a') self.assertEqual(len(attributes), 1) - self.assertIsInstance(attributes[0], nodes.Const) - self.assertEqual(attributes[0].value, 1) + a_inferred = next(attributes[0].infer()) + self.assertIsInstance(a_inferred, nodes.Const) + self.assertEqual(a_inferred.value, 1) def test_type__new__not_enough_arguments(self): ast_nodes = test_utils.extract_node(''' diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index 98efd8430b..45328b58e9 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -575,7 +575,8 @@ def test_cls_special_attributes_1(self): self.assertEqual(len(cls.getattr('__dict__')), 1) if not cls.newstyle: self.assertRaises(AttributeInferenceError, cls.getattr, '__mro__') - for cls in (nodes.List._proxied, nodes.Const(1)._proxied): + for cls in (nodes.List.from_constants((1,))._proxied, + nodes.Const(1)._proxied): self.assertEqual(len(cls.getattr('__bases__')), 1) self.assertEqual(len(cls.getattr('__name__')), 1) self.assertEqual(len(cls.getattr('__doc__')), 1, (cls, cls.getattr('__doc__'))) @@ -1354,10 +1355,12 @@ def test_type_three_arguments(self): self.assertIsInstance(first, nodes.ClassDef) self.assertEqual(first.name, "A") self.assertEqual(first.basenames, ["object"]) - self.assertIsInstance(first["a"], nodes.Const) - self.assertEqual(first["a"].value, 1) - self.assertIsInstance(first["b"], nodes.Const) - self.assertEqual(first["b"].value, 2) + a = next(first['a'].infer()) + self.assertIsInstance(a, nodes.Const) + self.assertEqual(a.value, 1) + b = next(first['b'].infer()) + self.assertIsInstance(b, nodes.Const) + self.assertEqual(b.value, 2) with self.assertRaises(AttributeInferenceError): first.getattr("missing") From d5def273c6f887b70ebf4853b848ed34413f97fc Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Thu, 5 Nov 2015 10:30:40 -0500 Subject: [PATCH 070/312] Clarify what helper function in unittest_helpers does --- astroid/tests/unittest_helpers.py | 34 +++++++++++++++---------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/astroid/tests/unittest_helpers.py b/astroid/tests/unittest_helpers.py index fede32adb3..c3b9c6922c 100644 --- a/astroid/tests/unittest_helpers.py +++ b/astroid/tests/unittest_helpers.py @@ -38,22 +38,22 @@ def setUp(self): self.builtins = astroid_manager.astroid_cache[builtins_name] self.manager = manager.AstroidManager() - def _extract(self, obj_name): + def _look_up_in_builtins(self, obj_name): return self.builtins.getattr(obj_name)[0] def test_object_type(self): pairs = [ - ('1', self._extract('int')), - ('[]', self._extract('list')), - ('{1, 2, 3}', self._extract('set')), - ('{1:2, 4:3}', self._extract('dict')), - ('type', self._extract('type')), - ('object', self._extract('type')), - ('object()', self._extract('object')), - ('lambda: None', self._extract(types.FunctionType.__name__)), - ('len', self._extract(types.BuiltinFunctionType.__name__)), - ('None', self._extract(type(None).__name__)), - ('import sys\nsys#@', self._extract(types.ModuleType.__name__)), + ('1', self._look_up_in_builtins('int')), + ('[]', self._look_up_in_builtins('list')), + ('{1, 2, 3}', self._look_up_in_builtins('set')), + ('{1:2, 4:3}', self._look_up_in_builtins('dict')), + ('type', self._look_up_in_builtins('type')), + ('object', self._look_up_in_builtins('type')), + ('object()', self._look_up_in_builtins('object')), + ('lambda: None', self._look_up_in_builtins(types.FunctionType.__name__)), + ('len', self._look_up_in_builtins(types.BuiltinFunctionType.__name__)), + ('None', self._look_up_in_builtins(type(None).__name__)), + ('import sys\nsys#@', self._look_up_in_builtins(types.ModuleType.__name__)), ] for code, expected in pairs: node = test_utils.extract_node(code) @@ -87,7 +87,7 @@ def static_method(): pass self.assertIs(from_self, cls) cls_type = helpers.object_type(ast_nodes[1]) - self.assertIs(cls_type, self._extract('type')) + self.assertIs(cls_type, self._look_up_in_builtins('type')) instance_type = helpers.object_type(ast_nodes[2]) cls = next(ast_nodes[2].infer())._proxied @@ -104,7 +104,7 @@ def static_method(): pass ] for node, expected in expected_method_types: node_type = helpers.object_type(node) - expected_type = self._extract(expected) + expected_type = self._look_up_in_builtins(expected) self.assertIs(node_type, expected_type) @test_utils.require_version(minver='3.0') @@ -173,7 +173,7 @@ class C(A): pass #@ cls_c = ast_nodes[2] int_subclass = ast_nodes[3] int_subclass = helpers.object_type(next(int_subclass.infer())) - base_int = self._extract('int') + base_int = self._look_up_in_builtins('int') self.assertTrue(helpers.is_subtype(int_subclass, base_int)) self.assertTrue(helpers.is_supertype(base_int, int_subclass)) @@ -235,7 +235,7 @@ def test_is_subtype_supertype_classes_no_type_ancestor(self): class A(object): #@ pass ''') - builtin_type = self._extract('type') + builtin_type = self._look_up_in_builtins('type') self.assertFalse(helpers.is_supertype(builtin_type, cls_a)) self.assertFalse(helpers.is_subtype(cls_a, builtin_type)) @@ -244,7 +244,7 @@ def test_is_subtype_supertype_classes_metaclasses(self): class A(type): #@ pass ''') - builtin_type = self._extract('type') + builtin_type = self._look_up_in_builtins('type') self.assertTrue(helpers.is_supertype(builtin_type, cls_a)) self.assertTrue(helpers.is_subtype(cls_a, builtin_type)) From 6c90e4d6a9293f9faf485e839cad05b82da8f6a5 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Fri, 6 Nov 2015 17:55:24 -0500 Subject: [PATCH 071/312] Rename Uninferable and instantiate_class, fix broken tests, improve testing. * Rename YES to Uninferable * Rename instanciate_class to instantiate_class. * Use six in test_namedtuple_advanced_inference. * Fix test_file_from_module failure on PyPy. * Add enum34 to unittest_brain. * Add unittest_brain dependencies tox.ini. --- astroid/__init__.py | 2 +- astroid/brain/brain_builtin_inference.py | 46 +++++----- astroid/brain/brain_stdlib.py | 4 +- astroid/builder.py | 2 +- astroid/decorators.py | 2 +- astroid/helpers.py | 4 +- astroid/inference.py | 76 ++++++++-------- astroid/interpreter/objects.py | 12 +-- astroid/interpreter/util.py | 12 +-- astroid/manager.py | 2 +- astroid/protocols.py | 36 ++++---- astroid/tests/unittest_brain.py | 14 +-- astroid/tests/unittest_builder.py | 2 +- astroid/tests/unittest_helpers.py | 8 +- astroid/tests/unittest_inference.py | 108 +++++++++++------------ astroid/tests/unittest_lookup.py | 2 +- astroid/tests/unittest_manager.py | 2 +- astroid/tests/unittest_nodes.py | 4 +- astroid/tests/unittest_protocols.py | 10 +-- astroid/tests/unittest_regrtest.py | 2 +- astroid/tests/unittest_scoped_nodes.py | 6 +- astroid/tree/base.py | 13 ++- astroid/tree/node_classes.py | 2 +- astroid/tree/scoped_nodes.py | 62 ++++++------- astroid/util.py | 7 +- tox.ini | 9 +- 26 files changed, 233 insertions(+), 216 deletions(-) diff --git a/astroid/__init__.py b/astroid/__init__.py index 836ba47743..8b07469e4e 100644 --- a/astroid/__init__.py +++ b/astroid/__init__.py @@ -67,7 +67,7 @@ from astroid.interpreter.util import are_exclusive, unpack_infer from astroid.tree.scoped_nodes import builtin_lookup from astroid.builder import parse -from astroid.util import YES +from astroid.util import Uninferable, YES # transform utilities (filters and decorator) diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index 75e1bf53af..1823015a0a 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -126,10 +126,10 @@ def _generic_inference(node, context, node_type, transform): inferred = next(arg.infer(context=context)) except (InferenceError, StopIteration): raise UseInferenceDefault() - if inferred is util.YES: + if inferred is util.Uninferable: raise UseInferenceDefault() transformed = transform(inferred) - if not transformed or transformed is util.YES: + if not transformed or transformed is util.Uninferable: raise UseInferenceDefault() return transformed @@ -320,7 +320,7 @@ def infer_super(node, context=None): if scope.type == 'classmethod': mro_type = cls else: - mro_type = cls.instanciate_class() + mro_type = cls.instantiate_class() else: # TODO(cpopa): support flow control (multiple inference values). try: @@ -332,7 +332,7 @@ def infer_super(node, context=None): except InferenceError: raise UseInferenceDefault - if mro_pointer is util.YES or mro_type is util.YES: + if mro_pointer is util.Uninferable or mro_type is util.Uninferable: # No way we could understand this. raise UseInferenceDefault @@ -356,11 +356,11 @@ def _infer_getattr_args(node, context): except InferenceError: raise UseInferenceDefault - if obj is util.YES or attr is util.YES: + if obj is util.Uninferable or attr is util.Uninferable: # If one of the arguments is something we can't infer, # then also make the result of the getattr call something # which is unknown. - return util.YES, util.YES + return util.Uninferable, util.Uninferable is_string = (isinstance(attr, nodes.Const) and isinstance(attr.value, six.string_types)) @@ -373,13 +373,13 @@ def _infer_getattr_args(node, context): def infer_getattr(node, context=None): """Understand getattr calls - If one of the arguments is an YES object, then the - result will be an YES object. Otherwise, the normal attribute + If one of the arguments is an Uninferable object, then the + result will be an Uninferable object. Otherwise, the normal attribute lookup will be done. """ obj, attr = _infer_getattr_args(node, context) - if obj is util.YES or attr is util.YES or not hasattr(obj, 'igetattr'): - return util.YES + if obj is util.Uninferable or attr is util.Uninferable or not hasattr(obj, 'igetattr'): + return util.Uninferable try: return next(obj.igetattr(attr, context=context)) @@ -400,17 +400,17 @@ def infer_hasattr(node, context=None): This always guarantees three possible outcomes for calling hasattr: Const(False) when we are sure that the object doesn't have the intended attribute, Const(True) when - we know that the object has the attribute and YES + we know that the object has the attribute and Uninferable when we are unsure of the outcome of the function call. """ try: obj, attr = _infer_getattr_args(node, context) - if obj is util.YES or attr is util.YES or not hasattr(obj, 'getattr'): - return util.YES + if obj is util.Uninferable or attr is util.Uninferable or not hasattr(obj, 'getattr'): + return util.Uninferable obj.getattr(attr, context=context) except UseInferenceDefault: # Can't infer something from this function call. - return util.YES + return util.Uninferable except NotFoundError: # Doesn't have it. return nodes.Const(False) @@ -433,9 +433,9 @@ def infer_callable(node, context=None): try: inferred = next(argument.infer(context=context)) except InferenceError: - return util.YES - if inferred is util.YES: - return util.YES + return util.Uninferable + if inferred is util.Uninferable: + return util.Uninferable return nodes.Const(inferred.callable()) @@ -452,13 +452,13 @@ def infer_bool(node, context=None): try: inferred = next(argument.infer(context=context)) except InferenceError: - return util.YES - if inferred is util.YES: - return util.YES + return util.Uninferable + if inferred is util.Uninferable: + return util.Uninferable bool_value = inferred.bool_value() - if bool_value is util.YES: - return util.YES + if bool_value is util.Uninferable: + return util.Uninferable return nodes.Const(bool_value) @@ -478,7 +478,7 @@ def infer_slice(node, context=None): args = list(map(interpreterutil.safe_infer, args)) for arg in args: - if not arg or arg is util.YES: + if not arg or arg is util.Uninferable: raise UseInferenceDefault if not isinstance(arg, nodes.Const): raise UseInferenceDefault diff --git a/astroid/brain/brain_stdlib.py b/astroid/brain/brain_stdlib.py index 68f3936bec..615242426e 100644 --- a/astroid/brain/brain_stdlib.py +++ b/astroid/brain/brain_stdlib.py @@ -24,7 +24,7 @@ def infer_func_form(node, base_type, context=None, enum=False): def infer_first(node): try: value = next(node.infer(context=context)) - if value is util.YES: + if value is util.Uninferable: raise UseInferenceDefault() else: return value @@ -332,7 +332,7 @@ def name(self): fake.parent = target.parent for method in node.mymethods(): fake.locals[method.name] = [method] - new_targets.append(fake.instanciate_class()) + new_targets.append(fake.instantiate_class()) node.locals[local] = new_targets break return node diff --git a/astroid/builder.py b/astroid/builder.py index 7252603fbb..75dacc14a6 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -220,7 +220,7 @@ def delayed_assattr(self, node): try: frame = node.frame() for inferred in node.expr.infer(): - if inferred is util.YES: + if inferred is util.Uninferable: continue try: if isinstance(inferred, runtimeabc.Instance): diff --git a/astroid/decorators.py b/astroid/decorators.py index d612fa9ada..f84566014b 100644 --- a/astroid/decorators.py +++ b/astroid/decorators.py @@ -113,7 +113,7 @@ def yes_if_nothing_inferred(func, instance, args, kwargs): inferred = True yield node if not inferred: - yield util.YES + yield util.Uninferable @wrapt.decorator diff --git a/astroid/helpers.py b/astroid/helpers.py index a0ba6c18e6..a9e369b795 100644 --- a/astroid/helpers.py +++ b/astroid/helpers.py @@ -89,7 +89,7 @@ def object_type(node, context=None): try: types = set(_object_type(node, context)) except exceptions.InferenceError: - return util.YES + return util.Uninferable if len(types) > 1 or not types: - return util.YES + return util.Uninferable return list(types)[0] diff --git a/astroid/inference.py b/astroid/inference.py index 67ff4d8978..a103512c75 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -109,7 +109,7 @@ def infer_call(self, context=None): keywords=self.keywords) callcontext.boundnode = None for callee in self.func.infer(context): - if callee is util.YES: + if callee is util.Uninferable: yield callee continue try: @@ -157,7 +157,7 @@ def infer_import_from(self, context=None, asname=True): def infer_attribute(self, context=None): """infer an Attribute node by using getattr on the associated object""" for owner in self.expr.infer(context): - if owner is util.YES: + if owner is util.Uninferable: yield owner continue try: @@ -225,13 +225,13 @@ def infer_subscript(self, context=None): """ value = next(self.value.infer(context)) - if value is util.YES: - yield util.YES + if value is util.Uninferable: + yield util.Uninferable return index = next(self.slice.infer(context)) - if index is util.YES: - yield util.YES + if index is util.Uninferable: + yield util.Uninferable return if isinstance(value, runtimeabc.Instance): @@ -264,8 +264,8 @@ def infer_subscript(self, context=None): # Prevent inferring if the inferred subscript # is the same as the original subscripted object. - if self is assigned or assigned is util.YES: - yield util.YES + if self is assigned or assigned is util.Uninferable: + yield util.Uninferable return for inferred in assigned.infer(context): yield inferred @@ -292,31 +292,31 @@ def _infer_boolop(self, context=None): try: values = [value.infer(context=context) for value in values] except exceptions.InferenceError: - yield util.YES + yield util.Uninferable return for pair in itertools.product(*values): - if any(item is util.YES for item in pair): - # Can't infer the final result, just yield YES. - yield util.YES + if any(item is util.Uninferable for item in pair): + # Can't infer the final result, just yield Uninferable. + yield util.Uninferable continue bool_values = [item.bool_value() for item in pair] - if any(item is util.YES for item in bool_values): - # Can't infer the final result, just yield YES. - yield util.YES + if any(item is util.Uninferable for item in bool_values): + # Can't infer the final result, just yield Uninferable. + yield util.Uninferable continue # Since the boolean operations are short circuited operations, # this code yields the first value for which the predicate is True # and if no value respected the predicate, then the last value will - # be returned (or YES if there was no last value). + # be returned (or Uninferable if there was no last value). # This is conforming to the semantics of `and` and `or`: # 1 and 0 -> 1 # 0 and 1 -> 0 # 1 or 0 -> 1 # 0 or 1 -> 1 - value = util.YES + value = util.Uninferable for value, bool_value in zip(pair, bool_values): if predicate(bool_value): yield value @@ -333,7 +333,7 @@ def _filter_operation_errors(self, infer_callable, context, error): # For the sake of .infer(), we don't care about operation # errors, which is the job of pylint. So return something # which shows that we can't infer the result. - yield util.YES + yield util.Uninferable else: yield result @@ -351,12 +351,12 @@ def infer_unaryop(self, context=None, nodes=None): if meth is None: # `not node`. Determine node's boolean # value and negate its result, unless it is - # YES, which will be returned as is. + # Uninferable, which will be returned as is. bool_value = operand.bool_value() - if bool_value is not util.YES: + if bool_value is not util.Uninferable: yield nodes.const_factory(not bool_value) else: - yield util.YES + yield util.Uninferable else: if not isinstance(operand, runtimeabc.Instance): # The operation was used on something which @@ -367,7 +367,7 @@ def infer_unaryop(self, context=None, nodes=None): try: meth = operand.getattr(meth, context=context)[0] inferred = next(meth.infer(context=context)) - if inferred is util.YES or not inferred.callable(): + if inferred is util.Uninferable or not inferred.callable(): continue context = contextmod.copy_context(context) @@ -383,7 +383,7 @@ def infer_unaryop(self, context=None, nodes=None): # The unary operation special method was not found. yield exceptions.UnaryOperationError(operand, self.op, exc) except exceptions.InferenceError: - yield util.YES + yield util.Uninferable @decorators.raise_if_nothing_inferred @@ -540,23 +540,23 @@ def _infer_binary_operation(left, right, op, context, flow_factory, nodes): except (AttributeError, exceptions.NotFoundError): continue except exceptions.InferenceError: - yield util.YES + yield util.Uninferable return else: - if any(result is util.YES for result in results): - yield util.YES + if any(result is util.Uninferable for result in results): + yield util.Uninferable return # TODO(cpopa): since the inferrence engine might return # more values than are actually possible, we decide - # to return util.YES if we have union types. + # to return util.Uninferable if we have union types. if all(map(_is_not_implemented, results)): continue not_implemented = sum(1 for result in results if _is_not_implemented(result)) if not_implemented and not_implemented != len(results): # Can't decide yet what this is, not yet though. - yield util.YES + yield util.Uninferable return for result in results: @@ -582,15 +582,15 @@ def infer_binop(self, context, nodes): rhs_context = context.clone() for lhs in left.infer(context=lhs_context): - if lhs is util.YES: + if lhs is util.Uninferable: # Don't know how to process this. - yield util.YES + yield util.Uninferable return for rhs in right.infer(context=rhs_context): - if rhs is util.YES: + if rhs is util.Uninferable: # Don't know how to process this. - yield util.YES + yield util.Uninferable return results = _infer_binary_operation(lhs, rhs, op, context, @@ -614,9 +614,9 @@ def infer_augassign(self, context=None, nodes=None): op = self.op for lhs in self.target.infer_lhs(context=context): - if lhs is util.YES: + if lhs is util.Uninferable: # Don't know how to process this. - yield util.YES + yield util.Uninferable return # TODO(cpopa): if we have A() * A(), trying to infer @@ -626,9 +626,9 @@ def infer_augassign(self, context=None, nodes=None): rhs_context = context.clone() rhs_context.path = set() for rhs in self.value.infer(context=rhs_context): - if rhs is util.YES: + if rhs is util.Uninferable: # Don't know how to process this. - yield util.YES + yield util.Uninferable return results = _infer_binary_operation(lhs, rhs, op, @@ -675,14 +675,14 @@ def infer_assign(self, context=None): @decorators.path_wrapper def infer_empty_node(self, context=None): if not self.has_underlying_object(): - yield util.YES + yield util.Uninferable else: try: for inferred in MANAGER.infer_ast_from_something(self.object, context=context): yield inferred except exceptions.AstroidError: - yield util.YES + yield util.Uninferable @infer.register(treeabc.Index) diff --git a/astroid/interpreter/objects.py b/astroid/interpreter/objects.py index 3e0bc60d67..51528dba6a 100644 --- a/astroid/interpreter/objects.py +++ b/astroid/interpreter/objects.py @@ -67,7 +67,7 @@ def is_property(meth): if PROPERTIES.intersection(meth.decoratornames()): return True stripped = {name.split(".")[-1] for name in meth.decoratornames() - if name is not util.YES} + if name is not util.Uninferable} return any(name in stripped for name in POSSIBLE_PROPERTIES) @@ -97,14 +97,14 @@ def _infer_method_result_truth(instance, method_name, context): meth = next(instance.igetattr(method_name, context=context), None) if meth and hasattr(meth, 'infer_call_result'): if not meth.callable(): - return util.YES + return util.Uninferable for value in meth.infer_call_result(instance, context=context): - if value is util.YES: + if value is util.Uninferable: return value inferred = next(value.infer(context=context)) return inferred.bool_value() - return util.YES + return util.Uninferable @@ -189,7 +189,7 @@ def infer_call_result(self, caller, context=None): """infer what a class instance is returning when called""" inferred = False for node in self._proxied.igetattr('__call__', context): - if node is util.YES or not node.callable(): + if node is util.Uninferable or not node.callable(): continue for res in node.infer_call_result(caller, context): inferred = True @@ -295,7 +295,7 @@ def infer_call_result(self, caller, context): if (self._proxied.name == '__new__' and self._proxied.parent.frame().qname() == '%s.object' % BUILTINS): infer = caller.args[0].infer() if caller.args else [] - return ((x is util.YES and x or Instance(x)) for x in infer) + return ((x is util.Uninferable and x or Instance(x)) for x in infer) return self._proxied.infer_call_result(caller, context) def bool_value(self): diff --git a/astroid/interpreter/util.py b/astroid/interpreter/util.py index 189ffa2c73..3302fea3b3 100644 --- a/astroid/interpreter/util.py +++ b/astroid/interpreter/util.py @@ -38,7 +38,7 @@ def infer_stmts(stmts, context, frame=None): context = contextmod.InferenceContext() for stmt in stmts: - if stmt is util.YES: + if stmt is util.Uninferable: yield stmt inferred = True continue @@ -50,7 +50,7 @@ def infer_stmts(stmts, context, frame=None): except exceptions.UnresolvableName: continue except exceptions.InferenceError: - yield util.YES + yield util.Uninferable inferred = True if not inferred: raise exceptions.InferenceError(str(stmt)) @@ -70,9 +70,9 @@ def unpack_infer(stmt, context=None): if inferred is stmt: yield inferred return - # else, infer recursivly, except YES object that should be returned as is + # else, infer recursivly, except Uninferable object that should be returned as is for inferred in stmt.infer(context): - if inferred is util.YES: + if inferred is util.Uninferable: yield inferred else: for inf_inf in unpack_infer(inferred, context): @@ -194,7 +194,7 @@ def has_known_bases(klass, context=None): def _type_check(type1, type2): if not all(map(has_known_bases, (type1, type2))): - return util.YES + return util.Uninferable if not all([type1.newstyle, type2.newstyle]): return False @@ -202,7 +202,7 @@ def _type_check(type1, type2): return type1 in type2.mro()[:-1] except exceptions.MroError: # The MRO is invalid. - return util.YES + return util.Uninferable def is_subtype(type1, type2): diff --git a/astroid/manager.py b/astroid/manager.py index 962d5b16ad..9a74f243fe 100644 --- a/astroid/manager.py +++ b/astroid/manager.py @@ -244,7 +244,7 @@ def infer_ast_from_something(self, obj, context=None): yield inferred else: for inferred in modastroid.igetattr(name, context): - yield inferred.instanciate_class() + yield inferred.instantiate_class() def register_failed_import_hook(self, hook): """Registers a hook to resolve imports that cannot be found otherwise. diff --git a/astroid/protocols.py b/astroid/protocols.py index c31b22088f..c6909fdac2 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -156,12 +156,12 @@ def const_infer_binary_op(self, operator, other, context, _, nodes): # ArithmeticError is not enough: float >> float is a TypeError yield not_implemented except Exception: # pylint: disable=broad-except - yield util.YES + yield util.Uninferable except TypeError: yield not_implemented elif isinstance(self.value, six.string_types) and operator == '%': # TODO(cpopa): implement string interpolation later on. - yield util.YES + yield util.Uninferable else: yield not_implemented @@ -172,7 +172,7 @@ def _multiply_seq_by_int(self, other, context): for elt in self.elts: infered = inferenceutil.safe_infer(elt, context) if infered is None: - infered = util.YES + infered = util.Uninferable elts.append(infered) node.elts = elts * other.value return node @@ -186,9 +186,9 @@ def tl_infer_binary_op(self, operator, other, context, method, nodes): if isinstance(other, self.__class__) and operator == '+': node = self.__class__() elts = [n for elt in self.elts for n in elt.infer(context) - if not n is util.YES] + if not n is util.Uninferable] elts += [n for elt in other.elts for n in elt.infer(context) - if not n is util.YES] + if not n is util.Uninferable] node.elts = elts yield node elif isinstance(other, treeabc.Const) and operator == '*': @@ -200,7 +200,7 @@ def tl_infer_binary_op(self, operator, other, context, method, nodes): # Verify if the instance supports __index__. as_index = inferenceutil.class_instance_as_index(other) if not as_index: - yield util.YES + yield util.Uninferable else: yield _multiply_seq_by_int(self, as_index, context) else: @@ -223,7 +223,7 @@ def _resolve_looppart(parts, asspath, context): asspath = asspath[:] index = asspath.pop(0) for part in parts: - if part is util.YES: + if part is util.Uninferable: continue # XXX handle __iter__ and log potentially detected errors if not hasattr(part, 'itered'): @@ -243,7 +243,7 @@ def _resolve_looppart(parts, asspath, context): # we achieved to resolved the assignment path, # don't infer the last part yield assigned - elif assigned is util.YES: + elif assigned is util.Uninferable: break else: # we are not yet on the last part of the path @@ -290,7 +290,7 @@ def _arguments_infer_argname(self, name, context, nodes): # arguments information may be missing, in which case we can't do anything # more if not (self.args or self.vararg or self.kwarg): - yield util.YES + yield util.Uninferable return # first argument of instance/class method if self.args and getattr(self.args[0], 'name', None) == name: @@ -303,7 +303,7 @@ def _arguments_infer_argname(self, name, context, nodes): yield cls return if functype == 'method': - yield self.parent.parent.frame().instanciate_class() + yield self.parent.parent.frame().instantiate_class() return if context and context.callcontext: @@ -324,15 +324,15 @@ def _arguments_infer_argname(self, name, context, nodes): kwarg.parent = self yield kwarg return - # if there is a default value, yield it. And then yield YES to reflect + # if there is a default value, yield it. And then yield Uninferable to reflect # we can't guess given argument value try: context = contextmod.copy_context(context) for inferred in self.default_value(name).infer(context): yield inferred - yield util.YES + yield util.Uninferable except exceptions.NoDefault: - yield util.YES + yield util.Uninferable @assigned_stmts.register(treeabc.Arguments) @@ -375,7 +375,7 @@ def _resolve_asspart(parts, asspath, context): # we achieved to resolved the assignment path, don't infer the # last part yield assigned - elif assigned is util.YES: + elif assigned is util.Uninferable: return else: # we are not yet on the last part of the path search on each @@ -393,7 +393,7 @@ def _resolve_asspart(parts, asspath, context): def excepthandler_assigned_stmts(self, nodes, node=None, context=None, asspath=None): for assigned in inferenceutil.unpack_infer(self.type): if isinstance(assigned, treeabc.ClassDef): - assigned = assigned.instanciate_class() + assigned = assigned.instantiate_class() yield assigned @@ -501,11 +501,11 @@ def starred_assigned_stmts(self, nodes, node=None, context=None, asspath=None): try: rhs = next(value.infer(context)) except exceptions.InferenceError: - yield util.YES + yield util.Uninferable return - if rhs is util.YES or not hasattr(rhs, 'elts'): + if rhs is util.Uninferable or not hasattr(rhs, 'elts'): # Not interested in inferred values without elts. - yield util.YES + yield util.Uninferable return if len(lhs.elts) > len(rhs.elts) + 1: diff --git a/astroid/tests/unittest_brain.py b/astroid/tests/unittest_brain.py index 9262391cf0..a5d1aa5ce6 100644 --- a/astroid/tests/unittest_brain.py +++ b/astroid/tests/unittest_brain.py @@ -46,7 +46,11 @@ import enum # pylint: disable=unused-import HAS_ENUM = True except ImportError: - HAS_ENUM = False + try: + import enum34 as enum + HAS_ENUM = True + except ImportError: + HAS_ENUM = False try: import dateutil # pylint: disable=unused-import @@ -120,17 +124,15 @@ def test_namedtuple_inference_failure(self): def foo(fields): return __(namedtuple("foo", fields)) """) - self.assertIs(util.YES, next(klass.infer())) + self.assertIs(util.Uninferable, next(klass.infer())) - @unittest.skipIf(sys.version_info[0] > 2, - 'namedtuple inference is broken on Python 3') def test_namedtuple_advanced_inference(self): # urlparse return an object of class ParseResult, which has a # namedtuple call and a mixin as base classes result = test_utils.extract_node(""" - import urlparse + import six - result = __(urlparse.urlparse('gopher://')) + result = __(six.moves.urllib.parse.urlparse('gopher://')) """) instance = next(result.infer()) self.assertEqual(len(instance.getattr('scheme')), 1) diff --git a/astroid/tests/unittest_builder.py b/astroid/tests/unittest_builder.py index 84bf50c235..bf23677c2f 100644 --- a/astroid/tests/unittest_builder.py +++ b/astroid/tests/unittest_builder.py @@ -482,7 +482,7 @@ def test_gen_expr_var_scope(self): n = test_utils.get_name_node(astroid, 'n') self.assertIsNot(n.scope(), astroid) self.assertEqual([i.__class__ for i in n.infer()], - [util.YES.__class__]) + [util.Uninferable.__class__]) def test_no_future_imports(self): mod = builder.parse("import sys") diff --git a/astroid/tests/unittest_helpers.py b/astroid/tests/unittest_helpers.py index 612f99428d..b0723d2326 100644 --- a/astroid/tests/unittest_helpers.py +++ b/astroid/tests/unittest_helpers.py @@ -155,7 +155,7 @@ def test_inference_errors(self): from unknown import Unknown u = Unknown #@ ''') - self.assertEqual(helpers.object_type(node), util.YES) + self.assertEqual(helpers.object_type(node), util.Uninferable) def test_object_type_too_many_types(self): node = test_utils.extract_node(''' @@ -167,7 +167,7 @@ def test(x): return 1 test(Unknown) #@ ''') - self.assertEqual(helpers.object_type(node), util.YES) + self.assertEqual(helpers.object_type(node), util.Uninferable) def test_is_subtype(self): ast_nodes = test_utils.extract_node(''' @@ -217,8 +217,8 @@ class E(C, B): pass #@ class F(D, E): pass #@ ''') self.assertFalse(interpreterutil.is_subtype(cls_e, cls_f)) - self.assertEqual(interpreterutil.is_subtype(cls_f, cls_e), util.YES) - self.assertEqual(interpreterutil.is_supertype(cls_e, cls_f), util.YES) + self.assertEqual(interpreterutil.is_subtype(cls_f, cls_e), util.Uninferable) + self.assertEqual(interpreterutil.is_supertype(cls_e, cls_f), util.Uninferable) self.assertFalse(interpreterutil.is_supertype(cls_f, cls_e)) def test_is_subtype_supertype_unknown_bases(self): diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index 4c49b45ece..f13979938c 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -314,7 +314,7 @@ def test_args_default_inference1(self): self.assertIsInstance(obj1, nodes.Const) self.assertEqual(obj1.value, 0) obj1 = next(inferred) - self.assertIs(obj1, util.YES, obj1) + self.assertIs(obj1, util.Uninferable, obj1) self.assertRaises(StopIteration, partial(next, inferred)) def test_args_default_inference2(self): @@ -323,13 +323,13 @@ def test_args_default_inference2(self): self.assertIsInstance(obj1, nodes.Const) self.assertEqual(obj1.value, 4) obj1 = next(inferred) - self.assertIs(obj1, util.YES, obj1) + self.assertIs(obj1, util.Uninferable, obj1) self.assertRaises(StopIteration, partial(next, inferred)) def test_inference_restrictions(self): inferred = test_utils.get_name_node(self.ast['C']['meth1'], 'arg1').infer() obj1 = next(inferred) - self.assertIs(obj1, util.YES, obj1) + self.assertIs(obj1, util.Uninferable, obj1) self.assertRaises(StopIteration, partial(next, inferred)) def test_ancestors_inference(self): @@ -565,7 +565,7 @@ def test_qqch(self): ast = parse(code, __name__) xxx = ast['xxx'] self.assertSetEqual({n.__class__ for n in xxx.inferred()}, - {nodes.Const, util.YES.__class__}) + {nodes.Const, util.Uninferable.__class__}) def test_method_argument(self): code = ''' @@ -581,13 +581,13 @@ def meth(self, e_type, *args, **kwargs): ast = parse(code, __name__) arg = test_utils.get_name_node(ast['ErudiEntitySchema']['__init__'], 'e_type') self.assertEqual([n.__class__ for n in arg.infer()], - [util.YES.__class__]) + [util.Uninferable.__class__]) arg = test_utils.get_name_node(ast['ErudiEntitySchema']['__init__'], 'kwargs') self.assertEqual([n.__class__ for n in arg.infer()], [nodes.Dict]) arg = test_utils.get_name_node(ast['ErudiEntitySchema']['meth'], 'e_type') self.assertEqual([n.__class__ for n in arg.infer()], - [util.YES.__class__]) + [util.Uninferable.__class__]) arg = test_utils.get_name_node(ast['ErudiEntitySchema']['meth'], 'args') self.assertEqual([n.__class__ for n in arg.infer()], [nodes.Tuple]) @@ -724,7 +724,7 @@ class InvalidGetitem2(object): for node in ast_nodes[:3]: self.assertRaises(InferenceError, next, node.infer()) for node in ast_nodes[3:]: - self.assertEqual(next(node.infer()), util.YES) + self.assertEqual(next(node.infer()), util.Uninferable) ast_nodes = test_utils.extract_node(''' [1, 2, 3][None] #@ 'lala'['bala'] #@ @@ -973,7 +973,7 @@ def __add__(self, other): self.assertEqual(first.value, 43) second = next(ast_nodes[1].infer()) - self.assertEqual(second, util.YES) + self.assertEqual(second, util.Uninferable) def test_binary_op_other_type_using_reflected_operands(self): ast_nodes = test_utils.extract_node(''' @@ -984,7 +984,7 @@ def __radd__(self, other): 1 + A() #@ ''') first = next(ast_nodes[0].infer()) - self.assertEqual(first, util.YES) + self.assertEqual(first, util.Uninferable) second = next(ast_nodes[1].infer()) self.assertIsInstance(second, nodes.Const) @@ -998,7 +998,7 @@ def __radd__(self, other): return NotImplemented 1 + A() #@ ''') first = next(ast_node.infer()) - self.assertEqual(first, util.YES) + self.assertEqual(first, util.Uninferable) def test_binary_op_list_mul(self): for code in ('a = [[]] * 2', 'a = 2 * [[]]'): @@ -1015,10 +1015,10 @@ def test_binary_op_list_mul_none(self): ast = builder.string_build('a = [1] * None\nb = [1] * "r"') inferred = ast['a'].inferred() self.assertEqual(len(inferred), 1) - self.assertEqual(inferred[0], util.YES) + self.assertEqual(inferred[0], util.Uninferable) inferred = ast['b'].inferred() self.assertEqual(len(inferred), 1) - self.assertEqual(inferred[0], util.YES) + self.assertEqual(inferred[0], util.Uninferable) def test_binary_op_list_mul_int(self): 'test correct handling on list multiplied by int when there are more than one' @@ -1090,7 +1090,7 @@ def f(g = lambda: None): callfuncnode = test_utils.extract_node(code) inferred = list(callfuncnode.infer()) self.assertEqual(len(inferred), 2, inferred) - inferred.remove(util.YES) + inferred.remove(util.Uninferable) self.assertIsInstance(inferred[0], nodes.Const) self.assertIsNone(inferred[0].value) @@ -1102,7 +1102,7 @@ def f(x): ast = parse(code, __name__) inferred = list(ast['f'].ilookup('a')) self.assertEqual(len(inferred), 1) - self.assertEqual(inferred[0], util.YES) + self.assertEqual(inferred[0], util.Uninferable) def test_nonregr_instance_attrs(self): """non regression for instance_attrs infinite loop : pylint / #4""" @@ -1154,7 +1154,7 @@ def test_python25_no_relative_import(self): self.assertTrue(ast.absolute_import_activated(), True) inferred = next(test_utils.get_name_node(ast, 'import_package_subpackage_module').infer()) # failed to import since absolute_import is activated - self.assertIs(inferred, util.YES) + self.assertIs(inferred, util.Uninferable) def test_nonregr_absolute_import(self): ast = resources.build_file('data/absimp/string.py', 'data.absimp.string') @@ -1272,7 +1272,7 @@ def qux(): ast = parse(code, __name__) inferred = list(test_utils.get_name_node(ast['foo'], 'spam').infer()) self.assertEqual(len(inferred), 1) - self.assertIs(inferred[0], util.YES) + self.assertIs(inferred[0], util.Uninferable) def test_nonregr_func_global(self): code = ''' @@ -1463,7 +1463,7 @@ def __mul__(self, other): ast = parse(code, __name__) sub = ast['sub'].inferred()[0] mul = ast['mul'].inferred()[0] - self.assertIs(sub, util.YES) + self.assertIs(sub, util.Uninferable) self.assertIsInstance(mul, nodes.Const) self.assertEqual(mul.value, 42) @@ -1482,7 +1482,7 @@ class B(A): ast = parse(code, __name__) sub = ast['sub'].inferred()[0] mul = ast['mul'].inferred()[0] - self.assertIs(sub, util. YES) + self.assertIs(sub, util. Uninferable) self.assertIsInstance(mul, nodes.Const) self.assertEqual(mul.value, 42) @@ -1502,7 +1502,7 @@ def __mul__(self, other): ast = parse(code, __name__) sub = ast['sub'].inferred()[0] mul = ast['mul'].inferred()[0] - self.assertIs(sub, util.YES) + self.assertIs(sub, util.Uninferable) self.assertIsInstance(mul, nodes.List) self.assertIsInstance(mul.elts[0], nodes.Const) self.assertEqual(mul.elts[0].value, 42) @@ -1519,12 +1519,12 @@ def __mul__(self, other): """ ast = parse(code, __name__) node = ast['c'] - self.assertEqual(node.inferred(), [util.YES]) + self.assertEqual(node.inferred(), [util.Uninferable]) def test_infer_empty_nodes(self): # Should not crash when trying to infer EmptyNodes. node = nodes.EmptyNode() - self.assertEqual(node.inferred(), [util.YES]) + self.assertEqual(node.inferred(), [util.Uninferable]) def test_infinite_loop_for_decorators(self): # Issue https://bitbucket.org/logilab/astroid/issue/50 @@ -2030,7 +2030,7 @@ def no_yield_mgr(): def test_unary_op_leaks_stop_iteration(self): node = test_utils.extract_node('+[] #@') - self.assertEqual(util.YES, next(node.infer())) + self.assertEqual(util.Uninferable, next(node.infer())) def test_unary_operands(self): ast_nodes = test_utils.extract_node(''' @@ -2082,7 +2082,7 @@ def lala(self): return 24 for bad_node in ast_nodes[6:]: inferred = next(bad_node.infer()) - self.assertEqual(inferred, util.YES) + self.assertEqual(inferred, util.Uninferable) def test_unary_op_instance_method_not_callable(self): ast_node = test_utils.extract_node(''' @@ -2279,11 +2279,11 @@ def true_value(): genexpr = next(module['genexpr'].infer()) self.assertTrue(genexpr.bool_value()) dict_comp = next(module['dict_comp'].infer()) - self.assertEqual(dict_comp, util.YES) + self.assertEqual(dict_comp, util.Uninferable) set_comp = next(module['set_comp'].infer()) - self.assertEqual(set_comp, util.YES) + self.assertEqual(set_comp, util.Uninferable) list_comp = next(module['list_comp'].infer()) - self.assertEqual(list_comp, util.YES) + self.assertEqual(list_comp, util.Uninferable) lambda_func = next(module['lambda_func'].infer()) self.assertTrue(lambda_func) unbound_method = next(module['unbound_method'].infer()) @@ -2297,13 +2297,13 @@ def true_value(): bin_op = module['bin_op'].parent.value self.assertTrue(bin_op.bool_value()) bool_op = module['bool_op'].parent.value - self.assertEqual(bool_op.bool_value(), util.YES) + self.assertEqual(bool_op.bool_value(), util.Uninferable) callfunc = module['callfunc'].parent.value - self.assertEqual(callfunc.bool_value(), util.YES) + self.assertEqual(callfunc.bool_value(), util.Uninferable) good_callfunc = next(module['good_callfunc'].infer()) self.assertTrue(good_callfunc.bool_value()) compare = module['compare'].parent.value - self.assertEqual(compare.bool_value(), util.YES) + self.assertEqual(compare.bool_value(), util.Uninferable) def test_bool_value_instances(self): instances = test_utils.extract_node(''' @@ -2336,7 +2336,7 @@ class NonMethods(object): AlwaysTrueInstance() #@ ErrorInstance() #@ '''.format(bool=BOOL_SPECIAL_METHOD)) - expected = (False, True, False, True, True, util.YES, util.YES) + expected = (False, True, False, True, True, util.Uninferable, util.Uninferable) for node, expected_value in zip(instances, expected): inferred = next(node.infer()) self.assertEqual(inferred.bool_value(), expected_value) @@ -2408,7 +2408,7 @@ class B(object): pass A() + B() #@ ''') inferred = next(node.infer()) - self.assertEqual(inferred, util.YES) + self.assertEqual(inferred, util.Uninferable) def test_binop_different_types_reflected_and_normal_not_implemented(self): node = test_utils.extract_node(''' @@ -2419,7 +2419,7 @@ def __radd__(self, other): return NotImplemented A() + B() #@ ''') inferred = next(node.infer()) - self.assertEqual(inferred, util.YES) + self.assertEqual(inferred, util.Uninferable) def test_binop_subtype(self): node = test_utils.extract_node(''' @@ -2452,7 +2452,7 @@ def __add__(self, other): return NotImplemented B() + A() #@ ''') inferred = next(node.infer()) - self.assertEqual(inferred, util.YES) + self.assertEqual(inferred, util.Uninferable) def test_binop_supertype(self): node = test_utils.extract_node(''' @@ -2491,7 +2491,7 @@ def __radd__(self, other): A() + B() #@ ''') inferred = next(node.infer()) - self.assertEqual(inferred, util.YES) + self.assertEqual(inferred, util.Uninferable) def test_binop_inferrence_errors(self): ast_nodes = test_utils.extract_node(''' @@ -2506,7 +2506,7 @@ def __add__(self, other): return Unknown A() + B() #@ ''') for node in ast_nodes: - self.assertEqual(next(node.infer()), util.YES) + self.assertEqual(next(node.infer()), util.Uninferable) def test_binop_ambiguity(self): ast_nodes = test_utils.extract_node(''' @@ -2529,7 +2529,7 @@ def __radd__(self, other): C() + A() #@ ''') for node in ast_nodes: - self.assertEqual(next(node.infer()), util.YES) + self.assertEqual(next(node.infer()), util.Uninferable) def test_bin_op_supertype_more_complicated_example(self): ast_node = test_utils.extract_node(''' @@ -2558,7 +2558,7 @@ def __iadd__(self, other): return NotImplemented def __add__(self, other): return NotImplemented A() + A() #@ ''') - self.assertEqual(next(ast_node.infer()), util.YES) + self.assertEqual(next(ast_node.infer()), util.Uninferable) def test_aug_op_same_type_aug_implemented(self): ast_node = test_utils.extract_node(''' @@ -2593,7 +2593,7 @@ class B(A): b = B() b+=A() #@ ''') - self.assertEqual(next(ast_node.infer()), util.YES) + self.assertEqual(next(ast_node.infer()), util.Uninferable) def test_aug_op_subtype_aug_op_is_implemented(self): ast_node = test_utils.extract_node(''' @@ -2628,7 +2628,7 @@ class B(object): pass f = A() f += B() #@ ''') - self.assertEqual(next(ast_node.infer()), util.YES) + self.assertEqual(next(ast_node.infer()), util.Uninferable) def test_aug_different_types_augop_implemented(self): ast_node = test_utils.extract_node(''' @@ -2676,7 +2676,7 @@ class B(object): pass a = A() a += B() #@ ''') - self.assertEqual(next(ast_node.infer()), util.YES) + self.assertEqual(next(ast_node.infer()), util.Uninferable) def test_augop_supertypes_not_implemented_returned_for_all(self): ast_node = test_utils.extract_node(''' @@ -2688,7 +2688,7 @@ def __add__(self, other): return NotImplemented a = A() a += B() #@ ''') - self.assertEqual(next(ast_node.infer()), util.YES) + self.assertEqual(next(ast_node.infer()), util.Uninferable) def test_augop_supertypes_augop_implemented(self): ast_node = test_utils.extract_node(''' @@ -2760,7 +2760,7 @@ def __index__(self): return None [1, 2, 1, 2]) for rest in ast_nodes[1:]: inferred = next(rest.infer()) - self.assertEqual(inferred, util.YES) + self.assertEqual(inferred, util.Uninferable) def test_subscript_supports__index__(self): ast_nodes = test_utils.extract_node(''' @@ -2929,7 +2929,7 @@ def __getitem__(self, index): A()[2:] #@ ''') for node in ast_nodes: - self.assertEqual(next(node.infer()), util.YES) + self.assertEqual(next(node.infer()), util.Uninferable) def test_type__new__with_metaclass(self): ast_node = test_utils.extract_node(''' @@ -3214,7 +3214,7 @@ def test_yes_when_unknown(self): for node in ast_nodes[4:]: inferred = next(node.infer()) - self.assertEqual(inferred, util.YES, node) + self.assertEqual(inferred, util.Uninferable, node) def test_attrname_not_string(self): ast_nodes = test_utils.extract_node(''' @@ -3302,7 +3302,7 @@ def test_lambda(self): getattr(lambda x: x, 'f') #@ ''') inferred = next(node.infer()) - self.assertEqual(inferred, util.YES) + self.assertEqual(inferred, util.Uninferable) class HasattrTest(unittest.TestCase): @@ -3318,7 +3318,7 @@ def test_inference_errors(self): ''') for node in ast_nodes: inferred = next(node.infer()) - self.assertEqual(inferred, util.YES) + self.assertEqual(inferred, util.Uninferable) def test_attribute_is_missing(self): ast_nodes = test_utils.extract_node(''' @@ -3361,7 +3361,7 @@ def test_lambda(self): hasattr(lambda x: x, 'f') #@ ''') inferred = next(node.infer()) - self.assertEqual(inferred, util.YES) + self.assertEqual(inferred, util.Uninferable) class BoolOpTest(unittest.TestCase): @@ -3398,7 +3398,7 @@ def test_yes_when_unknown(self): ''') for node in ast_nodes: inferred = next(node.infer()) - self.assertEqual(inferred, util.YES) + self.assertEqual(inferred, util.Uninferable) def test_other_nodes(self): ast_nodes = test_utils.extract_node(''' @@ -3478,7 +3478,7 @@ def test(): ''') for node in ast_nodes: inferred = next(node.infer()) - self.assertEqual(inferred, util.YES) + self.assertEqual(inferred, util.Uninferable) def test_not_callable(self): ast_nodes = test_utils.extract_node(''' @@ -3504,12 +3504,12 @@ def test_bool(self): ('bool(True)', True), ('bool(False)', False), ('bool(None)', False), - ('from unknown import Unknown; __(bool(Unknown))', util.YES), + ('from unknown import Unknown; __(bool(Unknown))', util.Uninferable), ] for code, expected in pairs: node = test_utils.extract_node(code) inferred = next(node.infer()) - if expected is util.YES: + if expected is util.Uninferable: self.assertEqual(expected, inferred) else: self.assertEqual(inferred.value, expected) @@ -3559,7 +3559,7 @@ class LenInvalid(object): '''.format(method=BOOL_SPECIAL_METHOD)) for node in ast_nodes: inferred = next(node.infer()) - self.assertEqual(inferred, util.YES) + self.assertEqual(inferred, util.Uninferable) class TestType(unittest.TestCase): @@ -3771,7 +3771,7 @@ def test(f=None): ''') for ast_node in ast_nodes: inferred = next(ast_node.infer()) - self.assertEqual(inferred, util.YES) + self.assertEqual(inferred, util.Uninferable) def test_fail_to_infer_args(self): ast_nodes = test_utils.extract_node(''' @@ -3801,7 +3801,7 @@ def test(*args): return args ''') for node in ast_nodes: inferred = next(node.infer()) - self.assertEqual(inferred, util.YES) + self.assertEqual(inferred, util.Uninferable) class SliceTest(unittest.TestCase): diff --git a/astroid/tests/unittest_lookup.py b/astroid/tests/unittest_lookup.py index 43dc9ee015..35c04a855f 100644 --- a/astroid/tests/unittest_lookup.py +++ b/astroid/tests/unittest_lookup.py @@ -172,7 +172,7 @@ def test_list_comp_target(self): """) var = astroid.body[1].value if sys.version_info < (3, 0): - self.assertEqual(var.inferred(), [util.YES]) + self.assertEqual(var.inferred(), [util.Uninferable]) else: self.assertRaises(exceptions.UnresolvableName, var.inferred) diff --git a/astroid/tests/unittest_manager.py b/astroid/tests/unittest_manager.py index 7e4eeae7d9..6fc0776286 100644 --- a/astroid/tests/unittest_manager.py +++ b/astroid/tests/unittest_manager.py @@ -33,7 +33,7 @@ def _get_file_from_object(obj): if platform.python_implementation() == 'Jython': return obj.__file__.split("$py.class")[0] + ".py" - if sys.version_info > (3, 0): + if sys.version_info > (3, 0) or platform.python_implementation() == 'PyPy': return obj.__file__ else: return obj.__file__[:-1] diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index f4bb897fb6..29a8567146 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -362,7 +362,7 @@ def test_bad_import_inference(self): method of this From node will be made by unpack_infer. inference.infer_from will try to import this module, which will fail and raise a InferenceException (by mixins.do_import_module). The infer_name - will catch this exception and yield and YES instead. + will catch this exception and yield and Uninferable instead. ''' code = ''' @@ -386,7 +386,7 @@ def test_bad_import_inference(self): # present in the other version. self.assertIsInstance(excs[0], nodes.ClassDef) self.assertEqual(excs[0].name, 'PickleError') - self.assertIs(excs[-1], util.YES) + self.assertIs(excs[-1], util.Uninferable) def test_absolute_import(self): astroid = resources.build_file('data/absimport.py') diff --git a/astroid/tests/unittest_protocols.py b/astroid/tests/unittest_protocols.py index 6c6cfc7fd9..5d978efbbc 100644 --- a/astroid/tests/unittest_protocols.py +++ b/astroid/tests/unittest_protocols.py @@ -67,7 +67,7 @@ def test_assigned_stmts_starred_for(self): for1_starred = next(assign_stmts.nodes_of_class(Starred)) assigned = next(for1_starred.assigned_stmts()) - self.assertEqual(assigned, util.YES) + self.assertEqual(assigned, util.Uninferable) def _get_starred_stmts(self, code): assign_stmt = extract_node("{} #@".format(code)) @@ -110,16 +110,16 @@ def test_assigned_stmts_starred_assnames(self): @require_version(minver='3.0') def test_assigned_stmts_starred_yes(self): # Not something iterable and known - self._helper_starred_expected("a, *b = range(3) #@", util.YES) + self._helper_starred_expected("a, *b = range(3) #@", util.Uninferable) # Not something inferrable - self._helper_starred_expected("a, *b = balou() #@", util.YES) + self._helper_starred_expected("a, *b = balou() #@", util.Uninferable) # In function, unknown. self._helper_starred_expected(""" def test(arg): - head, *tail = arg #@""", util.YES) + head, *tail = arg #@""", util.Uninferable) # These cases aren't worth supporting. self._helper_starred_expected( - "a, (*b, c), d = (1, (2, 3, 4), 5) #@", util.YES) + "a, (*b, c), d = (1, (2, 3, 4), 5) #@", util.Uninferable) @require_version(minver='3.0') def test_assign_stmts_starred_fails(self): diff --git a/astroid/tests/unittest_regrtest.py b/astroid/tests/unittest_regrtest.py index 9651f4738b..87664dfcbb 100644 --- a/astroid/tests/unittest_regrtest.py +++ b/astroid/tests/unittest_regrtest.py @@ -253,7 +253,7 @@ def test(x=False): def test_ancestors_yes_in_bases(self): # Test for issue https://bitbucket.org/logilab/astroid/issue/84 - # This used to crash astroid with a TypeError, because an YES + # This used to crash astroid with a TypeError, because an Uninferable # node was present in the bases node = extract_node(""" def with_metaclass(meta, *bases): diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index 9d502d3759..7012fb8f5c 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -1138,7 +1138,7 @@ class CompositeBuilder(object): instance = astroid['tgts'] # used to raise "'_Yes' object is not iterable", see # https://bitbucket.org/logilab/astroid/issue/17 - self.assertEqual(list(instance.infer()), [util.YES]) + self.assertEqual(list(instance.infer()), [util.Uninferable]) def test_slots(self): astroid = builder.parse(""" @@ -1379,7 +1379,7 @@ def test_implicit_metaclass_lookup(self): class A(object): pass ''') - instance = cls.instanciate_class() + instance = cls.instantiate_class() func = cls.getattr('mro') self.assertEqual(len(func), 1) self.assertRaises(NotFoundError, instance.getattr, 'mro') @@ -1402,7 +1402,7 @@ class Metaclass(type): class B(object): pass ''') cls = module['B'] - self.assertEqual(util.YES, next(cls.igetattr('foo'))) + self.assertEqual(util.Uninferable, next(cls.igetattr('foo'))) def test_metaclass_lookup(self): module = builder.parse(''' diff --git a/astroid/tree/base.py b/astroid/tree/base.py index f5b598a2e3..d9c1d43ed4 100644 --- a/astroid/tree/base.py +++ b/astroid/tree/base.py @@ -329,10 +329,17 @@ def infered(self): PendingDeprecationWarning, stacklevel=2) return self.inferred() - def instanciate_class(self): + def instantiate_class(self): """instanciate a node if it is a ClassDef node, else return self""" return self + def instanciate_class(self): + warnings.warn('%s.instanciate_class() is deprecated and slated for ' + ' removal in astroid 2.0, use %s.instantiate_class() ' + ' instead.' % (type(self).__name__, type(self).__name__), + PendingDeprecationWarning, stacklevel=2) + return self.instantiate_class() + def has_base(self, node): return False @@ -475,10 +482,10 @@ def bool_value(self): method. * True. Most of constructs are True by default: classes, functions, modules etc - * YES: the inference engine is uncertain of the + * Uninferable: the inference engine is uncertain of the node's value. """ - return util.YES + return util.Uninferable @six.add_metaclass(abc.ABCMeta) diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index e8ba256b46..1a6877354e 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -622,7 +622,7 @@ def getitem(self, lookup_key, context=None): except IndexError: continue for inferredkey in key.infer(context): - if inferredkey is util.YES: + if inferredkey is util.Uninferable: continue if isinstance(inferredkey, Const) \ and inferredkey.value == lookup_key: diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 4e67b55421..f3033444fb 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -553,7 +553,7 @@ def postinit(self, key=None, value=None, generators=None): self.generators = generators def bool_value(self): - return util.YES + return util.Uninferable @util.register_implementation(treeabc.SetComp) @@ -575,7 +575,7 @@ def postinit(self, elt=None, generators=None): self.generators = generators def bool_value(self): - return util.YES + return util.Uninferable @util.register_implementation(treeabc.ListComp) @@ -590,7 +590,7 @@ def postinit(self, elt=None, generators=None): self.generators = generators def bool_value(self): - return util.YES + return util.Uninferable if six.PY3: @@ -647,11 +647,11 @@ def __init__(self, funcnode, args, keywords): self.positional_arguments = [ arg for arg in self._unpacked_args - if arg is not util.YES + if arg is not util.Uninferable ] self.keyword_arguments = { key: value for key, value in self._unpacked_kwargs.items() - if value is not util.YES + if value is not util.Uninferable } def has_invalid_arguments(self): @@ -683,29 +683,29 @@ def _unpack_keywords(self, keywords): try: inferred = next(value.infer(context=context)) except exceptions.InferenceError: - values[name] = util.YES + values[name] = util.Uninferable continue if not isinstance(inferred, treeabc.Dict): # Not something we can work with. - values[name] = util.YES + values[name] = util.Uninferable continue for dict_key, dict_value in inferred.items: try: dict_key = next(dict_key.infer(context=context)) except exceptions.InferenceError: - values[name] = util.YES + values[name] = util.Uninferable continue if not isinstance(dict_key, treeabc.Const): - values[name] = util.YES + values[name] = util.Uninferable continue if not isinstance(dict_key.value, six.string_types): - values[name] = util.YES + values[name] = util.Uninferable continue if dict_key.value in values: # The name is already in the dictionary - values[dict_key.value] = util.YES + values[dict_key.value] = util.Uninferable self.duplicated_keywords.add(dict_key.value) continue values[dict_key.value] = dict_value @@ -722,14 +722,14 @@ def _unpack_args(args): try: inferred = next(arg.value.infer(context=context)) except exceptions.InferenceError: - values.append(util.YES) + values.append(util.Uninferable) continue - if inferred is util.YES: - values.append(util.YES) + if inferred is util.Uninferable: + values.append(util.Uninferable) continue if not hasattr(inferred, 'elts'): - values.append(util.YES) + values.append(util.Uninferable) continue values.extend(inferred.elts) else: @@ -1158,7 +1158,7 @@ def infer_call_result(self, caller, context=None): c.hide = True c.parent = self class_bases = [next(b.infer(context)) for b in caller.args[1:]] - c.bases = [base for base in class_bases if base != util.YES] + c.bases = [base for base in class_bases if base != util.Uninferable] c._metaclass = metaclass yield c return @@ -1171,7 +1171,7 @@ def infer_call_result(self, caller, context=None): for inferred in returnnode.value.infer(context): yield inferred except exceptions.InferenceError: - yield util.YES + yield util.Uninferable def bool_value(self): return True @@ -1213,7 +1213,7 @@ def _is_metaclass(klass, seen=None): if isinstance(baseobj, objects.Instance): # not abstract return False - if baseobj is util.YES: + if baseobj is util.Uninferable: continue if baseobj is klass: continue @@ -1384,7 +1384,7 @@ def _infer_type_call(self, caller, context): isinstance(name_node.value, six.string_types)): name = name_node.value else: - return util.YES + return util.Uninferable result = ClassDef(name, None) @@ -1394,9 +1394,9 @@ def _infer_type_call(self, caller, context): result.bases = class_bases.itered() else: # There is currently no AST node that can represent an 'unknown' - # node (YES is not an AST node), therefore we simply return YES here + # node (Uninferable is not an AST node), therefore we simply return Uninferable here # although we know at least the name of the class. - return util.YES + return util.Uninferable # Get the members of the class try: @@ -1561,7 +1561,7 @@ def instance_attr(self, name, context=None): raise exceptions.NotFoundError(name) return values - def instanciate_class(self): + def instantiate_class(self): """return Instance of ClassDef node, else return self""" return objects.Instance(self) @@ -1570,7 +1570,7 @@ def getattr(self, name, context=None, class_context=True): This method doesn't look in the instance_attrs dictionary since it's done by an Instance proxy at inference time. It - may return a YES object if the attribute has not been actually + may return a Uninferable object if the attribute has not been actually found but a __getattr__ or __getattribute__ method is defined. If *class_context* is given, then it's considered that the attribute is accessed from a class context, @@ -1658,7 +1658,7 @@ def igetattr(self, name, context=None): try: for inferred in infer_stmts(self.getattr(name, context), context, frame=self): - # yield YES object instead of descriptors when necessary + # yield Uninferable object instead of descriptors when necessary if (not isinstance(inferred, node_classes.Const) and isinstance(inferred, objects.Instance)): try: @@ -1666,13 +1666,13 @@ def igetattr(self, name, context=None): except exceptions.NotFoundError: yield inferred else: - yield util.YES + yield util.Uninferable else: yield function_to_method(inferred, self) except exceptions.NotFoundError: if not name.startswith('__') and self.has_dynamic_getattr(context): - # class handle some dynamic attributes, return a YES object - yield util.YES + # class handle some dynamic attributes, return a Uninferable object + yield util.Uninferable else: util.reraise(exceptions.InferenceError(name)) @@ -1753,7 +1753,7 @@ def declared_metaclass(self): # Expects this from Py3k TreeRebuilder try: return next(node for node in self._metaclass.infer() - if node is not util.YES) + if node is not util.Uninferable) except (exceptions.InferenceError, StopIteration): return None if six.PY3: @@ -1776,7 +1776,7 @@ def declared_metaclass(self): inferred = next(assignment.infer()) except exceptions.InferenceError: return - if inferred is util.YES: # don't expose this + if inferred is util.Uninferable: # don't expose this return None return inferred @@ -1835,7 +1835,7 @@ def _islots(self): values = [item[0] for item in slots.items] else: values = slots.itered() - if values is util.YES: + if values is util.Uninferable: continue if not values: # Stop the iteration, because the class @@ -1845,7 +1845,7 @@ def _islots(self): for elt in values: try: for inferred in elt.infer(): - if inferred is util.YES: + if inferred is util.Uninferable: continue if (not isinstance(inferred, node_classes.Const) or not isinstance(inferred.value, diff --git a/astroid/util.py b/astroid/util.py index 94818bc48d..5ece59d2d6 100644 --- a/astroid/util.py +++ b/astroid/util.py @@ -38,10 +38,10 @@ def reraise(exception): @object.__new__ -class YES(object): +class Uninferable(object): """Special inference object, which is returned when inference fails.""" def __repr__(self): - return 'YES' + return 'Uninferable' def __getattribute__(self, name): if name == 'next': @@ -73,6 +73,9 @@ def proxy_alias(alias_name, node_type): return proxy(lambda: node_type) +# Backwards-compatibility aliases +YES = Uninferable + def register_implementation(base): """Register an implementation for the given *base* diff --git a/tox.ini b/tox.ini index 15bda87d0b..30338e3901 100644 --- a/tox.ini +++ b/tox.ini @@ -3,14 +3,19 @@ # envlist = py27, py33, py34, py35, pypy, jython, pylint # drone.io -envlist = py27, py33, pylint +# envlist = py27, py33, pylint # For testing off drone.io---please don't delete. -# envlist = py27, py34, pypy, pylint +envlist = py27, py34, pypy, pylint [testenv] deps = + py27,py33,pypy,jython: enum34 lazy-object-proxy + nose + py27,py33,py34,py35: numpy + pytest + python-dateutil py27,py33,pypy,jython: singledispatch six wrapt From c7a06ba3ed136eb61b30ef0c4d6703485a86bf45 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Fri, 6 Nov 2015 18:21:05 -0500 Subject: [PATCH 072/312] Rename Uninferable and instantiate_class --- astroid/__init__.py | 2 +- astroid/arguments.py | 24 ++--- astroid/bases.py | 16 ++-- astroid/brain/brain_builtin_inference.py | 46 +++++----- astroid/brain/brain_stdlib.py | 4 +- astroid/decorators.py | 2 +- astroid/helpers.py | 8 +- astroid/inference.py | 76 ++++++++-------- astroid/manager.py | 2 +- astroid/node_classes.py | 14 +-- astroid/protocols.py | 32 +++---- astroid/scoped_nodes.py | 38 ++++---- astroid/tests/unittest_brain.py | 4 +- astroid/tests/unittest_builder.py | 2 +- astroid/tests/unittest_helpers.py | 8 +- astroid/tests/unittest_inference.py | 108 +++++++++++------------ astroid/tests/unittest_lookup.py | 2 +- astroid/tests/unittest_nodes.py | 4 +- astroid/tests/unittest_protocols.py | 10 +-- astroid/tests/unittest_regrtest.py | 2 +- astroid/tests/unittest_scoped_nodes.py | 6 +- astroid/util.py | 4 +- 22 files changed, 207 insertions(+), 207 deletions(-) diff --git a/astroid/__init__.py b/astroid/__init__.py index ab5108e40b..98168e2a72 100644 --- a/astroid/__init__.py +++ b/astroid/__init__.py @@ -64,7 +64,7 @@ from astroid.node_classes import are_exclusive, unpack_infer from astroid.scoped_nodes import builtin_lookup from astroid.builder import parse -from astroid.util import YES +from astroid.util import Uninferable # make a manager instance (borg) accessible from astroid package from astroid.manager import AstroidManager diff --git a/astroid/arguments.py b/astroid/arguments.py index 9a59075e61..bcc98eb124 100644 --- a/astroid/arguments.py +++ b/astroid/arguments.py @@ -44,11 +44,11 @@ def __init__(self, callcontext): self.positional_arguments = [ arg for arg in self._unpacked_args - if arg is not util.YES + if arg is not util.Uninferable ] self.keyword_arguments = { key: value for key, value in self._unpacked_kwargs.items() - if value is not util.YES + if value is not util.Uninferable } @classmethod @@ -87,29 +87,29 @@ def _unpack_keywords(self, keywords): try: inferred = next(value.infer(context=context)) except exceptions.InferenceError: - values[name] = util.YES + values[name] = util.Uninferable continue if not isinstance(inferred, nodes.Dict): # Not something we can work with. - values[name] = util.YES + values[name] = util.Uninferable continue for dict_key, dict_value in inferred.items: try: dict_key = next(dict_key.infer(context=context)) except exceptions.InferenceError: - values[name] = util.YES + values[name] = util.Uninferable continue if not isinstance(dict_key, nodes.Const): - values[name] = util.YES + values[name] = util.Uninferable continue if not isinstance(dict_key.value, six.string_types): - values[name] = util.YES + values[name] = util.Uninferable continue if dict_key.value in values: # The name is already in the dictionary - values[dict_key.value] = util.YES + values[dict_key.value] = util.Uninferable self.duplicated_keywords.add(dict_key.value) continue values[dict_key.value] = dict_value @@ -126,14 +126,14 @@ def _unpack_args(args): try: inferred = next(arg.value.infer(context=context)) except exceptions.InferenceError: - values.append(util.YES) + values.append(util.Uninferable) continue - if inferred is util.YES: - values.append(util.YES) + if inferred is util.Uninferable: + values.append(util.Uninferable) continue if not hasattr(inferred, 'elts'): - values.append(util.YES) + values.append(util.Uninferable) continue values.extend(inferred.elts) else: diff --git a/astroid/bases.py b/astroid/bases.py index a0d94e4ad5..78ef1ab67d 100644 --- a/astroid/bases.py +++ b/astroid/bases.py @@ -58,7 +58,7 @@ def _is_property(meth): if PROPERTIES.intersection(meth.decoratornames()): return True stripped = {name.split(".")[-1] for name in meth.decoratornames() - if name is not util.YES} + if name is not util.Uninferable} return any(name in stripped for name in POSSIBLE_PROPERTIES) @@ -94,7 +94,7 @@ def _infer_stmts(stmts, context, frame=None): context = contextmod.InferenceContext() for stmt in stmts: - if stmt is util.YES: + if stmt is util.Uninferable: yield stmt inferred = True continue @@ -106,7 +106,7 @@ def _infer_stmts(stmts, context, frame=None): except exceptions.NameInferenceError: continue except exceptions.InferenceError: - yield util.YES + yield util.Uninferable inferred = True if not inferred: raise exceptions.InferenceError( @@ -120,14 +120,14 @@ def _infer_method_result_truth(instance, method_name, context): meth = next(instance.igetattr(method_name, context=context), None) if meth and hasattr(meth, 'infer_call_result'): if not meth.callable(): - return util.YES + return util.Uninferable for value in meth.infer_call_result(instance, context=context): - if value is util.YES: + if value is util.Uninferable: return value inferred = next(value.infer(context=context)) return inferred.bool_value() - return util.YES + return util.Uninferable class Instance(Proxy): @@ -210,7 +210,7 @@ def infer_call_result(self, caller, context=None): """infer what a class instance is returning when called""" inferred = False for node in self._proxied.igetattr('__call__', context): - if node is util.YES or not node.callable(): + if node is util.Uninferable or not node.callable(): continue for res in node.infer_call_result(caller, context): inferred = True @@ -305,7 +305,7 @@ def infer_call_result(self, caller, context): if (self._proxied.name == '__new__' and self._proxied.parent.frame().qname() == '%s.object' % BUILTINS): infer = caller.args[0].infer() if caller.args else [] - return ((x is util.YES and x or Instance(x)) for x in infer) + return ((x is util.Uninferable and x or Instance(x)) for x in infer) return self._proxied.infer_call_result(caller, context) def bool_value(self): diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index f9e42b5b83..196f65880b 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -122,10 +122,10 @@ def _generic_inference(node, context, node_type, transform): inferred = next(arg.infer(context=context)) except (InferenceError, StopIteration): raise UseInferenceDefault() - if inferred is util.YES: + if inferred is util.Uninferable: raise UseInferenceDefault() transformed = transform(inferred) - if not transformed or transformed is util.YES: + if not transformed or transformed is util.Uninferable: raise UseInferenceDefault() return transformed @@ -295,7 +295,7 @@ def infer_super(node, context=None): if scope.type == 'classmethod': mro_type = cls else: - mro_type = cls.instanciate_class() + mro_type = cls.instantiate_class() else: # TODO(cpopa): support flow control (multiple inference values). try: @@ -307,7 +307,7 @@ def infer_super(node, context=None): except InferenceError: raise UseInferenceDefault - if mro_pointer is util.YES or mro_type is util.YES: + if mro_pointer is util.Uninferable or mro_type is util.Uninferable: # No way we could understand this. raise UseInferenceDefault @@ -331,11 +331,11 @@ def _infer_getattr_args(node, context): except InferenceError: raise UseInferenceDefault - if obj is util.YES or attr is util.YES: + if obj is util.Uninferable or attr is util.Uninferable: # If one of the arguments is something we can't infer, # then also make the result of the getattr call something # which is unknown. - return util.YES, util.YES + return util.Uninferable, util.Uninferable is_string = (isinstance(attr, nodes.Const) and isinstance(attr.value, six.string_types)) @@ -348,13 +348,13 @@ def _infer_getattr_args(node, context): def infer_getattr(node, context=None): """Understand getattr calls - If one of the arguments is an YES object, then the - result will be an YES object. Otherwise, the normal attribute + If one of the arguments is an Uninferable object, then the + result will be an Uninferable object. Otherwise, the normal attribute lookup will be done. """ obj, attr = _infer_getattr_args(node, context) - if obj is util.YES or attr is util.YES or not hasattr(obj, 'igetattr'): - return util.YES + if obj is util.Uninferable or attr is util.Uninferable or not hasattr(obj, 'igetattr'): + return util.Uninferable try: return next(obj.igetattr(attr, context=context)) @@ -375,17 +375,17 @@ def infer_hasattr(node, context=None): This always guarantees three possible outcomes for calling hasattr: Const(False) when we are sure that the object doesn't have the intended attribute, Const(True) when - we know that the object has the attribute and YES + we know that the object has the attribute and Uninferable when we are unsure of the outcome of the function call. """ try: obj, attr = _infer_getattr_args(node, context) - if obj is util.YES or attr is util.YES or not hasattr(obj, 'getattr'): - return util.YES + if obj is util.Uninferable or attr is util.Uninferable or not hasattr(obj, 'getattr'): + return util.Uninferable obj.getattr(attr, context=context) except UseInferenceDefault: # Can't infer something from this function call. - return util.YES + return util.Uninferable except AttributeInferenceError: # Doesn't have it. return nodes.Const(False) @@ -408,9 +408,9 @@ def infer_callable(node, context=None): try: inferred = next(argument.infer(context=context)) except InferenceError: - return util.YES - if inferred is util.YES: - return util.YES + return util.Uninferable + if inferred is util.Uninferable: + return util.Uninferable return nodes.Const(inferred.callable()) @@ -427,13 +427,13 @@ def infer_bool(node, context=None): try: inferred = next(argument.infer(context=context)) except InferenceError: - return util.YES - if inferred is util.YES: - return util.YES + return util.Uninferable + if inferred is util.Uninferable: + return util.Uninferable bool_value = inferred.bool_value() - if bool_value is util.YES: - return util.YES + if bool_value is util.Uninferable: + return util.Uninferable return nodes.Const(bool_value) @@ -453,7 +453,7 @@ def infer_slice(node, context=None): args = list(map(helpers.safe_infer, args)) for arg in args: - if not arg or arg is util.YES: + if not arg or arg is util.Uninferable: raise UseInferenceDefault if not isinstance(arg, nodes.Const): raise UseInferenceDefault diff --git a/astroid/brain/brain_stdlib.py b/astroid/brain/brain_stdlib.py index bc61967331..96e83ab887 100644 --- a/astroid/brain/brain_stdlib.py +++ b/astroid/brain/brain_stdlib.py @@ -21,7 +21,7 @@ def infer_first(node, context): try: value = next(node.infer(context=context)) - if value is util.YES: + if value is util.Uninferable: raise UseInferenceDefault() else: return value @@ -333,7 +333,7 @@ def name(self): fake.parent = target.parent for method in node.mymethods(): fake.locals[method.name] = [method] - new_targets.append(fake.instanciate_class()) + new_targets.append(fake.instantiate_class()) node.locals[local] = new_targets break return node diff --git a/astroid/decorators.py b/astroid/decorators.py index 481f0c2f76..d3ef71681a 100644 --- a/astroid/decorators.py +++ b/astroid/decorators.py @@ -125,7 +125,7 @@ def yes_if_nothing_inferred(func, instance, args, kwargs): inferred = True yield node if not inferred: - yield util.YES + yield util.Uninferable @wrapt.decorator diff --git a/astroid/helpers.py b/astroid/helpers.py index 2467a46772..8698a25ed9 100644 --- a/astroid/helpers.py +++ b/astroid/helpers.py @@ -78,9 +78,9 @@ def object_type(node, context=None): try: types = set(_object_type(node, context)) except exceptions.InferenceError: - return util.YES + return util.Uninferable if len(types) > 1 or not types: - return util.YES + return util.Uninferable return list(types)[0] @@ -124,7 +124,7 @@ def has_known_bases(klass, context=None): def _type_check(type1, type2): if not all(map(has_known_bases, (type1, type2))): - return util.YES + return util.Uninferable if not all([type1.newstyle, type2.newstyle]): return False @@ -132,7 +132,7 @@ def _type_check(type1, type2): return type1 in type2.mro()[:-1] except exceptions.MroError: # The MRO is invalid. - return util.YES + return util.Uninferable def is_subtype(type1, type2): diff --git a/astroid/inference.py b/astroid/inference.py index a38f039902..0c60d436b6 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -108,7 +108,7 @@ def infer_call(self, context=None): keywords=self.keywords) callcontext.boundnode = None for callee in self.func.infer(context): - if callee is util.YES: + if callee is util.Uninferable: yield callee continue try: @@ -168,7 +168,7 @@ def infer_import_from(self, context=None, asname=True): def infer_attribute(self, context=None): """infer an Attribute node by using getattr on the associated object""" for owner in self.expr.infer(context): - if owner is util.YES: + if owner is util.Uninferable: yield owner continue try: @@ -240,13 +240,13 @@ def infer_subscript(self, context=None): """ value = next(self.value.infer(context)) - if value is util.YES: - yield util.YES + if value is util.Uninferable: + yield util.Uninferable return index = next(self.slice.infer(context)) - if index is util.YES: - yield util.YES + if index is util.Uninferable: + yield util.Uninferable return if value.__class__ == bases.Instance: @@ -280,8 +280,8 @@ def infer_subscript(self, context=None): # Prevent inferring if the inferred subscript # is the same as the original subscripted object. - if self is assigned or assigned is util.YES: - yield util.YES + if self is assigned or assigned is util.Uninferable: + yield util.Uninferable return for inferred in assigned.infer(context): yield inferred @@ -312,31 +312,31 @@ def _infer_boolop(self, context=None): try: values = [value.infer(context=context) for value in values] except exceptions.InferenceError: - yield util.YES + yield util.Uninferable return for pair in itertools.product(*values): - if any(item is util.YES for item in pair): - # Can't infer the final result, just yield YES. - yield util.YES + if any(item is util.Uninferable for item in pair): + # Can't infer the final result, just yield Uninferable. + yield util.Uninferable continue bool_values = [item.bool_value() for item in pair] - if any(item is util.YES for item in bool_values): - # Can't infer the final result, just yield YES. - yield util.YES + if any(item is util.Uninferable for item in bool_values): + # Can't infer the final result, just yield Uninferable. + yield util.Uninferable continue # Since the boolean operations are short circuited operations, # this code yields the first value for which the predicate is True # and if no value respected the predicate, then the last value will - # be returned (or YES if there was no last value). + # be returned (or Uninferable if there was no last value). # This is conforming to the semantics of `and` and `or`: # 1 and 0 -> 1 # 0 and 1 -> 0 # 1 or 0 -> 1 # 0 or 1 -> 1 - value = util.YES + value = util.Uninferable for value, bool_value in zip(pair, bool_values): if predicate(bool_value): yield value @@ -359,7 +359,7 @@ def _filter_operation_errors(self, infer_callable, context, error): # For the sake of .infer(), we don't care about operation # errors, which is the job of pylint. So return something # which shows that we can't infer the result. - yield util.YES + yield util.Uninferable else: yield result @@ -377,12 +377,12 @@ def _infer_unaryop(self, context=None): if meth is None: # `not node`. Determine node's boolean # value and negate its result, unless it is - # YES, which will be returned as is. + # Uninferable, which will be returned as is. bool_value = operand.bool_value() - if bool_value is not util.YES: + if bool_value is not util.Uninferable: yield nodes.Const(not bool_value) else: - yield util.YES + yield util.Uninferable else: if not isinstance(operand, bases.Instance): # The operation was used on something which @@ -393,7 +393,7 @@ def _infer_unaryop(self, context=None): try: meth = operand.getattr(meth, context=context)[0] inferred = next(meth.infer(context=context)) - if inferred is util.YES or not inferred.callable(): + if inferred is util.Uninferable or not inferred.callable(): continue context = contextmod.copy_context(context) @@ -409,7 +409,7 @@ def _infer_unaryop(self, context=None): # The unary operation special method was not found. yield util.BadUnaryOperationMessage(operand, self.op, exc) except exceptions.InferenceError: - yield util.YES + yield util.Uninferable @decorators.raise_if_nothing_inferred @@ -570,23 +570,23 @@ def _infer_binary_operation(left, right, op, context, flow_factory): except exceptions.AttributeInferenceError: continue except exceptions.InferenceError: - yield util.YES + yield util.Uninferable return else: - if any(result is util.YES for result in results): - yield util.YES + if any(result is util.Uninferable for result in results): + yield util.Uninferable return # TODO(cpopa): since the inferrence engine might return # more values than are actually possible, we decide - # to return util.YES if we have union types. + # to return util.Uninferable if we have union types. if all(map(_is_not_implemented, results)): continue not_implemented = sum(1 for result in results if _is_not_implemented(result)) if not_implemented and not_implemented != len(results): # Can't decide yet what this is, not yet though. - yield util.YES + yield util.Uninferable return for result in results: @@ -612,15 +612,15 @@ def _infer_binop(self, context): rhs_context = context.clone() for lhs in left.infer(context=lhs_context): - if lhs is util.YES: + if lhs is util.Uninferable: # Don't know how to process this. - yield util.YES + yield util.Uninferable return for rhs in right.infer(context=rhs_context): - if rhs is util.YES: + if rhs is util.Uninferable: # Don't know how to process this. - yield util.YES + yield util.Uninferable return results = _infer_binary_operation(lhs, rhs, op, @@ -646,9 +646,9 @@ def _infer_augassign(self, context=None): op = self.op for lhs in self.target.infer_lhs(context=context): - if lhs is util.YES: + if lhs is util.Uninferable: # Don't know how to process this. - yield util.YES + yield util.Uninferable return # TODO(cpopa): if we have A() * A(), trying to infer @@ -658,9 +658,9 @@ def _infer_augassign(self, context=None): rhs_context = context.clone() rhs_context.path = set() for rhs in self.value.infer(context=rhs_context): - if rhs is util.YES: + if rhs is util.Uninferable: # Don't know how to process this. - yield util.YES + yield util.Uninferable return results = _infer_binary_operation(lhs, rhs, op, @@ -708,14 +708,14 @@ def infer_assign(self, context=None): @decorators.path_wrapper def infer_empty_node(self, context=None): if not self.has_underlying_object(): - yield util.YES + yield util.Uninferable else: try: for inferred in MANAGER.infer_ast_from_something(self.object, context=context): yield inferred except exceptions.AstroidError: - yield util.YES + yield util.Uninferable nodes.EmptyNode._infer = infer_empty_node diff --git a/astroid/manager.py b/astroid/manager.py index 05d33fe412..6c5e4e5fb1 100644 --- a/astroid/manager.py +++ b/astroid/manager.py @@ -252,7 +252,7 @@ def infer_ast_from_something(self, obj, context=None): yield inferred else: for inferred in modastroid.igetattr(name, context): - yield inferred.instanciate_class() + yield inferred.instantiate_class() def register_failed_import_hook(self, hook): """Registers a hook to resolve imports that cannot be found otherwise. diff --git a/astroid/node_classes.py b/astroid/node_classes.py index 2dc3aa6c93..6e46238d85 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -57,9 +57,9 @@ def unpack_infer(stmt, context=None): if inferred is stmt: yield inferred return - # else, infer recursivly, except YES object that should be returned as is + # else, infer recursivly, except Uninferable object that should be returned as is for inferred in stmt.infer(context): - if inferred is util.YES: + if inferred is util.Uninferable: yield inferred else: for inf_inf in unpack_infer(inferred, context): @@ -425,8 +425,8 @@ def infered(self): util.rename_warning((type(self).__name__, type(self).__name__)) return self.inferred() - def instanciate_class(self): - """instanciate a node if it is a ClassDef node, else return self""" + def instantiate_class(self): + """instantiate a node if it is a ClassDef node, else return self""" return self def has_base(self, node): @@ -568,10 +568,10 @@ def bool_value(self): method. * True. Most of constructs are True by default: classes, functions, modules etc - * YES: the inference engine is uncertain of the + * Uninferable: the inference engine is uncertain of the node's value. """ - return util.YES + return util.Uninferable class Statement(NodeNG): @@ -1338,7 +1338,7 @@ def getitem(self, lookup_key, context=None): except IndexError: continue for inferredkey in key.infer(context): - if inferredkey is util.YES: + if inferredkey is util.Uninferable: continue if isinstance(inferredkey, Const) \ and inferredkey.value == lookup_key: diff --git a/astroid/protocols.py b/astroid/protocols.py index 49528c2992..810424ca7b 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -129,12 +129,12 @@ def const_infer_binary_op(self, operator, other, context, _): # ArithmeticError is not enough: float >> float is a TypeError yield not_implemented except Exception: # pylint: disable=broad-except - yield util.YES + yield util.Uninferable except TypeError: yield not_implemented elif isinstance(self.value, six.string_types) and operator == '%': # TODO(cpopa): implement string interpolation later on. - yield util.YES + yield util.Uninferable else: yield not_implemented @@ -147,7 +147,7 @@ def _multiply_seq_by_int(self, other, context): for elt in self.elts: infered = helpers.safe_infer(elt, context) if infered is None: - infered = util.YES + infered = util.Uninferable elts.append(infered) node.elts = elts * other.value return node @@ -159,9 +159,9 @@ def tl_infer_binary_op(self, operator, other, context, method): if isinstance(other, self.__class__) and operator == '+': node = self.__class__() elts = [n for elt in self.elts for n in elt.infer(context) - if not n is util.YES] + if not n is util.Uninferable] elts += [n for elt in other.elts for n in elt.infer(context) - if not n is util.YES] + if not n is util.Uninferable] node.elts = elts yield node elif isinstance(other, nodes.Const) and operator == '*': @@ -173,7 +173,7 @@ def tl_infer_binary_op(self, operator, other, context, method): # Verify if the instance supports __index__. as_index = helpers.class_instance_as_index(other) if not as_index: - yield util.YES + yield util.Uninferable else: yield _multiply_seq_by_int(self, as_index, context) else: @@ -208,7 +208,7 @@ def _resolve_looppart(parts, asspath, context): asspath = asspath[:] index = asspath.pop(0) for part in parts: - if part is util.YES: + if part is util.Uninferable: continue # XXX handle __iter__ and log potentially detected errors if not hasattr(part, 'itered'): @@ -228,7 +228,7 @@ def _resolve_looppart(parts, asspath, context): # we achieved to resolved the assignment path, # don't infer the last part yield assigned - elif assigned is util.YES: + elif assigned is util.Uninferable: break else: # we are not yet on the last part of the path @@ -280,7 +280,7 @@ def _arguments_infer_argname(self, name, context): # arguments information may be missing, in which case we can't do anything # more if not (self.args or self.vararg or self.kwarg): - yield util.YES + yield util.Uninferable return # first argument of instance/class method if self.args and getattr(self.args[0], 'name', None) == name: @@ -305,15 +305,15 @@ def _arguments_infer_argname(self, name, context): if name == self.kwarg: yield nodes.Dict(parent=self) return - # if there is a default value, yield it. And then yield YES to reflect + # if there is a default value, yield it. And then yield Uninferable to reflect # we can't guess given argument value try: context = contextmod.copy_context(context) for inferred in self.default_value(name).infer(context): yield inferred - yield util.YES + yield util.Uninferable except exceptions.NoDefault: - yield util.YES + yield util.Uninferable def arguments_assigned_stmts(self, node, context, asspath=None): @@ -361,7 +361,7 @@ def _resolve_asspart(parts, asspath, context): # we achieved to resolved the assignment path, don't infer the # last part yield assigned - elif assigned is util.YES: + elif assigned is util.Uninferable: return else: # we are not yet on the last part of the path search on each @@ -521,11 +521,11 @@ def starred_assigned_stmts(self, node=None, context=None, asspath=None): try: rhs = next(value.infer(context)) except exceptions.InferenceError: - yield util.YES + yield util.Uninferable return - if rhs is util.YES or not hasattr(rhs, 'elts'): + if rhs is util.Uninferable or not hasattr(rhs, 'elts'): # Not interested in inferred values without elts. - yield util.YES + yield util.Uninferable return elts = collections.deque(rhs.elts[:]) diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index fc6497ad28..f9621e3ae1 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -628,7 +628,7 @@ def postinit(self, key=None, value=None, generators=None): self.generators = generators def bool_value(self): - return util.YES + return util.Uninferable class SetComp(ComprehensionScope): @@ -649,7 +649,7 @@ def postinit(self, elt=None, generators=None): self.generators = generators def bool_value(self): - return util.YES + return util.Uninferable class _ListComp(node_classes.NodeNG): @@ -663,7 +663,7 @@ def postinit(self, elt=None, generators=None): self.generators = generators def bool_value(self): - return util.YES + return util.Uninferable if six.PY3: @@ -1022,7 +1022,7 @@ def infer_call_result(self, caller, context=None): c.hide = True c.parent = self class_bases = [next(b.infer(context)) for b in caller.args[1:]] - c.bases = [base for base in class_bases if base != util.YES] + c.bases = [base for base in class_bases if base != util.Uninferable] c._metaclass = metaclass yield c return @@ -1035,7 +1035,7 @@ def infer_call_result(self, caller, context=None): for inferred in returnnode.value.infer(context): yield inferred except exceptions.InferenceError: - yield util.YES + yield util.Uninferable def bool_value(self): return True @@ -1076,7 +1076,7 @@ def _is_metaclass(klass, seen=None): if isinstance(baseobj, bases.Instance): # not abstract return False - if baseobj is util.YES: + if baseobj is util.Uninferable: continue if baseobj is klass: continue @@ -1251,7 +1251,7 @@ def _infer_type_call(self, caller, context): isinstance(name_node.value, six.string_types)): name = name_node.value else: - return util.YES + return util.Uninferable result = ClassDef(name, None, parent=caller.parent) @@ -1261,9 +1261,9 @@ def _infer_type_call(self, caller, context): bases = class_bases.itered() else: # There is currently no AST node that can represent an 'unknown' - # node (YES is not an AST node), therefore we simply return YES here + # node (Uninferable is not an AST node), therefore we simply return Uninferable here # although we know at least the name of the class. - return util.YES + return util.Uninferable # Get the members of the class try: @@ -1440,7 +1440,7 @@ def instance_attr(self, name, context=None): raise exceptions.AttributeInferenceError(target=self, attribute=name, context=context) - def instanciate_class(self): + def instantiate_class(self): """return Instance of ClassDef node, else return self""" return bases.Instance(self) @@ -1449,7 +1449,7 @@ def getattr(self, name, context=None, class_context=True): This method doesn't look in the instance_attrs dictionary since it's done by an Instance proxy at inference time. It - may return a YES object if the attribute has not been actually + may return a Uninferable object if the attribute has not been actually found but a __getattr__ or __getattribute__ method is defined. If *class_context* is given, then it's considered that the attribute is accessed from a class context, @@ -1539,7 +1539,7 @@ def igetattr(self, name, context=None): try: for inferred in bases._infer_stmts(self.getattr(name, context), context, frame=self): - # yield YES object instead of descriptors when necessary + # yield Uninferable object instead of descriptors when necessary if (not isinstance(inferred, node_classes.Const) and isinstance(inferred, bases.Instance)): try: @@ -1547,13 +1547,13 @@ def igetattr(self, name, context=None): except exceptions.AttributeInferenceError: yield inferred else: - yield util.YES + 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 YES object - yield util.YES + # class handle some dynamic attributes, return a Uninferable object + yield util.Uninferable else: util.reraise(exceptions.InferenceError( error.message, target=self, attribute=name, context=context)) @@ -1635,7 +1635,7 @@ def declared_metaclass(self): # Expects this from Py3k TreeRebuilder try: return next(node for node in self._metaclass.infer() - if node is not util.YES) + if node is not util.Uninferable) except (exceptions.InferenceError, StopIteration): return None if six.PY3: @@ -1658,7 +1658,7 @@ def declared_metaclass(self): inferred = next(assignment.infer()) except exceptions.InferenceError: return - if inferred is util.YES: # don't expose this + if inferred is util.Uninferable: # don't expose this return None return inferred @@ -1717,7 +1717,7 @@ def _islots(self): values = [item[0] for item in slots.items] else: values = slots.itered() - if values is util.YES: + if values is util.Uninferable: continue if not values: # Stop the iteration, because the class @@ -1727,7 +1727,7 @@ def _islots(self): for elt in values: try: for inferred in elt.infer(): - if inferred is util.YES: + if inferred is util.Uninferable: continue if (not isinstance(inferred, node_classes.Const) or not isinstance(inferred.value, diff --git a/astroid/tests/unittest_brain.py b/astroid/tests/unittest_brain.py index 1918ba6e91..5dfcc2f185 100644 --- a/astroid/tests/unittest_brain.py +++ b/astroid/tests/unittest_brain.py @@ -111,7 +111,7 @@ class X(namedtuple(name, fields)): if base.name == 'X': break self.assertSetEqual({"a", "b", "c"}, - set(base.instanciate_class().instance_attrs)) + set(base.instantiate_class().instance_attrs)) def test_namedtuple_inference_failure(self): klass = test_utils.extract_node(""" @@ -120,7 +120,7 @@ def test_namedtuple_inference_failure(self): def foo(fields): return __(namedtuple("foo", fields)) """) - self.assertIs(util.YES, next(klass.infer())) + self.assertIs(util.Uninferable, next(klass.infer())) def test_namedtuple_advanced_inference(self): # urlparse return an object of class ParseResult, which has a diff --git a/astroid/tests/unittest_builder.py b/astroid/tests/unittest_builder.py index 4fa2ff3ca7..2b58ded58d 100644 --- a/astroid/tests/unittest_builder.py +++ b/astroid/tests/unittest_builder.py @@ -486,7 +486,7 @@ def test_gen_expr_var_scope(self): n = test_utils.get_name_node(astroid, 'n') self.assertIsNot(n.scope(), astroid) self.assertEqual([i.__class__ for i in n.infer()], - [util.YES.__class__]) + [util.Uninferable.__class__]) def test_no_future_imports(self): mod = builder.parse("import sys") diff --git a/astroid/tests/unittest_helpers.py b/astroid/tests/unittest_helpers.py index c3b9c6922c..bac00f2192 100644 --- a/astroid/tests/unittest_helpers.py +++ b/astroid/tests/unittest_helpers.py @@ -145,7 +145,7 @@ def test_inference_errors(self): from unknown import Unknown u = Unknown #@ ''') - self.assertEqual(helpers.object_type(node), util.YES) + self.assertEqual(helpers.object_type(node), util.Uninferable) def test_object_type_too_many_types(self): node = test_utils.extract_node(''' @@ -157,7 +157,7 @@ def test(x): return 1 test(Unknown) #@ ''') - self.assertEqual(helpers.object_type(node), util.YES) + self.assertEqual(helpers.object_type(node), util.Uninferable) def test_is_subtype(self): ast_nodes = test_utils.extract_node(''' @@ -207,8 +207,8 @@ class E(C, B): pass #@ class F(D, E): pass #@ ''') self.assertFalse(helpers.is_subtype(cls_e, cls_f)) - self.assertEqual(helpers.is_subtype(cls_f, cls_e), util.YES) - self.assertEqual(helpers.is_supertype(cls_e, cls_f), util.YES) + self.assertEqual(helpers.is_subtype(cls_f, cls_e), util.Uninferable) + self.assertEqual(helpers.is_supertype(cls_e, cls_f), util.Uninferable) self.assertFalse(helpers.is_supertype(cls_f, cls_e)) def test_is_subtype_supertype_unknown_bases(self): diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index cc780e7b9c..d2d3e25099 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -314,7 +314,7 @@ def test_args_default_inference1(self): self.assertIsInstance(obj1, nodes.Const) self.assertEqual(obj1.value, 0) obj1 = next(inferred) - self.assertIs(obj1, util.YES, obj1) + self.assertIs(obj1, util.Uninferable, obj1) self.assertRaises(StopIteration, partial(next, inferred)) def test_args_default_inference2(self): @@ -323,13 +323,13 @@ def test_args_default_inference2(self): self.assertIsInstance(obj1, nodes.Const) self.assertEqual(obj1.value, 4) obj1 = next(inferred) - self.assertIs(obj1, util.YES, obj1) + self.assertIs(obj1, util.Uninferable, obj1) self.assertRaises(StopIteration, partial(next, inferred)) def test_inference_restrictions(self): inferred = test_utils.get_name_node(self.ast['C']['meth1'], 'arg1').infer() obj1 = next(inferred) - self.assertIs(obj1, util.YES, obj1) + self.assertIs(obj1, util.Uninferable, obj1) self.assertRaises(StopIteration, partial(next, inferred)) def test_ancestors_inference(self): @@ -565,7 +565,7 @@ def test_qqch(self): ast = parse(code, __name__) xxx = ast['xxx'] self.assertSetEqual({n.__class__ for n in xxx.inferred()}, - {nodes.NameConstant, util.YES.__class__}) + {nodes.NameConstant, util.Uninferable.__class__}) def test_method_argument(self): code = ''' @@ -581,13 +581,13 @@ def meth(self, e_type, *args, **kwargs): ast = parse(code, __name__) arg = test_utils.get_name_node(ast['ErudiEntitySchema']['__init__'], 'e_type') self.assertEqual([n.__class__ for n in arg.infer()], - [util.YES.__class__]) + [util.Uninferable.__class__]) arg = test_utils.get_name_node(ast['ErudiEntitySchema']['__init__'], 'kwargs') self.assertEqual([n.__class__ for n in arg.infer()], [nodes.Dict]) arg = test_utils.get_name_node(ast['ErudiEntitySchema']['meth'], 'e_type') self.assertEqual([n.__class__ for n in arg.infer()], - [util.YES.__class__]) + [util.Uninferable.__class__]) arg = test_utils.get_name_node(ast['ErudiEntitySchema']['meth'], 'args') self.assertEqual([n.__class__ for n in arg.infer()], [nodes.Tuple]) @@ -724,7 +724,7 @@ class InvalidGetitem2(object): for node in ast_nodes[:3]: self.assertRaises(InferenceError, next, node.infer()) for node in ast_nodes[3:]: - self.assertEqual(next(node.infer()), util.YES) + self.assertEqual(next(node.infer()), util.Uninferable) ast_nodes = test_utils.extract_node(''' [1, 2, 3][None] #@ 'lala'['bala'] #@ @@ -973,7 +973,7 @@ def __add__(self, other): self.assertEqual(first.value, 43) second = next(ast_nodes[1].infer()) - self.assertEqual(second, util.YES) + self.assertEqual(second, util.Uninferable) def test_binary_op_other_type_using_reflected_operands(self): ast_nodes = test_utils.extract_node(''' @@ -984,7 +984,7 @@ def __radd__(self, other): 1 + A() #@ ''') first = next(ast_nodes[0].infer()) - self.assertEqual(first, util.YES) + self.assertEqual(first, util.Uninferable) second = next(ast_nodes[1].infer()) self.assertIsInstance(second, nodes.Const) @@ -998,7 +998,7 @@ def __radd__(self, other): return NotImplemented 1 + A() #@ ''') first = next(ast_node.infer()) - self.assertEqual(first, util.YES) + self.assertEqual(first, util.Uninferable) def test_binary_op_list_mul(self): for code in ('a = [[]] * 2', 'a = 2 * [[]]'): @@ -1015,10 +1015,10 @@ def test_binary_op_list_mul_none(self): ast = builder.string_build('a = [1] * None\nb = [1] * "r"') inferred = ast['a'].inferred() self.assertEqual(len(inferred), 1) - self.assertEqual(inferred[0], util.YES) + self.assertEqual(inferred[0], util.Uninferable) inferred = ast['b'].inferred() self.assertEqual(len(inferred), 1) - self.assertEqual(inferred[0], util.YES) + self.assertEqual(inferred[0], util.Uninferable) def test_binary_op_list_mul_int(self): 'test correct handling on list multiplied by int when there are more than one' @@ -1090,7 +1090,7 @@ def f(g = lambda: None): callfuncnode = test_utils.extract_node(code) inferred = list(callfuncnode.infer()) self.assertEqual(len(inferred), 2, inferred) - inferred.remove(util.YES) + inferred.remove(util.Uninferable) self.assertIsInstance(inferred[0], nodes.Const) self.assertIsNone(inferred[0].value) @@ -1102,7 +1102,7 @@ def f(x): ast = parse(code, __name__) inferred = list(ast['f'].ilookup('a')) self.assertEqual(len(inferred), 1) - self.assertEqual(inferred[0], util.YES) + self.assertEqual(inferred[0], util.Uninferable) def test_nonregr_instance_attrs(self): """non regression for instance_attrs infinite loop : pylint / #4""" @@ -1154,7 +1154,7 @@ def test_python25_no_relative_import(self): self.assertTrue(ast.absolute_import_activated(), True) inferred = next(test_utils.get_name_node(ast, 'import_package_subpackage_module').infer()) # failed to import since absolute_import is activated - self.assertIs(inferred, util.YES) + self.assertIs(inferred, util.Uninferable) def test_nonregr_absolute_import(self): ast = resources.build_file('data/absimp/string.py', 'data.absimp.string') @@ -1272,7 +1272,7 @@ def qux(): ast = parse(code, __name__) inferred = list(test_utils.get_name_node(ast['foo'], 'spam').infer()) self.assertEqual(len(inferred), 1) - self.assertIs(inferred[0], util.YES) + self.assertIs(inferred[0], util.Uninferable) def test_nonregr_func_global(self): code = ''' @@ -1463,7 +1463,7 @@ def __mul__(self, other): ast = parse(code, __name__) sub = ast['sub'].inferred()[0] mul = ast['mul'].inferred()[0] - self.assertIs(sub, util.YES) + self.assertIs(sub, util.Uninferable) self.assertIsInstance(mul, nodes.Const) self.assertEqual(mul.value, 42) @@ -1482,7 +1482,7 @@ class B(A): ast = parse(code, __name__) sub = ast['sub'].inferred()[0] mul = ast['mul'].inferred()[0] - self.assertIs(sub, util. YES) + self.assertIs(sub, util. Uninferable) self.assertIsInstance(mul, nodes.Const) self.assertEqual(mul.value, 42) @@ -1502,7 +1502,7 @@ def __mul__(self, other): ast = parse(code, __name__) sub = ast['sub'].inferred()[0] mul = ast['mul'].inferred()[0] - self.assertIs(sub, util.YES) + self.assertIs(sub, util.Uninferable) self.assertIsInstance(mul, nodes.List) self.assertIsInstance(mul.elts[0], nodes.Const) self.assertEqual(mul.elts[0].value, 42) @@ -1519,12 +1519,12 @@ def __mul__(self, other): """ ast = parse(code, __name__) node = ast['c'] - self.assertEqual(node.inferred(), [util.YES]) + self.assertEqual(node.inferred(), [util.Uninferable]) def test_infer_empty_nodes(self): # Should not crash when trying to infer EmptyNodes. node = nodes.EmptyNode() - self.assertEqual(node.inferred(), [util.YES]) + self.assertEqual(node.inferred(), [util.Uninferable]) def test_infinite_loop_for_decorators(self): # Issue https://bitbucket.org/logilab/astroid/issue/50 @@ -2019,7 +2019,7 @@ def no_yield_mgr(): def test_unary_op_leaks_stop_iteration(self): node = test_utils.extract_node('+[] #@') - self.assertEqual(util.YES, next(node.infer())) + self.assertEqual(util.Uninferable, next(node.infer())) def test_unary_operands(self): ast_nodes = test_utils.extract_node(''' @@ -2071,7 +2071,7 @@ def lala(self): return 24 for bad_node in ast_nodes[6:]: inferred = next(bad_node.infer()) - self.assertEqual(inferred, util.YES) + self.assertEqual(inferred, util.Uninferable) def test_unary_op_instance_method_not_callable(self): ast_node = test_utils.extract_node(''' @@ -2268,11 +2268,11 @@ def true_value(): genexpr = next(module['genexpr'].infer()) self.assertTrue(genexpr.bool_value()) dict_comp = next(module['dict_comp'].infer()) - self.assertEqual(dict_comp, util.YES) + self.assertEqual(dict_comp, util.Uninferable) set_comp = next(module['set_comp'].infer()) - self.assertEqual(set_comp, util.YES) + self.assertEqual(set_comp, util.Uninferable) list_comp = next(module['list_comp'].infer()) - self.assertEqual(list_comp, util.YES) + self.assertEqual(list_comp, util.Uninferable) lambda_func = next(module['lambda_func'].infer()) self.assertTrue(lambda_func) unbound_method = next(module['unbound_method'].infer()) @@ -2286,13 +2286,13 @@ def true_value(): bin_op = module['bin_op'].parent.value self.assertTrue(bin_op.bool_value()) bool_op = module['bool_op'].parent.value - self.assertEqual(bool_op.bool_value(), util.YES) + self.assertEqual(bool_op.bool_value(), util.Uninferable) callfunc = module['callfunc'].parent.value - self.assertEqual(callfunc.bool_value(), util.YES) + self.assertEqual(callfunc.bool_value(), util.Uninferable) good_callfunc = next(module['good_callfunc'].infer()) self.assertTrue(good_callfunc.bool_value()) compare = module['compare'].parent.value - self.assertEqual(compare.bool_value(), util.YES) + self.assertEqual(compare.bool_value(), util.Uninferable) def test_bool_value_instances(self): instances = test_utils.extract_node(''' @@ -2325,7 +2325,7 @@ class NonMethods(object): AlwaysTrueInstance() #@ ErrorInstance() #@ '''.format(bool=BOOL_SPECIAL_METHOD)) - expected = (False, True, False, True, True, util.YES, util.YES) + expected = (False, True, False, True, True, util.Uninferable, util.Uninferable) for node, expected_value in zip(instances, expected): inferred = next(node.infer()) self.assertEqual(inferred.bool_value(), expected_value) @@ -2397,7 +2397,7 @@ class B(object): pass A() + B() #@ ''') inferred = next(node.infer()) - self.assertEqual(inferred, util.YES) + self.assertEqual(inferred, util.Uninferable) def test_binop_different_types_reflected_and_normal_not_implemented(self): node = test_utils.extract_node(''' @@ -2408,7 +2408,7 @@ def __radd__(self, other): return NotImplemented A() + B() #@ ''') inferred = next(node.infer()) - self.assertEqual(inferred, util.YES) + self.assertEqual(inferred, util.Uninferable) def test_binop_subtype(self): node = test_utils.extract_node(''' @@ -2441,7 +2441,7 @@ def __add__(self, other): return NotImplemented B() + A() #@ ''') inferred = next(node.infer()) - self.assertEqual(inferred, util.YES) + self.assertEqual(inferred, util.Uninferable) def test_binop_supertype(self): node = test_utils.extract_node(''' @@ -2480,7 +2480,7 @@ def __radd__(self, other): A() + B() #@ ''') inferred = next(node.infer()) - self.assertEqual(inferred, util.YES) + self.assertEqual(inferred, util.Uninferable) def test_binop_inferrence_errors(self): ast_nodes = test_utils.extract_node(''' @@ -2495,7 +2495,7 @@ def __add__(self, other): return Unknown A() + B() #@ ''') for node in ast_nodes: - self.assertEqual(next(node.infer()), util.YES) + self.assertEqual(next(node.infer()), util.Uninferable) def test_binop_ambiguity(self): ast_nodes = test_utils.extract_node(''' @@ -2518,7 +2518,7 @@ def __radd__(self, other): C() + A() #@ ''') for node in ast_nodes: - self.assertEqual(next(node.infer()), util.YES) + self.assertEqual(next(node.infer()), util.Uninferable) def test_bin_op_supertype_more_complicated_example(self): ast_node = test_utils.extract_node(''' @@ -2547,7 +2547,7 @@ def __iadd__(self, other): return NotImplemented def __add__(self, other): return NotImplemented A() + A() #@ ''') - self.assertEqual(next(ast_node.infer()), util.YES) + self.assertEqual(next(ast_node.infer()), util.Uninferable) def test_aug_op_same_type_aug_implemented(self): ast_node = test_utils.extract_node(''' @@ -2582,7 +2582,7 @@ class B(A): b = B() b+=A() #@ ''') - self.assertEqual(next(ast_node.infer()), util.YES) + self.assertEqual(next(ast_node.infer()), util.Uninferable) def test_aug_op_subtype_aug_op_is_implemented(self): ast_node = test_utils.extract_node(''' @@ -2617,7 +2617,7 @@ class B(object): pass f = A() f += B() #@ ''') - self.assertEqual(next(ast_node.infer()), util.YES) + self.assertEqual(next(ast_node.infer()), util.Uninferable) def test_aug_different_types_augop_implemented(self): ast_node = test_utils.extract_node(''' @@ -2665,7 +2665,7 @@ class B(object): pass a = A() a += B() #@ ''') - self.assertEqual(next(ast_node.infer()), util.YES) + self.assertEqual(next(ast_node.infer()), util.Uninferable) def test_augop_supertypes_not_implemented_returned_for_all(self): ast_node = test_utils.extract_node(''' @@ -2677,7 +2677,7 @@ def __add__(self, other): return NotImplemented a = A() a += B() #@ ''') - self.assertEqual(next(ast_node.infer()), util.YES) + self.assertEqual(next(ast_node.infer()), util.Uninferable) def test_augop_supertypes_augop_implemented(self): ast_node = test_utils.extract_node(''' @@ -2749,7 +2749,7 @@ def __index__(self): return None [1, 2, 1, 2]) for rest in ast_nodes[1:]: inferred = next(rest.infer()) - self.assertEqual(inferred, util.YES) + self.assertEqual(inferred, util.Uninferable) def test_subscript_supports__index__(self): ast_nodes = test_utils.extract_node(''' @@ -2918,7 +2918,7 @@ def __getitem__(self, index): A()[2:] #@ ''') for node in ast_nodes: - self.assertEqual(next(node.infer()), util.YES) + self.assertEqual(next(node.infer()), util.Uninferable) def test_type__new__with_metaclass(self): ast_node = test_utils.extract_node(''' @@ -3162,7 +3162,7 @@ def test_yes_when_unknown(self): for node in ast_nodes[4:]: inferred = next(node.infer()) - self.assertEqual(inferred, util.YES, node) + self.assertEqual(inferred, util.Uninferable, node) def test_attrname_not_string(self): ast_nodes = test_utils.extract_node(''' @@ -3252,7 +3252,7 @@ def test_lambda(self): getattr(lambda x: x, 'f') #@ ''') inferred = next(node.infer()) - self.assertEqual(inferred, util.YES) + self.assertEqual(inferred, util.Uninferable) class HasattrTest(unittest.TestCase): @@ -3268,7 +3268,7 @@ def test_inference_errors(self): ''') for node in ast_nodes: inferred = next(node.infer()) - self.assertEqual(inferred, util.YES) + self.assertEqual(inferred, util.Uninferable) def test_attribute_is_missing(self): ast_nodes = test_utils.extract_node(''' @@ -3311,7 +3311,7 @@ def test_lambda(self): hasattr(lambda x: x, 'f') #@ ''') inferred = next(node.infer()) - self.assertEqual(inferred, util.YES) + self.assertEqual(inferred, util.Uninferable) class BoolOpTest(unittest.TestCase): @@ -3348,7 +3348,7 @@ def test_yes_when_unknown(self): ''') for node in ast_nodes: inferred = next(node.infer()) - self.assertEqual(inferred, util.YES) + self.assertEqual(inferred, util.Uninferable) def test_other_nodes(self): ast_nodes = test_utils.extract_node(''' @@ -3428,7 +3428,7 @@ def test(): ''') for node in ast_nodes: inferred = next(node.infer()) - self.assertEqual(inferred, util.YES) + self.assertEqual(inferred, util.Uninferable) def test_not_callable(self): ast_nodes = test_utils.extract_node(''' @@ -3454,12 +3454,12 @@ def test_bool(self): ('bool(True)', True), ('bool(False)', False), ('bool(None)', False), - ('from unknown import Unknown; __(bool(Unknown))', util.YES), + ('from unknown import Unknown; __(bool(Unknown))', util.Uninferable), ] for code, expected in pairs: node = test_utils.extract_node(code) inferred = next(node.infer()) - if expected is util.YES: + if expected is util.Uninferable: self.assertEqual(expected, inferred) else: self.assertEqual(inferred.value, expected) @@ -3509,7 +3509,7 @@ class LenInvalid(object): '''.format(method=BOOL_SPECIAL_METHOD)) for node in ast_nodes: inferred = next(node.infer()) - self.assertEqual(inferred, util.YES) + self.assertEqual(inferred, util.Uninferable) class TestType(unittest.TestCase): @@ -3721,7 +3721,7 @@ def test(f=None): ''') for ast_node in ast_nodes: inferred = next(ast_node.infer()) - self.assertEqual(inferred, util.YES) + self.assertEqual(inferred, util.Uninferable) def test_fail_to_infer_args(self): ast_nodes = test_utils.extract_node(''' @@ -3751,7 +3751,7 @@ def test(*args): return args ''') for node in ast_nodes: inferred = next(node.infer()) - self.assertEqual(inferred, util.YES) + self.assertEqual(inferred, util.Uninferable) class SliceTest(unittest.TestCase): diff --git a/astroid/tests/unittest_lookup.py b/astroid/tests/unittest_lookup.py index ca5965ea3c..c347d42a6b 100644 --- a/astroid/tests/unittest_lookup.py +++ b/astroid/tests/unittest_lookup.py @@ -172,7 +172,7 @@ def test_list_comp_target(self): """) var = astroid.body[1].value if sys.version_info < (3, 0): - self.assertEqual(var.inferred(), [util.YES]) + self.assertEqual(var.inferred(), [util.Uninferable]) else: self.assertRaises(exceptions.NameInferenceError, var.inferred) diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index 9f0492a492..70aa33f1eb 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -360,7 +360,7 @@ def test_bad_import_inference(self): method of this From node will be made by unpack_infer. inference.infer_from will try to import this module, which will fail and raise a InferenceException (by mixins.do_import_module). The infer_name - will catch this exception and yield and YES instead. + will catch this exception and yield and Uninferable instead. ''' code = ''' @@ -384,7 +384,7 @@ def test_bad_import_inference(self): # present in the other version. self.assertIsInstance(excs[0], nodes.ClassDef) self.assertEqual(excs[0].name, 'PickleError') - self.assertIs(excs[-1], util.YES) + self.assertIs(excs[-1], util.Uninferable) def test_absolute_import(self): astroid = resources.build_file('data/absimport.py') diff --git a/astroid/tests/unittest_protocols.py b/astroid/tests/unittest_protocols.py index 9ba4f6ad1a..6d6bce6e88 100644 --- a/astroid/tests/unittest_protocols.py +++ b/astroid/tests/unittest_protocols.py @@ -67,7 +67,7 @@ def test_assigned_stmts_starred_for(self): for1_starred = next(assign_stmts.nodes_of_class(Starred)) assigned = next(for1_starred.assigned_stmts()) - self.assertEqual(assigned, util.YES) + self.assertEqual(assigned, util.Uninferable) def _get_starred_stmts(self, code): assign_stmt = extract_node("{} #@".format(code)) @@ -108,16 +108,16 @@ def test_assigned_stmts_starred_assnames(self): @require_version(minver='3.0') def test_assigned_stmts_starred_yes(self): # Not something iterable and known - self._helper_starred_expected("a, *b = range(3) #@", util.YES) + self._helper_starred_expected("a, *b = range(3) #@", util.Uninferable) # Not something inferrable - self._helper_starred_expected("a, *b = balou() #@", util.YES) + self._helper_starred_expected("a, *b = balou() #@", util.Uninferable) # In function, unknown. self._helper_starred_expected(""" def test(arg): - head, *tail = arg #@""", util.YES) + head, *tail = arg #@""", util.Uninferable) # These cases aren't worth supporting. self._helper_starred_expected( - "a, (*b, c), d = (1, (2, 3, 4), 5) #@", util.YES) + "a, (*b, c), d = (1, (2, 3, 4), 5) #@", util.Uninferable) @require_version(minver='3.0') def test_assign_stmts_starred_fails(self): diff --git a/astroid/tests/unittest_regrtest.py b/astroid/tests/unittest_regrtest.py index 0a363647b8..5d57734e4b 100644 --- a/astroid/tests/unittest_regrtest.py +++ b/astroid/tests/unittest_regrtest.py @@ -252,7 +252,7 @@ def test(x=False): def test_ancestors_yes_in_bases(self): # Test for issue https://bitbucket.org/logilab/astroid/issue/84 - # This used to crash astroid with a TypeError, because an YES + # This used to crash astroid with a TypeError, because an Uninferable # node was present in the bases node = extract_node(""" def with_metaclass(meta, *bases): diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index 45328b58e9..e3bae6004c 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -1135,7 +1135,7 @@ class CompositeBuilder(object): instance = astroid['tgts'] # used to raise "'_Yes' object is not iterable", see # https://bitbucket.org/logilab/astroid/issue/17 - self.assertEqual(list(instance.infer()), [util.YES]) + self.assertEqual(list(instance.infer()), [util.Uninferable]) def test_slots(self): astroid = builder.parse(""" @@ -1377,7 +1377,7 @@ def test_implicit_metaclass_lookup(self): class A(object): pass ''') - instance = cls.instanciate_class() + instance = cls.instantiate_class() func = cls.getattr('mro') self.assertEqual(len(func), 1) self.assertRaises(AttributeInferenceError, instance.getattr, 'mro') @@ -1400,7 +1400,7 @@ class Metaclass(type): class B(object): pass ''') cls = module['B'] - self.assertEqual(util.YES, next(cls.igetattr('foo'))) + self.assertEqual(util.Uninferable, next(cls.igetattr('foo'))) def test_metaclass_lookup(self): module = builder.parse(''' diff --git a/astroid/util.py b/astroid/util.py index 687b2854dd..0ec266bae3 100644 --- a/astroid/util.py +++ b/astroid/util.py @@ -53,10 +53,10 @@ def generate_warning(message, warning): PendingDeprecationWarning) @object.__new__ -class YES(object): +class Uninferable(object): """Special inference object, which is returned when inference fails.""" def __repr__(self): - return 'YES' + return 'Uninferable' def __getattribute__(self, name): if name == 'next': From d6e4dab4db323e998be1c23314a5540f5ad3a4ee Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Fri, 6 Nov 2015 18:55:49 -0500 Subject: [PATCH 073/312] Fix missing merge integrations --- astroid/brain/brain_stdlib.py | 46 ----------------------------------- astroid/raw_building.py | 9 ++++--- tox.ini | 3 ++- 3 files changed, 8 insertions(+), 50 deletions(-) diff --git a/astroid/brain/brain_stdlib.py b/astroid/brain/brain_stdlib.py index 4631562b18..b3b71035a6 100644 --- a/astroid/brain/brain_stdlib.py +++ b/astroid/brain/brain_stdlib.py @@ -18,53 +18,7 @@ PY33 = sys.version_info >= (3, 3) PY34 = sys.version_info >= (3, 4) -<<<<<<< variant A def infer_first(node, context): ->>>>>>> variant B -# general function - -def infer_func_form(node, base_type, context=None, enum=False): - """Specific inference function for namedtuple or Python 3 enum. """ - def infer_first(node): - try: - value = next(node.infer(context=context)) - if value is util.Uninferable: - raise UseInferenceDefault() - else: - return value - except StopIteration: - raise InferenceError() - - # node is a Call node, class name as first argument and generated class - # attributes as second argument - if len(node.args) != 2: - # something weird here, go back to class implementation - raise UseInferenceDefault() - # namedtuple or enums list of attributes can be a list of strings or a - # whitespace-separate string -####### Ancestor -# general function - -def infer_func_form(node, base_type, context=None, enum=False): - """Specific inference function for namedtuple or Python 3 enum. """ - def infer_first(node): - try: - value = next(node.infer(context=context)) - if value is util.YES: - raise UseInferenceDefault() - else: - return value - except StopIteration: - raise InferenceError() - - # node is a Call node, class name as first argument and generated class - # attributes as second argument - if len(node.args) != 2: - # something weird here, go back to class implementation - raise UseInferenceDefault() - # namedtuple or enums list of attributes can be a list of strings or a - # whitespace-separate string -======= end try: value = next(node.infer(context=context)) if value is util.Uninferable: diff --git a/astroid/raw_building.py b/astroid/raw_building.py index cdb3914974..2297a1a345 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -40,6 +40,9 @@ from ConfigParser import _Chainmap # TODO: complete this class _ChainMap(_Chainmap): + def __init__(self, *maps): + super(_ChainMap, self).__init__(*maps) + self.maps = self._maps def __setitem__(self, key, value): self._maps[0][key] = value def __delitem__(self, key): @@ -187,7 +190,7 @@ def ast_from_module(module, built_objects, parent_module, name=None, parent=None # implemented in pure Python. pure_python=bool(source_file)) built_objects[id(module)] = module_node - built_objects = _ChainMap({}, *built_objects._maps) + built_objects = _ChainMap({}, *built_objects.maps) MANAGER.cache_module(module_node) module_node.postinit(body=[_ast_from_object(m, built_objects, module, n, module_node) @@ -211,7 +214,7 @@ def ast_from_class(cls, built_objects, module, name=None, parent=None): return nodes.Name(name=name or cls.__name__, parent=parent) class_node = nodes.ClassDef(name=name or cls.__name__, doc=inspect.getdoc(cls), parent=parent) built_objects[id(cls)] = class_node - built_objects = _ChainMap({}, *built_objects._maps) + built_objects = _ChainMap({}, *built_objects.maps) try: bases = [nodes.Name(name=b.__name__, parent=class_node) for b in inspect.getmro(cls)[1:]] @@ -256,7 +259,7 @@ def ast_from_function(func, built_objects, module, name=None, parent=None): doc=inspect.getdoc(func), parent=parent) built_objects[id(func)] = func_node - built_objects = _ChainMap({}, *built_objects._maps) + built_objects = _ChainMap({}, *built_objects.maps) try: signature = _signature(func) except (ValueError, TypeError): diff --git a/tox.ini b/tox.ini index 119a0bb9b5..cac7a6bd4e 100644 --- a/tox.ini +++ b/tox.ini @@ -3,7 +3,7 @@ # envlist = py27, py33, py34, py35, pypy, jython, pylint # drone.io -envlist = py27, py33, pylint +# envlist = py27, py33, pylint # For testing off drone.io---please don't delete. # envlist = py27, py34, pypy, pylint @@ -14,6 +14,7 @@ commands = pylint -rn --rcfile={toxinidir}/pylintrc {envsitepackagesdir}/astroid [testenv] deps = + # Temporary only py27,pypy,jython: dictproxyhack py27,py33,pypy,jython: enum34 py27,pypy,jython: funcsigs From 9d41efa7fdec15cabe6dc794ebabd5f661572ba2 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Fri, 6 Nov 2015 21:07:37 -0500 Subject: [PATCH 074/312] Rename InterpreterObject from EmptyNode --- astroid/brain/brain_nose.py | 4 ++-- astroid/brain/brain_stdlib.py | 2 +- astroid/inference.py | 4 ++-- astroid/node_classes.py | 6 +++--- astroid/nodes.py | 4 ++-- astroid/raw_building.py | 8 ++++---- astroid/scoped_nodes.py | 7 +++---- astroid/tests/unittest_inference.py | 6 +++--- 8 files changed, 20 insertions(+), 21 deletions(-) diff --git a/astroid/brain/brain_nose.py b/astroid/brain/brain_nose.py index 463d9eefd5..80d6563da2 100644 --- a/astroid/brain/brain_nose.py +++ b/astroid/brain/brain_nose.py @@ -55,7 +55,7 @@ class Test(unittest.TestCase): def _nose_tools_transform(module_node): for method_name, method in _nose_tools_functions(): - module_node.body.append(astroid.EmptyNode(object_=method, + module_node.body.append(astroid.InterpreterObject(object_=method, name=method_name, parent=module_node)) @@ -67,7 +67,7 @@ def _nose_tools_trivial_transform(): for pep8_name, method in _nose_tools_functions(): all_entries.append(pep8_name) - stub.body.append(astroid.EmptyNode(object_=method, + stub.body.append(astroid.InterpreterObject(object_=method, name=pep8_name, parent=stub)) # Update the __all__ variable, since nose.tools diff --git a/astroid/brain/brain_stdlib.py b/astroid/brain/brain_stdlib.py index b3b71035a6..9c2eeb01a3 100644 --- a/astroid/brain/brain_stdlib.py +++ b/astroid/brain/brain_stdlib.py @@ -371,7 +371,7 @@ def Manager(): # We need to rebind this, since otherwise # it will have an extra argument (self). value = BoundMethod(value, node) - module.body.append(nodes.EmptyNode(object_=value, name=key, + module.body.append(nodes.InterpreterObject(object_=value, name=key, parent=module)) return module diff --git a/astroid/inference.py b/astroid/inference.py index 45888d7661..0b3f556860 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -706,7 +706,7 @@ def infer_assign(self, context=None): # no infer method on DelName and DelAttr (expected InferenceError) @decorators.path_wrapper -def infer_empty_node(self, context=None): +def infer_interpreter_object(self, context=None): if not self.has_underlying_object(): yield util.Uninferable else: @@ -716,7 +716,7 @@ def infer_empty_node(self, context=None): yield inferred except exceptions.AstroidError: yield util.Uninferable -nodes.EmptyNode._infer = infer_empty_node +nodes.InterpreterObject._infer = infer_interpreter_object def infer_index(self, context=None): diff --git a/astroid/node_classes.py b/astroid/node_classes.py index 6e46238d85..d5f1fd3040 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -1372,8 +1372,8 @@ def bool_value(self): return True -class EmptyNode(NodeNG): - '''EmptyNodes are used in manufactured ASTs that simulate features of +class InterpreterObject(NodeNG): + '''InterpreterObjects are used in manufactured ASTs that simulate features of real ASTs for inference, usually to handle behavior implemented in the interpreter or in C extensions. @@ -1384,7 +1384,7 @@ def __init__(self, object_=None, name=None, lineno=None, col_offset=None, parent if object_ is not None: self.object = object_ self.name = name - super(EmptyNode, self).__init__(lineno, col_offset, parent) + super(InterpreterObject, self).__init__(lineno, col_offset, parent) def has_underlying_object(self): return hasattr(self, 'object') diff --git a/astroid/nodes.py b/astroid/nodes.py index 7e89cb1a95..d34ff42b9b 100644 --- a/astroid/nodes.py +++ b/astroid/nodes.py @@ -50,7 +50,7 @@ # Node not present in the builtin ast module. DictUnpack, # Special nodes for building from live objects. - EmptyNode, NameConstant, ReservedName, Unknown + InterpreterObject, NameConstant, ReservedName, Unknown ) from astroid.scoped_nodes import ( Module, GeneratorExp, Lambda, DictComp, @@ -70,7 +70,7 @@ Call, ClassDef, Compare, Comprehension, Const, Continue, Decorators, DelAttr, DelName, Delete, Dict, DictComp, DictUnpack, Expr, - Ellipsis, EmptyNode, ExceptHandler, Exec, ExtSlice, + Ellipsis, InterpreterObject, ExceptHandler, Exec, ExtSlice, For, ImportFrom, FunctionDef, Attribute, GeneratorExp, Global, If, IfExp, Import, Index, diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 2297a1a345..506b738422 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -156,12 +156,12 @@ def _ast_from_object(object_, built_objects, module, name=None, parent=None): # if name: # parent = nodes.Assign(parent=parent) # name_node = nodes.AssignName(name, parent=parent) - empty_node = nodes.EmptyNode(name=name, object_=object_, parent=parent) + interpreter_object = nodes.InterpreterObject(name=name, object_=object_, parent=parent) # if name: - # parent.postinit(targets=[name_node], value=empty_node) + # parent.postinit(targets=[name_node], value=interpreter_object) # node = parent # else: - node = empty_node + node = interpreter_object return node @@ -477,7 +477,7 @@ def ast_from_ellipsis(ellipsis, built_objects, module, name=None, parent=None): # 'True', '__debug__', '__package__', '__spec__', 'copyright', # 'credits', 'exit', 'help', 'license', 'quit'): # for child in (n for n in node.body if -# isinstance(n, (nodes.Const, nodes.EmptyNode))): +# isinstance(n, (nodes.Const, nodes.InterpreterObject))): # if child.name == name: # locals_[name].append(child) # for n in node.get_children(): diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index f76b383250..8dc418f53d 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -44,7 +44,6 @@ from astroid import util # TODO: remove this, this is for writing the necessary code only. - try: from types import MappingProxyType except ImportError: @@ -1902,16 +1901,16 @@ def locals_name(node, locals_): new scope so shouldn't be recursed into.''' locals_[node.name].append(node) -@_get_locals.register(node_classes.EmptyNode) +@_get_locals.register(node_classes.InterpreterObject) def locals_empty(node, locals_): - '''EmptyNodes add an object to the local variables under a specified + '''InterpreterObjects add an object to the local variables under a specified name.''' if node.name: locals_[node.name].append(node) @_get_locals.register(node_classes.ReservedName) def locals_reserved_name(node, locals_): - '''EmptyNodes add an object to the local variables under a specified + '''InterpreterObjects add an object to the local variables under a specified name.''' locals_[node.name].append(node.value) diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index 11988ea0ae..9abfb60809 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -1519,9 +1519,9 @@ def __mul__(self, other): node = ast['c'] self.assertEqual(node.inferred(), [util.Uninferable]) - def test_infer_empty_nodes(self): - # Should not crash when trying to infer EmptyNodes. - node = nodes.EmptyNode() + def test_infer_interpreter_objects(self): + # Should not crash when trying to infer InterpreterObjects. + node = nodes.InterpreterObject() self.assertEqual(node.inferred(), [util.Uninferable]) def test_infinite_loop_for_decorators(self): From 1bfa4cf0c8e4f4c71d7a9d882f51e3e367d07077 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Sun, 8 Nov 2015 10:43:29 -0500 Subject: [PATCH 075/312] Fix docstring handling for classes in ast_from_object, change ast_from_object so it returns tuples to improve type-handling for instances and open the possibility of more improvements in the future --- astroid/__init__.py | 8 +- astroid/bases.py | 5 - astroid/node_classes.py | 6 +- astroid/raw_building.py | 243 +++++++++++-------------- astroid/tests/unittest_scoped_nodes.py | 3 - 5 files changed, 110 insertions(+), 155 deletions(-) diff --git a/astroid/__init__.py b/astroid/__init__.py index 45c26332be..2a56e95786 100644 --- a/astroid/__init__.py +++ b/astroid/__init__.py @@ -115,9 +115,9 @@ def transform(node, infer_function=infer_function): def register_module_extender(manager, module_name, get_extension_mod): def transform(module): extension_module = get_extension_mod() - # for name, obj in extension_module.locals.items(): - # node.locals[name] = obj - module.body.extend(extension_module.body) + for statement in extension_module.body: + statement.parent = module + module.body.append(statement) manager.register_transform(Module, transform, lambda n: n.name == module_name) @@ -132,6 +132,4 @@ def transform(module): # load modules in this directory for module in listdir(BRAIN_MODULES_DIR): if module.endswith('.py'): - # print(module[:-3]) - # __import__(module[:-3]) importlib.import_module(module[:-3]) diff --git a/astroid/bases.py b/astroid/bases.py index c9114eccd7..1f720337d4 100644 --- a/astroid/bases.py +++ b/astroid/bases.py @@ -272,11 +272,6 @@ def bool_value(self): def getitem(self, index, context=None): pass - @property - def instance_attrs(self): - return types.MappingProxyType(scoped_nodes.get_external_assignments(self, collections.defaultdict(list))) - - class UnboundMethod(Proxy): """a special node representing a method not bound to an instance""" diff --git a/astroid/node_classes.py b/astroid/node_classes.py index d5f1fd3040..2776a4388e 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -616,7 +616,7 @@ def from_constants(cls, elts=None): if elts is None: node.elts = [] else: - node.elts = [raw_building.ast_from_builtin_number_text_binary(e, {}, None, parent=node) for e in elts] + node.elts = [raw_building.ast_from_builtin_number_text_binary(e, {}, None, parent=node)[0] for e in elts] return node def itered(self): @@ -1304,9 +1304,9 @@ def from_constants(cls, items=None): node.items = [] else: node.items = [(raw_building.ast_from_scalar(k, {}, None, - parent=node), + parent=node)[0], raw_building.ast_from_scalar(v, {}, None, - parent=node)) + parent=node)[0]) for k, v in items.items()] return node diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 506b738422..d1fd90e6d9 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -67,6 +67,8 @@ def __delitem__(self, key): from astroid import scoped_nodes from astroid import util +MANAGER = manager.AstroidManager() + # This is a type used for some unbound methods implemented in C on # CPython and all unbound special methods on Jython. Bound methods # corresponding to unbound methods of this type have the type @@ -86,83 +88,33 @@ def __delitem__(self, key): # CPython. MethodWrapperType = type(object().__getattribute__) -MANAGER = manager.AstroidManager() - - -# def _io_discrepancy(member): -# # _io module names itself `io`: http://bugs.python.org/issue18602 -# member_self = getattr(member, '__self__', None) -# return (member_self and -# inspect.ismodule(member_self) and -# member_self.__name__ == '_io' and -# member.__module__ == 'io') - -# def _add_dunder_class(func, member): -# """Add a __class__ member to the given func node, if we can determine it.""" -# python_cls = member.__class__ -# cls_name = getattr(python_cls, '__name__', None) -# if not cls_name: -# return -# bases = [ancestor.__name__ for ancestor in python_cls.__bases__] -# ast_klass = build_class(cls_name, bases, python_cls.__doc__) -# func.instance_attrs['__class__'] = [ast_klass] - -# Parameter = collections.namedtuple('Parameter', 'name default annotation kind') -# DEFAULT_PARAMETER = Parameter(None, None, None, None) - -# def build_function(name, args=(), defaults=(), annotations=(), -# kwonlyargs=(), kwonly_defaults=(), -# kwonly_annotations=(), vararg=None, -# varargannotation=None, kwarg=None, kwargannotation=None, -# returns=None, doc=None, parent=None): -# """create and initialize an astroid FunctionDef node""" -# func = nodes.FunctionDef(name=name, doc=doc, parent=parent) -# args_node = nodes.Arguments(vararg=vararg, kwarg=kwarg, parent=func) -# args = [nodes.Name(name=a.name, parent=args_node) for n in args] -# kwonlyargs = [nodes.Name(name=a.name, parent=args_node) for a in kw_only] -# args_node.postinit(args, defaults, kwonlyargs, kw_defaults, -# annotations, kwonly_annotations, -# varargannotation, kwargannotation) -# func.postinit(args=args_node, body=[], returns=returns) -# return func - -# def object_build_function(parent, func, localname): -# """create astroid for a living function object""" -# signature = _signature(func) -# parameters = {k: tuple(g) for k, g in -# itertools.groupby(signature.parameters.values(), -# operator.attrgetter('kind'))} -# # This ignores POSITIONAL_ONLY args, because they only appear in -# # functions implemented in C and can't be mimicked by any Python -# # function. -# node = build_function(getattr(func, '__name__', None) or localname, -# parameters.get(_Parameter.POSITIONAL_OR_KEYWORD, ()), -# parameters.get(_Parameter.KEYWORD_ONLY, ()), -# parameters.get(_Parameter.VAR_POSITIONAL, None), -# parameters.get(_Parameter.VAR_KEYWORD, None), -# signature.return_annotation, -# func.__doc__, -# parent) -# return node - def ast_from_object(object_, name=None): return _ast_from_object(object_, _ChainMap({}), - inspect.getmodule(object_), name) + inspect.getmodule(object_), name)[0] @_singledispatch -def _ast_from_object(object_, built_objects, module, name=None, parent=None): - # if name: - # parent = nodes.Assign(parent=parent) - # name_node = nodes.AssignName(name, parent=parent) - interpreter_object = nodes.InterpreterObject(name=name, object_=object_, parent=parent) - # if name: - # parent.postinit(targets=[name_node], value=interpreter_object) - # node = parent - # else: - node = interpreter_object - return node +def _ast_from_object(instance, built_objects, module, name=None, parent=None): + # Since this ultimately inherits from object but not any type, + # it's presumably an instance of some kind. + cls = type(instance) + result = [] + result.extend(_ast_from_object(cls, built_objects, module, parent=parent)) + + # TODO: is this guaranteed? Should verify. + class_node = result[0] + + # TODO: remove this hack + if isinstance(class_node, nodes.ClassDef): + # Take the set difference of instance and class attributes + for name in set(dir(instance)) - set(dir(cls)): + class_node.instance_attrs[name].append(getattr(instance, name)) + + # Create an instance of the class we just created an AST for. + result.append(nodes.InterpreterObject(name=name, object_=class_node.instantiate_class(), parent=parent)) + result.append(nodes.InterpreterObject(name=name, object_=instance, parent=parent)) + return result # pylint: disable=unused-variable; doesn't understand singledispatch @@ -170,32 +122,31 @@ def _ast_from_object(object_, built_objects, module, name=None, parent=None): def ast_from_module(module, built_objects, parent_module, name=None, parent=None): if module is not parent_module: # This module has been imported into another. - return nodes.Import([[module.__name__, name]], parent=parent) + return (nodes.Import([[module.__name__, name]], parent=parent),) if id(module) in built_objects: - return nodes.Name(name=name or module.__name__, parent=parent_module) + return (nodes.Name(name=name or module.__name__, parent=parent_module),) try: source_file = inspect.getsourcefile(module) except TypeError: # inspect.getsourcefile raises TypeError for built-in modules. source_file = None - module_node = nodes.Module(name=name or module.__name__, - # inspect.getdoc returns None for - # modules without docstrings like - # Jython Java modules. - doc=inspect.getdoc(module), - source_file=source_file, - package=hasattr(module, '__path__'), - # Assume that if inspect couldn't find a - # Python source file, it's probably not - # implemented in pure Python. - pure_python=bool(source_file)) + module_node = nodes.Module( + name=name or module.__name__, + # inspect.getdoc returns None for modules without docstrings like + # Jython Java modules. + doc=inspect.getdoc(module), + source_file=source_file, + package=hasattr(module, '__path__'), + # Assume that if inspect couldn't find a Python source file, it's + # probably not implemented in pure Python. + pure_python=bool(source_file)) built_objects[id(module)] = module_node built_objects = _ChainMap({}, *built_objects.maps) MANAGER.cache_module(module_node) - module_node.postinit(body=[_ast_from_object(m, built_objects, module, - n, module_node) - for n, m in inspect.getmembers(module)]) - return module_node + body = [t for n, m in inspect.getmembers(module) + for t in _ast_from_object(m, built_objects, module, n, module_node)] + module_node.postinit(body=body) + return (module_node,) # pylint: disable=unused-variable; doesn't understand singledispatch @@ -205,28 +156,28 @@ def ast_from_module(module, built_objects, parent_module, name=None, parent=None def ast_from_class(cls, built_objects, module, name=None, parent=None): inspected_module = inspect.getmodule(cls) if inspected_module is not None and inspected_module is not module: - return nodes.ImportFrom(fromname= - getattr(inspected_module, '__name__', None), - names=[[cls.__name__, name]], - parent=parent) + return (nodes.ImportFrom(fromname= + getattr(inspected_module, '__name__', None), + names=[[cls.__name__, name]], + parent=parent),) if id(cls) in built_objects: - # return built_objects[id(cls)] - return nodes.Name(name=name or cls.__name__, parent=parent) + return (nodes.Name(name=name or cls.__name__, parent=parent),) class_node = nodes.ClassDef(name=name or cls.__name__, doc=inspect.getdoc(cls), parent=parent) built_objects[id(cls)] = class_node built_objects = _ChainMap({}, *built_objects.maps) try: bases = [nodes.Name(name=b.__name__, parent=class_node) - for b in inspect.getmro(cls)[1:]] - body = [_ast_from_object(a.object, built_objects, module, a.name, parent=class_node) - for a in _classify_class_attrs(cls) if a.defining_class is cls] + for b in cls.__bases__] + body = [ + t for a in _classify_class_attrs(cls) if a.defining_class is cls and a.name is not '__doc__' + for t in _ast_from_object(a.object, built_objects, module, a.name, parent=class_node)] except AttributeError: bases = () - body = [_ast_from_object(m, built_objects, module, n, parent=class_node) - for n, m in inspect.getmembers(cls)] + body = [t for n, m in inspect.getmembers(cls) if n is not '__doc__' + for t in _ast_from_object(m, built_objects, module, n, parent=class_node)] class_node.postinit(bases=bases, body=body, decorators=(), newstyle=isinstance(cls, type)) - return class_node + return (class_node,) # Old-style classes if six.PY2: _ast_from_object.register(types.ClassType, ast_from_class) @@ -249,12 +200,12 @@ def ast_from_class(cls, built_objects, module, name=None, parent=None): def ast_from_function(func, built_objects, module, name=None, parent=None): inspected_module = inspect.getmodule(func) if inspected_module is not None and inspected_module is not module: - return nodes.ImportFrom(fromname=getattr(inspected_module, '__name__', None), - names=[[func.__name__, name]], - parent=parent) + return (nodes.ImportFrom( + fromname=getattr(inspected_module, '__name__', None), + names=[[func.__name__, name]], + parent=parent),) if id(func) in built_objects: - # return built_objects[id(func)] - return nodes.Name(name=name or func.__name__, parent=parent) + return (nodes.Name(name=name or func.__name__, parent=parent),) func_node = nodes.FunctionDef(name=name or func.__name__, doc=inspect.getdoc(func), parent=parent) @@ -266,7 +217,7 @@ def ast_from_function(func, built_objects, module, name=None, parent=None): # signature() raises these errors for non-introspectable # callables. func_node.postinit(args=nodes.Unknown(parent=func_node), body=[]) - return func_node + return (func_node,) parameters = {k: tuple(g) for k, g in itertools.groupby(signature.parameters.values(), operator.attrgetter('kind'))} @@ -282,9 +233,9 @@ def extract_args(parameters, parent): for parameter in parameters: names.append(parameter.name) if parameter.default is not _Parameter.empty: - defaults.append(_ast_from_object(parameter.default, built_objects, module, parent=parent)) + defaults.extend(_ast_from_object(parameter.default, built_objects, module, parent=parent)) if parameter.annotation is not _Parameter.empty: - annotations.append(_ast_from_object(parameter.annotation, built_objects, module, parent=parent)) + annotations.extend(_ast_from_object(parameter.annotation, built_objects, module, parent=parent)) else: annotations.append(None) return names, defaults, annotations @@ -321,15 +272,15 @@ def extract_vararg(parameter): else: kwargannotation = None if signature.return_annotation is not _Parameter.empty: - returns=_ast_from_object(signature_return_annotation, - built_objects, - module, - parent=func_node) + returns = _ast_from_object(signature_return_annotation, + built_objects, + module, + parent=func_node)[0] args_node.postinit(args, defaults, kwonlyargs, kw_defaults, annotations, kwonly_annotations, varargannotation, kwargannotation) func_node.postinit(args=args_node, body=[]) - return func_node + return (func_node,) BUILTIN_CONTAINERS = {list: nodes.List, set: nodes.Set, frozenset: @@ -349,7 +300,7 @@ def ast_from_builtin_container(container, built_objects, module, name=None, if (id(container) in built_objects and built_objects[id(container)].targets[0].name == name): # return built_objects[id(container)] - return nodes.Name(name=name, parent=parent) + return (nodes.Name(name=name, parent=parent),) if name: parent = nodes.Assign(parent=parent) name_node = nodes.AssignName(name, parent=parent) @@ -365,11 +316,11 @@ def ast_from_builtin_container(container, built_objects, module, name=None, node = container_node built_objects[id(container)] = node container_node.postinit( - elts=[_ast_from_object(i, built_objects, module, parent=node) - for i in container]) + elts=[t for i in container + for t in _ast_from_object(i, built_objects, module, parent=node)]) if name: parent.postinit(targets=[name_node], value=container_node) - return node + return (node,) # pylint: disable=unused-variable; doesn't understand singledispatch @@ -378,8 +329,7 @@ def ast_from_dict(dictionary, built_objects, module, name=None, parent=None): if (id(dictionary) in built_objects and built_objects[id(dictionary)].targets[0].name == name): - # return built_objects[id(dictionary)] - return nodes.Name(name=name, parent=parent) + return (nodes.Name(name=name, parent=parent),) if name: parent = nodes.Assign(parent=parent) name_node = nodes.AssignName(name, parent=parent) @@ -390,12 +340,12 @@ def ast_from_dict(dictionary, built_objects, module, name=None, node = dict_node built_objects[id(dictionary)] = node dict_node.postinit(items=[ - (_ast_from_object(k, built_objects, module, parent=node), - _ast_from_object(v, built_objects, module, parent=node)) - for k, v in dictionary.items()]) + (x, y) for k, v in dictionary.items() + for x, y in zip(_ast_from_object(k, built_objects, module, parent=node), + _ast_from_object(v, built_objects, module, parent=node))]) if name: parent.postinit(targets=[name_node], value=dict_node) - return node + return (node,) if six.PY2: _ast_from_object.register(types.DictProxyType, ast_from_dict) @@ -418,7 +368,7 @@ def ast_from_builtin_number_text_binary(builtin_number_text_binary, built_object node = parent else: node = builtin_number_text_binary_node - return node + return (node,) if six.PY2: _ast_from_object.register(unicode, ast_from_builtin_number_text_binary) @@ -435,9 +385,14 @@ def ast_from_builtin_singleton(builtin_singleton, built_objects, module, name=No name_node = nodes.AssignName(name=name, parent=parent) # This case handles the initial assignment of singletons to names # in the builtins module. It can be triggered in other cases with - # an object that contains a builtin singleton by its own name, but - # there's never any reason to write that kind of code, and even if - # it happens it shouldn't cause any harm. + # an object that contains a builtin singleton by its own name, like, + # + # class C: + # None + # + # but there's never any reason to write that kind of code since + # it's a no-op, and even if it happens it shouldn't cause any + # harm. elif name and name == str(builtin_singleton): parent = nodes.ReservedName(name=name, parent=parent) builtin_singleton_node = nodes.NameConstant(value=builtin_singleton, parent=parent) @@ -449,7 +404,7 @@ def ast_from_builtin_singleton(builtin_singleton, built_objects, module, name=No node = parent else: node = builtin_singleton_node - return node + return (node,) @_ast_from_object.register(type(Ellipsis)) def ast_from_ellipsis(ellipsis, built_objects, module, name=None, parent=None): @@ -467,7 +422,7 @@ def ast_from_ellipsis(ellipsis, built_objects, module, name=None, parent=None): node = parent else: node = ellipsis_node - return node + return (node,) # @scoped_nodes.get_locals.register(Builtins) @@ -490,20 +445,12 @@ def ast_from_ellipsis(ellipsis, built_objects, module, name=None, parent=None): types.BuiltinFunctionType, types.ModuleType)) -# BUILTIN_TYPES = {type(None): 'NoneType', -# type(NotImplemented): 'NotImplementedType', -# types.GeneratorType: 'GeneratorType', -# types.FunctionType: 'FunctionType', -# types.MethodType: 'MethodType', -# types.BuiltinFunctionType: 'BuiltinFunctionType', -# types.ModuleType: 'ModuleType'} - # Initialize the built_objects map for the builtins mock AST to ensure # that the types are included as Name nodes, not explicit ASTs. built_objects = _ChainMap({t: True for t in BUILTIN_TYPES}) astroid_builtin = _ast_from_object(six.moves.builtins, _ChainMap({t: True for t in BUILTIN_TYPES}), - six.moves.builtins) + six.moves.builtins)[0] astroid_builtins = astroid_builtin for builtin_type in BUILTIN_TYPES: @@ -511,8 +458,26 @@ def ast_from_ellipsis(ellipsis, built_objects, module, name=None, parent=None): # AST for it is created by _ast_from_object. del built_objects[builtin_type] class_node = _ast_from_object(builtin_type, built_objects, - six.moves.builtins) + six.moves.builtins)[0] astroid_builtins.body.append(class_node) class_node.parent = astroid_builtins bases.Generator._proxied = astroid_builtins.getattr(types.GeneratorType.__name__)[0] + +# def _io_discrepancy(member): +# # _io module names itself `io`: http://bugs.python.org/issue18602 +# member_self = getattr(member, '__self__', None) +# return (member_self and +# inspect.ismodule(member_self) and +# member_self.__name__ == '_io' and +# member.__module__ == 'io') + +# def _add_dunder_class(func, member): +# """Add a __class__ member to the given func node, if we can determine it.""" +# python_cls = member.__class__ +# cls_name = getattr(python_cls, '__name__', None) +# if not cls_name: +# return +# bases = [ancestor.__name__ for ancestor in python_cls.__bases__] +# ast_klass = build_class(cls_name, bases, python_cls.__doc__) +# func.instance_attrs['__class__'] = [ast_klass] diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index 5bd900b3c0..7bbaeeddca 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -1035,9 +1035,6 @@ def with_metaclass(meta, base=object): class ClassWithMeta(with_metaclass(type)): #@ pass """) - # print(klass.repr_tree()) - # print(klass.locals) - # print(tuple(klass.ancestors())) self.assertEqual( ['NewBase', 'object'], [base.name for base in klass.ancestors()]) From 420608d1b40791167ad43e627e1ca762d5e1dbe9 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Mon, 9 Nov 2015 14:26:22 -0500 Subject: [PATCH 076/312] With the tox bug fixed, it's now possible to use one tox.ini for all testing --- tox.ini | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/tox.ini b/tox.ini index 30338e3901..bfbe575f64 100644 --- a/tox.ini +++ b/tox.ini @@ -1,12 +1,9 @@ [tox] -# Official list -# envlist = py27, py33, py34, py35, pypy, jython, pylint +envlist = py27, py33, py34, py35, pypy, jython, pylint +skip_missing_interpreters = true -# drone.io -# envlist = py27, py33, pylint - -# For testing off drone.io---please don't delete. -envlist = py27, py34, pypy, pylint +[testenv:pylint] +commands = pylint -rn --rcfile={toxinidir}/pylintrc {envsitepackagesdir}/astroid [testenv] deps = From 05d360c57d866207a258c96fa6476e58b889df4b Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Mon, 9 Nov 2015 15:36:14 -0500 Subject: [PATCH 077/312] Fix str/unicode method inference, prevent ast_from_object from double-counting __doc__ by special-casing special attributes, and include tox.ini improvements --- astroid/brain/brain_builtin_inference.py | 36 ++++++++++++++---------- astroid/raw_building.py | 30 ++++++++++++++++---- astroid/scoped_nodes.py | 27 +++++++++--------- tox.ini | 14 ++------- 4 files changed, 62 insertions(+), 45 deletions(-) diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index 196f65880b..3d6e51cb0b 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -1,8 +1,8 @@ """Astroid hooks for various builtins.""" -from functools import partial +import functools import sys -from textwrap import dedent +import textwrap import six from astroid import (MANAGER, UseInferenceDefault, AttributeInferenceError, @@ -20,7 +20,7 @@ def _extend_str(class_node, rvalue): # TODO(cpopa): this approach will make astroid to believe # that some arguments can be passed by keyword, but # unfortunately, strings and bytes don't accept keyword arguments. - code = dedent(''' + code = textwrap.dedent(''' class whatever(object): def join(self, iterable): return {rvalue} @@ -64,7 +64,15 @@ def ljust(self, width, fillchar=None): code = code.format(rvalue=rvalue) fake = AstroidBuilder(MANAGER).string_build(code)['whatever'] for method in fake.mymethods(): - class_node.locals[method.name] = [method] + # TODO: remove this ugly hack by actually handling version + # differences correctly. + try: + # Find the index where the method in question is located in + # the mock AST's body. + index = class_node.body.index(class_node.locals[method.name][0]) + class_node.body[index] = method + except IndexError: + class_node.body.append(method) method.parent = class_node def extend_builtins(class_transforms): @@ -74,13 +82,11 @@ def extend_builtins(class_transforms): transform(builtin_ast[class_name]) if sys.version_info > (3, 0): - extend_builtins({'bytes': partial(_extend_str, rvalue="b''"), - 'str': partial(_extend_str, rvalue="''")}) + extend_builtins({'bytes': functools.partial(_extend_str, rvalue="b''"), + 'str': functools.partial(_extend_str, rvalue="''")}) else: - # TODO - pass - # extend_builtins({'str': partial(_extend_str, rvalue="''"), - # 'unicode': partial(_extend_str, rvalue="u''")}) + extend_builtins({'str': functools.partial(_extend_str, rvalue="''"), + 'unicode': functools.partial(_extend_str, rvalue="u''")}) def register_builtin_transform(transform, builtin_name): @@ -156,7 +162,7 @@ def _generic_transform(arg, klass, iterables, build_elts): def _infer_builtin(node, context, klass=None, iterables=None, build_elts=None): - transform_func = partial( + transform_func = functools.partial( _generic_transform, klass=klass, iterables=iterables, @@ -165,25 +171,25 @@ def _infer_builtin(node, context, return _generic_inference(node, context, klass, transform_func) # pylint: disable=invalid-name -infer_tuple = partial( +infer_tuple = functools.partial( _infer_builtin, klass=nodes.Tuple, iterables=(nodes.List, nodes.Set, objects.FrozenSet), build_elts=tuple) -infer_list = partial( +infer_list = functools.partial( _infer_builtin, klass=nodes.List, iterables=(nodes.Tuple, nodes.Set, objects.FrozenSet), build_elts=list) -infer_set = partial( +infer_set = functools.partial( _infer_builtin, klass=nodes.Set, iterables=(nodes.List, nodes.Tuple, objects.FrozenSet), build_elts=set) -infer_frozenset = partial( +infer_frozenset = functools.partial( _infer_builtin, klass=objects.FrozenSet, iterables=(nodes.List, nodes.Tuple, nodes.Set, objects.FrozenSet), diff --git a/astroid/raw_building.py b/astroid/raw_building.py index d1fd90e6d9..85e1662394 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -94,6 +94,8 @@ def ast_from_object(object_, name=None): inspect.getmodule(object_), name)[0] +INSTANCE_SPECIAL_ATTRIBUTES = frozenset(('__dict__', '__class__')) + @_singledispatch def _ast_from_object(instance, built_objects, module, name=None, parent=None): # Since this ultimately inherits from object but not any type, @@ -107,9 +109,12 @@ def _ast_from_object(instance, built_objects, module, name=None, parent=None): # TODO: remove this hack if isinstance(class_node, nodes.ClassDef): - # Take the set difference of instance and class attributes + # Take the set difference of instance and class attributes; + # maybe consider using __dict__, but the problem is that not + # all instances have a __dict__. for name in set(dir(instance)) - set(dir(cls)): - class_node.instance_attrs[name].append(getattr(instance, name)) + if name not in INSTANCE_SPECIAL_ATTRIBUTES: + class_node.instance_attrs[name].append(getattr(instance, name)) # Create an instance of the class we just created an AST for. result.append(nodes.InterpreterObject(name=name, object_=class_node.instantiate_class(), parent=parent)) @@ -117,6 +122,8 @@ def _ast_from_object(instance, built_objects, module, name=None, parent=None): return result +MODULE_SPECIAL_ATTRIBUTES = frozenset(('__name__', '__doc__', '__file__', '__path__', '__dict__')) + # pylint: disable=unused-variable; doesn't understand singledispatch @_ast_from_object.register(types.ModuleType) def ast_from_module(module, built_objects, parent_module, name=None, parent=None): @@ -143,14 +150,19 @@ def ast_from_module(module, built_objects, parent_module, name=None, parent=None built_objects[id(module)] = module_node built_objects = _ChainMap({}, *built_objects.maps) MANAGER.cache_module(module_node) - body = [t for n, m in inspect.getmembers(module) + body = [t for n, m in inspect.getmembers(module) if n not in MODULE_SPECIAL_ATTRIBUTES for t in _ast_from_object(m, built_objects, module, n, module_node)] module_node.postinit(body=body) return (module_node,) +CLASS_SPECIAL_ATTRIBUTES = frozenset(('__name__', '__module__', '__dict__', '__bases__', '__doc__', '__qualname__', '__mro__', '__class__')) +# __class__ is only necessary because currently GetSetDescriptor and +# MemberDescriptorType are conflated with classes. + # pylint: disable=unused-variable; doesn't understand singledispatch @_ast_from_object.register(type) +# Properly speaking I think these are instances of a type, not a type. @_ast_from_object.register(types.GetSetDescriptorType) @_ast_from_object.register(types.MemberDescriptorType) def ast_from_class(cls, built_objects, module, name=None, parent=None): @@ -169,11 +181,11 @@ def ast_from_class(cls, built_objects, module, name=None, parent=None): bases = [nodes.Name(name=b.__name__, parent=class_node) for b in cls.__bases__] body = [ - t for a in _classify_class_attrs(cls) if a.defining_class is cls and a.name is not '__doc__' + t for a in _classify_class_attrs(cls) if a.defining_class is cls and a.name not in CLASS_SPECIAL_ATTRIBUTES for t in _ast_from_object(a.object, built_objects, module, a.name, parent=class_node)] except AttributeError: bases = () - body = [t for n, m in inspect.getmembers(cls) if n is not '__doc__' + body = [t for n, m in inspect.getmembers(cls) if n not in CLASS_SPECIAL_ATTRIBUTES for t in _ast_from_object(m, built_objects, module, n, parent=class_node)] class_node.postinit(bases=bases, body=body, decorators=(), newstyle=isinstance(cls, type)) @@ -183,6 +195,11 @@ def ast_from_class(cls, built_objects, module, name=None, parent=None): _ast_from_object.register(types.ClassType, ast_from_class) +# Not used at the moment +FUNCTION_SPECIAL_ATTRIBUTES = frozenset(('__doc__', '__name__', '__qualname__', '__module__', '__defaults__', '__code__', '__globals__', '__dict__', '__closure__', '__annotations__', '__kwdefaults__')) + + + # pylint: disable=unused-variable; doesn't understand singledispatch # These two types are the same on CPython but not necessarily the same @@ -280,6 +297,9 @@ def extract_vararg(parameter): annotations, kwonly_annotations, varargannotation, kwargannotation) func_node.postinit(args=args_node, body=[]) + for name in set(dir(func)) - set(dir(type(func))): + if name not in FUNCTION_SPECIAL_ATTRIBUTES: + func_node.instance_attrs[name].append(getattr(func, name)) return (func_node,) diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index 8dc418f53d..1d55029d65 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -1184,7 +1184,8 @@ def postinit(self, bases, body, decorators, newstyle=None, metaclass=None): @property def locals(self): - return get_locals(self) + # return get_locals(self) + return MappingProxyType(get_locals(self)) # @property # def instance_attrs(self): @@ -1870,24 +1871,24 @@ def not_scoped_node(node): def scoped_node(node): locals_ = collections.defaultdict(list) for n in node.get_children(): - _get_locals(n, locals_) + _get_locals(n, locals_, node) return locals_ @_singledispatch -def _get_locals(node, locals_): +def _get_locals(node, locals_, root): raise TypeError('Non-astroid object in an astroid AST: %s' % type(node)) # pylint: disable=unused-variable; doesn't understand singledispatch @_get_locals.register(node_classes.NodeNG) -def locals_generic(node, locals_): +def locals_generic(node, locals_, root): '''Generic nodes don't create name bindings or scopes.''' for n in node.get_children(): - _get_locals(n, locals_) + _get_locals(n, locals_, root) # # pylint: disable=unused-variable; doesn't understand singledispatch @_get_locals.register(LocalsDictNodeNG) -def locals_new_scope(node, locals_): +def locals_new_scope(node, locals_, root): '''These nodes start a new scope, so terminate recursion here.''' # pylint: disable=unused-variable; doesn't understand singledispatch @@ -1895,28 +1896,28 @@ def locals_new_scope(node, locals_): @_get_locals.register(node_classes.DelName) @_get_locals.register(FunctionDef) @_get_locals.register(ClassDef) -def locals_name(node, locals_): +def locals_name(node, locals_, root): '''These nodes add a name to the local variables. AssignName and DelName have no children while FunctionDef and ClassDef start a new scope so shouldn't be recursed into.''' locals_[node.name].append(node) @_get_locals.register(node_classes.InterpreterObject) -def locals_empty(node, locals_): +def locals_interpreter_object(node, locals_, root): '''InterpreterObjects add an object to the local variables under a specified name.''' if node.name: locals_[node.name].append(node) @_get_locals.register(node_classes.ReservedName) -def locals_reserved_name(node, locals_): +def locals_reserved_name(node, locals_, root): '''InterpreterObjects add an object to the local variables under a specified name.''' locals_[node.name].append(node.value) # pylint: disable=unused-variable; doesn't understand singledispatch @_get_locals.register(node_classes.Arguments) -def locals_arguments(node, locals_): +def locals_arguments(node, locals_, root): '''Other names assigned by functions have AssignName nodes that are children of an Arguments node.''' if node.vararg: @@ -1924,18 +1925,18 @@ def locals_arguments(node, locals_): if node.kwarg: locals_[node.kwarg].append(node) for n in node.get_children(): - _get_locals(n, locals_) + _get_locals(n, locals_, root) # pylint: disable=unused-variable; doesn't understand singledispatch @_get_locals.register(node_classes.Import) -def locals_import(node, locals_): +def locals_import(node, locals_, root): for name, asname in node.names: name = asname or name locals_[name.split('.')[0]].append(node) # pylint: disable=unused-variable; doesn't understand singledispatch @_get_locals.register(node_classes.ImportFrom) -def locals_import_from(node, locals_): +def locals_import_from(node, locals_, root): # Don't add future imports to locals. if node.modname == '__future__': return diff --git a/tox.ini b/tox.ini index cac7a6bd4e..bfbe575f64 100644 --- a/tox.ini +++ b/tox.ini @@ -1,23 +1,13 @@ [tox] -# Official list -# envlist = py27, py33, py34, py35, pypy, jython, pylint - -# drone.io -# envlist = py27, py33, pylint - -# For testing off drone.io---please don't delete. -# envlist = py27, py34, pypy, pylint -envlist = py27, py34 +envlist = py27, py33, py34, py35, pypy, jython, pylint +skip_missing_interpreters = true [testenv:pylint] commands = pylint -rn --rcfile={toxinidir}/pylintrc {envsitepackagesdir}/astroid [testenv] deps = - # Temporary only - py27,pypy,jython: dictproxyhack py27,py33,pypy,jython: enum34 - py27,pypy,jython: funcsigs lazy-object-proxy nose py27,py33,py34,py35: numpy From a790fad442a160cd8609f451dca7c04e32a52aaa Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Fri, 13 Nov 2015 15:12:01 +0200 Subject: [PATCH 078/312] Move the bootstrap of the builtins from Manager.clear_cache into testutils It's needed only by tests. --- astroid/manager.py | 9 +-------- astroid/test_utils.py | 12 +++++++++++- astroid/tests/unittest_manager.py | 4 +++- astroid/tests/unittest_regrtest.py | 6 ++++-- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/astroid/manager.py b/astroid/manager.py index 300fd6d27e..2c7dddf0bb 100644 --- a/astroid/manager.py +++ b/astroid/manager.py @@ -267,13 +267,6 @@ def cache_module(self, module): """Cache a module if no module with the same name is known yet.""" self.astroid_cache.setdefault(module.name, module) - def clear_cache(self, astroid_builtin=None): + def clear_cache(self): # XXX clear transforms self.astroid_cache.clear() - # force bootstrap again, else we may ends up with cache inconsistency - # between the manager and CONST_PROXY, making - # unittest_lookup.LookupTC.test_builtin_lookup fail depending on the - # test order - import astroid.raw_building - astroid.raw_building._astroid_bootstrapping( - astroid_builtin=astroid_builtin) diff --git a/astroid/test_utils.py b/astroid/test_utils.py index 4ebb4ba9a8..023a5f10d9 100644 --- a/astroid/test_utils.py +++ b/astroid/test_utils.py @@ -2,8 +2,9 @@ import functools import sys -from astroid import nodes from astroid import builder +from astroid import raw_building +from astroid import nodes from astroid import util @@ -202,3 +203,12 @@ def new_f(self, *args, **kwargs): def get_name_node(start_from, name, index=0): return [n for n in start_from.nodes_of_class(nodes.Name) if n.name == name][index] + + +def bootstrap(astroid_builtin=None): + # force bootstrap again, else we may ends up with cache inconsistency + # between the manager and CONST_PROXY, making + # unittest_lookup.LookupTC.test_builtin_lookup fail depending on the + # test order + raw_building._astroid_bootstrapping( + astroid_builtin=astroid_builtin) diff --git a/astroid/tests/unittest_manager.py b/astroid/tests/unittest_manager.py index 6fc0776286..7438429d76 100644 --- a/astroid/tests/unittest_manager.py +++ b/astroid/tests/unittest_manager.py @@ -25,6 +25,7 @@ from astroid import exceptions from astroid import manager from astroid.tests import resources +from astroid import test_utils BUILTINS = six.moves.builtins.__name__ @@ -46,7 +47,8 @@ class AstroidManagerTest(resources.SysPathSetup, def setUp(self): super(AstroidManagerTest, self).setUp() self.manager = manager.AstroidManager() - self.manager.clear_cache(self._builtins) # take care of borg + self.manager.clear_cache() # take care of borg + test_utils.bootstrap(self._builtins) def test_ast_from_file(self): filepath = unittest.__file__ diff --git a/astroid/tests/unittest_regrtest.py b/astroid/tests/unittest_regrtest.py index 87664dfcbb..b6644ac131 100644 --- a/astroid/tests/unittest_regrtest.py +++ b/astroid/tests/unittest_regrtest.py @@ -26,7 +26,7 @@ from astroid import exceptions from astroid.raw_building import build_module from astroid.manager import AstroidManager -from astroid.test_utils import require_version, extract_node +from astroid.test_utils import require_version, extract_node, bootstrap from astroid.tests import resources from astroid import transforms @@ -46,7 +46,8 @@ def tearDown(self): # Since we may have created a brainless manager, leading # to a new cache builtin module and proxy classes in the constants, # clear out the global manager cache. - MANAGER.clear_cache(self._builtins) + MANAGER.clear_cache() + bootstrap(self._builtins) MANAGER.always_load_extensions = False sys.path.pop(0) sys.path_importer_cache.pop(resources.find('data'), None) @@ -61,6 +62,7 @@ def brainless_manager(self): manager._mod_file_cache = {} manager._transform = transforms.TransformVisitor() manager.clear_cache() # trigger proper bootstraping + bootstrap() return manager def test_module_path(self): From fa5b7f346e9121ae715a1548d35f6c55c325f2b8 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Sat, 14 Nov 2015 00:06:25 -0500 Subject: [PATCH 079/312] Finish most needed changes for building ASTs from runtime objects. * Add handling to generate ClassInstance nodes for instance objects. * Fix a number of special cases for building mock ASTs. * Fix an infinite recursion on Python 2 by using the new future_imports property to look up whether absolute_import is active. * Do some minor reorganization on how the builtins module is set up and accessed from various places, mostly to make debugging easier. This is in lieu of a more serious reorganization which needs to happen. * Remove Manager.infer_ast_from_something and Manager.ast_from_class as superceded by the new functions in raw_building. --- astroid/__init__.py | 1 + astroid/as_string.py | 2 +- astroid/bases.py | 3 + astroid/brain/brain_builtin_inference.py | 3 +- astroid/builder.py | 3 +- astroid/helpers.py | 16 +- astroid/inference.py | 3 +- astroid/manager.py | 68 +--- astroid/mixins.py | 2 - astroid/node_classes.py | 4 +- astroid/objects.py | 3 +- astroid/raw_building.py | 360 ++++++++++-------- astroid/scoped_nodes.py | 12 +- astroid/tests/testdata/python2/data/module.py | 1 - astroid/tests/testdata/python3/data/module.py | 1 - astroid/tests/unittest_builder.py | 11 +- astroid/tests/unittest_inference.py | 1 + astroid/tests/unittest_manager.py | 26 -- astroid/tests/unittest_raw_building.py | 28 ++ tox.ini | 2 + 20 files changed, 280 insertions(+), 270 deletions(-) diff --git a/astroid/__init__.py b/astroid/__init__.py index 2a56e95786..0c56296b10 100644 --- a/astroid/__init__.py +++ b/astroid/__init__.py @@ -60,6 +60,7 @@ # more stuff available from astroid import raw_building +from astroid.raw_building import builtins_ast as builtins from astroid.bases import Instance, BoundMethod, UnboundMethod from astroid.node_classes import are_exclusive, unpack_infer from astroid.scoped_nodes import builtin_lookup diff --git a/astroid/as_string.py b/astroid/as_string.py index f907c489f4..a83dff6518 100644 --- a/astroid/as_string.py +++ b/astroid/as_string.py @@ -184,7 +184,7 @@ def visit_expr(self, node): """return an astroid.Discard node as string""" return node.value.accept(self) - def visit_emptynode(self, node): + def visit_interpreterobject(self, node): """dummy method for visiting an Empty node""" return '' diff --git a/astroid/bases.py b/astroid/bases.py index 1f720337d4..3cd296b63d 100644 --- a/astroid/bases.py +++ b/astroid/bases.py @@ -413,6 +413,9 @@ class Generator(Instance): Proxied class is set once for all in raw_building. """ + def __init__(self, parent): + self.parent = parent + def callable(self): return False diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index 3d6e51cb0b..d3523b9a65 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -85,7 +85,8 @@ def extend_builtins(class_transforms): extend_builtins({'bytes': functools.partial(_extend_str, rvalue="b''"), 'str': functools.partial(_extend_str, rvalue="''")}) else: - extend_builtins({'str': functools.partial(_extend_str, rvalue="''"), + # TODO: what about unicode_literals? This is hopelessly broken. + extend_builtins({'bytes': functools.partial(_extend_str, rvalue="''"), # Ugly hack to get it working for now. 'unicode': functools.partial(_extend_str, rvalue="u''")}) diff --git a/astroid/builder.py b/astroid/builder.py index b6aad0909d..a17fd52ee3 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -31,10 +31,11 @@ from astroid import manager from astroid import modutils from astroid import nodes -from astroid import raw_building from astroid import rebuilder from astroid import util +raw_building = util.lazy_import('raw_building') + def _parse(string): return compile(string, "", 'exec', _ast.PyCF_ONLY_AST) diff --git a/astroid/helpers.py b/astroid/helpers.py index 8698a25ed9..55193a5892 100644 --- a/astroid/helpers.py +++ b/astroid/helpers.py @@ -43,26 +43,26 @@ def _object_type(node, context=None): if metaclass: yield metaclass continue - yield raw_building.astroid_builtins.getattr('type')[0] + yield raw_building.builtins_ast.getattr('type')[0] elif isinstance(inferred, (scoped_nodes.Lambda, bases.UnboundMethod)): if isinstance(inferred, scoped_nodes.Lambda): - if inferred.root() is raw_building.astroid_builtins: - yield raw_building.astroid_builtins[types.BuiltinFunctionType.__name__] + if inferred.root() is raw_building.builtins_ast: + yield raw_building.builtins_ast[types.BuiltinFunctionType.__name__] else: - yield raw_building.astroid_builtins[types.FunctionType.__name__] + yield raw_building.builtins_ast[types.FunctionType.__name__] elif isinstance(inferred, bases.BoundMethod): - yield raw_building.astroid_builtins[types.MethodType.__name__] + yield raw_building.builtins_ast[types.MethodType.__name__] elif isinstance(inferred, bases.UnboundMethod): if six.PY2: - yield raw_building.astroid_builtins[types.MethodType.__name__] + yield raw_building.builtins_ast[types.MethodType.__name__] else: - yield raw_building.astroid_builtins[types.FunctionType.__name__] + yield raw_building.builtins_ast[types.FunctionType.__name__] else: raise InferenceError('Function {func!r} inferred from {node!r} ' 'has no identifiable type.', node=node, func=inferred, contex=context) elif isinstance(inferred, scoped_nodes.Module): - yield raw_building.astroid_builtins[types.ModuleType.__name__] + yield raw_building.builtins_ast[types.ModuleType.__name__] else: yield inferred._proxied diff --git a/astroid/inference.py b/astroid/inference.py index 0b3f556860..1ed8022901 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -711,8 +711,7 @@ def infer_interpreter_object(self, context=None): yield util.Uninferable else: try: - for inferred in MANAGER.infer_ast_from_something(self.object, - context=context): + for inferred in self.object.infer(): yield inferred except exceptions.AstroidError: yield util.Uninferable diff --git a/astroid/manager.py b/astroid/manager.py index 1f66ddfb93..2365bf0a65 100644 --- a/astroid/manager.py +++ b/astroid/manager.py @@ -32,6 +32,7 @@ from astroid import transforms from astroid import util +builder = util.lazy_import('builder') def safe_repr(obj): try: @@ -85,16 +86,14 @@ def ast_from_file(self, filepath, modname=None, fallback=True, source=False): if modname in self.astroid_cache and self.astroid_cache[modname].source_file == filepath: return self.astroid_cache[modname] if source: - from astroid.builder import AstroidBuilder - return AstroidBuilder(self).file_build(filepath, modname) + return builder.AstroidBuilder(self).file_build(filepath, modname) elif fallback and modname: return self.ast_from_module_name(modname) raise exceptions.AstroidBuildingException( 'Unable to build an AST for {path}.', path=filepath) def _build_stub_module(self, modname): - from astroid.builder import AstroidBuilder - return AstroidBuilder(self).string_build('', modname) + return builder.AstroidBuilder(self).string_build('', modname) def _can_load_extension(self, modname): if self.always_load_extensions: @@ -153,8 +152,7 @@ def ast_from_module_name(self, modname, context_file=None): def zip_import_data(self, filepath): if zipimport is None: return None - from astroid.builder import AstroidBuilder - builder = AstroidBuilder(self) + astroid_builder = builder.AstroidBuilder(self) for ext in ('.zip', '.egg'): try: eggpath, resource = filepath.rsplit(ext + os.path.sep, 1) @@ -165,7 +163,7 @@ def zip_import_data(self, filepath): zmodname = resource.replace(os.path.sep, '.') if importer.is_package(resource): zmodname = zmodname + '.__init__' - module = builder.string_build(importer.get_source(resource), + module = astroid_builder.string_build(importer.get_source(resource), zmodname, filepath) return module except Exception: # pylint: disable=broad-except @@ -204,55 +202,7 @@ def ast_from_module(self, module, modname=None): return self.ast_from_file(filepath, modname) except AttributeError: pass - from astroid.builder import AstroidBuilder - return AstroidBuilder(self).module_build(module, modname) - - def ast_from_class(self, klass, modname=None): - """get astroid for the given class""" - if modname is None: - try: - modname = klass.__module__ - except AttributeError: - util.reraise(exceptions.AstroidBuildingException( - 'Unable to get module for class {class_name}.', - cls=klass, class_repr=safe_repr(klass), modname=modname)) - modastroid = self.ast_from_module_name(modname) - return modastroid.getattr(klass.__name__)[0] # XXX - - def infer_ast_from_something(self, obj, context=None): - """infer astroid for the given class""" - if hasattr(obj, '__class__') and not isinstance(obj, type): - klass = obj.__class__ - else: - klass = obj - try: - modname = klass.__module__ - except AttributeError: - util.reraise(exceptions.AstroidBuildingException( - 'Unable to get module for {class_repr}.', - cls=klass, class_repr=safe_repr(klass))) - except Exception as ex: # pylint: disable=broad-except - util.reraise(exceptions.AstroidBuildingException( - 'Unexpected error while retrieving module for {class_repr}:\n' - '{error}', cls=klass, class_repr=safe_repr(klass), error=ex)) - try: - name = klass.__name__ - except AttributeError: - util.reraise(exceptions.AstroidBuildingException( - 'Unable to get name for {class_repr}:\n', - cls=klass, class_repr=safe_repr(klass))) - except Exception as ex: # pylint: disable=broad-except - util.reraise(exceptions.AstroidBuildingException( - 'Unexpected error while retrieving name for {class_repr}:\n' - '{error}', cls=klass, class_repr=safe_repr(klass), error=ex)) - # take care, on living object __module__ is regularly wrong :( - modastroid = self.ast_from_module_name(modname) - if klass is obj: - for inferred in modastroid.igetattr(name, context): - yield inferred - else: - for inferred in modastroid.igetattr(name, context): - yield inferred.instantiate_class() + return builder.AstroidBuilder(self).module_build(module, modname) def register_failed_import_hook(self, hook): """Registers a hook to resolve imports that cannot be found otherwise. @@ -275,7 +225,5 @@ def clear_cache(self, astroid_builtin=None): # between the manager and CONST_PROXY, making # unittest_lookup.LookupTC.test_builtin_lookup fail depending on the # test order - import astroid.raw_building - # astroid.raw_building._astroid_bootstrapping( - # astroid_builtin=astroid_builtin) - astroid.raw_building.ast_from_object(six.moves.builtins) + from astroid import raw_building + self.astroid_cache[six.moves.builtins.__name__] = raw_building.builtins_ast diff --git a/astroid/mixins.py b/astroid/mixins.py index 5557f3c8de..b7e1ae7ff9 100644 --- a/astroid/mixins.py +++ b/astroid/mixins.py @@ -18,8 +18,6 @@ """This module contains some mixins for the different nodes. """ -import warnings - from astroid import decorators from astroid import exceptions from astroid import util diff --git a/astroid/node_classes.py b/astroid/node_classes.py index 2776a4388e..0da9b21acc 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -1303,9 +1303,9 @@ def from_constants(cls, items=None): if items is None: node.items = [] else: - node.items = [(raw_building.ast_from_scalar(k, {}, None, + node.items = [(raw_building.ast_from_builtin_number_text_binary(k, {}, None, parent=node)[0], - raw_building.ast_from_scalar(v, {}, None, + raw_building.ast_from_builtin_number_text_binary(v, {}, None, parent=node)[0]) for k, v in items.items()] return node diff --git a/astroid/objects.py b/astroid/objects.py index b679b9b00f..691eef7e65 100644 --- a/astroid/objects.py +++ b/astroid/objects.py @@ -30,11 +30,12 @@ from astroid import bases from astroid import decorators from astroid import exceptions -from astroid import MANAGER from astroid import node_classes from astroid import scoped_nodes from astroid import util +manager = util.lazy_import('manager') +MANAGER = manager.AstroidManager() BUILTINS = six.moves.builtins.__name__ diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 85e1662394..c52bf852ca 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -23,30 +23,32 @@ import inspect import itertools import operator -import os import sys import types -import warnings # This is a useful function for introspecting class attributes in # inspect that is for some reason not exported. from inspect import classify_class_attrs as _classify_class_attrs -# ChainMap was made available in Python 3, but a precursor lives in -# ConfigParser in 2.7. try: from collections import ChainMap as _ChainMap except ImportError: - from ConfigParser import _Chainmap - # TODO: complete this - class _ChainMap(_Chainmap): + class _ChainMap(collections.MutableMapping): def __init__(self, *maps): - super(_ChainMap, self).__init__(*maps) - self.maps = self._maps + self.maps = list(maps) or [{}] + def __getitem__(self, key): + for mapping in self.maps: + if key in mapping: + return mapping[key] + raise KeyError(key) def __setitem__(self, key, value): - self._maps[0][key] = value + self.maps[0][key] = value def __delitem__(self, key): - del self._maps[0][key] + del self.maps[0][key] + def __len__(self): + return len(set().union(*self.maps)) + def __iter__(self): + return iter(set().union(*self.maps)) try: from functools import singledispatch as _singledispatch @@ -62,11 +64,12 @@ def __delitem__(self, key): from astroid import bases from astroid import manager -from astroid import node_classes from astroid import nodes +from astroid import objects from astroid import scoped_nodes from astroid import util + MANAGER = manager.AstroidManager() # This is a type used for some unbound methods implemented in C on @@ -83,13 +86,46 @@ def __delitem__(self, key): WrapperDescriptorType = type(object.__getattribute__) # Both the reprs, for objects of this type and the type itself, refer -# to this type as "method-wrapper". It's used for bound methods -# corresponding to unbound methods with the wrapper_descriptor type on -# CPython. +# to this type as "method-wrapper". On CPython it's used for bound +# methods corresponding to unbound methods with the wrapper_descriptor +# type. MethodWrapperType = type(object().__getattribute__) def ast_from_object(object_, name=None): + '''Returns a mock AST for a Python object. + + This function is intended for analyzing objects that aren't + implemented in Python or that are created dynamically in ways that + static analysis can't capture. The mock AST this function returns + has both more and less information than an AST created by + processing Python code would: it includes special nodes + representing objects only created at runtime but can't, for + instance, know anything about the action of functions on their + arguments. It uses InterpreterObject nodes as containers to + insert astroid objects representing runtime-only objects like + ClassInstances and FrozenSets. ReservedNames are for special + names in the builtins module and Unknown nodes are used in place + of Arguments nodes for functions who arguments can't be + introspected. + + This function returns an AST whose root is a node of a type + appropriate to the object it was passed. In some cases, this + object may have links to other objects. The only case where an + AST will necessarily have a Module node as its root is when it's + called on a Module. + + Args: + object_ (Any): Any Python object. + + Returns: + An AST representing the object and its attributes, methods, etc. + + Raises: + TypeError: When called on an instance where it's not possible + to construct an appropriate AST. + + ''' return _ast_from_object(object_, _ChainMap({}), inspect.getmodule(object_), name)[0] @@ -98,82 +134,76 @@ def ast_from_object(object_, name=None): @_singledispatch def _ast_from_object(instance, built_objects, module, name=None, parent=None): + # Since all ClassInstances pointing to the same ClassDef are + # identical, they can all share the same node. + if id(instance) in built_objects: + return (built_objects[id(instance)],) + # Since this ultimately inherits from object but not any type, # it's presumably an instance of some kind. cls = type(instance) - result = [] - result.extend(_ast_from_object(cls, built_objects, module, parent=parent)) - - # TODO: is this guaranteed? Should verify. - class_node = result[0] - - # TODO: remove this hack - if isinstance(class_node, nodes.ClassDef): - # Take the set difference of instance and class attributes; - # maybe consider using __dict__, but the problem is that not - # all instances have a __dict__. - for name in set(dir(instance)) - set(dir(cls)): - if name not in INSTANCE_SPECIAL_ATTRIBUTES: - class_node.instance_attrs[name].append(getattr(instance, name)) - - # Create an instance of the class we just created an AST for. - result.append(nodes.InterpreterObject(name=name, object_=class_node.instantiate_class(), parent=parent)) - result.append(nodes.InterpreterObject(name=name, object_=instance, parent=parent)) - return result + result = list(_ast_from_object(cls, built_objects, module, parent=parent)) + + # The type of an instance should always be some kind of type, and + # ast_from_class should always return a sequence of nodes ending + # with a ClassDef, ImportFrom, or Name node. + node = result[-1] + + if isinstance(node, nodes.ClassDef): + class_node = node + elif isinstance(node, nodes.ImportFrom): + # Handle ImportFrom chains. + while True: + modname = node.modname + node = MANAGER.ast_from_module_name(modname).getattr(cls.__name__)[0] + if isinstance(node, nodes.ClassDef): + class_node = node + break + elif isinstance(node, nodes.Name): + # A Name node means a ClassDef node already exists somewhere, + # so there's no need to add another one. + class_node = built_objects[id(cls)] + result = [] + else: + raise TypeError('Unexpected node, %s, when calling _ast_from_object on ' + 'the type of %s.' % (node, instance)) + # Take the set difference of instance and class attributes. + for name in set(dir(instance)) - set(dir(cls)): + if name not in INSTANCE_SPECIAL_ATTRIBUTES: + class_node.instance_attrs[name].append(getattr(instance, name)) -MODULE_SPECIAL_ATTRIBUTES = frozenset(('__name__', '__doc__', '__file__', '__path__', '__dict__')) - -# pylint: disable=unused-variable; doesn't understand singledispatch -@_ast_from_object.register(types.ModuleType) -def ast_from_module(module, built_objects, parent_module, name=None, parent=None): - if module is not parent_module: - # This module has been imported into another. - return (nodes.Import([[module.__name__, name]], parent=parent),) - if id(module) in built_objects: - return (nodes.Name(name=name or module.__name__, parent=parent_module),) - try: - source_file = inspect.getsourcefile(module) - except TypeError: - # inspect.getsourcefile raises TypeError for built-in modules. - source_file = None - module_node = nodes.Module( - name=name or module.__name__, - # inspect.getdoc returns None for modules without docstrings like - # Jython Java modules. - doc=inspect.getdoc(module), - source_file=source_file, - package=hasattr(module, '__path__'), - # Assume that if inspect couldn't find a Python source file, it's - # probably not implemented in pure Python. - pure_python=bool(source_file)) - built_objects[id(module)] = module_node - built_objects = _ChainMap({}, *built_objects.maps) - MANAGER.cache_module(module_node) - body = [t for n, m in inspect.getmembers(module) if n not in MODULE_SPECIAL_ATTRIBUTES - for t in _ast_from_object(m, built_objects, module, n, module_node)] - module_node.postinit(body=body) - return (module_node,) + # Create an instance of the class we just created an AST for. + result.append(nodes.InterpreterObject(name=name, object_=class_node.instantiate_class(), parent=parent)) + built_objects[id(instance)] = result[-1] + return result -CLASS_SPECIAL_ATTRIBUTES = frozenset(('__name__', '__module__', '__dict__', '__bases__', '__doc__', '__qualname__', '__mro__', '__class__')) -# __class__ is only necessary because currently GetSetDescriptor and -# MemberDescriptorType are conflated with classes. +CLASS_SPECIAL_ATTRIBUTES = frozenset(('__name__', '__module__', '__dict__', '__bases__', '__doc__', '__qualname__', '__mro__')) # pylint: disable=unused-variable; doesn't understand singledispatch @_ast_from_object.register(type) -# Properly speaking I think these are instances of a type, not a type. -@_ast_from_object.register(types.GetSetDescriptorType) -@_ast_from_object.register(types.MemberDescriptorType) def ast_from_class(cls, built_objects, module, name=None, parent=None): + if id(cls) in built_objects: + return (nodes.Name(name=name or cls.__name__, parent=parent),) inspected_module = inspect.getmodule(cls) - if inspected_module is not None and inspected_module is not module: + # In some situations, a class claims to be from a module but isn't + # available in that module. For example, the quit instance in + # builtins is of type Quitter. On Python 2, this claims its + # module is site, but Quitter isn't in dir(site) or available as + # site.Quitter. On Python3 in the REPL, Quitter claims its module + # is _sitebuiltins, which actually does have a Quitter class + # available. However, when running the tests, Quitter again + # claims its module is site, which still doesn't have it + # available. If this happens, this statement is ignored and an + # appropriate ClassDef node is returned. Arguably, it would be + # possible to check if a class is in a module under another name, + # but does this ever happen? + if inspected_module is not None and inspected_module is not module and hasattr(inspected_module, cls.__name__): return (nodes.ImportFrom(fromname= getattr(inspected_module, '__name__', None), names=[[cls.__name__, name]], parent=parent),) - if id(cls) in built_objects: - return (nodes.Name(name=name or cls.__name__, parent=parent),) class_node = nodes.ClassDef(name=name or cls.__name__, doc=inspect.getdoc(cls), parent=parent) built_objects[id(cls)] = class_node built_objects = _ChainMap({}, *built_objects.maps) @@ -195,10 +225,41 @@ def ast_from_class(cls, built_objects, module, name=None, parent=None): _ast_from_object.register(types.ClassType, ast_from_class) -# Not used at the moment -FUNCTION_SPECIAL_ATTRIBUTES = frozenset(('__doc__', '__name__', '__qualname__', '__module__', '__defaults__', '__code__', '__globals__', '__dict__', '__closure__', '__annotations__', '__kwdefaults__')) +MODULE_SPECIAL_ATTRIBUTES = frozenset(('__name__', '__doc__', '__file__', '__path__', '__dict__')) + +# pylint: disable=unused-variable; doesn't understand singledispatch +@_ast_from_object.register(types.ModuleType) +def ast_from_module(module, built_objects, parent_module, name=None, parent=None): + if id(module) in built_objects: + return (nodes.Name(name=name or module.__name__, parent=parent_module),) + if module is not parent_module: + # This module has been imported into another. + return (nodes.Import([[module.__name__, name]], parent=parent),) + try: + source_file = inspect.getsourcefile(module) + except TypeError: + # inspect.getsourcefile raises TypeError for built-in modules. + source_file = None + module_node = nodes.Module( + name=name or module.__name__, + # inspect.getdoc returns None for modules without docstrings like + # Jython Java modules. + doc=inspect.getdoc(module), + source_file=source_file, + package=hasattr(module, '__path__'), + # Assume that if inspect couldn't find a Python source file, it's + # probably not implemented in pure Python. + pure_python=bool(source_file)) + built_objects[id(module)] = module_node + built_objects = _ChainMap({}, *built_objects.maps) + MANAGER.cache_module(module_node) + body = [t for n, m in inspect.getmembers(module) if n not in MODULE_SPECIAL_ATTRIBUTES + for t in _ast_from_object(m, built_objects, module, n, module_node)] + module_node.postinit(body=body) + return (module_node,) +FUNCTION_SPECIAL_ATTRIBUTES = frozenset(('__doc__', '__name__', '__qualname__', '__module__', '__defaults__', '__code__', '__globals__', '__dict__', '__closure__', '__annotations__', '__kwdefaults__')) # pylint: disable=unused-variable; doesn't understand singledispatch @@ -215,14 +276,14 @@ def ast_from_class(cls, built_objects, module, name=None, parent=None): @_ast_from_object.register(types.FunctionType) @_ast_from_object.register(types.MethodType) def ast_from_function(func, built_objects, module, name=None, parent=None): + if id(func) in built_objects: + return (nodes.Name(name=name or func.__name__, parent=parent),) inspected_module = inspect.getmodule(func) if inspected_module is not None and inspected_module is not module: return (nodes.ImportFrom( fromname=getattr(inspected_module, '__name__', None), names=[[func.__name__, name]], parent=parent),) - if id(func) in built_objects: - return (nodes.Name(name=name or func.__name__, parent=parent),) func_node = nodes.FunctionDef(name=name or func.__name__, doc=inspect.getdoc(func), parent=parent) @@ -304,7 +365,7 @@ def extract_vararg(parameter): BUILTIN_CONTAINERS = {list: nodes.List, set: nodes.Set, frozenset: - nodes.Set, tuple: nodes.Tuple} + objects.FrozenSet, tuple: nodes.Tuple} # pylint: disable=unused-variable; doesn't understand singledispatch @_ast_from_object.register(list) @@ -319,7 +380,6 @@ def ast_from_builtin_container(container, built_objects, module, name=None, ''' if (id(container) in built_objects and built_objects[id(container)].targets[0].name == name): - # return built_objects[id(container)] return (nodes.Name(name=name, parent=parent),) if name: parent = nodes.Assign(parent=parent) @@ -373,6 +433,7 @@ def ast_from_dict(dictionary, built_objects, module, name=None, _ast_from_object.register(types.MappingProxyType, ast_from_dict) +# pylint: disable=unused-variable; doesn't understand singledispatch @_ast_from_object.register(str) @_ast_from_object.register(bytes) @_ast_from_object.register(int) @@ -395,54 +456,44 @@ def ast_from_builtin_number_text_binary(builtin_number_text_binary, built_object _ast_from_object.register(long, ast_from_builtin_number_text_binary) -@_ast_from_object.register(type(None)) -@_ast_from_object.register(type(NotImplemented)) -@_ast_from_object.register(bool) -def ast_from_builtin_singleton(builtin_singleton, built_objects, module, name=None, parent=None): - # A builtin singleton is assigned to a name. - if name and name != str(builtin_singleton): - parent = nodes.Assign(parent=parent) - name_node = nodes.AssignName(name=name, parent=parent) - # This case handles the initial assignment of singletons to names - # in the builtins module. It can be triggered in other cases with - # an object that contains a builtin singleton by its own name, like, - # - # class C: - # None - # - # but there's never any reason to write that kind of code since - # it's a no-op, and even if it happens it shouldn't cause any - # harm. - elif name and name == str(builtin_singleton): - parent = nodes.ReservedName(name=name, parent=parent) - builtin_singleton_node = nodes.NameConstant(value=builtin_singleton, parent=parent) - if name and name != str(builtin_singleton): - parent.postinit(targets=[name_node], value=builtin_singleton_node) - node = parent - elif name and name == str(builtin_singleton): - parent.postinit(value=builtin_singleton_node) - node = parent - else: - node = builtin_singleton_node - return (node,) - -@_ast_from_object.register(type(Ellipsis)) -def ast_from_ellipsis(ellipsis, built_objects, module, name=None, parent=None): - if name and name != str(ellipsis): - parent = nodes.Assign(parent=parent) - name_node = nodes.AssignName(name=name, parent=parent) - elif name and name == str(ellipsis): - parent = nodes.ReservedName(name=name, parent=parent) - ellipsis_node = nodes.Ellipsis(parent=parent) - if name and name != str(ellipsis): - parent.postinit(targets=[name_node], value=ellipsis_node) - node = parent - elif name and name == str(ellipsis): - parent.postinit(value=ellipsis_node) - node = parent - else: - node = ellipsis_node - return (node,) +def ast_from_builtin_singleton_factory(node_class): + def ast_from_builtin_singleton(builtin_singleton, built_objects, module, name=None, parent=None, node_class=node_class): + # A builtin singleton is assigned to a name. + if name and name != str(builtin_singleton): + parent = nodes.Assign(parent=parent) + name_node = nodes.AssignName(name=name, parent=parent) + # This case handles the initial assignment of singletons to names + # in the builtins module. It can be triggered in other cases with + # an object that contains a builtin singleton by its own name, like, + # + # class C: + # None + # + # but there's never any reason to write that kind of code since + # it's a no-op, and even if it happens it shouldn't cause any + # harm. + elif name and name == str(builtin_singleton): + parent = nodes.ReservedName(name=name, parent=parent) + builtin_singleton_node = node_class(value=builtin_singleton, parent=parent) + if name and name != str(builtin_singleton): + parent.postinit(targets=[name_node], value=builtin_singleton_node) + node = parent + elif name and name == str(builtin_singleton): + parent.postinit(value=builtin_singleton_node) + node = parent + else: + node = builtin_singleton_node + return (node,) + return ast_from_builtin_singleton + +BUILTIN_SINGLETONS = {type(None): nodes.NameConstant, + type(NotImplemented): nodes.NameConstant, + bool: nodes.NameConstant, + type(Ellipsis): lambda value=None, parent=None: + nodes.Ellipsis(parent=parent)} + +for singleton_type, node_type in BUILTIN_SINGLETONS.items(): + _ast_from_object.register(singleton_type, ast_from_builtin_singleton_factory(node_type)) # @scoped_nodes.get_locals.register(Builtins) @@ -459,30 +510,33 @@ def ast_from_ellipsis(ellipsis, built_objects, module, name=None, parent=None): # scoped_nodes._get_locals(n, locals_) # return locals_ -BUILTIN_TYPES = frozenset((type(None), type(NotImplemented), - types.GeneratorType, types.FunctionType, - types.MethodType, - types.BuiltinFunctionType, - types.ModuleType)) - -# Initialize the built_objects map for the builtins mock AST to ensure -# that the types are included as Name nodes, not explicit ASTs. -built_objects = _ChainMap({t: True for t in BUILTIN_TYPES}) -astroid_builtin = _ast_from_object(six.moves.builtins, - _ChainMap({t: True for t in BUILTIN_TYPES}), - six.moves.builtins)[0] -astroid_builtins = astroid_builtin - -for builtin_type in BUILTIN_TYPES: - # Now delete each builtin type from built_objects to ensure a real - # AST for it is created by _ast_from_object. - del built_objects[builtin_type] - class_node = _ast_from_object(builtin_type, built_objects, - six.moves.builtins)[0] - astroid_builtins.body.append(class_node) - class_node.parent = astroid_builtins - -bases.Generator._proxied = astroid_builtins.getattr(types.GeneratorType.__name__)[0] + +BUILTIN_TYPES = (types.GetSetDescriptorType, + types.MemberDescriptorType, type(None), type(NotImplemented), + types.GeneratorType, types.FunctionType, types.MethodType, + types.BuiltinFunctionType, types.ModuleType) + +def ast_from_builtins(): + # Initialize the built_objects map for the builtins mock AST to ensure + # that the types are included as Name nodes, not explicit ASTs. + built_objects = _ChainMap({}) + for builtin_type in BUILTIN_TYPES: + built_objects[id(builtin_type)] = _ast_from_object(builtin_type, built_objects, six.moves.builtins)[0] + + builtins_ast = _ast_from_object(six.moves.builtins, + built_objects, + six.moves.builtins)[0] + + for builtin_type in BUILTIN_TYPES: + type_node = built_objects[id(builtin_type)] + builtins_ast.body.append(type_node) + type_node.parent = builtins_ast + + bases.Generator._proxied = builtins_ast.getattr(types.GeneratorType.__name__)[0] + return builtins_ast + +builtins_ast = ast_from_builtins() +astroid_builtin = builtins_ast # def _io_discrepancy(member): # # _io module names itself `io`: http://bugs.python.org/issue18602 diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index 1d55029d65..b22197717f 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -480,10 +480,10 @@ def next_sibling(self): if six.PY2: @decorators_mod.cachedproperty def _absolute_import_activated(self): - for stmt in self.locals.get('absolute_import', ()): - if isinstance(stmt, node_classes.ImportFrom) and stmt.modname == '__future__': - return True - return False + if 'absolute_import' in self.future_imports: + return True + else: + return False else: _absolute_import_activated = True @@ -1002,9 +1002,7 @@ def is_generator(self): def infer_call_result(self, caller, context=None): """infer what a function is returning when called""" if self.is_generator(): - result = bases.Generator() - result.parent = self - yield result + yield bases.Generator(self) return # This is really a gigantic hack to work around metaclass # generators that return transient class-generating diff --git a/astroid/tests/testdata/python2/data/module.py b/astroid/tests/testdata/python2/data/module.py index 6a67b9b684..0f5afe5930 100644 --- a/astroid/tests/testdata/python2/data/module.py +++ b/astroid/tests/testdata/python2/data/module.py @@ -46,7 +46,6 @@ def __init__(self): def method(self): """method test""" - global MY_DICT try: MY_DICT = {} local = None diff --git a/astroid/tests/testdata/python3/data/module.py b/astroid/tests/testdata/python3/data/module.py index 2a5fb58cd8..9eecb37e8f 100644 --- a/astroid/tests/testdata/python3/data/module.py +++ b/astroid/tests/testdata/python3/data/module.py @@ -46,7 +46,6 @@ def __init__(self): def method(self): """method test""" - global MY_DICT try: MY_DICT = {} local = None diff --git a/astroid/tests/unittest_builder.py b/astroid/tests/unittest_builder.py index 2b58ded58d..6ad786d073 100644 --- a/astroid/tests/unittest_builder.py +++ b/astroid/tests/unittest_builder.py @@ -438,6 +438,7 @@ class F: self.assertTrue(mod_ast['D'].newstyle) self.assertTrue(mod_ast['F'].newstyle) + @unittest.expectedFailure def test_globals(self): data = ''' CSTE = 1 @@ -599,6 +600,8 @@ def test_module_base_props(self): self.assertEqual(module.statement(), module) self.assertEqual(module.statement(), module) + # TODO: change this test so it doesn't contain a global statement + @unittest.expectedFailure def test_module_locals(self): """test the 'locals' dictionary of a astroid module""" module = self.module @@ -702,11 +705,11 @@ def test_method_locals(self): _locals = method.locals keys = sorted(_locals) if sys.version_info < (3, 0): - self.assertEqual(len(_locals), 5) - self.assertEqual(keys, ['a', 'autre', 'b', 'local', 'self']) + self.assertEqual(len(_locals), 6) + self.assertEqual(keys, ['MY_DICT', 'a', 'autre', 'b', 'local', 'self']) else:# ListComp variables are no more accessible outside - self.assertEqual(len(_locals), 3) - self.assertEqual(keys, ['autre', 'local', 'self']) + self.assertEqual(len(_locals), 4) + self.assertEqual(keys, ['MY_DICT', 'autre', 'local', 'self']) class ModuleBuildTest(resources.SysPathSetup, FileBuildTest): diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index 9abfb60809..2d6fd5776d 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -1272,6 +1272,7 @@ def qux(): self.assertEqual(len(inferred), 1) self.assertIs(inferred[0], util.Uninferable) + @unittest.expectedFailure def test_nonregr_func_global(self): code = ''' active_application = None diff --git a/astroid/tests/unittest_manager.py b/astroid/tests/unittest_manager.py index 2a8f1c61cb..18ace375ea 100644 --- a/astroid/tests/unittest_manager.py +++ b/astroid/tests/unittest_manager.py @@ -156,32 +156,6 @@ def test_ast_from_module_cache(self): self.assertEqual(astroid.name, 'unittest') self.assertIn('unittest', self.manager.astroid_cache) - def test_ast_from_class(self): - astroid = self.manager.ast_from_class(int) - self.assertEqual(astroid.name, 'int') - self.assertEqual(astroid.parent.frame().name, BUILTINS) - - astroid = self.manager.ast_from_class(object) - self.assertEqual(astroid.name, 'object') - self.assertEqual(astroid.parent.frame().name, BUILTINS) - self.assertIn('__setattr__', astroid) - - def test_ast_from_class_with_module(self): - """check if the method works with the module name""" - astroid = self.manager.ast_from_class(int, int.__module__) - self.assertEqual(astroid.name, 'int') - self.assertEqual(astroid.parent.frame().name, BUILTINS) - - astroid = self.manager.ast_from_class(object, object.__module__) - self.assertEqual(astroid.name, 'object') - self.assertEqual(astroid.parent.frame().name, BUILTINS) - self.assertIn('__setattr__', astroid) - - def test_ast_from_class_attr_error(self): - """give a wrong class at the ast_from_class method""" - self.assertRaises(exceptions.AstroidBuildingException, - self.manager.ast_from_class, None) - def testFailedImportHooks(self): def hook(modname): if modname == 'foo.bar': diff --git a/astroid/tests/unittest_raw_building.py b/astroid/tests/unittest_raw_building.py index fb5690e28e..ae560fa394 100644 --- a/astroid/tests/unittest_raw_building.py +++ b/astroid/tests/unittest_raw_building.py @@ -44,5 +44,33 @@ def test_open_is_inferred_correctly(self): self.assertEqual(inferred.root().name, BUILTINS, name) + + # def test_ast_from_class(self): + # astroid = self.manager.ast_from_class(int) + # self.assertEqual(astroid.name, 'int') + # self.assertEqual(astroid.parent.frame().name, BUILTINS) + + # astroid = self.manager.ast_from_class(object) + # self.assertEqual(astroid.name, 'object') + # self.assertEqual(astroid.parent.frame().name, BUILTINS) + # self.assertIn('__setattr__', astroid) + + # def test_ast_from_class_with_module(self): + # """check if the method works with the module name""" + # astroid = self.manager.ast_from_class(int, int.__module__) + # self.assertEqual(astroid.name, 'int') + # self.assertEqual(astroid.parent.frame().name, BUILTINS) + + # astroid = self.manager.ast_from_class(object, object.__module__) + # self.assertEqual(astroid.name, 'object') + # self.assertEqual(astroid.parent.frame().name, BUILTINS) + # self.assertIn('__setattr__', astroid) + + # def test_ast_from_class_attr_error(self): + # """give a wrong class at the ast_from_class method""" + # self.assertRaises(exceptions.AstroidBuildingException, + # self.manager.ast_from_class, None) + + if __name__ == '__main__': unittest.main() diff --git a/tox.ini b/tox.ini index bfbe575f64..1da62d745c 100644 --- a/tox.ini +++ b/tox.ini @@ -7,6 +7,8 @@ commands = pylint -rn --rcfile={toxinidir}/pylintrc {envsitepackagesdir}/astroid [testenv] deps = + # Temporary hack, ignore this. + dictproxyhack py27,py33,pypy,jython: enum34 lazy-object-proxy nose From 5f2879660760ec34ccf278762fa3b2b31356031c Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Sat, 14 Nov 2015 12:29:39 -0500 Subject: [PATCH 080/312] Fix two shallow test failures on Python 2 --- astroid/raw_building.py | 3 ++- astroid/tests/unittest_builder.py | 20 +++++++++----------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/astroid/raw_building.py b/astroid/raw_building.py index c52bf852ca..fd084bc6f7 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -130,6 +130,7 @@ def ast_from_object(object_, name=None): inspect.getmodule(object_), name)[0] + INSTANCE_SPECIAL_ATTRIBUTES = frozenset(('__dict__', '__class__')) @_singledispatch @@ -259,7 +260,7 @@ def ast_from_module(module, built_objects, parent_module, name=None, parent=None return (module_node,) -FUNCTION_SPECIAL_ATTRIBUTES = frozenset(('__doc__', '__name__', '__qualname__', '__module__', '__defaults__', '__code__', '__globals__', '__dict__', '__closure__', '__annotations__', '__kwdefaults__')) +FUNCTION_SPECIAL_ATTRIBUTES = frozenset(('__doc__', '__name__', '__qualname__', '__module__', '__defaults__', '__code__', '__globals__', '__dict__', '__closure__', '__annotations__', '__kwdefaults__', '__func__', '__self__')) # __func__ and __self__ are method-only special attributes. # pylint: disable=unused-variable; doesn't understand singledispatch diff --git a/astroid/tests/unittest_builder.py b/astroid/tests/unittest_builder.py index 6ad786d073..e11bedf7f2 100644 --- a/astroid/tests/unittest_builder.py +++ b/astroid/tests/unittest_builder.py @@ -284,7 +284,7 @@ def test_inspect_build0(self): self.assertIsInstance(fclass['read'], nodes.FunctionDef) # check builtin function has args.args == None dclass = builtin_ast['dict'] - self.assertIsNone(dclass['has_key'].args.args) + self.assertIsInstance(dclass['has_key'].args, nodes.Unknown) # just check type and object are there builtin_ast.getattr('type') objectastroid = builtin_ast.getattr('object')[0] @@ -336,14 +336,14 @@ def test_inspect_build3(self): def test_inspect_build_instance(self): """test astroid tree build from a living object""" import exceptions - builtin_ast = raw_building.ast_from_object(exceptions) # self.builder.inspect_build(exceptions) - fclass = builtin_ast['OSError'] - # things like OSError.strerror are now (2.5) data descriptors on the - # class instead of entries in the __dict__ of an instance - container = fclass - self.assertIn('errno', container) - self.assertIn('strerror', container) - self.assertIn('filename', container) + exceptions_ast = raw_building.ast_from_object(exceptions) + environment_error = exceptions_ast['EnvironmentError'] + # things like EnvironmentError.strerror are now (2.5) data + # descriptors on the class instead of entries in the __dict__ + # of an instance + self.assertIn('errno', environment_error) + self.assertIn('strerror', environment_error) + self.assertIn('filename', environment_error) def test_inspect_build_type_object(self): builtin_ast = MANAGER.ast_from_module_name(BUILTINS) @@ -600,8 +600,6 @@ def test_module_base_props(self): self.assertEqual(module.statement(), module) self.assertEqual(module.statement(), module) - # TODO: change this test so it doesn't contain a global statement - @unittest.expectedFailure def test_module_locals(self): """test the 'locals' dictionary of a astroid module""" module = self.module From 17c7515226e16a85e626ae0b44815779cefbdc25 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Sat, 14 Nov 2015 18:40:05 -0500 Subject: [PATCH 081/312] Fix issues covered in code review --- astroid/bases.py | 19 ++++++++++++++ astroid/brain/brain_stdlib.py | 8 +++--- astroid/node_classes.py | 2 +- astroid/raw_building.py | 48 ++++++++++++++++------------------- astroid/scoped_nodes.py | 36 ++++++++++++++++---------- 5 files changed, 69 insertions(+), 44 deletions(-) diff --git a/astroid/bases.py b/astroid/bases.py index 3cd296b63d..569c3dadb9 100644 --- a/astroid/bases.py +++ b/astroid/bases.py @@ -25,11 +25,15 @@ import types from astroid import context as contextmod +from astroid import decorators from astroid import exceptions from astroid import util node_classes = util.lazy_import('node_classes') scoped_nodes = util.lazy_import('scoped_nodes') +manager = util.lazy_import('manager') + +MANAGER = manager.AstroidManager() if sys.version_info >= (3, 0): @@ -76,6 +80,8 @@ def __getattr__(self, name): return getattr(self.__class__, '_proxied') if name in self.__dict__: return self.__dict__[name] + if name == 'special_attributes' and hasattr(self, 'special_attributes'): + return self.special_attributes return getattr(self._proxied, name) def infer(self, context=None): @@ -132,6 +138,7 @@ def _infer_method_result_truth(instance, method_name, context): class Instance(Proxy): """A special node representing a class instance.""" + special_attributes = frozenset(('__dict__', '__class__')) def getattr(self, name, context=None, lookupclass=True): try: @@ -309,6 +316,13 @@ def bool_value(self): class BoundMethod(UnboundMethod): """a special node representing a method bound to an instance""" + # __func__ and __self__ are method-only special attributes, the + # rest are general function special attributes. + special_attributes = frozenset( + ('__doc__', '__name__', '__qualname__', '__module__', '__defaults__', + '__code__', '__globals__', '__dict__', '__closure__', + '__annotations__', '__kwdefaults__', '__func__', '__self__')) + def __init__(self, proxy, bound): UnboundMethod.__init__(self, proxy) self.bound = bound @@ -433,3 +447,8 @@ def __repr__(self): def __str__(self): return 'Generator(%s)' % (self._proxied.name) + + @decorators.cachedproperty + def _proxied(self): + builtins = MANAGER.astroid_cache[BUILTINS] + return builtins.getattr('generator')[0] diff --git a/astroid/brain/brain_stdlib.py b/astroid/brain/brain_stdlib.py index 9c2eeb01a3..7c0d127e58 100644 --- a/astroid/brain/brain_stdlib.py +++ b/astroid/brain/brain_stdlib.py @@ -225,10 +225,12 @@ def infer_namedtuple(namedtuple_call, context=None): """Specific inference function for namedtuple Call node""" type_name, fields = infer_namedtuple_enum_fields(namedtuple_call, context) - try: + if isinstance(fields, nodes.Const) and isinstance(fields.value, str): field_names = tuple(fields.value.replace(',', ' ').split()) - except AttributeError: + elif isinstance(fields, (nodes.Tuple, nodes.List)): field_names = tuple(infer_first(const, context).value for const in fields.elts) + else: + raise UseInferenceDefault() class_definition = _class_template.format( typename = type_name, @@ -242,7 +244,7 @@ def infer_namedtuple(namedtuple_call, context=None): ) # TODO: maybe memoize this call for efficiency, if it's needed. - namedtuple_node = AstroidBuilder(MANAGER).string_build(class_definition).body[3] + namedtuple_node = AstroidBuilder(MANAGER).string_build(class_definition).getattr(type_name)[0] return iter([namedtuple_node]) diff --git a/astroid/node_classes.py b/astroid/node_classes.py index 0da9b21acc..7104e3368a 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -966,7 +966,7 @@ class Unknown(NodeNG): ''' def infer(self, context=None, **kwargs): '''Inference on an Unknown node immediately terminates.''' - raise StopIteration + yield util.Uninferable class AssignAttr(mixins.ParentAssignTypeMixin, NodeNG): diff --git a/astroid/raw_building.py b/astroid/raw_building.py index fd084bc6f7..538b16607f 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -131,8 +131,6 @@ def ast_from_object(object_, name=None): -INSTANCE_SPECIAL_ATTRIBUTES = frozenset(('__dict__', '__class__')) - @_singledispatch def _ast_from_object(instance, built_objects, module, name=None, parent=None): # Since all ClassInstances pointing to the same ClassDef are @@ -171,8 +169,10 @@ def _ast_from_object(instance, built_objects, module, name=None, parent=None): # Take the set difference of instance and class attributes. for name in set(dir(instance)) - set(dir(cls)): - if name not in INSTANCE_SPECIAL_ATTRIBUTES: - class_node.instance_attrs[name].append(getattr(instance, name)) + if name not in bases.Instance.special_attributes: + ast = _ast_from_object(getattr(instance, name), built_objects, + module, name=name, parent=parent) + class_node.instance_attrs[name].append(ast) # Create an instance of the class we just created an AST for. result.append(nodes.InterpreterObject(name=name, object_=class_node.instantiate_class(), parent=parent)) @@ -180,8 +180,6 @@ def _ast_from_object(instance, built_objects, module, name=None, parent=None): return result -CLASS_SPECIAL_ATTRIBUTES = frozenset(('__name__', '__module__', '__dict__', '__bases__', '__doc__', '__qualname__', '__mro__')) - # pylint: disable=unused-variable; doesn't understand singledispatch @_ast_from_object.register(type) def ast_from_class(cls, built_objects, module, name=None, parent=None): @@ -200,7 +198,9 @@ def ast_from_class(cls, built_objects, module, name=None, parent=None): # appropriate ClassDef node is returned. Arguably, it would be # possible to check if a class is in a module under another name, # but does this ever happen? - if inspected_module is not None and inspected_module is not module and hasattr(inspected_module, cls.__name__): + if (inspected_module is not None and + inspected_module is not module and + hasattr(inspected_module, cls.__name__)): return (nodes.ImportFrom(fromname= getattr(inspected_module, '__name__', None), names=[[cls.__name__, name]], @@ -208,16 +208,11 @@ def ast_from_class(cls, built_objects, module, name=None, parent=None): class_node = nodes.ClassDef(name=name or cls.__name__, doc=inspect.getdoc(cls), parent=parent) built_objects[id(cls)] = class_node built_objects = _ChainMap({}, *built_objects.maps) - try: - bases = [nodes.Name(name=b.__name__, parent=class_node) - for b in cls.__bases__] - body = [ - t for a in _classify_class_attrs(cls) if a.defining_class is cls and a.name not in CLASS_SPECIAL_ATTRIBUTES - for t in _ast_from_object(a.object, built_objects, module, a.name, parent=class_node)] - except AttributeError: - bases = () - body = [t for n, m in inspect.getmembers(cls) if n not in CLASS_SPECIAL_ATTRIBUTES - for t in _ast_from_object(m, built_objects, module, n, parent=class_node)] + bases = [nodes.Name(name=b.__name__, parent=class_node) + for b in cls.__bases__] + body = [ + t for a in _classify_class_attrs(cls) if a.defining_class is cls and a.name not in nodes.ClassDef.special_attributes + for t in _ast_from_object(a.object, built_objects, module, a.name, parent=class_node)] class_node.postinit(bases=bases, body=body, decorators=(), newstyle=isinstance(cls, type)) return (class_node,) @@ -226,8 +221,6 @@ def ast_from_class(cls, built_objects, module, name=None, parent=None): _ast_from_object.register(types.ClassType, ast_from_class) -MODULE_SPECIAL_ATTRIBUTES = frozenset(('__name__', '__doc__', '__file__', '__path__', '__dict__')) - # pylint: disable=unused-variable; doesn't understand singledispatch @_ast_from_object.register(types.ModuleType) def ast_from_module(module, built_objects, parent_module, name=None, parent=None): @@ -254,14 +247,13 @@ def ast_from_module(module, built_objects, parent_module, name=None, parent=None built_objects[id(module)] = module_node built_objects = _ChainMap({}, *built_objects.maps) MANAGER.cache_module(module_node) - body = [t for n, m in inspect.getmembers(module) if n not in MODULE_SPECIAL_ATTRIBUTES - for t in _ast_from_object(m, built_objects, module, n, module_node)] + body = [ + t for n, m in inspect.getmembers(module) if n not in nodes.Module.special_attributes + for t in _ast_from_object(m, built_objects, module, n, module_node)] module_node.postinit(body=body) return (module_node,) -FUNCTION_SPECIAL_ATTRIBUTES = frozenset(('__doc__', '__name__', '__qualname__', '__module__', '__defaults__', '__code__', '__globals__', '__dict__', '__closure__', '__annotations__', '__kwdefaults__', '__func__', '__self__')) # __func__ and __self__ are method-only special attributes. - # pylint: disable=unused-variable; doesn't understand singledispatch # These two types are the same on CPython but not necessarily the same @@ -360,8 +352,12 @@ def extract_vararg(parameter): varargannotation, kwargannotation) func_node.postinit(args=args_node, body=[]) for name in set(dir(func)) - set(dir(type(func))): - if name not in FUNCTION_SPECIAL_ATTRIBUTES: - func_node.instance_attrs[name].append(getattr(func, name)) + # This checks against method special attributes because + # methods are also dispatched through this function. + if name not in bases.BoundMethod.special_attributes: + ast = _ast_from_object(getattr(func, name), built_objects, + module, name=name, parent=parent) + func_node.instance_attrs[name].append(ast) return (func_node,) @@ -391,6 +387,7 @@ def ast_from_builtin_container(container, built_objects, module, name=None, for container_type in BUILTIN_CONTAINERS: if isinstance(container, container_type): container_node = BUILTIN_CONTAINERS[container_type](parent=parent) + break if name: node = parent else: @@ -533,7 +530,6 @@ def ast_from_builtins(): builtins_ast.body.append(type_node) type_node.parent = builtins_ast - bases.Generator._proxied = builtins_ast.getattr(types.GeneratorType.__name__)[0] return builtins_ast builtins_ast = ast_from_builtins() diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index b22197717f..8c191b0b7e 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -281,11 +281,17 @@ class Module(LocalsDictNodeNG): # boolean for package module package = None - # names of python special attributes (handled by getattr impl.) - special_attributes = set(('__name__', '__doc__', '__file__', '__path__', - '__dict__')) - # names of module attributes available through the global scope - scope_attrs = set(('__name__', '__doc__', '__file__', '__path__')) + special_attributes = frozenset( + ('__name__', '__doc__', '__file__', '__dict__', '__package__', + '__cached__', '__spec__', '__loader__', six.moves.builtins.__name__)) + + # TODO: does __path__ even exist? + # # names of python special attributes (handled by getattr impl.) + # special_attributes = set(('__name__', '__doc__', '__file__', '__path__', + # '__dict__')) + # # names of module attributes available through the global scope + # scope_attrs = set(('__name__', '__doc__', '__file__', '__path__')) + scope_attrs = frozenset(('__name__', '__doc__', '__file__')) if six.PY2: _other_fields = ('name', 'doc', 'file_encoding', 'package', @@ -305,7 +311,6 @@ def __init__(self, name, doc, package=None, parent=None, self.source_code = source_code self.source_file = source_file self.body = [] - # self.future_imports = set() self.external_attrs = collections.defaultdict(list) def postinit(self, body=None): @@ -480,10 +485,7 @@ def next_sibling(self): if six.PY2: @decorators_mod.cachedproperty def _absolute_import_activated(self): - if 'absolute_import' in self.future_imports: - return True - else: - return False + return 'absolute_import' in self.future_imports else: _absolute_import_activated = True @@ -781,7 +783,11 @@ class FunctionDef(node_classes.Statement, Lambda): else: _astroid_fields = ('decorators', 'args', 'body') decorators = None - special_attributes = set(('__name__', '__doc__', '__dict__')) + + special_attributes = frozenset( + ('__doc__', '__name__', '__qualname__', '__module__', '__defaults__', + '__code__', '__globals__', '__dict__', '__closure__', '__annotations__', + '__kwdefaults__')) is_function = True # attributes below are set by the builder module or by raw factories _other_fields = ('name', 'doc') @@ -793,7 +799,7 @@ def __init__(self, name=None, doc=None, lineno=None, col_offset=None, parent=None): self.name = name self.doc = doc - # self.instance_attrs = {} + self.instance_attrs = {} super(FunctionDef, self).__init__(lineno, col_offset, parent) # pylint: disable=arguments-differ; different than Lambdas @@ -1149,8 +1155,10 @@ class ClassDef(mixins.FilterStmtsMixin, LocalsDictNodeNG, _astroid_fields = ('decorators', 'bases', 'body') decorators = None - special_attributes = set(('__name__', '__doc__', '__dict__', '__module__', - '__bases__', '__mro__', '__subclasses__')) + special_attributes = frozenset( + ('__name__', '__module__', '__dict__', '__bases__', '__doc__', + '__qualname__', '__mro__', '__subclasses__')) + _type = None _metaclass_hack = False hide = False From e2f7a1526b81b3a19dcc6d7f2abecd166f5d2ed6 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Sat, 14 Nov 2015 23:54:28 -0500 Subject: [PATCH 082/312] Add __class__ to ClassDef.special_attributes to deal with metaclasses --- astroid/scoped_nodes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index 8c191b0b7e..4b2aede16a 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -1157,7 +1157,7 @@ class ClassDef(mixins.FilterStmtsMixin, LocalsDictNodeNG, decorators = None special_attributes = frozenset( ('__name__', '__module__', '__dict__', '__bases__', '__doc__', - '__qualname__', '__mro__', '__subclasses__')) + '__qualname__', '__mro__', '__subclasses__', '__class__')) _type = None _metaclass_hack = False From 8b5307c7417ca9c3c72dfd6e37e8f70ca5195104 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Sun, 15 Nov 2015 13:49:10 -0500 Subject: [PATCH 083/312] Fix remaining code review issues and add docstrings --- astroid/as_string.py | 12 +++--- astroid/nodes.py | 12 +++--- astroid/raw_building.py | 61 +++++++++++++++++++++++++++--- astroid/scoped_nodes.py | 84 +++++++++++++++++++++++++++++------------ 4 files changed, 127 insertions(+), 42 deletions(-) diff --git a/astroid/as_string.py b/astroid/as_string.py index a83dff6518..37dc77d559 100644 --- a/astroid/as_string.py +++ b/astroid/as_string.py @@ -181,12 +181,14 @@ def visit_dictcomp(self, node): ' '.join([n.accept(self) for n in node.generators])) def visit_expr(self, node): - """return an astroid.Discard node as string""" + """return an astroid.Expr node as string""" return node.value.accept(self) - def visit_interpreterobject(self, node): - """dummy method for visiting an Empty node""" + def visit_unknown(self, node): + """dummy method for visiting nodes for special nodes.""" return '' + visit_reservedname = visit_unknown + visit_interpreterobject = visit_unknown def visit_excepthandler(self, node): if node.type: @@ -396,10 +398,6 @@ def visit_unaryop(self, node): operator = node.op return '%s%s' % (operator, node.operand.accept(self)) - def visit_unknown(self, node): - """dummy method for visiting an Unknown node""" - return '' - def visit_while(self, node): """return an astroid.While node as string""" whiles = 'while %s:\n%s' % (node.test.accept(self), diff --git a/astroid/nodes.py b/astroid/nodes.py index d34ff42b9b..4ceeae46cf 100644 --- a/astroid/nodes.py +++ b/astroid/nodes.py @@ -42,15 +42,15 @@ Comprehension, Const, Continue, Decorators, DelAttr, DelName, Delete, Dict, Expr, Ellipsis, ExceptHandler, Exec, ExtSlice, For, ImportFrom, Attribute, Global, If, IfExp, Import, Index, Keyword, - List, Name, Nonlocal, Pass, Print, Raise, Return, Set, Slice, Starred, Subscript, - TryExcept, TryFinally, Tuple, UnaryOp, While, With, Yield, YieldFrom, - AsyncFor, Await, AsyncWith, + List, Name, NameConstant, Nonlocal, Pass, Print, Raise, Return, Set, Slice, + Starred, Subscript, TryExcept, TryFinally, Tuple, UnaryOp, While, With, + Yield, YieldFrom, AsyncFor, Await, AsyncWith, # Backwards-compatibility aliases Backquote, Discard, AssName, AssAttr, Getattr, CallFunc, From, # Node not present in the builtin ast module. DictUnpack, # Special nodes for building from live objects. - InterpreterObject, NameConstant, ReservedName, Unknown + InterpreterObject, ReservedName, Unknown ) from astroid.scoped_nodes import ( Module, GeneratorExp, Lambda, DictComp, @@ -70,10 +70,10 @@ Call, ClassDef, Compare, Comprehension, Const, Continue, Decorators, DelAttr, DelName, Delete, Dict, DictComp, DictUnpack, Expr, - Ellipsis, InterpreterObject, ExceptHandler, Exec, ExtSlice, + Ellipsis, ExceptHandler, Exec, ExtSlice, For, ImportFrom, FunctionDef, Attribute, GeneratorExp, Global, - If, IfExp, Import, Index, + If, IfExp, Import, Index, InterpreterObject, Keyword, Lambda, List, ListComp, Name, NameConstant, Nonlocal, diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 538b16607f..5ec9a67e1c 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -133,6 +133,42 @@ def ast_from_object(object_, name=None): @_singledispatch def _ast_from_object(instance, built_objects, module, name=None, parent=None): + '''Returns a mock AST for an instance. + + This is the internal recursive generic function for building an + AST from a runtime object. Unlike for most generic functions, + this implementation, for object, is not a stub, because this + generic function needs to handle all kinds of Python objects. + This implementation handles instances, but except where noted this + documentation applies to all implementations of the generic + function. + + Args: + instance (Any): The Python object to return a mock AST for. + This implementation should only receive instances, with all other + objects directed to other implementations. + built_objects (ChainMap): Maps id()s for objects to mock ASTs for + objects, recording what objects already have a mock AST constructed. + id() is used because not all Python objects are hashable. + The internal maps of the ChainMap represent scopes within the object + being recursed over, with a new map for each scope, to ensure that + ASTs added in inner scopes are duplicated if necessary in other + scopes. + module (types.Module): The module of the root object, used to determine + if any child objects come from different modules. + name (str): The name the parent object uses for the a child object, if + any. + parent (NodeNG): The node corresponding to a parent object, if any. + + Returns: + A Sequence of nodes representing the object and its attributes, methods, + etc. + + Raises: + TypeError: When called on an instance where it's not possible + to construct an appropriate AST. + + ''' # Since all ClassInstances pointing to the same ClassDef are # identical, they can all share the same node. if id(instance) in built_objects: @@ -183,6 +219,7 @@ def _ast_from_object(instance, built_objects, module, name=None, parent=None): # pylint: disable=unused-variable; doesn't understand singledispatch @_ast_from_object.register(type) def ast_from_class(cls, built_objects, module, name=None, parent=None): + '''Handles classes and other types not handled explicitly elsewhere.''' if id(cls) in built_objects: return (nodes.Name(name=name or cls.__name__, parent=parent),) inspected_module = inspect.getmodule(cls) @@ -269,6 +306,7 @@ def ast_from_module(module, built_objects, parent_module, name=None, parent=None @_ast_from_object.register(types.FunctionType) @_ast_from_object.register(types.MethodType) def ast_from_function(func, built_objects, module, name=None, parent=None): + '''Handles functions, including all kinds of methods.''' if id(func) in built_objects: return (nodes.Name(name=name or func.__name__, parent=parent),) inspected_module = inspect.getmodule(func) @@ -371,10 +409,7 @@ def extract_vararg(parameter): @_ast_from_object.register(tuple) def ast_from_builtin_container(container, built_objects, module, name=None, parent=None): - '''Handles builtin containers that have their own AST nodes, like list - but not range. - - ''' + '''Handles builtin container types except for mappings.''' if (id(container) in built_objects and built_objects[id(container)].targets[0].name == name): return (nodes.Name(name=name, parent=parent),) @@ -405,6 +440,7 @@ def ast_from_builtin_container(container, built_objects, module, name=None, @_ast_from_object.register(dict) def ast_from_dict(dictionary, built_objects, module, name=None, parent=None): + '''Handles dictionaries, including DictProxyType and MappingProxyType.''' if (id(dictionary) in built_objects and built_objects[id(dictionary)].targets[0].name == name): return (nodes.Name(name=name, parent=parent),) @@ -438,6 +474,7 @@ def ast_from_dict(dictionary, built_objects, module, name=None, @_ast_from_object.register(float) @_ast_from_object.register(complex) def ast_from_builtin_number_text_binary(builtin_number_text_binary, built_objects, module, name=None, parent=None): + '''Handles the builtin numeric and text/binary sequence types.''' if name: parent = nodes.Assign(parent=parent) name_node = nodes.AssignName(name, parent=parent) @@ -455,7 +492,21 @@ def ast_from_builtin_number_text_binary(builtin_number_text_binary, built_object def ast_from_builtin_singleton_factory(node_class): - def ast_from_builtin_singleton(builtin_singleton, built_objects, module, name=None, parent=None, node_class=node_class): + '''A higher-order function for building functions for handling the + builtin singleton types. + + Args: + node_class (NodeNG): The constructor for the AST node for the builtin + singleton. + + Returns: + A function that handles the type corresponding to node_class. + ''' + def ast_from_builtin_singleton(builtin_singleton, built_objects, + module, name=None, parent=None, + node_class=node_class): + '''Handles builtin singletons, currently True, False, None, + NotImplemented, and Ellipsis.''' # A builtin singleton is assigned to a name. if name and name != str(builtin_singleton): parent = nodes.Assign(parent=parent) diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index 4b2aede16a..e94dc51e82 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -132,7 +132,7 @@ def builtin_lookup(name): return builtin_astroid, () stmts = builtin_astroid.locals.get(name, ()) # Use inference to find what AssignName nodes point to in builtins. - # stmts = [next(s.infer()) if isinstance(s, node_classes.AssignName) else s + # stmts = [next(s.infer()) if isinstance(s, node_classes.ImportFrom) else s # for s in stmts] return builtin_astroid, stmts @@ -207,10 +207,10 @@ def set_local(self, name, stmt): __setitem__ = set_local - def _append_node(self, child): - """append a child, linking it in the tree""" - self.body.append(child) - child.parent = self + # def _append_node(self, child): + # """append a child, linking it in the tree""" + # self.body.append(child) + # child.parent = self # def add_local_node(self, child_node, name=None): # """append a child which should alter locals to the given node""" @@ -364,13 +364,24 @@ def globals(self): def future_imports(self): index = 0 future_imports = [] - while (index < len(self.body) and - ((isinstance(self.body[index], node_classes.ImportFrom) - and self.body[index].modname == '__future__') or - (index == 0 and isinstance(self.body[0], - node_classes.Expr)))): - future_imports.extend(n[0] for n in getattr(self.body[index], - 'names', ())) + + # The start of a Python module has an optional docstring + # followed by any number of `from __future__ import` + # statements. This doesn't try to test for incorrect ASTs, + # but should function on all correct ones. + while (index < len(self.body)): + if (isinstance(self.body[index], node_classes.ImportFrom) + and self.body[index].modname == '__future__'): + # This is a `from __future__ import` statement. + future_imports.extend(n[0] for n in getattr(self.body[index], + 'names', ())) + elif (index == 0 and isinstance(self.body[0], node_classes.Expr)): + # This is a docstring, so do nothing. + pass + else: + # This is some other kind of statement, so the future + # imports must be finished. + break index += 1 return frozenset(future_imports) @@ -1864,6 +1875,15 @@ class attributes defined in the class body, also what a locals() avoid incorrectly adding a class's, function's, or module's name to its own local variables. + Args: + node (LocalsDictNodeNG): A node defining a scope to return locals for. + + Returns: + A defaultdict(list) mapping names (strings) to lists of nodes. + + Raises: + TypeError: When called on a node that doesn't represent a scope or a + non-node object. ''' raise TypeError("This isn't an astroid node: %s" % type(node)) @@ -1877,24 +1897,39 @@ def not_scoped_node(node): def scoped_node(node): locals_ = collections.defaultdict(list) for n in node.get_children(): - _get_locals(n, locals_, node) + _get_locals(n, locals_) return locals_ @_singledispatch -def _get_locals(node, locals_, root): +def _get_locals(node, locals_): + '''Return the local variables for a node. + + This is the internal recursive generic function for gathering + nodes into a local variables mapping. The locals mapping is + passed down and mutated by each function. + + Args: + node (NodeNG): The node to inspect for assignments to locals. + locals_ (defaultdict(list)): A mapping of (strings) to lists of nodes. + + Raises: + TypeError: When called on a non-node object. + + ''' + raise TypeError('Non-astroid object in an astroid AST: %s' % type(node)) # pylint: disable=unused-variable; doesn't understand singledispatch @_get_locals.register(node_classes.NodeNG) -def locals_generic(node, locals_, root): +def locals_generic(node, locals_): '''Generic nodes don't create name bindings or scopes.''' for n in node.get_children(): - _get_locals(n, locals_, root) + _get_locals(n, locals_) # # pylint: disable=unused-variable; doesn't understand singledispatch @_get_locals.register(LocalsDictNodeNG) -def locals_new_scope(node, locals_, root): +def locals_new_scope(node, locals_): '''These nodes start a new scope, so terminate recursion here.''' # pylint: disable=unused-variable; doesn't understand singledispatch @@ -1902,28 +1937,28 @@ def locals_new_scope(node, locals_, root): @_get_locals.register(node_classes.DelName) @_get_locals.register(FunctionDef) @_get_locals.register(ClassDef) -def locals_name(node, locals_, root): +def locals_name(node, locals_): '''These nodes add a name to the local variables. AssignName and DelName have no children while FunctionDef and ClassDef start a new scope so shouldn't be recursed into.''' locals_[node.name].append(node) @_get_locals.register(node_classes.InterpreterObject) -def locals_interpreter_object(node, locals_, root): +def locals_interpreter_object(node, locals_): '''InterpreterObjects add an object to the local variables under a specified name.''' if node.name: locals_[node.name].append(node) @_get_locals.register(node_classes.ReservedName) -def locals_reserved_name(node, locals_, root): +def locals_reserved_name(node, locals_): '''InterpreterObjects add an object to the local variables under a specified name.''' locals_[node.name].append(node.value) # pylint: disable=unused-variable; doesn't understand singledispatch @_get_locals.register(node_classes.Arguments) -def locals_arguments(node, locals_, root): +def locals_arguments(node, locals_): '''Other names assigned by functions have AssignName nodes that are children of an Arguments node.''' if node.vararg: @@ -1931,21 +1966,22 @@ def locals_arguments(node, locals_, root): if node.kwarg: locals_[node.kwarg].append(node) for n in node.get_children(): - _get_locals(n, locals_, root) + _get_locals(n, locals_) # pylint: disable=unused-variable; doesn't understand singledispatch @_get_locals.register(node_classes.Import) -def locals_import(node, locals_, root): +def locals_import(node, locals_): for name, asname in node.names: name = asname or name locals_[name.split('.')[0]].append(node) # pylint: disable=unused-variable; doesn't understand singledispatch @_get_locals.register(node_classes.ImportFrom) -def locals_import_from(node, locals_, root): +def locals_import_from(node, locals_): # Don't add future imports to locals. if node.modname == '__future__': return + # Inherited code, I don't know why this function sorts this list. def sort_locals(my_list): my_list.sort(key=lambda node: node.fromlineno) From f4fad7a967e956bb283afb196468832f3c93d7d6 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Mon, 16 Nov 2015 15:16:37 -0500 Subject: [PATCH 084/312] Remove debugging statement --- astroid/tree/scoped_nodes.py | 1 - 1 file changed, 1 deletion(-) diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index b8eee4bc27..8daed13f2e 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -1612,7 +1612,6 @@ def ancestors(self, recurs=True, context=None): context = contextmod.InferenceContext() if six.PY3: if not self.bases and self.qname() != 'builtins.object': - print(MANAGER.astroid_cache['builtins']) yield builtin_lookup("object")[1][0] return From 58c9a608fdb58bd5147e5c938b12c9c7a7b69040 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Mon, 16 Nov 2015 15:38:33 -0500 Subject: [PATCH 085/312] Add a workaround for singledispatch raising AttributeError on old-style classes --- astroid/interpreter/scope.py | 2 ++ astroid/raw_building.py | 7 +------ astroid/util.py | 18 ++++++++++++++++-- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/astroid/interpreter/scope.py b/astroid/interpreter/scope.py index a4ddf41cfc..4cca5c5c56 100644 --- a/astroid/interpreter/scope.py +++ b/astroid/interpreter/scope.py @@ -26,6 +26,8 @@ from astroid import util +# import pdb; pdb.set_trace() + @util.singledispatch def _scope_by_parent(parent, node): """Detect the scope of the *node* by parent's rules. diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 00589a48be..5f8916e848 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -50,11 +50,6 @@ def __len__(self): def __iter__(self): return iter(set().union(*self.maps)) -try: - from functools import singledispatch as _singledispatch -except ImportError: - from singledispatch import singledispatch as _singledispatch - try: from inspect import signature as _signature, Parameter as _Parameter except ImportError: @@ -130,7 +125,7 @@ def ast_from_object(object_, name=None): -@_singledispatch +@util.singledispatch def _ast_from_object(instance, built_objects, module, name=None, parent=None): '''Returns a mock AST for an instance. diff --git a/astroid/util.py b/astroid/util.py index 712817487c..9c20eb4b9d 100644 --- a/astroid/util.py +++ b/astroid/util.py @@ -26,13 +26,27 @@ import lazy_object_proxy import six +import wrapt JYTHON = True if platform.python_implementation() == 'Jython' else False try: - from functools import singledispatch as singledispatch + from functools import singledispatch as _singledispatch except ImportError: - from singledispatch import singledispatch as singledispatch + from singledispatch import singledispatch as _singledispatch + + +def singledispatch(func): + old_generic_func = _singledispatch(func) + @wrapt.decorator + def wrapper(func, instance, args, kws): + return old_generic_func.dispatch(type(args[0]))(*args, **kws) + new_generic_func = wrapper(func) + new_generic_func.register = old_generic_func.register + new_generic_func.dispatch = old_generic_func.dispatch + new_generic_func.registry = old_generic_func.registry + new_generic_func._clear_cache = old_generic_func._clear_cache + return new_generic_func def lazy_import(module_name): From e4c4e6947306149109cf773ad9a2b38021458ff5 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 16 Nov 2015 22:47:09 +0200 Subject: [PATCH 086/312] Change the call to raw_building.ast_from_builtins. --- astroid/test_utils.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/astroid/test_utils.py b/astroid/test_utils.py index 023a5f10d9..e16930786c 100644 --- a/astroid/test_utils.py +++ b/astroid/test_utils.py @@ -210,5 +210,4 @@ def bootstrap(astroid_builtin=None): # between the manager and CONST_PROXY, making # unittest_lookup.LookupTC.test_builtin_lookup fail depending on the # test order - raw_building._astroid_bootstrapping( - astroid_builtin=astroid_builtin) + raw_building.ast_from_builtins() From a468dd8946ff68661f30d7770be4b75c8ed10273 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 16 Nov 2015 23:06:21 +0200 Subject: [PATCH 087/312] Change the implementation of _from_constants to use ast_from_object instead, since the other function is private and should not be used as an external API. --- astroid/brain/brain_builtin_inference.py | 18 ++++++++++++------ astroid/tests/unittest_scoped_nodes.py | 4 +++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index c5d469446f..9d514a6545 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -143,22 +143,28 @@ def _generic_inference(node, context, node_type, transform): return transformed +def _ast_from_object(obj, parent): + ast_obj = raw_building.ast_from_object(obj) + ast_obj.parent = parent + ast_obj.col_offset = parent.col_offset + ast_obj.lineno = parent.lineno + return ast_obj + + @util.singledispatch def _from_constants(cls, elts): """Get an instance of the given *cls* with the given elements set.""" - elts = [raw_building.ast_from_builtin_number_text_binary(e, {}, None, parent=node)[0] for e in elts] instance = cls() + elts = [_ast_from_object(obj, instance) for obj in elts] instance.postinit(elts=elts) return instance + @_from_constants.register(nodes.Dict) def _dict_from_constants(cls, elts): - node.items = [(raw_building.ast_from_builtin_number_text_binary(k, {}, None, - parent=node)[0], - raw_building.ast_from_builtin_number_text_binary(v, {}, None, - parent=node)[0]) - for k, v in items.items()] instance = cls() + items = [(_ast_from_object(key, instance), _ast_from_object(value, instance)) + for (key, value) in elts.items()] instance.postinit(items=items) return instance diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index c920cee5c5..b3852b0b59 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -580,7 +580,9 @@ def test_cls_special_attributes_1(self): self.assertEqual(len(cls.getattr('__dict__')), 1) if not cls.newstyle: self.assertRaises(AttributeInferenceError, cls.getattr, '__mro__') - for cls in (nodes.List.from_constants((1,))._proxied, + + ast_list = test_utils.extract_node("[1]") + for cls in (ast_list._proxied, nodes.Const(1)._proxied): self.assertEqual(len(cls.getattr('__bases__')), 1) self.assertEqual(len(cls.getattr('__name__')), 1) From 1217cedd88134342f322f2a33aa6281c6db87696 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 16 Nov 2015 23:18:07 +0200 Subject: [PATCH 088/312] Don't depend on raw_building in helpers. --- astroid/helpers.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/astroid/helpers.py b/astroid/helpers.py index 6975d59d14..4e816693ea 100644 --- a/astroid/helpers.py +++ b/astroid/helpers.py @@ -31,8 +31,13 @@ from astroid import util +MANAGER = manager.AstroidManager() +BUILTINS = six.moves.builtins.__name__ + + def _object_type(node, context=None): context = context or contextmod.InferenceContext() + builtins_ast = MANAGER.astroid_cache[BUILTINS] for inferred in node.infer(context=context): if isinstance(inferred, treeabc.ClassDef): @@ -41,26 +46,27 @@ def _object_type(node, context=None): if metaclass: yield metaclass continue - yield raw_building.builtins_ast.getattr('type')[0] + yield builtins_ast.getattr('type')[0] elif isinstance(inferred, (treeabc.Lambda, runtimeabc.UnboundMethod)): if isinstance(inferred, treeabc.Lambda): - if inferred.root() is raw_building.builtins_ast: - yield raw_building.builtins_ast[types.BuiltinFunctionType.__name__] + if inferred.root() is builtins_ast: + yield builtins_ast[types.BuiltinFunctionType.__name__] else: - yield raw_building.builtins_ast[types.FunctionType.__name__] + yield builtins_ast[types.FunctionType.__name__] elif isinstance(inferred, runtimeabc.BoundMethod): - yield raw_building.builtins_ast[types.MethodType.__name__] + yield builtins_ast[types.MethodType.__name__] elif isinstance(inferred, runtimeabc.UnboundMethod): if six.PY2: - yield raw_building.builtins_ast[types.MethodType.__name__] + yield builtins_ast[types.MethodType.__name__] else: - yield raw_building.builtins_ast[types.FunctionType.__name__] + yield builtins_ast[types.FunctionType.__name__] else: - raise InferenceError('Function {func!r} inferred from {node!r} ' - 'has no identifiable type.', - node=node, func=inferred, contex=context) + raise exceptions.InferenceError( + 'Function {func!r} inferred from {node!r} ' + 'has no identifiable type.', + node=node, func=inferred, contex=context) elif isinstance(inferred, treeabc.Module): - yield raw_building.builtins_ast[types.ModuleType.__name__] + yield builtins_ast[types.ModuleType.__name__] else: yield inferred._proxied @@ -70,7 +76,7 @@ def object_type(node, context=None): This is used to implement the ``type`` builtin, which means that it's used for inferring type calls, as well as used in a couple of other places - in the inference. + in the inference. The node will be inferred first, so this function can support all sorts of objects, as long as they support inference. """ From 3e485d0f4b0bb973e00cb151f16da54cbf473d6d Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 16 Nov 2015 23:23:24 +0200 Subject: [PATCH 089/312] Remove astroid.helpers, merge it with interpreter.util. --- astroid/brain/brain_builtin_inference.py | 3 +- astroid/helpers.py | 90 ------------------------ astroid/inference.py | 5 +- astroid/interpreter/util.py | 62 ++++++++++++++++ 4 files changed, 65 insertions(+), 95 deletions(-) delete mode 100644 astroid/helpers.py diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index 9d514a6545..27a37c4942 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -9,7 +9,6 @@ from astroid import (MANAGER, UseInferenceDefault, AttributeInferenceError, inference_tip, InferenceError, NameInferenceError) from astroid.builder import AstroidBuilder -from astroid import helpers from astroid.interpreter import objects from astroid.interpreter import util as interpreterutil from astroid import nodes @@ -485,7 +484,7 @@ def infer_type(node, context=None): if len(node.args) != 1: raise UseInferenceDefault - return helpers.object_type(node.args[0], context) + return interpreterutil.object_type(node.args[0], context) def infer_slice(node, context=None): diff --git a/astroid/helpers.py b/astroid/helpers.py deleted file mode 100644 index 4e816693ea..0000000000 --- a/astroid/helpers.py +++ /dev/null @@ -1,90 +0,0 @@ -# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . - -""" -Various helper utilities. -""" -import types - -import six - -from astroid import context as contextmod -from astroid import exceptions -from astroid.interpreter import runtimeabc -from astroid import manager -from astroid.tree import treeabc -from astroid import util - - -MANAGER = manager.AstroidManager() -BUILTINS = six.moves.builtins.__name__ - - -def _object_type(node, context=None): - context = context or contextmod.InferenceContext() - builtins_ast = MANAGER.astroid_cache[BUILTINS] - - for inferred in node.infer(context=context): - if isinstance(inferred, treeabc.ClassDef): - if inferred.newstyle: - metaclass = inferred.metaclass() - if metaclass: - yield metaclass - continue - yield builtins_ast.getattr('type')[0] - elif isinstance(inferred, (treeabc.Lambda, runtimeabc.UnboundMethod)): - if isinstance(inferred, treeabc.Lambda): - if inferred.root() is builtins_ast: - yield builtins_ast[types.BuiltinFunctionType.__name__] - else: - yield builtins_ast[types.FunctionType.__name__] - elif isinstance(inferred, runtimeabc.BoundMethod): - yield builtins_ast[types.MethodType.__name__] - elif isinstance(inferred, runtimeabc.UnboundMethod): - if six.PY2: - yield builtins_ast[types.MethodType.__name__] - else: - yield builtins_ast[types.FunctionType.__name__] - else: - raise exceptions.InferenceError( - 'Function {func!r} inferred from {node!r} ' - 'has no identifiable type.', - node=node, func=inferred, contex=context) - elif isinstance(inferred, treeabc.Module): - yield builtins_ast[types.ModuleType.__name__] - else: - yield inferred._proxied - - -def object_type(node, context=None): - """Obtain the type of the given node - - This is used to implement the ``type`` builtin, which means that it's - used for inferring type calls, as well as used in a couple of other places - in the inference. - The node will be inferred first, so this function can support all - sorts of objects, as long as they support inference. - """ - - try: - types = set(_object_type(node, context)) - except exceptions.InferenceError: - return util.Uninferable - if len(types) > 1 or not types: - return util.Uninferable - return list(types)[0] diff --git a/astroid/inference.py b/astroid/inference.py index 220bd7cbe2..bc31195281 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -27,7 +27,6 @@ from astroid import context as contextmod from astroid import decorators from astroid import exceptions -from astroid import helpers from astroid.interpreter import runtimeabc from astroid.interpreter import util as inferenceutil from astroid import protocols @@ -555,8 +554,8 @@ def _infer_binary_operation(left, right, op, context, flow_factory, nodes): """ context, reverse_context = _get_binop_contexts(context, left, right) - left_type = helpers.object_type(left) - right_type = helpers.object_type(right) + left_type = inferenceutil.object_type(left) + right_type = inferenceutil.object_type(right) methods = flow_factory(left, left_type, op, right, right_type, context, reverse_context, nodes) for method in methods: diff --git a/astroid/interpreter/util.py b/astroid/interpreter/util.py index 3302fea3b3..cdc457ad1e 100644 --- a/astroid/interpreter/util.py +++ b/astroid/interpreter/util.py @@ -17,14 +17,21 @@ # with astroid. If not, see . """Utilities for inference.""" +import types + +import six from astroid import context as contextmod from astroid import exceptions from astroid.interpreter import runtimeabc +from astroid import manager from astroid.tree import treeabc from astroid import util +MANAGER = manager.AstroidManager() +BUILTINS = six.moves.builtins.__name__ + def infer_stmts(stmts, context, frame=None): """Return an iterator on statements inferred by each statement in *stmts*.""" @@ -213,3 +220,58 @@ def is_subtype(type1, type2): def is_supertype(type1, type2): """Check if *type2* is a supertype of *type1*.""" return _type_check(type1, type2) + + +def _object_type(node, context=None): + context = context or contextmod.InferenceContext() + builtins_ast = MANAGER.astroid_cache[BUILTINS] + + for inferred in node.infer(context=context): + if isinstance(inferred, treeabc.ClassDef): + if inferred.newstyle: + metaclass = inferred.metaclass() + if metaclass: + yield metaclass + continue + yield builtins_ast.getattr('type')[0] + elif isinstance(inferred, (treeabc.Lambda, runtimeabc.UnboundMethod)): + if isinstance(inferred, treeabc.Lambda): + if inferred.root() is builtins_ast: + yield builtins_ast[types.BuiltinFunctionType.__name__] + else: + yield builtins_ast[types.FunctionType.__name__] + elif isinstance(inferred, runtimeabc.BoundMethod): + yield builtins_ast[types.MethodType.__name__] + elif isinstance(inferred, runtimeabc.UnboundMethod): + if six.PY2: + yield builtins_ast[types.MethodType.__name__] + else: + yield builtins_ast[types.FunctionType.__name__] + else: + raise exceptions.InferenceError( + 'Function {func!r} inferred from {node!r} ' + 'has no identifiable type.', + node=node, func=inferred, contex=context) + elif isinstance(inferred, treeabc.Module): + yield builtins_ast[types.ModuleType.__name__] + else: + yield inferred._proxied + + +def object_type(node, context=None): + """Obtain the type of the given node + + This is used to implement the ``type`` builtin, which means that it's + used for inferring type calls, as well as used in a couple of other places + in the inference. + The node will be inferred first, so this function can support all + sorts of objects, as long as they support inference. + """ + + try: + types = set(_object_type(node, context)) + except exceptions.InferenceError: + return util.Uninferable + if len(types) > 1 or not types: + return util.Uninferable + return list(types)[0] From 8adc68667f6561d434eee17d4be4bdadadcc54bc Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 16 Nov 2015 23:27:21 +0200 Subject: [PATCH 090/312] Fix some NameErrors and use the return annotation for the function nodes. --- astroid/protocols.py | 2 +- astroid/raw_building.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/astroid/protocols.py b/astroid/protocols.py index bb211d98a7..550762cd14 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -436,7 +436,7 @@ def _infer_context_manager(self, mgr, context, nodes): if yield_point: if not yield_point.value: # TODO(cpopa): an empty yield. Should be wrapped to Const. - const = nodes.NameConstant(None, lineno=yield_proint.lineno, parent=yield_point) + const = nodes.NameConstant(None, lineno=yield_point.lineno, parent=yield_point) yield const else: for inferred in yield_point.value.infer(context=context): diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 5f8916e848..64cfec2be1 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -374,15 +374,16 @@ def extract_vararg(parameter): kwargannotation = kwarg.annotation else: kwargannotation = None + returns = None if signature.return_annotation is not _Parameter.empty: - returns = _ast_from_object(signature_return_annotation, + returns = _ast_from_object(signature.return_annotation, built_objects, module, parent=func_node)[0] args_node.postinit(args, defaults, kwonlyargs, kw_defaults, annotations, kwonly_annotations, varargannotation, kwargannotation) - func_node.postinit(args=args_node, body=[]) + func_node.postinit(args=args_node, body=[], returns=returns) for name in set(dir(func)) - set(dir(type(func))): # This checks against method special attributes because # methods are also dispatched through this function. From dfd6004a268214a031626fedabe4b4047cf9443a Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 17 Nov 2015 00:39:50 +0200 Subject: [PATCH 091/312] Add missing _proxied methods. --- astroid/interpreter/objects.py | 6 ++++++ astroid/tree/node_classes.py | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/astroid/interpreter/objects.py b/astroid/interpreter/objects.py index b866f1bdd0..9d5d003841 100644 --- a/astroid/interpreter/objects.py +++ b/astroid/interpreter/objects.py @@ -25,6 +25,7 @@ Call(func=Name('frozenset'), args=Tuple(...)) """ import sys +import types import six @@ -360,6 +361,11 @@ def __repr__(self): def __str__(self): return 'Generator(%s)' % (self._proxied.name) + @decorators.cachedproperty + def _proxied(self): + builtins = MANAGER.astroid_cache[BUILTINS] + return builtins.getattr(types.GeneratorType.__name__)[0] + @util.register_implementation(runtimeabc.FrozenSet) class FrozenSet(base.BaseContainer, Instance): diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index 5afdc71001..8e01c9fb1d 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -944,6 +944,11 @@ def pytype(self): def getitem(self, index, context=None): return _container_getitem(self, self.elts, index) + @decorators.cachedproperty + def _proxied(self): + builtins = MANAGER.astroid_cache[BUILTINS] + return builtins.getattr('list')[0] + @util.register_implementation(treeabc.Nonlocal) class Nonlocal(Statement): @@ -1027,6 +1032,11 @@ class Set(base.BaseContainer, objects.BaseInstance): def pytype(self): return '%s.set' % BUILTINS + @decorators.cachedproperty + def _proxied(self): + builtins = MANAGER.astroid_cache[BUILTINS] + return builtins.getattr('set')[0] + @util.register_implementation(treeabc.Slice) class Slice(base.NodeNG): @@ -1155,6 +1165,11 @@ def pytype(self): def getitem(self, index, context=None): return _container_getitem(self, self.elts, index) + @decorators.cachedproperty + def _proxied(self): + builtins = MANAGER.astroid_cache[BUILTINS] + return builtins.getattr('tuple')[0] + @util.register_implementation(treeabc.UnaryOp) class UnaryOp(base.NodeNG): From 8350772355032bec138b073c196b1712f946531f Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 17 Nov 2015 00:46:38 +0200 Subject: [PATCH 092/312] Remove any occurence of helpers. --- astroid/tests/unittest_helpers.py | 25 ++++++++++++------------- astroid/tests/unittest_inference.py | 1 - 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/astroid/tests/unittest_helpers.py b/astroid/tests/unittest_helpers.py index 214d388ce5..6be6512a5a 100644 --- a/astroid/tests/unittest_helpers.py +++ b/astroid/tests/unittest_helpers.py @@ -23,7 +23,6 @@ from six.moves import builtins from astroid import builder -from astroid import helpers from astroid.interpreter import util as interpreterutil from astroid import manager from astroid import nodes @@ -58,7 +57,7 @@ def test_object_type(self): ] for code, expected in pairs: node = test_utils.extract_node(code) - objtype = helpers.object_type(node) + objtype = interpreterutil.object_type(node) self.assertIs(objtype, expected) def test_object_type_classes_and_functions(self): @@ -83,14 +82,14 @@ def static_method(): pass A().static_method #@ generator() #@ ''') - from_self = helpers.object_type(ast_nodes[0]) + from_self = interpreterutil.object_type(ast_nodes[0]) cls = next(ast_nodes[1].infer()) self.assertIs(from_self, cls) - cls_type = helpers.object_type(ast_nodes[1]) + cls_type = interpreterutil.object_type(ast_nodes[1]) self.assertIs(cls_type, self._look_up_in_builtins('type')) - instance_type = helpers.object_type(ast_nodes[2]) + instance_type = interpreterutil.object_type(ast_nodes[2]) cls = next(ast_nodes[2].infer())._proxied self.assertIs(instance_type, cls) @@ -104,7 +103,7 @@ def static_method(): pass (ast_nodes[9], types.GeneratorType.__name__), ] for node, expected in expected_method_types: - node_type = helpers.object_type(node) + node_type = interpreterutil.object_type(node) expected_type = self._look_up_in_builtins(expected) self.assertIs(node_type, expected_type) @@ -116,11 +115,11 @@ class Meta(metaclass=abc.ABCMeta): pass meta_instance = Meta() ''') - meta_type = helpers.object_type(module['Meta']) + meta_type = interpreterutil.object_type(module['Meta']) self.assertIs(meta_type, module['Meta'].metaclass()) meta_instance = next(module['meta_instance'].infer()) - instance_type = helpers.object_type(meta_instance) + instance_type = interpreterutil.object_type(meta_instance) self.assertIs(instance_type, module['Meta']) @test_utils.require_version(minver='3.0') @@ -138,7 +137,7 @@ class D(B , C): #@ ''') metaclass = node.metaclass() self.assertEqual(metaclass.name, 'A') - obj_type = helpers.object_type(node) + obj_type = interpreterutil.object_type(node) self.assertEqual(metaclass, obj_type) def test_inference_errors(self): @@ -146,7 +145,7 @@ def test_inference_errors(self): from unknown import Unknown u = Unknown #@ ''') - self.assertEqual(helpers.object_type(node), util.Uninferable) + self.assertEqual(interpreterutil.object_type(node), util.Uninferable) def test_object_type_too_many_types(self): node = test_utils.extract_node(''' @@ -158,7 +157,7 @@ def test(x): return 1 test(Unknown) #@ ''') - self.assertEqual(helpers.object_type(node), util.Uninferable) + self.assertEqual(interpreterutil.object_type(node), util.Uninferable) def test_is_subtype(self): ast_nodes = test_utils.extract_node(''' @@ -173,7 +172,7 @@ class C(A): pass #@ cls_b = ast_nodes[1] cls_c = ast_nodes[2] int_subclass = ast_nodes[3] - int_subclass = helpers.object_type(next(int_subclass.infer())) + int_subclass = interpreterutil.object_type(next(int_subclass.infer())) base_int = self._look_up_in_builtins('int') self.assertTrue(interpreterutil.is_subtype(int_subclass, base_int)) self.assertTrue(interpreterutil.is_supertype(base_int, int_subclass)) @@ -254,7 +253,7 @@ def test_old_style_class(self): # TODO: what is this test supposed to be testing? It will crash as-is because it calls helpers. cls = test_utils.extract_node('''class A: pass''') builtin_type = self._look_up_in_builtins('type') - self.assertEqual(helpers.object_type(cls), builtin_type) + self.assertEqual(interpreterutil.object_type(cls), builtin_type) if __name__ == '__main__': diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index 830e09d077..13e4108af1 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -35,7 +35,6 @@ from astroid.interpreter.util import safe_infer from astroid import decorators as decoratorsmod -from astroid import helpers from astroid import test_utils from astroid import util from astroid.tests import resources From f55de589a0283e57b7b6926f627858775844b58f Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 17 Nov 2015 01:50:49 +0200 Subject: [PATCH 093/312] Add into instance_attrs the actual AST, not the resulting tuple. --- astroid/builder.py | 2 +- astroid/raw_building.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/astroid/builder.py b/astroid/builder.py index 9577607280..2ef2e4b786 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -218,7 +218,7 @@ def delayed_assignments(root): # FunctionDef, or Lambda nodes, or an Instance object # corresponding to a ClassDef node. for inferred in node.expr.infer(): - if type(inferred) is runtimeabc.Instance: + if isinstance(inferred, runtimeabc.Instance): values = inferred._proxied.instance_attrs[node.attrname] elif isinstance(inferred, treeabc.Lambda): values = inferred.instance_attrs[node.attrname] diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 64cfec2be1..6e4ee41c65 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -201,7 +201,7 @@ def _ast_from_object(instance, built_objects, module, name=None, parent=None): for name in set(dir(instance)) - set(dir(cls)): if name not in objects.Instance.special_attributes: ast = _ast_from_object(getattr(instance, name), built_objects, - module, name=name, parent=parent) + module, name=name, parent=parent)[0] class_node.instance_attrs[name].append(ast) # Create an instance of the class we just created an AST for. @@ -389,7 +389,7 @@ def extract_vararg(parameter): # methods are also dispatched through this function. if name not in objects.BoundMethod.special_attributes: ast = _ast_from_object(getattr(func, name), built_objects, - module, name=name, parent=parent) + module, name=name, parent=parent)[0] func_node.instance_attrs[name].append(ast) return (func_node,) From 402cb4be3fd10aebe632179d2b230b8f0e493ba2 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Tue, 17 Nov 2015 01:08:29 -0500 Subject: [PATCH 094/312] Make AST creation lazy for instances of imported classes in ast_from_object --- astroid/raw_building.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 6e4ee41c65..5ad2f4e1b6 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -55,6 +55,7 @@ def __iter__(self): except ImportError: from funcsigs import signature as _signature, Parameter as _Parameter +import lazy_object_proxy import six from astroid.interpreter import objects @@ -181,13 +182,17 @@ def _ast_from_object(instance, built_objects, module, name=None, parent=None): if isinstance(node, scoped_nodes.ClassDef): class_node = node elif isinstance(node, node_classes.ImportFrom): - # Handle ImportFrom chains. - while True: - modname = node.modname - node = MANAGER.ast_from_module_name(modname).getattr(cls.__name__)[0] - if isinstance(node, scoped_nodes.ClassDef): - class_node = node - break + def lazy_instance(node=node, cls=cls, name=name, parent=parent): + # Handle ImportFrom chains. + while True: + modname = node.modname + node = MANAGER.ast_from_module_name(modname).getattr(cls.__name__)[0] + if isinstance(node, scoped_nodes.ClassDef): + # class_node = node + # break + return node_classes.InterpreterObject(name=name, object_=node.instantiate_class(), parent=parent) + result.append(lazy_object_proxy.Proxy(lazy_instance)) + return result elif isinstance(node, node_classes.Name): # A Name node means a ClassDef node already exists somewhere, # so there's no need to add another one. From 4b142b1c8805f52b083475479319b4f4a67c7cb4 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Tue, 17 Nov 2015 17:13:48 -0500 Subject: [PATCH 095/312] Fixed bugs introduced by merging modular-locals into 2.0. * Add comment and memoization to the lazy instance in raw_building. * Work around obscure pylib/apipkg issue. * Add a skip to numpy test that's not terminating on Python 2. * Fix module special attributes. * Make errors for old-style classes in mro() TypeErrors, not NotImplementedErrors. * Add special nodes to treeabc. * Add {posargs} to tox.ini to enable passing -v to unittest. --- astroid/raw_building.py | 22 ++++++++++++++++++- astroid/tests/unittest_regrtest.py | 1 + astroid/tree/node_classes.py | 5 ++++- astroid/tree/scoped_nodes.py | 34 ++++++++++++++++++------------ astroid/tree/treeabc.py | 14 ++++++++++++ tox.ini | 2 +- 6 files changed, 62 insertions(+), 16 deletions(-) diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 5ad2f4e1b6..4e683fb8a8 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -182,6 +182,10 @@ def _ast_from_object(instance, built_objects, module, name=None, parent=None): if isinstance(node, scoped_nodes.ClassDef): class_node = node elif isinstance(node, node_classes.ImportFrom): + # TODO: this solution is not complete, because it doesn't add + # instance attributes, but adding instance attributes to class + # nodes causes problems in general and laziness in the + # building system needs to be refatored in general. def lazy_instance(node=node, cls=cls, name=name, parent=parent): # Handle ImportFrom chains. while True: @@ -192,6 +196,7 @@ def lazy_instance(node=node, cls=cls, name=name, parent=parent): # break return node_classes.InterpreterObject(name=name, object_=node.instantiate_class(), parent=parent) result.append(lazy_object_proxy.Proxy(lazy_instance)) + built_objects[id(instance)] = result[-1] return result elif isinstance(node, node_classes.Name): # A Name node means a ClassDef node already exists somewhere, @@ -264,7 +269,22 @@ def ast_from_module(module, built_objects, parent_module, name=None, parent=None return (node_classes.Name(name=name or module.__name__, parent=parent_module),) if module is not parent_module: # This module has been imported into another. - return (node_classes.Import([[module.__name__, name]], parent=parent),) + + # TODO: this work around an obscure issue in pylib/apikpkg, + # https://pylib.readthedocs.org/en/latest/index.html / + # https://bitbucket.org/hpk42/apipkg. In its + # __init__.py, pylib has the following line: + + # sys.modules['py.error'] = _apipkg.AliasModule("py.error", "py._error", 'error') + + # AliasModule is a very odd object that has a getattr method + # that raises an AttributeError for, among other things, + # __name__. This probably doesn't comply with the contract + # for things that should be in sys.modules. + try: + return (node_classes.Import([[module.__name__, name]], parent=parent),) + except AttributeError: + return () try: source_file = inspect.getsourcefile(module) except TypeError: diff --git a/astroid/tests/unittest_regrtest.py b/astroid/tests/unittest_regrtest.py index 9ae8663df1..e4e5f6ee0a 100644 --- a/astroid/tests/unittest_regrtest.py +++ b/astroid/tests/unittest_regrtest.py @@ -137,6 +137,7 @@ def test_pylint_config_attr(self): self.assertEqual(inferred[0].root().name, 'optparse') self.assertEqual(inferred[0].name, 'Values') + @unittest.skipIf(six.PY2, "TODO: numpy is currently infinitely looping") def test_numpy_crash(self): """test don't crash on numpy""" #a crash occured somewhere in the past, and an diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index 8e01c9fb1d..24fa019c98 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -279,6 +279,7 @@ def _format_args(args, defaults=None, annotations=None): return ', '.join(values) +@util.register_implementation(treeabc.Unknown) class Unknown(base.NodeNG): '''This node represents a node in a constructed AST where introspection is not possible. At the moment, it's only used in @@ -557,7 +558,8 @@ def _proxied(self): builtins = MANAGER.astroid_cache[BUILTINS] return builtins.getattr(type(self.value).__name__)[0] - + +@util.register_implementation(treeabc.NameConstant) class NameConstant(Const): """Represents a builtin singleton, at the moment True, False, None, and NotImplemented. @@ -571,6 +573,7 @@ class NameConstant(Const): # # return builtins.getattr(str(self.value))[0] +@util.register_implementation(treeabc.ReservedName) class ReservedName(base.NodeNG): '''Used in the builtins AST to assign names to singletons.''' _astroid_fields = ('value',) diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 8daed13f2e..dad309cf28 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -284,15 +284,23 @@ class Module(LocalsDictNodeNG): package = None special_attributes = frozenset( - ('__name__', '__doc__', '__file__', '__dict__', '__package__', - '__cached__', '__spec__', '__loader__', six.moves.builtins.__name__)) - - # TODO: does __path__ even exist? - # # names of python special attributes (handled by getattr impl.) - # special_attributes = set(('__name__', '__doc__', '__file__', '__path__', - # '__dict__')) - # # names of module attributes available through the global scope - # scope_attrs = set(('__name__', '__doc__', '__file__', '__path__')) + # These attributes are listed in the data model documentation. + # https://docs.python.org/3/reference/datamodel.html#the-standard-type-hierarchy + ('__name__', '__doc__', '__file__', '__dict__', + # __path__ is a standard attribute on *packages* not + # non-package modules. The only mention of it in the + # official 2.7 documentation I can find is in the + # tutorial. __package__ isn't mentioned anywhere outside a PEP: + # https://www.python.org/dev/peps/pep-0366/ + '__path__', '__package__', + # The builtins package is a member of every module's namespace. + six.moves.builtins.__name__, + # These are related to the Python 3 implementation of the + # import system, + # https://docs.python.org/3/reference/import.html#import-related-module-attributes + '__loader__', '__spec__', '__cached__')) + + # names of module attributes available through the global scope scope_attrs = frozenset(('__name__', '__doc__', '__file__')) if six.PY2: @@ -2023,7 +2031,7 @@ def slots(self): Otherwise, it will return a list of slot names. """ if not self.newstyle: - raise NotImplementedError( + raise TypeError( "The concept of slots is undefined for old-style classes.") slots = self._islots() @@ -2078,11 +2086,11 @@ def mro(self, context=None): """Get the method resolution order, using C3 linearization. It returns the list of ancestors sorted by the mro. - This will raise `NotImplementedError` for old-style classes, since + This will raise `MroError` for old-style classes, since they don't have the concept of MRO. """ if not self.newstyle: - raise NotImplementedError( + raise TypeError( "Could not obtain mro for old-style classes.") bases = list(self._inferred_bases(context=context)) @@ -2091,7 +2099,7 @@ def mro(self, context=None): try: mro = base.mro(context=context) bases_mro.append(mro) - except NotImplementedError: + except TypeError: # Some classes have in their ancestors both newstyle and # old style classes. For these we can't retrieve the .mro, # although in Python it's possible, since the class we are diff --git a/astroid/tree/treeabc.py b/astroid/tree/treeabc.py index b66dac7830..c9fa2269f7 100644 --- a/astroid/tree/treeabc.py +++ b/astroid/tree/treeabc.py @@ -119,6 +119,12 @@ class Const(NodeNG): """Represent a constant node like num, str, bool, None, bytes""" +class NameConstant(Const): + """Represents a builtin singleton, at the moment True, False, None, + and NotImplemented. + + """ + class Continue(Statement): """Class representing a Continue node""" @@ -321,3 +327,11 @@ class FunctionDef(Statement, Lambda): class InterpreterObject(NodeNG): pass + + +class ReservedName(NodeNG): + pass + + +class Unknown(NodeNG): + pass diff --git a/tox.ini b/tox.ini index 1da62d745c..c5c7924a32 100644 --- a/tox.ini +++ b/tox.ini @@ -19,4 +19,4 @@ deps = six wrapt pylint: hg+https://bitbucket.org/logilab/pylint -commands = python -m unittest discover -s {envsitepackagesdir}/astroid/tests -p "unittest*.py" +commands = python -m unittest discover {posargs} -s {envsitepackagesdir}/astroid/tests -p "unittest*.py" From c36ad29447bc6da965391c5cbbff64f32b2ae2d5 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Tue, 17 Nov 2015 22:58:53 -0500 Subject: [PATCH 096/312] Fix TypeErrors for old-style classes for __slots__ and MROs --- astroid/tests/unittest_scoped_nodes.py | 4 ++-- astroid/tree/scoped_nodes.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index b3852b0b59..17d5bddf23 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -1206,7 +1206,7 @@ class OldStyle: __slots__ = ("a", "b") """) msg = "The concept of slots is undefined for old-style classes." - with self.assertRaises(NotImplementedError) as cm: + with self.assertRaises(TypeError) as cm: module['OldStyle'].slots() self.assertEqual(str(cm.exception), msg) @@ -1227,7 +1227,7 @@ def assertEqualMro(self, klass, expected_mro): def test_no_mro_for_old_style(self): node = test_utils.extract_node(""" class Old: pass""") - with self.assertRaises(NotImplementedError) as cm: + with self.assertRaises(TypeError) as cm: node.mro() self.assertEqual(str(cm.exception), "Could not obtain mro for " "old-style classes.") diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index dad309cf28..fa95420c0d 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -2086,7 +2086,7 @@ def mro(self, context=None): """Get the method resolution order, using C3 linearization. It returns the list of ancestors sorted by the mro. - This will raise `MroError` for old-style classes, since + This will raise `TypeError` for old-style classes, since they don't have the concept of MRO. """ if not self.newstyle: From acebb2504bf3295f3f08e96e65322cff45dd052f Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Wed, 18 Nov 2015 00:13:03 -0500 Subject: [PATCH 097/312] Replace const_factory with Const call in CallSite.infer_arguments --- astroid/tree/scoped_nodes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index fa95420c0d..d9b89ddbd8 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -929,7 +929,7 @@ def infer_argument(self, name, context): kwarg = node_classes.Dict(lineno=self._funcnode.args.lineno, col_offset=self._funcnode.args.col_offset, parent=self._funcnode.args) - kwarg.postinit([(node_classes.const_factory(key), value) + kwarg.postinit([(node_classes.Const(key, parent=kwarg), value) for key, value in kwargs.items()]) return iter((kwarg, )) elif self._funcnode.args.vararg == name: From ab282dac0ad098433e81a81d610bced0c66742b9 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Wed, 18 Nov 2015 23:04:56 -0500 Subject: [PATCH 098/312] Make classes, modules, and functions available under both intrinsic and extrinsic names in mock ASTs --- astroid/brain/brain_builtin_inference.py | 2 +- astroid/raw_building.py | 92 ++++++++++++++++-------- astroid/tree/scoped_nodes.py | 4 +- 3 files changed, 66 insertions(+), 32 deletions(-) diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index 27a37c4942..5928a94588 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -91,7 +91,7 @@ def extend_builtins(class_transforms): 'str': functools.partial(_extend_str, rvalue="''")}) else: # TODO: what about unicode_literals? This is hopelessly broken. - extend_builtins({'bytes': functools.partial(_extend_str, rvalue="''"), # Ugly hack to get it working for now. + extend_builtins({'str': functools.partial(_extend_str, rvalue="''"), 'unicode': functools.partial(_extend_str, rvalue="u''")}) diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 4e683fb8a8..d07f407b15 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -99,7 +99,7 @@ def ast_from_object(object_, name=None): instance, know anything about the action of functions on their arguments. It uses InterpreterObject nodes as containers to insert astroid objects representing runtime-only objects like - ClassInstances and FrozenSets. ReservedNames are for special + Instances and FrozenSets. ReservedNames are for special names in the builtins module and Unknown nodes are used in place of Arguments nodes for functions who arguments can't be introspected. @@ -125,7 +125,6 @@ def ast_from_object(object_, name=None): inspect.getmodule(object_), name)[0] - @util.singledispatch def _ast_from_object(instance, built_objects, module, name=None, parent=None): '''Returns a mock AST for an instance. @@ -164,10 +163,10 @@ def _ast_from_object(instance, built_objects, module, name=None, parent=None): to construct an appropriate AST. ''' - # Since all ClassInstances pointing to the same ClassDef are + # Since all Instances pointing to the same ClassDef are # identical, they can all share the same node. - if id(instance) in built_objects: - return (built_objects[id(instance)],) + if name is not None and id(instance) in built_objects: + return (_make_assignment(name, built_objects[id(instance)].name, parent),) # Since this ultimately inherits from object but not any type, # it's presumably an instance of some kind. @@ -185,23 +184,31 @@ def _ast_from_object(instance, built_objects, module, name=None, parent=None): # TODO: this solution is not complete, because it doesn't add # instance attributes, but adding instance attributes to class # nodes causes problems in general and laziness in the - # building system needs to be refatored in general. + # building system needs to be refactored in general. Note + # that encapsulating the instance attributes setting in a + # function won't work, because it contains a call to + # _ast_from_object, which takes a mutable object whose state + # won't be preserved in a closure. def lazy_instance(node=node, cls=cls, name=name, parent=parent): # Handle ImportFrom chains. while True: - modname = node.modname + try: + modname = node.modname + except AttributeError: + # print(node, parent) + raise node = MANAGER.ast_from_module_name(modname).getattr(cls.__name__)[0] if isinstance(node, scoped_nodes.ClassDef): # class_node = node # break return node_classes.InterpreterObject(name=name, object_=node.instantiate_class(), parent=parent) result.append(lazy_object_proxy.Proxy(lazy_instance)) - built_objects[id(instance)] = result[-1] + built_objects[id(instance)] = _NameAST(name, result[-1]) return result - elif isinstance(node, node_classes.Name): + elif isinstance(node, node_classes.Assign): # A Name node means a ClassDef node already exists somewhere, # so there's no need to add another one. - class_node = built_objects[id(cls)] + class_node = built_objects[id(cls)].ast result = [] else: raise TypeError('Unexpected node, %s, when calling _ast_from_object on ' @@ -216,7 +223,8 @@ def lazy_instance(node=node, cls=cls, name=name, parent=parent): # Create an instance of the class we just created an AST for. result.append(node_classes.InterpreterObject(name=name, object_=class_node.instantiate_class(), parent=parent)) - built_objects[id(instance)] = result[-1] + if name is not None: + built_objects[id(instance)] = _NameAST(name, result[-1]) return result @@ -224,9 +232,12 @@ def lazy_instance(node=node, cls=cls, name=name, parent=parent): @_ast_from_object.register(type) def ast_from_class(cls, built_objects, module, name=None, parent=None): '''Handles classes and other types not handled explicitly elsewhere.''' + # TODO: this can create duplicate objects if id(cls) in built_objects: - return (node_classes.Name(name=name or cls.__name__, parent=parent),) + return (_make_assignment(name, built_objects[id(cls)].name, parent),) + inspected_module = inspect.getmodule(cls) + # In some situations, a class claims to be from a module but isn't # available in that module. For example, the quit instance in # builtins is of type Quitter. On Python 2, this claims its @@ -246,8 +257,11 @@ def ast_from_class(cls, built_objects, module, name=None, parent=None): getattr(inspected_module, '__name__', None), names=[[cls.__name__, name]], parent=parent),) - class_node = scoped_nodes.ClassDef(name=name or cls.__name__, doc=inspect.getdoc(cls), parent=parent) - built_objects[id(cls)] = class_node + class_node = scoped_nodes.ClassDef(name=cls.__name__, doc=inspect.getdoc(cls), parent=parent) + result = [class_node] + if name is not None and name != cls.__name__: + result.append(_make_assignment(name, cls.__name__, parent)) + built_objects[id(cls)] = _NameAST(cls.__name__, class_node) built_objects = _ChainMap({}, *built_objects.maps) bases = [node_classes.Name(name=b.__name__, parent=class_node) for b in cls.__bases__] @@ -256,7 +270,7 @@ def ast_from_class(cls, built_objects, module, name=None, parent=None): for t in _ast_from_object(a.object, built_objects, module, a.name, parent=class_node)] class_node.postinit(bases=bases, body=body, decorators=(), newstyle=isinstance(cls, type)) - return (class_node,) + return result # Old-style classes if six.PY2: _ast_from_object.register(types.ClassType, ast_from_class) @@ -266,7 +280,7 @@ def ast_from_class(cls, built_objects, module, name=None, parent=None): @_ast_from_object.register(types.ModuleType) def ast_from_module(module, built_objects, parent_module, name=None, parent=None): if id(module) in built_objects: - return (node_classes.Name(name=name or module.__name__, parent=parent_module),) + return (_make_assignment(name, built_objects[id(module)].name, parent),) if module is not parent_module: # This module has been imported into another. @@ -300,7 +314,7 @@ def ast_from_module(module, built_objects, parent_module, name=None, parent=None # Assume that if inspect couldn't find a Python source file, it's # probably not implemented in pure Python. pure_python=bool(source_file)) - built_objects[id(module)] = module_node + built_objects[id(module)] = _NameAST(module_node.name, module_node) built_objects = _ChainMap({}, *built_objects.maps) # MANAGER.cache_module(module_node) body = [ @@ -327,17 +341,20 @@ def ast_from_module(module, built_objects, parent_module, name=None, parent=None def ast_from_function(func, built_objects, module, name=None, parent=None): '''Handles functions, including all kinds of methods.''' if id(func) in built_objects: - return (node_classes.Name(name=name or func.__name__, parent=parent),) + return (_make_assignment(name, built_objects[id(func)].name, parent),) inspected_module = inspect.getmodule(func) if inspected_module is not None and inspected_module is not module: return (node_classes.ImportFrom( fromname=getattr(inspected_module, '__name__', None), names=[[func.__name__, name]], parent=parent),) - func_node = scoped_nodes.FunctionDef(name=name or func.__name__, + func_node = scoped_nodes.FunctionDef(name=func.__name__, doc=inspect.getdoc(func), parent=parent) - built_objects[id(func)] = func_node + result = [func_node] + if name is not None and name != func.__name__: + result.append(_make_assignment(name, func.__name__, parent)) + built_objects[id(func)] = _NameAST(func.__name__, func_node) built_objects = _ChainMap({}, *built_objects.maps) try: signature = _signature(func) @@ -416,7 +433,7 @@ def extract_vararg(parameter): ast = _ast_from_object(getattr(func, name), built_objects, module, name=name, parent=parent)[0] func_node.instance_attrs[name].append(ast) - return (func_node,) + return result BUILTIN_CONTAINERS = {list: node_classes.List, set: node_classes.Set, frozenset: @@ -431,8 +448,8 @@ def ast_from_builtin_container(container, built_objects, module, name=None, parent=None): '''Handles builtin container types except for mappings.''' if (id(container) in built_objects and - built_objects[id(container)].targets[0].name == name): - return (node_classes.Name(name=name, parent=parent),) + built_objects[id(container)].name != name): + return (_make_assignment(name, built_objects[id(container)].name, parent),) if name: parent = node_classes.Assign(parent=parent) name_node = node_classes.AssignName(name, parent=parent) @@ -447,7 +464,8 @@ def ast_from_builtin_container(container, built_objects, module, name=None, node = parent else: node = container_node - built_objects[id(container)] = node + if name is not None: + built_objects[id(container)] = _NameAST(name, node) container_node.postinit( elts=[t for i in container for t in _ast_from_object(i, built_objects, module, parent=node)]) @@ -462,8 +480,8 @@ def ast_from_dict(dictionary, built_objects, module, name=None, parent=None): '''Handles dictionaries, including DictProxyType and MappingProxyType.''' if (id(dictionary) in built_objects and - built_objects[id(dictionary)].targets[0].name == name): - return (node_classes.Name(name=name, parent=parent),) + built_objects[id(dictionary)].name != name): + return (_make_assignment(name, built_objects[id(dictionary)].name, parent),) if name: parent = node_classes.Assign(parent=parent) name_node = node_classes.AssignName(name, parent=parent) @@ -472,7 +490,8 @@ def ast_from_dict(dictionary, built_objects, module, name=None, node = parent else: node = dict_node - built_objects[id(dictionary)] = node + if name is not None: + built_objects[id(dictionary)] = _NameAST(name, node) dict_node.postinit(items=[ (x, y) for k, v in dictionary.items() for x, y in zip(_ast_from_object(k, built_objects, module, parent=node), @@ -495,10 +514,15 @@ def ast_from_dict(dictionary, built_objects, module, name=None, @_ast_from_object.register(complex) def ast_from_builtin_number_text_binary(builtin_number_text_binary, built_objects, module, name=None, parent=None): '''Handles the builtin numeric and text/binary sequence types.''' + if (id(builtin_number_text_binary) in built_objects and + built_objects[id(builtin_number_text_binary)].name != name): + return (_make_assignment(name, built_objects[id(builtin_number_text_binary)].name, parent),) if name: parent = node_classes.Assign(parent=parent) name_node = node_classes.AssignName(name, parent=parent) builtin_number_text_binary_node = node_classes.Const(value=builtin_number_text_binary, parent=parent) + if name is not None: + built_objects[id(builtin_number_text_binary)] = _NameAST(name, builtin_number_text_binary_node) if name: parent.postinit(targets=[name_node], value=builtin_number_text_binary_node) node = parent @@ -565,6 +589,16 @@ def ast_from_builtin_singleton(builtin_singleton, built_objects, _ast_from_object.register(singleton_type, ast_from_builtin_singleton_factory(node_type)) +def _make_assignment(new_name, old_name, parent): + assign_node = node_classes.Assign(parent=parent) + target_node = node_classes.AssignName(new_name, parent=assign_node) + value_node = node_classes.Name(old_name, parent=assign_node) + assign_node.postinit(targets=[target_node], value=value_node) + return assign_node + +_NameAST = collections.namedtuple('_NameAST', 'name ast') + + # @scoped_nodes.get_locals.register(Builtins) # def scoped_node(node): # locals_ = collections.defaultdict(list) @@ -590,14 +624,14 @@ def ast_from_builtins(): # that the types are included as Name nodes, not explicit ASTs. built_objects = _ChainMap({}) for builtin_type in BUILTIN_TYPES: - built_objects[id(builtin_type)] = _ast_from_object(builtin_type, built_objects, six.moves.builtins)[0] + built_objects[id(builtin_type)] = _NameAST(builtin_type.__name__, _ast_from_object(builtin_type, built_objects, six.moves.builtins)[0]) builtins_ast = _ast_from_object(six.moves.builtins, built_objects, six.moves.builtins)[0] for builtin_type in BUILTIN_TYPES: - type_node = built_objects[id(builtin_type)] + type_node = built_objects[id(builtin_type)].ast builtins_ast.body.append(type_node) type_node.parent = builtins_ast diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index d9b89ddbd8..10aece2d08 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -139,8 +139,8 @@ def builtin_lookup(name): return builtin_astroid, () stmts = builtin_astroid.locals.get(name, ()) # Use inference to find what AssignName nodes point to in builtins. - # stmts = [next(s.infer()) if isinstance(s, node_classes.ImportFrom) else s - # for s in stmts] + stmts = [next(s.infer()) if isinstance(s, node_classes.AssignName) else s + for s in stmts] return builtin_astroid, stmts From 5bd237f7e52fa74eb06d897910fee6c810e71a83 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Wed, 18 Nov 2015 23:56:03 -0500 Subject: [PATCH 099/312] Ensure ReservedName nodes are only created in builtins --- astroid/raw_building.py | 54 ++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/astroid/raw_building.py b/astroid/raw_building.py index d07f407b15..8dae36347e 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -551,42 +551,25 @@ def ast_from_builtin_singleton(builtin_singleton, built_objects, node_class=node_class): '''Handles builtin singletons, currently True, False, None, NotImplemented, and Ellipsis.''' - # A builtin singleton is assigned to a name. if name and name != str(builtin_singleton): - parent = node_classes.Assign(parent=parent) - name_node = node_classes.AssignName(name=name, parent=parent) - # This case handles the initial assignment of singletons to names - # in the builtins module. It can be triggered in other cases with - # an object that contains a builtin singleton by its own name, like, - # - # class C: - # None - # - # but there's never any reason to write that kind of code since - # it's a no-op, and even if it happens it shouldn't cause any - # harm. - elif name and name == str(builtin_singleton): - parent = node_classes.ReservedName(name=name, parent=parent) - builtin_singleton_node = node_class(value=builtin_singleton, parent=parent) - if name and name != str(builtin_singleton): - parent.postinit(targets=[name_node], value=builtin_singleton_node) - node = parent - elif name and name == str(builtin_singleton): - parent.postinit(value=builtin_singleton_node) - node = parent + assign_node = node_classes.Assign(parent=parent) + target_node = node_classes.AssignName(name, parent=assign_node) + value_node = node_class(builtin_singleton, parent=assign_node) + assign_node.postinit(targets=[target_node], value=value_node) + return (assign_node,) else: - node = builtin_singleton_node - return (node,) + return (node_class(builtin_singleton, parent=parent),) return ast_from_builtin_singleton -BUILTIN_SINGLETONS = {type(None): node_classes.NameConstant, - type(NotImplemented): node_classes.NameConstant, - bool: node_classes.NameConstant, - type(Ellipsis): lambda value=None, parent=None: +BUILTIN_SINGLETONS = {None: node_classes.NameConstant, + NotImplemented: node_classes.NameConstant, + False: node_classes.NameConstant, + True: node_classes.NameConstant, + Ellipsis: lambda value=None, parent=None: node_classes.Ellipsis(parent=parent)} -for singleton_type, node_type in BUILTIN_SINGLETONS.items(): - _ast_from_object.register(singleton_type, ast_from_builtin_singleton_factory(node_type)) +for singleton, node_class in BUILTIN_SINGLETONS.items(): + _ast_from_object.register(type(singleton), ast_from_builtin_singleton_factory(node_class)) def _make_assignment(new_name, old_name, parent): @@ -626,6 +609,12 @@ def ast_from_builtins(): for builtin_type in BUILTIN_TYPES: built_objects[id(builtin_type)] = _NameAST(builtin_type.__name__, _ast_from_object(builtin_type, built_objects, six.moves.builtins)[0]) + for singleton, node_class in BUILTIN_SINGLETONS.items(): + reserved_name = node_classes.ReservedName(name=str(singleton)) + singleton_node = node_class(value=singleton, parent=reserved_name) + reserved_name.postinit(value=singleton_node) + built_objects[id(singleton)] = _NameAST(str(singleton), reserved_name) + builtins_ast = _ast_from_object(six.moves.builtins, built_objects, six.moves.builtins)[0] @@ -635,6 +624,11 @@ def ast_from_builtins(): builtins_ast.body.append(type_node) type_node.parent = builtins_ast + for singleton, _ in BUILTIN_SINGLETONS.items(): + reserved_name = built_objects[id(singleton)].ast + builtins_ast.body.append(reserved_name) + reserved_name.parent = builtins_ast + MANAGER.astroid_cache[six.moves.builtins.__name__] = builtins_ast return builtins_ast From 8b562744228c69923eb8549e56a0a4bacb7f76f9 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Thu, 19 Nov 2015 09:25:42 -0500 Subject: [PATCH 100/312] Manually merge code into infer_type_dunder_new() --- astroid/brain/brain_builtin_inference.py | 35 ++++++++++++------------ 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index 5928a94588..4b5236cc3b 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -523,7 +523,7 @@ def infer_type_dunder_new(caller, context=None): """ if len(caller.args) != 4: raise UseInferenceDefault - + # Verify the metaclass mcs = next(caller.args[0].infer(context=context)) if not isinstance(mcs, nodes.ClassDef): @@ -543,42 +543,43 @@ def infer_type_dunder_new(caller, context=None): raise UseInferenceDefault # Verify the bases - cls_bases = next(caller.args[2].infer(context=context)) - if not isinstance(cls_bases, nodes.Tuple): + bases = next(caller.args[2].infer(context=context)) + if not isinstance(bases, nodes.Tuple): # Needs to be a tuple. raise UseInferenceDefault inferred_bases = [next(elt.infer(context=context)) - for elt in cls_bases.elts] - if any(not isinstance(base, nodes.ClassDef) for base in inferred_bases): + for elt in bases.elts] + if not all(isinstance(base, nodes.ClassDef) + for base in inferred_bases): # All the bases needs to be Classes raise UseInferenceDefault + cls = nodes.Class(name=name.value, lineno=caller.lineno, + col_offset=caller.col_offset, parent=caller) + # Verify the attributes. attrs = next(caller.args[3].infer(context=context)) if not isinstance(attrs, nodes.Dict): # Needs to be a dictionary. raise UseInferenceDefault - cls_locals = collections.defaultdict(list) + body = [] for key, value in attrs.items: key = next(key.infer(context=context)) value = next(value.infer(context=context)) if not isinstance(key, nodes.Const): # Something invalid as an attribute. - raise UseInferenceDefault + raise UseInferenceDefault if not isinstance(key.value, str): # Not a proper attribute. - raise UseInferenceDefault - cls_locals[key.value].append(value) + raise UseInferenceDefault + assign = nodes.Assign(parent=cls) + assign.postinit(targets=nodes.AssignName(key.value, parent=assign), + value=value) + body.append(assign) - # Build the class from now. - cls = nodes.Class(name=name.value, lineno=caller.lineno, - col_offset=caller.col_offset, - parent=caller) - empty = nodes.Pass() - cls.postinit(bases=cls_bases.elts, body=[empty], decorators=[], + cls.postinit(bases=bases.elts, body=body, decorators=[], newstyle=True, metaclass=mcs) - cls.locals = cls_locals - return iter([cls]) + return iter((cls,)) def _looks_like_type_dunder_new(node): From 041114d5f3906f8511f8ebb70409cb819077b528 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Thu, 19 Nov 2015 11:56:12 -0500 Subject: [PATCH 101/312] Fix inappropriate insertion fo Nones in raw_building --- astroid/raw_building.py | 98 ++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 49 deletions(-) diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 8dae36347e..79f5f31e56 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -276,54 +276,6 @@ def ast_from_class(cls, built_objects, module, name=None, parent=None): _ast_from_object.register(types.ClassType, ast_from_class) -# pylint: disable=unused-variable; doesn't understand singledispatch -@_ast_from_object.register(types.ModuleType) -def ast_from_module(module, built_objects, parent_module, name=None, parent=None): - if id(module) in built_objects: - return (_make_assignment(name, built_objects[id(module)].name, parent),) - if module is not parent_module: - # This module has been imported into another. - - # TODO: this work around an obscure issue in pylib/apikpkg, - # https://pylib.readthedocs.org/en/latest/index.html / - # https://bitbucket.org/hpk42/apipkg. In its - # __init__.py, pylib has the following line: - - # sys.modules['py.error'] = _apipkg.AliasModule("py.error", "py._error", 'error') - - # AliasModule is a very odd object that has a getattr method - # that raises an AttributeError for, among other things, - # __name__. This probably doesn't comply with the contract - # for things that should be in sys.modules. - try: - return (node_classes.Import([[module.__name__, name]], parent=parent),) - except AttributeError: - return () - try: - source_file = inspect.getsourcefile(module) - except TypeError: - # inspect.getsourcefile raises TypeError for built-in modules. - source_file = None - module_node = scoped_nodes.Module( - name=name or module.__name__, - # inspect.getdoc returns None for modules without docstrings like - # Jython Java modules. - doc=inspect.getdoc(module), - source_file=source_file, - package=hasattr(module, '__path__'), - # Assume that if inspect couldn't find a Python source file, it's - # probably not implemented in pure Python. - pure_python=bool(source_file)) - built_objects[id(module)] = _NameAST(module_node.name, module_node) - built_objects = _ChainMap({}, *built_objects.maps) - # MANAGER.cache_module(module_node) - body = [ - t for n, m in inspect.getmembers(module) if n not in scoped_nodes.Module.special_attributes - for t in _ast_from_object(m, built_objects, module, n, module_node)] - module_node.postinit(body=body) - return (module_node,) - - # pylint: disable=unused-variable; doesn't understand singledispatch # These two types are the same on CPython but not necessarily the same @@ -436,6 +388,54 @@ def extract_vararg(parameter): return result +# pylint: disable=unused-variable; doesn't understand singledispatch +@_ast_from_object.register(types.ModuleType) +def ast_from_module(module, built_objects, parent_module, name=None, parent=None): + if id(module) in built_objects: + return (_make_assignment(name, built_objects[id(module)].name, parent),) + if module is not parent_module: + # This module has been imported into another. + + # TODO: this work around an obscure issue in pylib/apikpkg, + # https://pylib.readthedocs.org/en/latest/index.html / + # https://bitbucket.org/hpk42/apipkg. In its + # __init__.py, pylib has the following line: + + # sys.modules['py.error'] = _apipkg.AliasModule("py.error", "py._error", 'error') + + # AliasModule is a very odd object that has a getattr method + # that raises an AttributeError for, among other things, + # __name__. This probably doesn't comply with the contract + # for things that should be in sys.modules. + try: + return (node_classes.Import([[module.__name__, name]], parent=parent),) + except AttributeError: + return () + try: + source_file = inspect.getsourcefile(module) + except TypeError: + # inspect.getsourcefile raises TypeError for built-in modules. + source_file = None + module_node = scoped_nodes.Module( + name=name or module.__name__, + # inspect.getdoc returns None for modules without docstrings like + # Jython Java modules. + doc=inspect.getdoc(module), + source_file=source_file, + package=hasattr(module, '__path__'), + # Assume that if inspect couldn't find a Python source file, it's + # probably not implemented in pure Python. + pure_python=bool(source_file)) + built_objects[id(module)] = _NameAST(module_node.name, module_node) + built_objects = _ChainMap({}, *built_objects.maps) + # MANAGER.cache_module(module_node) + body = [ + t for n, m in inspect.getmembers(module) if n not in scoped_nodes.Module.special_attributes + for t in _ast_from_object(m, built_objects, module, n, module_node)] + module_node.postinit(body=body) + return (module_node,) + + BUILTIN_CONTAINERS = {list: node_classes.List, set: node_classes.Set, frozenset: objects.FrozenSet, tuple: node_classes.Tuple} @@ -514,7 +514,7 @@ def ast_from_dict(dictionary, built_objects, module, name=None, @_ast_from_object.register(complex) def ast_from_builtin_number_text_binary(builtin_number_text_binary, built_objects, module, name=None, parent=None): '''Handles the builtin numeric and text/binary sequence types.''' - if (id(builtin_number_text_binary) in built_objects and + if (name is not None and id(builtin_number_text_binary) in built_objects and built_objects[id(builtin_number_text_binary)].name != name): return (_make_assignment(name, built_objects[id(builtin_number_text_binary)].name, parent),) if name: From 50690e4b23dd3463de6835134ffb533197791b2e Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Thu, 19 Nov 2015 23:06:55 -0500 Subject: [PATCH 102/312] Fix bugs introduced during modular-locals merge --- astroid/protocols.py | 2 +- astroid/tests/unittest_inference.py | 2 ++ astroid/tree/scoped_nodes.py | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/astroid/protocols.py b/astroid/protocols.py index 550762cd14..c756011f64 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -149,7 +149,7 @@ def infer_binary_op(self, operator, other, context, method, nodes): @infer_binary_op.register(treeabc.Const) @decorators.yes_if_nothing_inferred def const_infer_binary_op(self, operator, other, context, _, nodes): - not_implemented = nodes.const_factory(NotImplemented) + not_implemented = nodes.NameConstant(NotImplemented) if isinstance(other, treeabc.Const): try: impl = BIN_OP_IMPL[operator] diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index 13e4108af1..8dd92a4048 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -2807,8 +2807,10 @@ def pos(self): self.assertEqual(inferred.value, 42) def _slicing_test_helper(self, pairs, cls, get_elts): + # print('Slicing test:', pairs) for code, expected in pairs: ast_node = test_utils.extract_node(code) + # print(ast_node.repr_tree()) inferred = next(ast_node.infer()) self.assertIsInstance(inferred, cls) self.assertEqual(get_elts(inferred), expected, diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 10aece2d08..05c699c622 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -301,7 +301,7 @@ class Module(LocalsDictNodeNG): '__loader__', '__spec__', '__cached__')) # names of module attributes available through the global scope - scope_attrs = frozenset(('__name__', '__doc__', '__file__')) + scope_attrs = frozenset(('__name__', '__doc__', '__file__', '__path__')) if six.PY2: _other_fields = ('name', 'doc', 'file_encoding', 'package', From 58e2a355d0db36c77774a79bfaa9a99a1d30a141 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Fri, 20 Nov 2015 14:25:22 -0500 Subject: [PATCH 103/312] Delete whitespace and debug statements --- astroid/brain/brain_numpy.py | 2 +- astroid/raw_building.py | 2 ++ astroid/tests/unittest_inference.py | 2 -- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/astroid/brain/brain_numpy.py b/astroid/brain/brain_numpy.py index 75f4f18f33..25647ee9f3 100644 --- a/astroid/brain/brain_numpy.py +++ b/astroid/brain/brain_numpy.py @@ -56,7 +56,7 @@ def numpy_transform(): 'show_config'] + core.__all__ + _mat.__all__ + lib.__all__ ''') - + astroid.register_module_extender(astroid.MANAGER, 'numpy.core', numpy_core_transform) astroid.register_module_extender(astroid.MANAGER, 'numpy', numpy_transform) diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 79f5f31e56..8c4e195f50 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -573,6 +573,8 @@ def ast_from_builtin_singleton(builtin_singleton, built_objects, def _make_assignment(new_name, old_name, parent): + # if new_name is None: + # raise TypeError assign_node = node_classes.Assign(parent=parent) target_node = node_classes.AssignName(new_name, parent=assign_node) value_node = node_classes.Name(old_name, parent=assign_node) diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index 8dd92a4048..13e4108af1 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -2807,10 +2807,8 @@ def pos(self): self.assertEqual(inferred.value, 42) def _slicing_test_helper(self, pairs, cls, get_elts): - # print('Slicing test:', pairs) for code, expected in pairs: ast_node = test_utils.extract_node(code) - # print(ast_node.repr_tree()) inferred = next(ast_node.infer()) self.assertIsInstance(inferred, cls) self.assertEqual(get_elts(inferred), expected, From a228993b9e3a6415af15abf984794efcc2eda37e Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Fri, 20 Nov 2015 14:42:27 -0500 Subject: [PATCH 104/312] Add more legible docstring for repr_tree() that was lost in the merge, print_tree() shortcut method --- astroid/tree/base.py | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/astroid/tree/base.py b/astroid/tree/base.py index e81ded55ec..8a3b73805b 100644 --- a/astroid/tree/base.py +++ b/astroid/tree/base.py @@ -357,23 +357,19 @@ 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. + Args: + ids (bool): If true, includes the ids with the node type names. + include_linenos (bool): If true, includes the line numbers and + column offsets. + ast_state (bool): If true, includes information derived from + the whole AST like local and global variables. + indent (str): A string to use to indent the output string. + max_depth (int): If set to a positive integer, won't return + nodes deeper than max_depth in the string. + max_width (int): 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. """ @util.singledispatch def _repr_tree(node, result, done, cur_indent='', depth=1): @@ -471,6 +467,10 @@ def _repr_node(node, result, done, cur_indent='', depth=1): _repr_tree(self, result, set()) return ''.join(result) + def print_tree(self, *args, **kws): + """Shortcut method to print the result of repr_tree().""" + print(self.repr_tree(*args, **kws)) + def bool_value(self): """Determine the bool value of this node From 514440ed1390cd053a986d0f2679ea8379894a14 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sat, 21 Nov 2015 02:10:31 +0200 Subject: [PATCH 105/312] Materialize the items of the dict when building a Dict node from a live dictionary This fixes a bug that can occur when the dict object is changed during iteration. It can happen when it is sys.modules, coming through inference at some point and if there's an additional installed library that modifies sys.modules when it is imported, as it is the case for pytest.AliasModule. --- astroid/raw_building.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 79f5f31e56..72fa55fc62 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -493,7 +493,7 @@ def ast_from_dict(dictionary, built_objects, module, name=None, if name is not None: built_objects[id(dictionary)] = _NameAST(name, node) dict_node.postinit(items=[ - (x, y) for k, v in dictionary.items() + (x, y) for k, v in list(dictionary.items()) for x, y in zip(_ast_from_object(k, built_objects, module, parent=node), _ast_from_object(v, built_objects, module, parent=node))]) if name: From 1f09587f22490cfbbbe8764a15cc79ef298efbe1 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Sat, 21 Nov 2015 01:07:49 -0500 Subject: [PATCH 106/312] Prevent ast_from_object from inserting None instead of a string into trees and move caching from raw_building to manager --- astroid/manager.py | 4 +++- astroid/raw_building.py | 19 +++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/astroid/manager.py b/astroid/manager.py index 5824f0bea4..918ed9dfd5 100644 --- a/astroid/manager.py +++ b/astroid/manager.py @@ -201,7 +201,9 @@ def ast_from_module(self, module, modname=None): return self.ast_from_file(filepath, modname) except AttributeError: pass - return builder.AstroidBuilder(self).module_build(module, modname) + mock_ast = builder.AstroidBuilder(self).module_build(module, modname) + self.astroid_cache[modname] = mock_ast + return mock_ast def register_failed_import_hook(self, hook): """Registers a hook to resolve imports that cannot be found otherwise. diff --git a/astroid/raw_building.py b/astroid/raw_building.py index f7ccc2fdf4..96a5ffc910 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -65,6 +65,10 @@ def __iter__(self): from astroid import util +# TODO: handle the names of lambdas better, recurse into imported +# bases and metaclass. + + MANAGER = manager.AstroidManager() # This is a type used for some unbound methods implemented in C on @@ -234,7 +238,7 @@ def ast_from_class(cls, built_objects, module, name=None, parent=None): '''Handles classes and other types not handled explicitly elsewhere.''' # TODO: this can create duplicate objects if id(cls) in built_objects: - return (_make_assignment(name, built_objects[id(cls)].name, parent),) + return (_make_assignment(name or cls.__name__, built_objects[id(cls)].name, parent),) inspected_module = inspect.getmodule(cls) @@ -293,7 +297,7 @@ def ast_from_class(cls, built_objects, module, name=None, parent=None): def ast_from_function(func, built_objects, module, name=None, parent=None): '''Handles functions, including all kinds of methods.''' if id(func) in built_objects: - return (_make_assignment(name, built_objects[id(func)].name, parent),) + return (_make_assignment(name or func.__name__, built_objects[id(func)].name, parent),) inspected_module = inspect.getmodule(func) if inspected_module is not None and inspected_module is not module: return (node_classes.ImportFrom( @@ -396,7 +400,7 @@ def ast_from_module(module, built_objects, parent_module, name=None, parent=None if module is not parent_module: # This module has been imported into another. - # TODO: this work around an obscure issue in pylib/apikpkg, + # TODO: this works around an obscure issue in pylib/apikpkg, # https://pylib.readthedocs.org/en/latest/index.html / # https://bitbucket.org/hpk42/apipkg. In its # __init__.py, pylib has the following line: @@ -428,7 +432,6 @@ def ast_from_module(module, built_objects, parent_module, name=None, parent=None pure_python=bool(source_file)) built_objects[id(module)] = _NameAST(module_node.name, module_node) built_objects = _ChainMap({}, *built_objects.maps) - # MANAGER.cache_module(module_node) body = [ t for n, m in inspect.getmembers(module) if n not in scoped_nodes.Module.special_attributes for t in _ast_from_object(m, built_objects, module, n, module_node)] @@ -493,6 +496,12 @@ def ast_from_dict(dictionary, built_objects, module, name=None, if name is not None: built_objects[id(dictionary)] = _NameAST(name, node) dict_node.postinit(items=[ + # This explicit list construction works around an obscure + # interaction: if _ast_from_dict is called on sys.modules and + # sys.modules contains a module that alters sys.modules upon + # import, as pytest.AliasModule does, then the recursive call + # can alter sys.modules's items() while it's being iterated + # over, causing a RuntimeError. (x, y) for k, v in list(dictionary.items()) for x, y in zip(_ast_from_object(k, built_objects, module, parent=node), _ast_from_object(v, built_objects, module, parent=node))]) @@ -573,8 +582,6 @@ def ast_from_builtin_singleton(builtin_singleton, built_objects, def _make_assignment(new_name, old_name, parent): - # if new_name is None: - # raise TypeError assign_node = node_classes.Assign(parent=parent) target_node = node_classes.AssignName(new_name, parent=assign_node) value_node = node_classes.Name(old_name, parent=assign_node) From 8167ae6e161590d1c54518363d8a59ff9648153e Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Sat, 21 Nov 2015 10:43:52 -0500 Subject: [PATCH 107/312] Fix the use of __all__ in wildcard_import_names for transformed modules --- astroid/raw_building.py | 7 +++---- astroid/tree/scoped_nodes.py | 7 ++++++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 96a5ffc910..c550ec8a3e 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -65,10 +65,6 @@ def __iter__(self): from astroid import util -# TODO: handle the names of lambdas better, recurse into imported -# bases and metaclass. - - MANAGER = manager.AstroidManager() # This is a type used for some unbound methods implemented in C on @@ -129,6 +125,9 @@ def ast_from_object(object_, name=None): inspect.getmodule(object_), name)[0] +# TODO: handle the names of lambdas better, recurse into imported +# bases and metaclass. + @util.singledispatch def _ast_from_object(instance, built_objects, module, name=None, parent=None): '''Returns a mock AST for an instance. diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 05c699c622..a5fe473cce 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -563,7 +563,12 @@ def wildcard_import_names(self): # to avoid catching too many Exceptions default = [name for name in self.keys() if not name.startswith('_')] if '__all__' in self: - all = self['__all__'] + # This is a workaround for the case where a module defines + # __all__ and then dynamically alters it, requiring a + # custom hack to define a static __all__ for the module. + # The static __all__ ends up at the end of the locals list + # for __all__, not the beginning. + all = self.locals['__all__'][-1] else: return default try: From d05d91f9fee915a7375f70df5df1e7d46fa79c15 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Sat, 21 Nov 2015 12:43:03 -0500 Subject: [PATCH 108/312] Remove some cases where Assign statements could wrongly contain None and add SimpleNamespace to the builtins mock ast --- astroid/raw_building.py | 56 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/astroid/raw_building.py b/astroid/raw_building.py index c550ec8a3e..6f89efc777 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -168,7 +168,10 @@ def _ast_from_object(instance, built_objects, module, name=None, parent=None): ''' # Since all Instances pointing to the same ClassDef are # identical, they can all share the same node. - if name is not None and id(instance) in built_objects: + if (name is not None and + id(instance) in built_objects and + built_objects[id(instance)].name is not None and + built_objects[id(instance)].name != name): return (_make_assignment(name, built_objects[id(instance)].name, parent),) # Since this ultimately inherits from object but not any type, @@ -449,7 +452,9 @@ def ast_from_module(module, built_objects, parent_module, name=None, parent=None def ast_from_builtin_container(container, built_objects, module, name=None, parent=None): '''Handles builtin container types except for mappings.''' - if (id(container) in built_objects and + if (name is not None and + id(container) in built_objects and + built_objects[id(container)].name is not None and built_objects[id(container)].name != name): return (_make_assignment(name, built_objects[id(container)].name, parent),) if name: @@ -466,8 +471,7 @@ def ast_from_builtin_container(container, built_objects, module, name=None, node = parent else: node = container_node - if name is not None: - built_objects[id(container)] = _NameAST(name, node) + built_objects[id(container)] = _NameAST(name, node) container_node.postinit( elts=[t for i in container for t in _ast_from_object(i, built_objects, module, parent=node)]) @@ -481,7 +485,9 @@ def ast_from_builtin_container(container, built_objects, module, name=None, def ast_from_dict(dictionary, built_objects, module, name=None, parent=None): '''Handles dictionaries, including DictProxyType and MappingProxyType.''' - if (id(dictionary) in built_objects and + if (name is not None and + id(dictionary) in built_objects and + built_objects[id(dictionary)].name is not None and built_objects[id(dictionary)].name != name): return (_make_assignment(name, built_objects[id(dictionary)].name, parent),) if name: @@ -522,7 +528,9 @@ def ast_from_dict(dictionary, built_objects, module, name=None, @_ast_from_object.register(complex) def ast_from_builtin_number_text_binary(builtin_number_text_binary, built_objects, module, name=None, parent=None): '''Handles the builtin numeric and text/binary sequence types.''' - if (name is not None and id(builtin_number_text_binary) in built_objects and + if (name is not None and + id(builtin_number_text_binary) in built_objects and + built_objects[id(builtin_number_text_binary)].name is not None and built_objects[id(builtin_number_text_binary)].name != name): return (_make_assignment(name, built_objects[id(builtin_number_text_binary)].name, parent),) if name: @@ -610,6 +618,7 @@ def _make_assignment(new_name, old_name, parent): types.GeneratorType, types.FunctionType, types.MethodType, types.BuiltinFunctionType, types.ModuleType) + def ast_from_builtins(): # Initialize the built_objects map for the builtins mock AST to ensure # that the types are included as Name nodes, not explicit ASTs. @@ -637,6 +646,41 @@ def ast_from_builtins(): builtins_ast.body.append(reserved_name) reserved_name.parent = builtins_ast + # There's another builtin type in CPython 3.3+ called + # SimpleNamespace. While there's a statement defining it in the + # types standard library module, + + # SimpleNamespace = type(sys.implementation) + + # this doesn't create a class for it, just refer to an existing + # class that's probably implemented in C. To avoid crashes that + # occur when ast_from_object tries to process instances of + # SimpleNamespace, this adds the type to the builtins mock ast. + if hasattr(types, 'SimpleNamespace'): + # + simple_namespace_type = _ast_from_object(types.SimpleNamespace, + built_objects, + types)[0] + builtins_ast.body.append(simple_namespace_type) + simple_namespace_type.parent = builtins_ast + @_ast_from_object.register(types.SimpleNamespace) + def ast_from_simple_namespace(simple_namespace, built_objects, + module, name=None, parent=None): + if (name is not None and + id(simple_namespace) in built_objects and + built_objects[id(simple_namespace)].name is not None and + built_objects[id(simple_namespace)].name != name): + return (_make_assignment(name, built_objects[id(simple_namespace)].name, parent),) + simple_namespace_node = simple_namespace_type.instantiate_class() + built_objects[id(simple_namespace)] = _NameAST(name, simple_namespace_node) + for name in set(dir(simple_namespace)) - set(dir(simple_namespace_type)): + if name not in objects.Instance.special_attributes: + ast = _ast_from_object(getattr(simple_namespace, name), + built_objects, module, name=name, + parent=parent)[0] + simple_namespace_type.instance_attrs[name].append(ast) + return (node_classes.InterpreterObject(name=name, object_=simple_namespace_node, parent=parent),) + MANAGER.astroid_cache[six.moves.builtins.__name__] = builtins_ast return builtins_ast From b02f0d7df4b9bf5c2acd0cf0714a32fb30cc76f9 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Sat, 21 Nov 2015 16:27:17 -0500 Subject: [PATCH 109/312] Clean up caching of and references to builtins mock AST --- astroid/__init__.py | 1 - astroid/manager.py | 3 +-- astroid/tree/scoped_nodes.py | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/astroid/__init__.py b/astroid/__init__.py index ed0ab6382c..270e18baab 100644 --- a/astroid/__init__.py +++ b/astroid/__init__.py @@ -65,7 +65,6 @@ from astroid import inference from astroid import raw_building -from astroid.raw_building import builtins_ast as builtins from astroid.interpreter.util import are_exclusive, unpack_infer from astroid.tree.scoped_nodes import builtin_lookup from astroid.builder import parse diff --git a/astroid/manager.py b/astroid/manager.py index 918ed9dfd5..8d4e5026f4 100644 --- a/astroid/manager.py +++ b/astroid/manager.py @@ -222,5 +222,4 @@ def cache_module(self, module): def clear_cache(self): self.astroid_cache.clear() from astroid import raw_building - # self.astroid_cache[six.moves.builtins.__name__] = raw_building.ast_from_builtins() - self.astroid_cache[six.moves.builtins.__name__] = raw_building.builtins_ast + self.astroid_cache[six.moves.builtins.__name__] = raw_building.ast_from_builtins() diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index a5fe473cce..c9cd843e2a 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -968,7 +968,7 @@ def infer_argument(self, name, context): @util.register_implementation(treeabc.Lambda) class Lambda(mixins.FilterStmtsMixin, LocalsDictNodeNG): _astroid_fields = ('args', 'body',) - _other_other_fields = ('locals',) + # _other_other_fields = ('locals',) name = '' # function's type, 'function' | 'method' | 'staticmethod' | 'classmethod' From 922d1e883cf9155104d1e43796bc5b1228cbaafb Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Sat, 21 Nov 2015 22:56:14 -0500 Subject: [PATCH 110/312] Fix class_methods test and some enum/namedtuple issues --- astroid/__init__.py | 2 ++ astroid/brain/brain_stdlib.py | 49 +++++++++++++++++--------- astroid/raw_building.py | 6 +--- astroid/tests/unittest_scoped_nodes.py | 5 +-- 4 files changed, 38 insertions(+), 24 deletions(-) diff --git a/astroid/__init__.py b/astroid/__init__.py index 270e18baab..ecb9ef4768 100644 --- a/astroid/__init__.py +++ b/astroid/__init__.py @@ -65,6 +65,8 @@ from astroid import inference from astroid import raw_building +# Cache the builtins AST +raw_building.ast_from_builtins() from astroid.interpreter.util import are_exclusive, unpack_infer from astroid.tree.scoped_nodes import builtin_lookup from astroid.builder import parse diff --git a/astroid/brain/brain_stdlib.py b/astroid/brain/brain_stdlib.py index 3a34082391..a56fdf9e47 100644 --- a/astroid/brain/brain_stdlib.py +++ b/astroid/brain/brain_stdlib.py @@ -13,6 +13,7 @@ from astroid.interpreter.objects import BoundMethod from astroid import nodes from astroid.builder import AstroidBuilder +from astroid import parse from astroid import util PY3K = sys.version_info > (3, 0) @@ -244,8 +245,20 @@ def infer_namedtuple(namedtuple_call, context=None): for index, name in enumerate(field_names)) ) - # TODO: maybe memoize this call for efficiency, if it's needed. - namedtuple_node = AstroidBuilder(MANAGER).string_build(class_definition).getattr(type_name)[0] + namedtuple_node = parse(class_definition).getattr(type_name)[0] + init_template = '''def __init__(self, {args}): +{assignments}''' + init_definition = init_template.format( + args=', '.join(field_names), + assignments='\n'.join((' '*4 + 'self.{f} = {f}').format(f=f) for f in field_names)) + init_node = parse(init_definition).getattr('__init__')[0] + init_node.parent = namedtuple_node + namedtuple_node.body.append(init_node) + # This is an ugly hack to work around the normal process for + # assigning to instance_attrs relying on inference and thus being + # affected by instance_attrs already present. + for assignment in init_node.body: + namedtuple_node.instance_attrs[assignment.targets[0].attrname].append(assignment.targets[0]) return iter([namedtuple_node]) @@ -290,25 +303,25 @@ def __init__(self, {attributes}): """Fake __init__ for enums""" ''') code = template.format(name=type_name, attributes=', '.join(attributes)) - assignment_lines = [(' '*8 + 'self.%(a)s = %(a)s' % {'a': a}) + assignment_lines = [(' '*8 + 'self.{a} = {a}'.format(a=a)) for a in attributes] code += '\n'.join(assignment_lines) module = AstroidBuilder(MANAGER).string_build(code) return iter([module.body[1]]) -def infer_enum_class(node): +def infer_enum_class(enum_node): """ Specific inference for enums. """ - names = set(('Enum', 'IntEnum', 'enum.Enum', 'enum.IntEnum')) - for basename in node.basenames: + names = {'Enum', 'IntEnum', 'enum.Enum', 'enum.IntEnum'} + for basename in enum_node.basenames: # TODO: doesn't handle subclasses yet. This implementation # is a hack to support enums. if basename not in names: continue - if node.root().name == 'enum': + if enum_node.root().name == 'enum': # Skip if the class is directly from enum module. break - for local, values in node.locals.items(): + for local, values in enum_node.locals.items(): if any(not isinstance(value, nodes.AssignName) for value in values): continue @@ -319,7 +332,7 @@ def infer_enum_class(node): else: targets = stmt.targets - new_targets = [] + # new_targets = [] for target in targets: # Replace all the assignments with our mocked class. classdef = textwrap.dedent(''' @@ -331,15 +344,17 @@ def value(self): @property def name(self): return %(name)r - ''' % {'name': target.name, 'types': ', '.join(node.basenames)}) - fake = AstroidBuilder(MANAGER).string_build(classdef)[target.name] - fake.parent = target.parent - for method in node.mymethods(): - fake.locals[method.name] = [method] - new_targets.append(fake.instantiate_class()) - node.locals[local] = new_targets + ''' % {'name': target.name, 'types': ', '.join(enum_node.basenames)}) + class_node = parse(classdef)[target.name] + class_node.parent = target.parent + for method in enum_node.mymethods(): + class_node.body.append(method) + method.parent = class_node + instance_node = nodes.InterpreterObject(name=local, object_=class_node.instantiate_class(), parent=enum_node.parent) + # Replace the Assign node with the Enum instance + enum_node.body[enum_node.body.index(target.parent)] = instance_node break - return node + return enum_node def multiprocessing_transform(): module = AstroidBuilder(MANAGER).string_build(textwrap.dedent(''' diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 6f89efc777..e223117b31 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -652,12 +652,11 @@ def ast_from_builtins(): # SimpleNamespace = type(sys.implementation) - # this doesn't create a class for it, just refer to an existing + # this doesn't create a class for it, just refers to an existing # class that's probably implemented in C. To avoid crashes that # occur when ast_from_object tries to process instances of # SimpleNamespace, this adds the type to the builtins mock ast. if hasattr(types, 'SimpleNamespace'): - # simple_namespace_type = _ast_from_object(types.SimpleNamespace, built_objects, types)[0] @@ -684,9 +683,6 @@ def ast_from_simple_namespace(simple_namespace, built_objects, MANAGER.astroid_cache[six.moves.builtins.__name__] = builtins_ast return builtins_ast -builtins_ast = ast_from_builtins() -astroid_builtin = builtins_ast - # def _io_discrepancy(member): # # _io module names itself `io`: http://bugs.python.org/issue18602 # member_self = getattr(member, '__self__', None) diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index 17d5bddf23..b66e83d562 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -783,8 +783,9 @@ def registered(cls, application): ''' astroid = builder.parse(data, __name__) cls = astroid['WebAppObject'] - self.assertEqual(sorted(cls.locals.keys()), - ['appli', 'config', 'registered', 'schema']) + self.assertEqual(list(cls.locals.keys()), ['registered']) + self.assertEqual(sorted(cls.external_attrs.keys()), + ['appli', 'config', 'schema']) def test_class_getattr(self): data = ''' From d1dfe04da01bbfe01e65b1efe14bf4cc8d26bf86 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Sat, 21 Nov 2015 23:24:18 -0500 Subject: [PATCH 111/312] Fix namedtuple_advanced_inference to account for namedtuple mock ASTs now having an __init__ --- astroid/tests/unittest_brain.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astroid/tests/unittest_brain.py b/astroid/tests/unittest_brain.py index cf7be5bb37..31991e771e 100644 --- a/astroid/tests/unittest_brain.py +++ b/astroid/tests/unittest_brain.py @@ -136,7 +136,7 @@ def test_namedtuple_advanced_inference(self): result = __(six.moves.urllib.parse.urlparse('gopher://')) """) instance = next(result.infer()) - self.assertEqual(len(instance.getattr('scheme')), 1) + self.assertEqual(len(instance.getattr('scheme')), 2) self.assertEqual(len(instance.getattr('port')), 1) with self.assertRaises(astroid.AttributeInferenceError): instance.getattr('foo') From 85e264aab49b4cc00418f0063b1498b0ee00bbda Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Sun, 22 Nov 2015 15:54:41 -0500 Subject: [PATCH 112/312] Clean up debug statements and fix trivial test failures --- astroid/raw_building.py | 8 +------- astroid/tests/unittest_inference.py | 2 +- astroid/tree/scoped_nodes.py | 2 +- 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/astroid/raw_building.py b/astroid/raw_building.py index e223117b31..2505ba5c05 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -198,15 +198,9 @@ def _ast_from_object(instance, built_objects, module, name=None, parent=None): def lazy_instance(node=node, cls=cls, name=name, parent=parent): # Handle ImportFrom chains. while True: - try: - modname = node.modname - except AttributeError: - # print(node, parent) - raise + modname = node.modname node = MANAGER.ast_from_module_name(modname).getattr(cls.__name__)[0] if isinstance(node, scoped_nodes.ClassDef): - # class_node = node - # break return node_classes.InterpreterObject(name=name, object_=node.instantiate_class(), parent=parent) result.append(lazy_object_proxy.Proxy(lazy_instance)) built_objects[id(instance)] = _NameAST(name, result[-1]) diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index 13e4108af1..88ea0a05f2 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -646,7 +646,7 @@ def process_line(word_pos): self.assertEqual(len(list(ast['process_line'].infer_call_result(None))), 3) self.assertEqual(len(list(ast['tupletest'].infer())), 3) values = [' Date: Mon, 23 Nov 2015 13:44:11 +0200 Subject: [PATCH 113/312] Handle the cases when a List can contain Uninferable as its elements This can happen for instance when the list contains objects which weren't inferable in the first place. There were a bunch of places affected by this bug: unpack_infer, the inference of list additions and the handling of the namedtuple's fields. --- astroid/brain/brain_stdlib.py | 6 +- astroid/interpreter/util.py | 3 + astroid/protocols.py | 15 ++- astroid/tests/unittest_brain.py | 11 +++ astroid/tests/unittest_inference.py | 14 ++- astroid/tests/unittest_utils.py | 142 +++++++++++++++------------- 6 files changed, 121 insertions(+), 70 deletions(-) diff --git a/astroid/brain/brain_stdlib.py b/astroid/brain/brain_stdlib.py index a56fdf9e47..536a72efeb 100644 --- a/astroid/brain/brain_stdlib.py +++ b/astroid/brain/brain_stdlib.py @@ -21,6 +21,9 @@ PY34 = sys.version_info >= (3, 4) def infer_first(node, context): + if node is util.Uninferable: + util.reraise(InferenceError()) + try: value = next(node.infer(context=context)) if value is util.Uninferable: @@ -230,7 +233,8 @@ def infer_namedtuple(namedtuple_call, context=None): if isinstance(fields, nodes.Const) and isinstance(fields.value, str): field_names = tuple(fields.value.replace(',', ' ').split()) elif isinstance(fields, (nodes.Tuple, nodes.List)): - field_names = tuple(infer_first(const, context).value for const in fields.elts) + field_names = tuple(infer_first(const, context).value + for const in fields.elts) else: raise UseInferenceDefault() diff --git a/astroid/interpreter/util.py b/astroid/interpreter/util.py index cdc457ad1e..4a2d5b46b5 100644 --- a/astroid/interpreter/util.py +++ b/astroid/interpreter/util.py @@ -69,6 +69,9 @@ def unpack_infer(stmt, context=None): """ if isinstance(stmt, (treeabc.List, treeabc.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 return diff --git a/astroid/protocols.py b/astroid/protocols.py index c756011f64..fb129c14da 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -181,6 +181,15 @@ def _multiply_seq_by_int(self, other, context): return node +def _filter_uninferable_nodes(elts, context): + for elt in elts: + if elt is util.Uninferable: + yield elt + else: + for inferred in elt.infer(context): + yield inferred + + @infer_binary_op.register(treeabc.Tuple) @infer_binary_op.register(treeabc.List) @decorators.yes_if_nothing_inferred @@ -188,10 +197,8 @@ def tl_infer_binary_op(self, operator, other, context, method, nodes): not_implemented = nodes.NameConstant(NotImplemented) if isinstance(other, self.__class__) and operator == '+': node = self.__class__() - elts = [n for elt in self.elts for n in elt.infer(context) - if not n is util.Uninferable] - elts += [n for elt in other.elts for n in elt.infer(context) - if not n is util.Uninferable] + elts = list(_filter_uninferable_nodes(self.elts, context)) + elts += list(_filter_uninferable_nodes(other.elts, context)) node.elts = elts yield node elif isinstance(other, treeabc.Const) and operator == '*': diff --git a/astroid/tests/unittest_brain.py b/astroid/tests/unittest_brain.py index 31991e771e..2d9d87e3f7 100644 --- a/astroid/tests/unittest_brain.py +++ b/astroid/tests/unittest_brain.py @@ -90,6 +90,7 @@ def test_hashlib(self): class NamedTupleTest(unittest.TestCase): + def test_namedtuple_base(self): klass = test_utils.extract_node(""" from collections import namedtuple @@ -152,6 +153,16 @@ def test_namedtuple_instance_attrs(self): for name, attr in inferred.instance_attrs.items(): self.assertEqual(attr[0].attrname, name) + def test_namedtuple_uninferable_fields(self): + node = test_utils.extract_node(''' + x = [A] * 2 + from collections import namedtuple + l = namedtuple('a', x) + l(1) + ''') + inferred = next(node.infer()) + self.assertIs(util.Uninferable, inferred) + class ModuleExtenderTest(unittest.TestCase): def testExtensionModules(self): diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index 88ea0a05f2..edb46e3780 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -1323,7 +1323,8 @@ def test(): ast = parse(code, __name__) inferred = next(ast['Z'].infer()) self.assertIsInstance(inferred, nodes.List) - self.assertEqual(len(inferred.elts), 0) + self.assertEqual(len(inferred.elts), 1) + self.assertIs(inferred.elts[0], util.Uninferable) def test__new__(self): code = ''' @@ -2351,6 +2352,17 @@ def test_infer_coercion_rules_for_floats_complex(self): inferred = next(node.infer()) self.assertEqual(inferred.value, expected) + def test_binop_list_with_elts(self): + ast_node = test_utils.extract_node(''' + x = [A] * 1 + [1] + x + ''') + inferred = next(ast_node.infer()) + self.assertIsInstance(inferred, nodes.List) + self.assertEqual(len(inferred.elts), 2) + self.assertIsInstance(inferred.elts[0], nodes.Const) + self.assertIs(inferred.elts[1], util.Uninferable) + def test_binop_same_types(self): ast_nodes = test_utils.extract_node(''' class A(object): diff --git a/astroid/tests/unittest_utils.py b/astroid/tests/unittest_utils.py index 23c795880a..68ddc979f7 100644 --- a/astroid/tests/unittest_utils.py +++ b/astroid/tests/unittest_utils.py @@ -1,4 +1,4 @@ -# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. +# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr # # This file is part of astroid. @@ -17,85 +17,99 @@ # with astroid. If not, see . import unittest -from astroid import builder, nodes -from astroid.interpreter.util import are_exclusive +from astroid import builder +from astroid import nodes +from astroid.interpreter import util +from astroid import test_utils +from astroid import util as astroid_util -builder = builder.AstroidBuilder() -class AreExclusiveTC(unittest.TestCase): +class InferenceUtil(unittest.TestCase): + def test_not_exclusive(self): - astroid = builder.string_build(""" -x = 10 -for x in range(5): - print (x) + module = builder.parse(""" + x = 10 + for x in range(5): + print (x) -if x > 0: - print ('#' * x) + if x > 0: + print ('#' * x) """, __name__, __file__) - xass1 = astroid.locals['x'][0] + xass1 = module.locals['x'][0] assert xass1.lineno == 2 - xnames = [n for n in astroid.nodes_of_class(nodes.Name) if n.name == 'x'] + xnames = [n for n in module.nodes_of_class(nodes.Name) if n.name == 'x'] assert len(xnames) == 3 assert xnames[1].lineno == 6 - self.assertEqual(are_exclusive(xass1, xnames[1]), False) - self.assertEqual(are_exclusive(xass1, xnames[2]), False) + self.assertEqual(util.are_exclusive(xass1, xnames[1]), False) + self.assertEqual(util.are_exclusive(xass1, xnames[2]), False) def test_if(self): - astroid = builder.string_build(''' - -if 1: - a = 1 - a = 2 -elif 2: - a = 12 - a = 13 -else: - a = 3 - a = 4 + module = builder.parse(''' + if 1: + a = 1 + a = 2 + elif 2: + a = 12 + a = 13 + else: + a = 3 + a = 4 ''') - a1 = astroid.locals['a'][0] - a2 = astroid.locals['a'][1] - a3 = astroid.locals['a'][2] - a4 = astroid.locals['a'][3] - a5 = astroid.locals['a'][4] - a6 = astroid.locals['a'][5] - self.assertEqual(are_exclusive(a1, a2), False) - self.assertEqual(are_exclusive(a1, a3), True) - self.assertEqual(are_exclusive(a1, a5), True) - self.assertEqual(are_exclusive(a3, a5), True) - self.assertEqual(are_exclusive(a3, a4), False) - self.assertEqual(are_exclusive(a5, a6), False) + a1 = module.locals['a'][0] + a2 = module.locals['a'][1] + a3 = module.locals['a'][2] + a4 = module.locals['a'][3] + a5 = module.locals['a'][4] + a6 = module.locals['a'][5] + self.assertEqual(util.are_exclusive(a1, a2), False) + self.assertEqual(util.are_exclusive(a1, a3), True) + self.assertEqual(util.are_exclusive(a1, a5), True) + self.assertEqual(util.are_exclusive(a3, a5), True) + self.assertEqual(util.are_exclusive(a3, a4), False) + self.assertEqual(util.are_exclusive(a5, a6), False) def test_try_except(self): - astroid = builder.string_build(''' -try: - def exclusive_func2(): - "docstring" -except TypeError: - def exclusive_func2(): - "docstring" -except: - def exclusive_func2(): - "docstring" -else: - def exclusive_func2(): - "this one redefine the one defined line 42" + module = builder.parse(''' + try: + def exclusive_func2(): + "docstring" + except TypeError: + def exclusive_func2(): + "docstring" + except: + def exclusive_func2(): + "docstring" + else: + def exclusive_func2(): + "this one redefine the one defined line 42" + ''') + f1 = module.locals['exclusive_func2'][0] + f2 = module.locals['exclusive_func2'][1] + f3 = module.locals['exclusive_func2'][2] + f4 = module.locals['exclusive_func2'][3] + self.assertEqual(util.are_exclusive(f1, f2), True) + self.assertEqual(util.are_exclusive(f1, f3), True) + self.assertEqual(util.are_exclusive(f1, f4), False) + self.assertEqual(util.are_exclusive(f2, f4), True) + self.assertEqual(util.are_exclusive(f3, f4), True) + self.assertEqual(util.are_exclusive(f3, f2), True) + + self.assertEqual(util.are_exclusive(f2, f1), True) + self.assertEqual(util.are_exclusive(f4, f1), False) + self.assertEqual(util.are_exclusive(f4, f2), True) + def test_unpack_infer_uninferable_nodes(self): + node = test_utils.extract_node(''' + x = [A] * 1 + f = [x, [A] * 2] + f ''') - f1 = astroid.locals['exclusive_func2'][0] - f2 = astroid.locals['exclusive_func2'][1] - f3 = astroid.locals['exclusive_func2'][2] - f4 = astroid.locals['exclusive_func2'][3] - self.assertEqual(are_exclusive(f1, f2), True) - self.assertEqual(are_exclusive(f1, f3), True) - self.assertEqual(are_exclusive(f1, f4), False) - self.assertEqual(are_exclusive(f2, f4), True) - self.assertEqual(are_exclusive(f3, f4), True) - self.assertEqual(are_exclusive(f3, f2), True) + inferred = next(node.infer()) + unpacked = list(util.unpack_infer(inferred)) + self.assertEqual(len(unpacked), 3) + self.assertTrue(all(elt is astroid_util.Uninferable + for elt in unpacked)) - self.assertEqual(are_exclusive(f2, f1), True) - self.assertEqual(are_exclusive(f4, f1), False) - self.assertEqual(are_exclusive(f4, f2), True) if __name__ == '__main__': unittest.main() From 9952e1b92173b96219a9b52d54e61816a1b47704 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Mon, 23 Nov 2015 11:28:00 -0500 Subject: [PATCH 114/312] Make enum tests use inference for InterpreterObjects --- astroid/tests/unittest_brain.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/astroid/tests/unittest_brain.py b/astroid/tests/unittest_brain.py index 31991e771e..437852dd28 100644 --- a/astroid/tests/unittest_brain.py +++ b/astroid/tests/unittest_brain.py @@ -373,7 +373,7 @@ def mymethod(self, x): """) enum = next(module['MyEnum'].infer()) - one = enum['one'] + one = next(enum['one'].infer()) self.assertEqual(one.pytype(), '.MyEnum.one') property_type = '{}.property'.format(BUILTINS) @@ -381,8 +381,8 @@ def mymethod(self, x): prop = next(iter(one.getattr(propname))) self.assertIn(property_type, prop.decoratornames()) - meth = one.getattr('mymethod')[0] - self.assertIsInstance(meth, astroid.FunctionDef) + meth = next(one.igetattr('mymethod')) + self.assertIsInstance(meth, objects.BoundMethod) def test_looks_like_enum_false_positive(self): # Test that a class named Enumeration is not considered a builtin enum. @@ -409,8 +409,9 @@ class MyEnum(Mixin, enum.Enum): enum = next(module['MyEnum'].infer()) one = enum['one'] - clazz = one.getattr('__class__')[0] - self.assertTrue(clazz.is_subtype_of('.Mixin'), + instance = next(one.infer()) + cls = next(instance.igetattr('__class__')) + self.assertTrue(cls.is_subtype_of('.Mixin'), 'Enum instance should share base classes with generating class') def test_int_enum(self): @@ -424,9 +425,10 @@ class MyEnum(enum.IntEnum): enum = next(module['MyEnum'].infer()) one = enum['one'] - clazz = one.getattr('__class__')[0] + instance = next(one.infer()) + cls = next(instance.igetattr('__class__')) int_type = '{}.{}'.format(BUILTINS, 'int') - self.assertTrue(clazz.is_subtype_of(int_type), + self.assertTrue(cls.is_subtype_of(int_type), 'IntEnum based enums should be a subtype of int') def test_enum_func_form_is_class_not_instance(self): From bbb4c987ff7652af6e84d1ffbd4a2856328990c1 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Thu, 26 Nov 2015 14:31:21 +0200 Subject: [PATCH 115/312] Add __setitem__ to deque's brain tip. --- astroid/brain/brain_stdlib.py | 1 + 1 file changed, 1 insertion(+) diff --git a/astroid/brain/brain_stdlib.py b/astroid/brain/brain_stdlib.py index 536a72efeb..28e147e1f1 100644 --- a/astroid/brain/brain_stdlib.py +++ b/astroid/brain/brain_stdlib.py @@ -90,6 +90,7 @@ def rotate(self, n): pass def __iter__(self): return self def __reversed__(self): return self.iterable[::-1] def __getitem__(self, index): pass + def __setitem__(self, index, value): pass ''') From 0f1802052f817c7ac7eb64792e8ab60722875eff Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Thu, 26 Nov 2015 17:11:42 +0200 Subject: [PATCH 116/312] Add __delitem__ to deque's brain tip. --- astroid/brain/brain_stdlib.py | 1 + 1 file changed, 1 insertion(+) diff --git a/astroid/brain/brain_stdlib.py b/astroid/brain/brain_stdlib.py index 28e147e1f1..a9caf24bdb 100644 --- a/astroid/brain/brain_stdlib.py +++ b/astroid/brain/brain_stdlib.py @@ -91,6 +91,7 @@ def __iter__(self): return self def __reversed__(self): return self.iterable[::-1] def __getitem__(self, index): pass def __setitem__(self, index, value): pass + def __delitem__(self, index): pass ''') From 689a4365cf52ddb91e27bfba404e4b971ebeecc9 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Thu, 3 Dec 2015 17:23:23 +0200 Subject: [PATCH 117/312] Remove the visit method for AssignAttr in rebuilder, since AssignAttr does not exist as a node in the builtin ast module. --- astroid/tree/rebuilder.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/astroid/tree/rebuilder.py b/astroid/tree/rebuilder.py index 9f15999dec..8cf6cc0a54 100644 --- a/astroid/tree/rebuilder.py +++ b/astroid/tree/rebuilder.py @@ -185,13 +185,6 @@ def visit_arguments(self, node, parent, assign_ctx=None): varargannotation, kwargannotation) return newnode - def visit_assignattr(self, node, parent, assign_ctx=None): - """visit a AssignAttr node by returning a fresh instance of it""" - newnode = nodes.AssignAttr(node.lineno, node.col_offset, parent) - newnode.postinit(self.visit(node.expr, newnode, None)) - self._delayed_assattr.append(newnode) - return newnode - def visit_assert(self, node, parent, assign_ctx=None): """visit a Assert node by returning a fresh instance of it""" newnode = nodes.Assert(node.lineno, node.col_offset, parent) @@ -485,7 +478,6 @@ def visit_attribute(self, node, parent, assign_ctx=None): newnode = nodes.DelAttr(node.attr, node.lineno, node.col_offset, parent) elif assign_ctx == "Assign": - # FIXME : maybe we should call visit_assignattr ? newnode = nodes.AssignAttr(node.attr, node.lineno, node.col_offset, parent) else: From a5debeece2475cb1f4dca31e4dd551eebb01205c Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sat, 5 Dec 2015 13:25:34 +0200 Subject: [PATCH 118/312] assigned_stmts methods have the same signature from now on. They used to have different signatures and each one made assumptions about what could be passed to other implementations, leading to various possible crashes when one or more arguments weren't given. Closes issue #277. --- ChangeLog | 7 +++++++ astroid/protocols.py | 12 ++++++++++-- astroid/tests/unittest_protocols.py | 16 ++++++++++++++++ astroid/tree/scoped_nodes.py | 1 + 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 0ed445e2dc..78c1e6c763 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,13 @@ Change log for the astroid package (used to be astng) -- + * assigned_stmts methods have the same signature from now on. + + They used to have different signatures and each one made + assumptions about what could be passed to other implementations, + leading to various possible crashes when one or more arguments + weren't given. Closes issue #277. + * Fix a bug in the inference of Starred nodes, when the left hand side of an assignment has an extra element comparing with the right hand side. Closes issue #239. diff --git a/astroid/protocols.py b/astroid/protocols.py index fb129c14da..c9e2842d5e 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -290,8 +290,16 @@ def for_assigned_stmts(self, nodes, node=None, context=None, asspath=None): def mulass_assigned_stmts(self, nodes, node=None, context=None, asspath=None): if asspath is None: asspath = [] - asspath.insert(0, self.elts.index(node)) - return self.parent.assigned_stmts(self, context, asspath) + + try: + index = self.elts.index(node) + except ValueError: + util.reraise(exceptions.InferenceError( + 'Tried to retrieve a node {node!r} which does not exist', + node=self, assign_path=asspath, context=context)) + + asspath.insert(0, index) + return self.parent.assigned_stmts(node=self, context=context, asspath=asspath) @assigned_stmts.register(treeabc.AssignName) diff --git a/astroid/tests/unittest_protocols.py b/astroid/tests/unittest_protocols.py index 5d978efbbc..cea3945214 100644 --- a/astroid/tests/unittest_protocols.py +++ b/astroid/tests/unittest_protocols.py @@ -16,8 +16,10 @@ # You should have received a copy of the GNU Lesser General Public License along # with astroid. If not, see . +import contextlib import unittest +import astroid from astroid.test_utils import extract_node, require_version from astroid import InferenceError from astroid import nodes @@ -25,6 +27,15 @@ from astroid.tree.node_classes import AssignName, Const, Name, Starred +@contextlib.contextmanager +def _add_transform(manager, node, transform, predicate=None): + manager.register_transform(node, transform, predicate) + try: + yield + finally: + manager.unregister_transform(node, transform, predicate) + + class ProtocolTests(unittest.TestCase): def assertConstNodesEqual(self, nodes_list_expected, nodes_list_got): @@ -151,6 +162,11 @@ def test_assigned_stmts_assignments(self): assigned = list(simple_mul_assnode_2.assigned_stmts()) self.assertNameNodesEqual(['c'], assigned) + def test_sequence_assigned_stmts_not_accepting_empty_node(self): + node = extract_node('f = __([1])') + inferred = next(node.infer()) + inferred.assigned_stmts(inferred.elts[0]) + if __name__ == '__main__': unittest.main() diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index a5fe473cce..b9f29ca40d 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -571,6 +571,7 @@ def wildcard_import_names(self): all = self.locals['__all__'][-1] else: return default + try: explicit = next(all.assigned_stmts()) except exceptions.InferenceError: From 95b2579c06858171b3c997b301d253ffd4577818 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 6 Dec 2015 17:06:11 +0200 Subject: [PATCH 119/312] Add two new exceptions, AstroidImportError and AstroidSyntaxError. They are subclasses of AstroidBuildingException and are raised when a module can't be imported from various reasons. Also do_import_module lets the errors to bubble up without converting them to InferenceError. This particular conversion happens only during the inference. --- ChangeLog | 11 ++++++++ astroid/builder.py | 28 +++++++------------ astroid/exceptions.py | 10 +++++++ astroid/inference.py | 20 +++++++++---- astroid/manager.py | 8 +++--- astroid/mixins.py | 20 ++----------- .../testdata/python2/data/invalid_encoding.py | 1 + .../testdata/python3/data/invalid_encoding.py | 1 + astroid/tests/unittest_builder.py | 8 ++++-- astroid/tree/scoped_nodes.py | 2 +- 10 files changed, 62 insertions(+), 47 deletions(-) create mode 100644 astroid/tests/testdata/python2/data/invalid_encoding.py create mode 100644 astroid/tests/testdata/python3/data/invalid_encoding.py diff --git a/ChangeLog b/ChangeLog index 78c1e6c763..ed4e658424 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,17 @@ Change log for the astroid package (used to be astng) -- + * Add two new exceptions, AstroidImportError and AstroidSyntaxError. + They are subclasses of AstroidBuildingException and are raised when + a module can't be imported from various reasons. + Also do_import_module lets the errors to bubble up without converting + them to InferenceError. This particular conversion happens only + during the inference. + + * Revert to using printf-style formatting in as_string, in order + to avoid a potential problem with encodings when using .format. + Closes issue #273. Patch by notsqrt. + * assigned_stmts methods have the same signature from now on. They used to have different signatures and each one made diff --git a/astroid/builder.py b/astroid/builder.py index 2ef2e4b786..98e1fbe8c8 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -49,13 +49,7 @@ def open_source_file(filename): with open(filename, 'rb') as byte_stream: encoding = detect_encoding(byte_stream.readline)[0] stream = open(filename, 'r', newline=None, encoding=encoding) - try: - data = stream.read() - except UnicodeError: # wrong encoding - # detect_encoding returns utf-8 if no encoding specified - util.reraise(exceptions.AstroidBuildingException( - 'Wrong ({encoding}) or no encoding specified for {filename}.', - encoding=encoding, filename=filename)) + data = stream.read() return stream, encoding, data else: @@ -131,9 +125,14 @@ def file_build(self, path, modname=None): 'Unable to load file {path}:\n{error}', modname=modname, path=path, error=exc)) except (SyntaxError, LookupError) as exc: - util.reraise(exceptions.AstroidBuildingException( + util.reraise(exceptions.AstroidSyntaxError( 'Python 3 encoding specification error or unknown encoding:\n' '{error}', modname=modname, path=path, error=exc)) + except UnicodeError: # wrong encoding + # detect_encoding returns utf-8 if no encoding specified + util.reraise(exceptions.AstroidBuildingException( + 'Wrong ({encoding}) or no encoding specified for {filename}.', + encoding=encoding, filename=filename)) with stream: # get module name if necessary if modname is None: @@ -166,17 +165,10 @@ def _data_build(self, data, modname, path): """Build tree node from data and add some informations""" try: node = _parse(data + '\n') - except (TypeError, ValueError) as exc: - util.reraise(exceptions.AstroidBuildingException( + except (TypeError, ValueError, SyntaxError) as exc: + util.reraise(exceptions.AstroidSyntaxError( 'Parsing Python code failed:\n{error}', source=data, modname=modname, path=path, error=exc)) - except SyntaxError as exc: - # Pass the entire exception object to AstroidBuildingException, - # since pylint uses this as an introspection method, - # in order to find what error happened. - util.reraise(exceptions.AstroidBuildingException( - 'Syntax error in Python source: {error}', - source=data, modname=modname, path=path, error=exc)) if path is not None: node_file = os.path.abspath(path) else: @@ -237,7 +229,7 @@ def delayed_assignments(root): values.insert(0, node) else: values.append(node) - except exceptions.InferenceError: + except (exceptions.InferenceError, exceptions.AstroidBuildingException): pass diff --git a/astroid/exceptions.py b/astroid/exceptions.py index 39278fb586..a7ac7d3213 100644 --- a/astroid/exceptions.py +++ b/astroid/exceptions.py @@ -54,6 +54,14 @@ def __init__(self, message='Failed to import module {modname}.', **kws): super(AstroidBuildingException, self).__init__(message, **kws) +class AstroidImportError(AstroidBuildingException): + """Exception class used when a module can't be imported by astroid.""" + + +class AstroidSyntaxError(AstroidBuildingException): + """Exception class used when a module can't be parsed.""" + + class NoDefault(AstroidError): """raised by function's `default_value` method when an argument has no default value @@ -96,9 +104,11 @@ def __str__(self): for m in self.mros) return self.message.format(mros=mro_names, cls=self.cls) + class DuplicateBasesError(MroError): """Error raised when there are duplicate bases in the same class bases.""" + class InconsistentMroError(MroError): """Error raised when a class's MRO is inconsistent.""" diff --git a/astroid/inference.py b/astroid/inference.py index bc31195281..368268e06c 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -131,10 +131,14 @@ def infer_import(self, context=None, asname=True): name = context.lookupname if name is None: raise exceptions.InferenceError(node=self, context=context) - if asname: - yield self.do_import_module(self.real_name(name)) - else: - yield self.do_import_module(name) + try: + if asname: + yield self.do_import_module(self.real_name(name)) + else: + yield self.do_import_module(name) + except exceptions.AstroidBuildingException as exc: + util.reraise(exceptions.InferenceError(node=self, error=exc, + context=context)) @infer.register(treeabc.ImportFrom) @@ -146,7 +150,13 @@ def infer_import_from(self, context=None, asname=True): raise exceptions.InferenceError(node=self, context=context) if asname: name = self.real_name(name) - module = self.do_import_module() + + try: + module = self.do_import_module() + except exceptions.AstroidBuildingException as exc: + util.reraise(exceptions.InferenceError(node=self, error=exc, + context=context)) + try: context = contextmod.copy_context(context) context.lookupname = name diff --git a/astroid/manager.py b/astroid/manager.py index 8d4e5026f4..91121aa99f 100644 --- a/astroid/manager.py +++ b/astroid/manager.py @@ -125,16 +125,16 @@ def ast_from_module_name(self, modname, context_file=None): try: module = modutils.load_module_from_name(modname) except Exception as ex: # pylint: disable=broad-except - util.reraise(exceptions.AstroidBuildingException( + util.reraise(exceptions.AstroidImportError( 'Loading {modname} failed with:\n{error}', modname=modname, path=filepath, error=ex)) return self.ast_from_module(module, modname) elif mp_type == imp.PY_COMPILED: - raise exceptions.AstroidBuildingException( + raise exceptions.AstroidImportError( "Unable to load compiled module {modname}.", modname=modname, path=filepath) if filepath is None: - raise exceptions.AstroidBuildingException( + raise exceptions.AstroidImportError( "Can't find a file for module {modname}.", modname=modname) return self.ast_from_file(filepath, modname, fallback=False) @@ -179,7 +179,7 @@ def file_from_module_name(self, modname, contextfile): modname.split('.'), context_file=contextfile) traceback = sys.exc_info()[2] except ImportError as ex: - value = exceptions.AstroidBuildingException( + value = exceptions.AstroidImportError( 'Failed to import module {modname} with error:\n{error}.', modname=modname, error=ex) traceback = sys.exc_info()[2] diff --git a/astroid/mixins.py b/astroid/mixins.py index 5979d05569..6e4f29fc0b 100644 --- a/astroid/mixins.py +++ b/astroid/mixins.py @@ -119,23 +119,9 @@ def do_import_module(self, modname=None): if mymodule.relative_to_absolute_name(modname, level) == mymodule.name: # FIXME: we used to raise InferenceError here, but why ? return mymodule - try: - return mymodule.import_module(modname, level=level, - relative_only=level and level >= 1) - except exceptions.AstroidBuildingException as ex: - if isinstance(getattr(ex, 'error', None), SyntaxError): - util.reraise(exceptions.InferenceError( - 'Could not import {modname} because of SyntaxError:\n' - '{syntax_error}', modname=modname, syntax_error=ex.error, - import_node=self)) - util.reraise(exceptions.InferenceError('Could not import {modname}.', - modname=modname, - import_node=self)) - except SyntaxError as ex: - util.reraise(exceptions.InferenceError( - 'Could not import {modname} because of SyntaxError:\n' - '{syntax_error}', modname=modname, syntax_error=ex, - import_node=self)) + + return mymodule.import_module(modname, level=level, + relative_only=level and level >= 1) def real_name(self, asname): """get name from 'as' name""" diff --git a/astroid/tests/testdata/python2/data/invalid_encoding.py b/astroid/tests/testdata/python2/data/invalid_encoding.py new file mode 100644 index 0000000000..dddd208e6a --- /dev/null +++ b/astroid/tests/testdata/python2/data/invalid_encoding.py @@ -0,0 +1 @@ +# -*- coding: lala -*- \ No newline at end of file diff --git a/astroid/tests/testdata/python3/data/invalid_encoding.py b/astroid/tests/testdata/python3/data/invalid_encoding.py new file mode 100644 index 0000000000..dddd208e6a --- /dev/null +++ b/astroid/tests/testdata/python3/data/invalid_encoding.py @@ -0,0 +1 @@ +# -*- coding: lala -*- \ No newline at end of file diff --git a/astroid/tests/unittest_builder.py b/astroid/tests/unittest_builder.py index e11bedf7f2..788d3e1627 100644 --- a/astroid/tests/unittest_builder.py +++ b/astroid/tests/unittest_builder.py @@ -256,11 +256,11 @@ def setUp(self): self.builder = builder.AstroidBuilder() def test_data_build_null_bytes(self): - with self.assertRaises(exceptions.AstroidBuildingException): + with self.assertRaises(exceptions.AstroidSyntaxError): self.builder.string_build('\x00') def test_data_build_invalid_x_escape(self): - with self.assertRaises(exceptions.AstroidBuildingException): + with self.assertRaises(exceptions.AstroidSyntaxError): self.builder.string_build('"\\x1"') def test_missing_newline(self): @@ -709,6 +709,10 @@ def test_method_locals(self): self.assertEqual(len(_locals), 4) self.assertEqual(keys, ['MY_DICT', 'autre', 'local', 'self']) + def test_unknown_encoding(self): + with self.assertRaises(exceptions.AstroidSyntaxError): + resources.build_file('data/invalid_encoding.py') + class ModuleBuildTest(resources.SysPathSetup, FileBuildTest): diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index b9f29ca40d..5c93dc9713 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -2252,7 +2252,7 @@ def sort_locals(my_list): if name == '*': try: imported = node.do_import_module() - except exceptions.InferenceError: + except exceptions.AstroidBuildingException: continue for name in imported.wildcard_import_names(): locals_[name].append(node) From 853726a295a8b4000bbb7ff9032cd1573e5ddf6c Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 6 Dec 2015 17:11:50 +0200 Subject: [PATCH 120/312] AstroidBuildingException is now AstroidBuildingError, the former being removed in 2.0. --- ChangeLog | 3 +++ astroid/brain/brain_gi.py | 6 +++--- astroid/brain/brain_six.py | 4 ++-- astroid/builder.py | 6 +++--- astroid/exceptions.py | 8 ++++---- astroid/inference.py | 4 ++-- astroid/manager.py | 12 ++++++------ astroid/tests/unittest_builder.py | 2 +- astroid/tests/unittest_manager.py | 12 ++++++------ astroid/tests/unittest_nodes.py | 2 +- astroid/tree/scoped_nodes.py | 6 +++--- 11 files changed, 34 insertions(+), 31 deletions(-) diff --git a/ChangeLog b/ChangeLog index ed4e658424..1b340b2e2f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,9 @@ Change log for the astroid package (used to be astng) -- + * AstroidBuildingException is now AstroidBuildingError. The first + name will exist until astroid 2.0. + * Add two new exceptions, AstroidImportError and AstroidSyntaxError. They are subclasses of AstroidBuildingException and are raised when a module can't be imported from various reasons. diff --git a/astroid/brain/brain_gi.py b/astroid/brain/brain_gi.py index 0860207c9b..d2c133d372 100644 --- a/astroid/brain/brain_gi.py +++ b/astroid/brain/brain_gi.py @@ -9,7 +9,7 @@ import re import warnings -from astroid import MANAGER, AstroidBuildingException, nodes +from astroid import MANAGER, AstroidBuildingError, nodes from astroid.builder import AstroidBuilder @@ -114,7 +114,7 @@ def _gi_build_stub(parent): def _import_gi_module(modname): # we only consider gi.repository submodules if not modname.startswith('gi.repository.'): - raise AstroidBuildingException(modname=modname) + raise AstroidBuildingError(modname=modname) # build astroid representation unless we already tried so if modname not in _inspected_modules: modnames = [modname] @@ -155,7 +155,7 @@ def _import_gi_module(modname): else: astng = _inspected_modules[modname] if astng is None: - raise AstroidBuildingException(modname=modname) + raise AstroidBuildingError(modname=modname) return astng def _looks_like_require_version(node): diff --git a/astroid/brain/brain_six.py b/astroid/brain/brain_six.py index 3b2b9452d7..1c0ddf6396 100644 --- a/astroid/brain/brain_six.py +++ b/astroid/brain/brain_six.py @@ -23,7 +23,7 @@ from astroid import MANAGER, register_module_extender from astroid.builder import AstroidBuilder -from astroid.exceptions import AstroidBuildingException, InferenceError +from astroid.exceptions import AstroidBuildingError, InferenceError from astroid import nodes @@ -254,7 +254,7 @@ class Moves(object): def _six_fail_hook(modname): if modname != 'six.moves': - raise AstroidBuildingException(modname=modname) + raise AstroidBuildingError(modname=modname) module = AstroidBuilder(MANAGER).string_build(_IMPORTS) module.name = 'six.moves' return module diff --git a/astroid/builder.py b/astroid/builder.py index 98e1fbe8c8..43a3bc4c98 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -121,7 +121,7 @@ def file_build(self, path, modname=None): try: stream, encoding, data = open_source_file(path) except IOError as exc: - util.reraise(exceptions.AstroidBuildingException( + util.reraise(exceptions.AstroidBuildingError( 'Unable to load file {path}:\n{error}', modname=modname, path=path, error=exc)) except (SyntaxError, LookupError) as exc: @@ -130,7 +130,7 @@ def file_build(self, path, modname=None): '{error}', modname=modname, path=path, error=exc)) except UnicodeError: # wrong encoding # detect_encoding returns utf-8 if no encoding specified - util.reraise(exceptions.AstroidBuildingException( + util.reraise(exceptions.AstroidBuildingError( 'Wrong ({encoding}) or no encoding specified for {filename}.', encoding=encoding, filename=filename)) with stream: @@ -229,7 +229,7 @@ def delayed_assignments(root): values.insert(0, node) else: values.append(node) - except (exceptions.InferenceError, exceptions.AstroidBuildingException): + except (exceptions.InferenceError, exceptions.AstroidBuildingError): pass diff --git a/astroid/exceptions.py b/astroid/exceptions.py index a7ac7d3213..9d57058c1e 100644 --- a/astroid/exceptions.py +++ b/astroid/exceptions.py @@ -42,7 +42,7 @@ def __str__(self): return self.message.format(**vars(self)) -class AstroidBuildingException(AstroidError): +class AstroidBuildingError(AstroidError): """exception class when we are unable to build an astroid representation Standard attributes: @@ -51,14 +51,14 @@ class AstroidBuildingException(AstroidError): """ def __init__(self, message='Failed to import module {modname}.', **kws): - super(AstroidBuildingException, self).__init__(message, **kws) + super(AstroidBuildingError, self).__init__(message, **kws) -class AstroidImportError(AstroidBuildingException): +class AstroidImportError(AstroidBuildingError): """Exception class used when a module can't be imported by astroid.""" -class AstroidSyntaxError(AstroidBuildingException): +class AstroidSyntaxError(AstroidBuildingError): """Exception class used when a module can't be parsed.""" diff --git a/astroid/inference.py b/astroid/inference.py index 368268e06c..eb050b784c 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -136,7 +136,7 @@ def infer_import(self, context=None, asname=True): yield self.do_import_module(self.real_name(name)) else: yield self.do_import_module(name) - except exceptions.AstroidBuildingException as exc: + except exceptions.AstroidBuildingError as exc: util.reraise(exceptions.InferenceError(node=self, error=exc, context=context)) @@ -153,7 +153,7 @@ def infer_import_from(self, context=None, asname=True): try: module = self.do_import_module() - except exceptions.AstroidBuildingException as exc: + except exceptions.AstroidBuildingError as exc: util.reraise(exceptions.InferenceError(node=self, error=exc, context=context)) diff --git a/astroid/manager.py b/astroid/manager.py index 91121aa99f..ed1c27393b 100644 --- a/astroid/manager.py +++ b/astroid/manager.py @@ -88,7 +88,7 @@ def ast_from_file(self, filepath, modname=None, fallback=True, source=False): return builder.AstroidBuilder(self).file_build(filepath, modname) elif fallback and modname: return self.ast_from_module_name(modname) - raise exceptions.AstroidBuildingException( + raise exceptions.AstroidBuildingError( 'Unable to build an AST for {path}.', path=filepath) def _build_stub_module(self, modname): @@ -138,11 +138,11 @@ def ast_from_module_name(self, modname, context_file=None): "Can't find a file for module {modname}.", modname=modname) return self.ast_from_file(filepath, modname, fallback=False) - except exceptions.AstroidBuildingException as e: + except exceptions.AstroidBuildingError as e: for hook in self._failed_import_hooks: try: return hook(modname) - except exceptions.AstroidBuildingException: + except exceptions.AstroidBuildingError: pass raise e finally: @@ -184,8 +184,8 @@ def file_from_module_name(self, modname, contextfile): modname=modname, error=ex) traceback = sys.exc_info()[2] self._mod_file_cache[(modname, contextfile)] = value - if isinstance(value, exceptions.AstroidBuildingException): - six.reraise(exceptions.AstroidBuildingException, + if isinstance(value, exceptions.AstroidBuildingError): + six.reraise(exceptions.AstroidBuildingError, value, traceback) return value @@ -211,7 +211,7 @@ def register_failed_import_hook(self, hook): `hook` must be a function that accepts a single argument `modname` which contains the name of the module or package that could not be imported. If `hook` can resolve the import, must return a node of type `astroid.Module`, - otherwise, it must raise `AstroidBuildingException`. + otherwise, it must raise `AstroidBuildingError`. """ self._failed_import_hooks.append(hook) diff --git a/astroid/tests/unittest_builder.py b/astroid/tests/unittest_builder.py index 788d3e1627..206c7da275 100644 --- a/astroid/tests/unittest_builder.py +++ b/astroid/tests/unittest_builder.py @@ -268,7 +268,7 @@ def test_missing_newline(self): resources.build_file('data/noendingnewline.py') def test_missing_file(self): - with self.assertRaises(exceptions.AstroidBuildingException): + with self.assertRaises(exceptions.AstroidBuildingError): resources.build_file('data/inexistant.py') def test_inspect_build0(self): diff --git a/astroid/tests/unittest_manager.py b/astroid/tests/unittest_manager.py index d8b5429005..cdc03e6499 100644 --- a/astroid/tests/unittest_manager.py +++ b/astroid/tests/unittest_manager.py @@ -70,7 +70,7 @@ def test_ast_from_file_astro_builder(self): self.assertIn('unittest', self.manager.astroid_cache) def test_ast_from_file_name_astro_builder_exception(self): - self.assertRaises(exceptions.AstroidBuildingException, + self.assertRaises(exceptions.AstroidBuildingError, self.manager.ast_from_file, 'unhandledName') def test_do_not_expose_main(self): @@ -90,7 +90,7 @@ def test_ast_from_module_name_not_python_source(self): self.assertEqual(astroid.pure_python, False) def test_ast_from_module_name_astro_builder_exception(self): - self.assertRaises(exceptions.AstroidBuildingException, + self.assertRaises(exceptions.AstroidBuildingError, self.manager.ast_from_module_name, 'unhandledModule') @@ -142,7 +142,7 @@ def test_file_from_module(self): def test_file_from_module_name_astro_building_exception(self): """check if the method launch a exception with a wrong module name""" - self.assertRaises(exceptions.AstroidBuildingException, + self.assertRaises(exceptions.AstroidBuildingError, self.manager.file_from_module_name, 'unhandledModule', None) def test_ast_from_module(self): @@ -163,13 +163,13 @@ def hook(modname): if modname == 'foo.bar': return unittest else: - raise exceptions.AstroidBuildingException() + raise exceptions.AstroidBuildingError() - with self.assertRaises(exceptions.AstroidBuildingException): + with self.assertRaises(exceptions.AstroidBuildingError): self.manager.ast_from_module_name('foo.bar') self.manager.register_failed_import_hook(hook) self.assertEqual(unittest, self.manager.ast_from_module_name('foo.bar')) - with self.assertRaises(exceptions.AstroidBuildingException): + with self.assertRaises(exceptions.AstroidBuildingError): self.manager.ast_from_module_name('foo.bar.baz') del self.manager._failed_import_hooks[0] diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index 101bccd63f..d240783abc 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -452,7 +452,7 @@ def hello(False): del True """ if sys.version_info >= (3, 0): - with self.assertRaises(exceptions.AstroidBuildingException): + with self.assertRaises(exceptions.AstroidBuildingError): builder.parse(code) else: ast = builder.parse(code) diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 5c93dc9713..6aa5948cec 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -460,7 +460,7 @@ def getattr(self, name, context=None, ignore_locals=False): elif self.package: try: result = [self.import_module(name, relative_only=True)] - except (exceptions.AstroidBuildingException, SyntaxError): + except (exceptions.AstroidBuildingError, SyntaxError): util.reraise(exceptions.AttributeInferenceError(target=self, attribute=name, context=context)) @@ -521,7 +521,7 @@ def import_module(self, modname, relative_only=False, level=None): absmodname = self.relative_to_absolute_name(modname, level) try: return MANAGER.ast_from_module_name(absmodname) - except exceptions.AstroidBuildingException: + except exceptions.AstroidBuildingError: # we only want to import a sub module or package of this module, # skip here if relative_only: @@ -2252,7 +2252,7 @@ def sort_locals(my_list): if name == '*': try: imported = node.do_import_module() - except exceptions.AstroidBuildingException: + except exceptions.AstroidBuildingError: continue for name in imported.wildcard_import_names(): locals_[name].append(node) From f77f5e36632bf6ea76d044bf14a132ff6e22dbfb Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 6 Dec 2015 18:00:01 +0200 Subject: [PATCH 121/312] relative_to_absolute_name will now raise TooManyLevelsError when a relative import is trying to access something beyond the top-level package. --- ChangeLog | 3 +++ astroid/exceptions.py | 15 +++++++++++++++ astroid/tests/unittest_scoped_nodes.py | 13 +++++++++++++ astroid/tree/scoped_nodes.py | 4 ++++ 4 files changed, 35 insertions(+) diff --git a/ChangeLog b/ChangeLog index 1b340b2e2f..acc875063c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,9 @@ Change log for the astroid package (used to be astng) ===================================================== -- + * relative_to_absolute_name or methods calling it will now raise + TooManyLevelsError when a relative import was trying to + access something beyond the top-level package. * AstroidBuildingException is now AstroidBuildingError. The first name will exist until astroid 2.0. diff --git a/astroid/exceptions.py b/astroid/exceptions.py index 9d57058c1e..21e9b61f6c 100644 --- a/astroid/exceptions.py +++ b/astroid/exceptions.py @@ -58,6 +58,21 @@ class AstroidImportError(AstroidBuildingError): """Exception class used when a module can't be imported by astroid.""" +class TooManyLevelsError(AstroidImportError): + """Exception class which is raised when a relative import was beyond the top-level. + + Standard attributes: + level: The level which was attempted. + name: the name of the module on which the relative import was attempted. + """ + level = None + name = None + + def __init__(self, message='Relative import with too many levels ' + '({level}) for module {name!r}', **kws): + super(TooManyLevelsError, self).__init__(message, **kws) + + class AstroidSyntaxError(AstroidBuildingError): """Exception class used when a module can't be parsed.""" diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index b66e83d562..8467099cfd 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -35,6 +35,7 @@ InferenceError, AttributeInferenceError, NoDefault, ResolveError, MroError, InconsistentMroError, DuplicateBasesError, + TooManyLevelsError, ) from astroid.interpreter.objects import ( Instance, BoundMethod, UnboundMethod, Generator @@ -165,6 +166,18 @@ def test_relative_to_absolute_name(self): modname = mod.relative_to_absolute_name('', 1) self.assertEqual(modname, 'very.multi') + def test_relative_to_absolute_name_beyond_top_level(self): + mod = nodes.Module('a.b.c', '') + mod.package = True + for level in (5, 4): + with self.assertRaises(TooManyLevelsError) as cm: + mod.relative_to_absolute_name('test', level) + + expected = ("Relative import with too many levels " + "({level}) for module {name!r}".format( + level=level - 1, name=mod.name)) + self.assertEqual(expected, str(cm.exception)) + def test_import_1(self): data = '''from . import subpackage''' sys.path.insert(0, resources.find('data')) diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 6aa5948cec..ae8da65f8e 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -541,6 +541,10 @@ def relative_to_absolute_name(self, modname, level): if level: if self.package: level = level - 1 + if level and self.name.count('.') < level: + raise exceptions.TooManyLevelsError( + level=level, name=self.name) + package_name = self.name.rsplit('.', level)[0] elif self.package: package_name = self.name From 1236717275902072fbeb7e5819e477ca7cda0f7c Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 8 Dec 2015 10:42:36 +0200 Subject: [PATCH 122/312] Some nodes got a new attribute, 'ctx', which tells in which context the said node was used. The possible values for the contexts are `Load` ('a'), `Del` ('del a'), `Store` ('a = 4') and the nodes that got the new attribute are Starred, Subscript, List and Tuple. The builtin ast module provides contexts for Name and Attribute as well, but we took a different approach in the past, by having different nodes for each type of context. For instance, Name used in a Del context is a DelName, while Name used in a Store context is AssignName. Since this is ingrained in astroid since quite some time, it makes no sense to change them as well, even though it's a loss of consistency. The patch introduces a new dependency to enum34 on older Python versions, which is used for building the three possible enum values for the contexts. Closes issue #267. --- ChangeLog | 8 + astroid/__init__.py | 10 + astroid/__pkginfo__.py | 16 +- astroid/tests/unittest_nodes.py | 49 ++++ astroid/tree/node_classes.py | 25 ++ astroid/tree/rebuilder.py | 472 ++++++++++++++++---------------- 6 files changed, 341 insertions(+), 239 deletions(-) diff --git a/ChangeLog b/ChangeLog index acc875063c..56f1e90901 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,14 @@ Change log for the astroid package (used to be astng) ===================================================== -- + + * Some nodes got a new attribute, 'ctx', which tells in which context + the said node was used. + + The possible values for the contexts are `Load` ('a'), `Del` + ('del a'), `Store` ('a = 4') and the nodes that got the new + attribute are Starred, Subscript, List and Tuple. Closes issue #267. + * relative_to_absolute_name or methods calling it will now raise TooManyLevelsError when a relative import was trying to access something beyond the top-level package. diff --git a/astroid/__init__.py b/astroid/__init__.py index ecb9ef4768..b18c9d2803 100644 --- a/astroid/__init__.py +++ b/astroid/__init__.py @@ -45,6 +45,16 @@ import sys from operator import attrgetter +import enum + + +_Context = enum.Enum('Context', 'Load Store Del') +Load = _Context.Load +Store = _Context.Store +Del = _Context.Del +del _Context + + # WARNING: internal imports order matters ! # pylint: disable=redefined-builtin, wildcard-import diff --git a/astroid/__pkginfo__.py b/astroid/__pkginfo__.py index ff9716ce78..3706d2b396 100644 --- a/astroid/__pkginfo__.py +++ b/astroid/__pkginfo__.py @@ -25,13 +25,15 @@ numversion = (1, 4, 0) version = '.'.join([str(num) for num in numversion]) -if sys.version_info >= (3, 4): - install_requires = ['lazy_object_proxy', 'six', 'wrapt'] -elif sys.version_info == (3, 3): - install_requires = ['lazy_object_proxy', 'singledispatch', 'six', 'wrapt'] -else: - install_requires = ['funcsigs', 'lazy_object_proxy', 'singledispatch', 'six', - 'wrapt'] + +install_requires = ['lazy_object_proxy', 'six', 'wrapt'] + +if sys.version_info <= (3, 3): + install_requires.append('singledispatch') + install_requires.append('enum34') + + if sys.version_info < (3, 0): + install_requires.append('funcsigs') license = 'LGPL' diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index d240783abc..c3a6e463ba 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -25,6 +25,7 @@ import six +import astroid from astroid import builder from astroid import context as contextmod from astroid import exceptions @@ -891,5 +892,53 @@ def test_scope_elt_of_generator_exp(self): self.assertIsInstance(scope, nodes.GeneratorExp) +class ContextTest(unittest.TestCase): + + def test_subscript_load(self): + node = test_utils.extract_node('f[1]') + self.assertIs(node.ctx, astroid.Load) + + def test_subscript_del(self): + node = test_utils.extract_node('del f[1]') + self.assertIs(node.targets[0].ctx, astroid.Del) + + def test_subscript_store(self): + node = test_utils.extract_node('f[1] = 2') + subscript = node.targets[0] + self.assertIs(subscript.ctx, astroid.Store) + + def test_list_load(self): + node = test_utils.extract_node('[]') + self.assertIs(node.ctx, astroid.Load) + + def test_list_del(self): + node = test_utils.extract_node('del []') + self.assertIs(node.targets[0].ctx, astroid.Del) + + def test_list_store(self): + with self.assertRaises(exceptions.AstroidSyntaxError): + test_utils.extract_node('[0] = 2') + + def test_tuple_load(self): + node = test_utils.extract_node('(1, )') + self.assertIs(node.ctx, astroid.Load) + + def test_tuple_store(self): + with self.assertRaises(exceptions.AstroidSyntaxError): + test_utils.extract_node('(1, ) = 3') + + @test_utils.require_version(minver='3.5') + def test_starred_load(self): + node = test_utils.extract_node('a = *b') + starred = node.value + self.assertIs(starred.ctx, astroid.Load) + + @test_utils.require_version(minver='3.0') + def test_starred_store(self): + node = test_utils.extract_node('a, *b = 1, 2') + starred = node.targets[0].elts[1] + self.assertIs(starred.ctx, astroid.Store) + + if __name__ == '__main__': unittest.main() diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index 24fa019c98..f29885956a 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -940,6 +940,12 @@ def postinit(self, value=None): @util.register_implementation(runtimeabc.BuiltinInstance) class List(base.BaseContainer, AssignedStmtsMixin, objects.BaseInstance): """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 @@ -1087,8 +1093,14 @@ def getattr(self, attrname, context=None): class Starred(mixins.ParentAssignTypeMixin, AssignedStmtsMixin, base.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 @@ -1097,9 +1109,15 @@ def postinit(self, value=None): class Subscript(base.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) + def postinit(self, value=None, slice=None): self.value = value self.slice = slice @@ -1162,6 +1180,13 @@ def block_range(self, lineno): class Tuple(base.BaseContainer, AssignedStmtsMixin, objects.BaseInstance): """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 diff --git a/astroid/tree/rebuilder.py b/astroid/tree/rebuilder.py index 8cf6cc0a54..81ef48d442 100644 --- a/astroid/tree/rebuilder.py +++ b/astroid/tree/rebuilder.py @@ -22,6 +22,7 @@ import ast import sys +import astroid from astroid import nodes @@ -78,6 +79,11 @@ } PY3 = sys.version_info >= (3, 0) PY34 = sys.version_info >= (3, 4) +CONTEXTS = {ast.Load: astroid.Load, + ast.Store: astroid.Store, + ast.Del: astroid.Del, + ast.Param: astroid.Store} + def _get_doc(node): try: @@ -90,8 +96,7 @@ def _get_doc(node): except IndexError: return node, None - -def _visit_or_none(node, attr, visitor, parent, assign_ctx, visit='visit', +def _visit_or_none(node, attr, visitor, parent, visit='visit', **kws): """If the given node has an attribute, visits the attribute, and otherwise returns None. @@ -99,11 +104,15 @@ def _visit_or_none(node, attr, visitor, parent, assign_ctx, visit='visit', """ value = getattr(node, attr, None) if value: - return getattr(visitor, visit)(value, parent, assign_ctx, **kws) + return getattr(visitor, visit)(value, parent, **kws) else: return None +def _get_context(node): + return CONTEXTS.get(type(node.ctx), astroid.Load) + + class TreeRebuilder(object): """Rebuilds the ast tree to become an Astroid tree""" @@ -119,7 +128,7 @@ def visit_module(self, node, modname, modpath, package): newnode.postinit([self.visit(child, newnode) for child in node.body]) return newnode - def visit(self, node, parent, assign_ctx=None): + def visit(self, node, parent): cls = node.__class__ if cls in self._visit_meths: visit_method = self._visit_meths[cls] @@ -128,9 +137,9 @@ def visit(self, node, parent, assign_ctx=None): visit_name = 'visit_' + REDIRECT.get(cls_name, cls_name).lower() visit_method = getattr(self, visit_name) self._visit_meths[cls] = visit_method - return visit_method(node, parent, assign_ctx) + return visit_method(node, parent) - def visit_arguments(self, node, parent, assign_ctx=None): + def visit_arguments(self, node, parent): """visit a Arguments node by returning a fresh instance of it""" vararg, kwarg = node.vararg, node.kwarg if PY34: @@ -139,8 +148,8 @@ def visit_arguments(self, node, parent, assign_ctx=None): parent) else: newnode = nodes.Arguments(vararg, kwarg, parent) - args = [self.visit(child, newnode, "Assign") for child in node.args] - defaults = [self.visit(child, newnode, assign_ctx) + args = [self.visit(child, newnode) for child in node.args] + defaults = [self.visit(child, newnode) for child in node.defaults] varargannotation = None kwargannotation = None @@ -150,29 +159,29 @@ def visit_arguments(self, node, parent, assign_ctx=None): if PY34: if node.vararg.annotation: varargannotation = self.visit(node.vararg.annotation, - newnode, assign_ctx) + newnode) vararg = vararg.arg elif PY3 and node.varargannotation: varargannotation = self.visit(node.varargannotation, - newnode, assign_ctx) + newnode) if kwarg: if PY34: if node.kwarg.annotation: kwargannotation = self.visit(node.kwarg.annotation, - newnode, assign_ctx) + newnode) kwarg = kwarg.arg elif PY3: if node.kwargannotation: kwargannotation = self.visit(node.kwargannotation, - newnode, assign_ctx) + newnode) if PY3: - kwonlyargs = [self.visit(child, newnode, "Assign") for child + kwonlyargs = [self.visit(child, newnode) for child in node.kwonlyargs] - kw_defaults = [self.visit(child, newnode, None) if child else + kw_defaults = [self.visit(child, newnode) if child else None for child in node.kw_defaults] - annotations = [self.visit(arg.annotation, newnode, None) if + annotations = [self.visit(arg.annotation, newnode) if arg.annotation else None for arg in node.args] - kwonly_annotations = [self.visit(arg.annotation, newnode, None) + kwonly_annotations = [self.visit(arg.annotation, newnode) if arg.annotation else None for arg in node.kwonlyargs] else: @@ -185,80 +194,76 @@ def visit_arguments(self, node, parent, assign_ctx=None): varargannotation, kwargannotation) return newnode - def visit_assert(self, node, parent, assign_ctx=None): + def visit_assert(self, node, parent): """visit a Assert node by returning a fresh instance of it""" newnode = nodes.Assert(node.lineno, node.col_offset, parent) if node.msg: - msg = self.visit(node.msg, newnode, assign_ctx) + msg = self.visit(node.msg, newnode) else: msg = None - newnode.postinit(self.visit(node.test, newnode, assign_ctx), msg) + newnode.postinit(self.visit(node.test, newnode), msg) return newnode - def visit_assign(self, node, parent, assign_ctx=None): + def visit_assign(self, node, parent): """visit a Assign node by returning a fresh instance of it""" newnode = nodes.Assign(node.lineno, node.col_offset, parent) - newnode.postinit([self.visit(child, newnode, "Assign") + newnode.postinit([self.visit(child, newnode) for child in node.targets], - self.visit(node.value, newnode, None)) + self.visit(node.value, newnode)) return newnode - def visit_assignname(self, node, parent, assign_ctx=None, node_name=None): + def visit_assignname(self, node, parent, node_name=None): '''visit a node and return a AssignName node''' - # assign_ctx is not used here, it takes that argument only to - # maintain consistency with the other visit functions. newnode = nodes.AssignName(node_name, getattr(node, 'lineno', None), getattr(node, 'col_offset', None), parent) return newnode - def visit_augassign(self, node, parent, assign_ctx=None): + def visit_augassign(self, node, parent): """visit a AugAssign node by returning a fresh instance of it""" newnode = nodes.AugAssign(_BIN_OP_CLASSES[type(node.op)] + "=", node.lineno, node.col_offset, parent) - newnode.postinit(self.visit(node.target, newnode, "Assign"), - self.visit(node.value, newnode, None)) + newnode.postinit(self.visit(node.target, newnode), + self.visit(node.value, newnode)) return newnode - def visit_repr(self, node, parent, assign_ctx=None): + def visit_repr(self, node, parent): """visit a Backquote node by returning a fresh instance of it""" newnode = nodes.Repr(node.lineno, node.col_offset, parent) - newnode.postinit(self.visit(node.value, newnode, assign_ctx)) + newnode.postinit(self.visit(node.value, newnode)) return newnode - def visit_binop(self, node, parent, assign_ctx=None): + def visit_binop(self, node, parent): """visit a BinOp node by returning a fresh instance of it""" newnode = nodes.BinOp(_BIN_OP_CLASSES[type(node.op)], node.lineno, node.col_offset, parent) - newnode.postinit(self.visit(node.left, newnode, assign_ctx), - self.visit(node.right, newnode, assign_ctx)) + newnode.postinit(self.visit(node.left, newnode), + self.visit(node.right, newnode)) return newnode - def visit_boolop(self, node, parent, assign_ctx=None): + def visit_boolop(self, node, parent): """visit a BoolOp node by returning a fresh instance of it""" newnode = nodes.BoolOp(_BOOL_OP_CLASSES[type(node.op)], node.lineno, node.col_offset, parent) - newnode.postinit([self.visit(child, newnode, assign_ctx) + newnode.postinit([self.visit(child, newnode) for child in node.values]) return newnode - def visit_break(self, node, parent, assign_ctx=None): + def visit_break(self, node, parent): """visit a Break node by returning a fresh instance of it""" return nodes.Break(getattr(node, 'lineno', None), getattr(node, 'col_offset', None), parent) - def visit_call(self, node, parent, assign_ctx=None): + def visit_call(self, node, parent): """visit a CallFunc node by returning a fresh instance of it""" newnode = nodes.Call(node.lineno, node.col_offset, parent) - starargs = _visit_or_none(node, 'starargs', self, newnode, - assign_ctx) - kwargs = _visit_or_none(node, 'kwargs', self, newnode, - assign_ctx) - args = [self.visit(child, newnode, assign_ctx) + starargs = _visit_or_none(node, 'starargs', self, newnode) + kwargs = _visit_or_none(node, 'kwargs', self, newnode) + args = [self.visit(child, newnode) for child in node.args] if node.keywords: - keywords = [self.visit(child, newnode, assign_ctx) + keywords = [self.visit(child, newnode) for child in node.keywords] else: keywords = None @@ -277,11 +282,12 @@ def visit_call(self, node, parent, assign_ctx=None): keywords.append(new_kwargs) else: keywords = [new_kwargs] - newnode.postinit(self.visit(node.func, newnode, assign_ctx), + + newnode.postinit(self.visit(node.func, newnode), args, keywords) return newnode - def visit_classdef(self, node, parent, assign_ctx=None, newstyle=None): + def visit_classdef(self, node, parent, newstyle=None): """visit a ClassDef node to become astroid""" node, doc = _get_doc(node) newnode = nodes.ClassDef(node.name, doc, node.lineno, @@ -290,146 +296,143 @@ def visit_classdef(self, node, parent, assign_ctx=None, newstyle=None): if PY3: for keyword in node.keywords: if keyword.arg == 'metaclass': - metaclass = self.visit(keyword, newnode, assign_ctx).value + metaclass = self.visit(keyword, newnode).value break if node.decorator_list: - decorators = self.visit_decorators(node, newnode, assign_ctx) + decorators = self.visit_decorators(node, newnode) else: decorators = None - newnode.postinit([self.visit(child, newnode, assign_ctx) + newnode.postinit([self.visit(child, newnode) for child in node.bases], - [self.visit(child, newnode, assign_ctx) + [self.visit(child, newnode) for child in node.body], decorators, newstyle, metaclass) return newnode - def visit_const(self, node, parent, assign_ctx=None): + def visit_const(self, node, parent): """visit a Const node by returning a fresh instance of it""" return nodes.Const(node.value, getattr(node, 'lineno', None), getattr(node, 'col_offset', None), parent) - def visit_continue(self, node, parent, assign_ctx=None): + def visit_continue(self, node, parent): """visit a Continue node by returning a fresh instance of it""" return nodes.Continue(getattr(node, 'lineno', None), getattr(node, 'col_offset', None), parent) - def visit_compare(self, node, parent, assign_ctx=None): + def visit_compare(self, node, parent): """visit a Compare node by returning a fresh instance of it""" newnode = nodes.Compare(node.lineno, node.col_offset, parent) - newnode.postinit(self.visit(node.left, newnode, assign_ctx), + newnode.postinit(self.visit(node.left, newnode), [(_CMP_OP_CLASSES[op.__class__], - self.visit(expr, newnode, assign_ctx)) + self.visit(expr, newnode)) for (op, expr) in zip(node.ops, node.comparators)]) return newnode - def visit_comprehension(self, node, parent, assign_ctx=None): + def visit_comprehension(self, node, parent): """visit a Comprehension node by returning a fresh instance of it""" newnode = nodes.Comprehension(parent) - newnode.postinit(self.visit(node.target, newnode, "Assign"), - self.visit(node.iter, newnode, None), - [self.visit(child, newnode, None) + newnode.postinit(self.visit(node.target, newnode), + self.visit(node.iter, newnode), + [self.visit(child, newnode) for child in node.ifs]) return newnode - def visit_decorators(self, node, parent, assign_ctx=None): + def visit_decorators(self, node, parent): """visit a Decorators node by returning a fresh instance of it""" # /!\ node is actually a ast.FunctionDef node while # parent is a astroid.nodes.FunctionDef node newnode = nodes.Decorators(node.lineno, node.col_offset, parent) - newnode.postinit([self.visit(child, newnode, assign_ctx) + newnode.postinit([self.visit(child, newnode) for child in node.decorator_list]) return newnode - def visit_delete(self, node, parent, assign_ctx=None): + def visit_delete(self, node, parent): """visit a Delete node by returning a fresh instance of it""" newnode = nodes.Delete(node.lineno, node.col_offset, parent) - newnode.postinit([self.visit(child, newnode, "Del") + newnode.postinit([self.visit(child, newnode) for child in node.targets]) return newnode - def _visit_dict_items(self, node, parent, newnode, assign_ctx): + def _visit_dict_items(self, node, parent, newnode): for key, value in zip(node.keys, node.values): - rebuilt_value = self.visit(value, newnode, assign_ctx) + rebuilt_value = self.visit(value, newnode) if not key: # Python 3.5 and extended unpacking rebuilt_key = nodes.DictUnpack(rebuilt_value.lineno, rebuilt_value.col_offset, parent) else: - rebuilt_key = self.visit(key, newnode, assign_ctx) + rebuilt_key = self.visit(key, newnode) yield rebuilt_key, rebuilt_value - def visit_dict(self, node, parent, assign_ctx=None): + def visit_dict(self, node, parent): """visit a Dict node by returning a fresh instance of it""" newnode = nodes.Dict(node.lineno, node.col_offset, parent) - items = list(self._visit_dict_items(node, parent, newnode, assign_ctx)) + items = list(self._visit_dict_items(node, parent, newnode)) newnode.postinit(items) return newnode - def visit_dictcomp(self, node, parent, assign_ctx=None): + def visit_dictcomp(self, node, parent): """visit a DictComp node by returning a fresh instance of it""" newnode = nodes.DictComp(node.lineno, node.col_offset, parent) - newnode.postinit(self.visit(node.key, newnode, assign_ctx), - self.visit(node.value, newnode, assign_ctx), - [self.visit(child, newnode, assign_ctx) + newnode.postinit(self.visit(node.key, newnode), + self.visit(node.value, newnode), + [self.visit(child, newnode) for child in node.generators]) return newnode - def visit_expr(self, node, parent, assign_ctx=None): + def visit_expr(self, node, parent): """visit a Expr node by returning a fresh instance of it""" newnode = nodes.Expr(node.lineno, node.col_offset, parent) - newnode.postinit(self.visit(node.value, newnode, assign_ctx)) + newnode.postinit(self.visit(node.value, newnode)) return newnode - def visit_ellipsis(self, node, parent, assign_ctx=None): + def visit_ellipsis(self, node, parent): """visit an Ellipsis node by returning a fresh instance of it""" return nodes.Ellipsis(getattr(node, 'lineno', None), getattr(node, 'col_offset', None), parent) - def visit_excepthandler(self, node, parent, assign_ctx=None): + def visit_excepthandler(self, node, parent): """visit an ExceptHandler node by returning a fresh instance of it""" newnode = nodes.ExceptHandler(node.lineno, node.col_offset, parent) # /!\ node.name can be a tuple - newnode.postinit(_visit_or_none(node, 'type', self, newnode, assign_ctx), - _visit_or_none(node, 'name', self, newnode, 'Assign'), - [self.visit(child, newnode, None) + newnode.postinit(_visit_or_none(node, 'type', self, newnode), + _visit_or_none(node, 'name', self, newnode), + [self.visit(child, newnode) for child in node.body]) return newnode - def visit_exec(self, node, parent, assign_ctx=None): + def visit_exec(self, node, parent): """visit an Exec node by returning a fresh instance of it""" newnode = nodes.Exec(node.lineno, node.col_offset, parent) - newnode.postinit(self.visit(node.body, newnode, assign_ctx), - _visit_or_none(node, 'globals', self, newnode, - assign_ctx), - _visit_or_none(node, 'locals', self, newnode, - assign_ctx)) + newnode.postinit(self.visit(node.body, newnode), + _visit_or_none(node, 'globals', self, newnode), + _visit_or_none(node, 'locals', self, newnode)) return newnode - def visit_extslice(self, node, parent, assign_ctx=None): + def visit_extslice(self, node, parent): """visit an ExtSlice node by returning a fresh instance of it""" newnode = nodes.ExtSlice(parent=parent) - newnode.postinit([self.visit(dim, newnode, assign_ctx) + newnode.postinit([self.visit(dim, newnode) for dim in node.dims]) return newnode - def _visit_for(self, cls, node, parent, assign_ctx=None): + def _visit_for(self, cls, node, parent): """visit a For node by returning a fresh instance of it""" newnode = cls(node.lineno, node.col_offset, parent) - newnode.postinit(self.visit(node.target, newnode, "Assign"), - self.visit(node.iter, newnode, None), - [self.visit(child, newnode, None) + newnode.postinit(self.visit(node.target, newnode), + self.visit(node.iter, newnode), + [self.visit(child, newnode) for child in node.body], - [self.visit(child, newnode, None) + [self.visit(child, newnode) for child in node.orelse]) return newnode - def visit_for(self, node, parent, assign_ctx=None): - return self._visit_for(nodes.For, node, parent, - assign_ctx=assign_ctx) + def visit_for(self, node, parent): + return self._visit_for(nodes.For, node, parent) - def visit_importfrom(self, node, parent, assign_ctx=None): + def visit_importfrom(self, node, parent): """visit an ImportFrom node by returning a fresh instance of it""" names = [(alias.name, alias.asname) for alias in node.names] newnode = nodes.ImportFrom(node.module or '', names, node.level or None, @@ -437,56 +440,56 @@ def visit_importfrom(self, node, parent, assign_ctx=None): getattr(node, 'col_offset', None), parent) return newnode - def _visit_functiondef(self, cls, node, parent, assign_ctx=None): + def _visit_functiondef(self, cls, node, parent): """visit an FunctionDef node to become astroid""" self._global_names.append({}) node, doc = _get_doc(node) newnode = cls(node.name, doc, node.lineno, node.col_offset, parent) if node.decorator_list: - decorators = self.visit_decorators(node, newnode, assign_ctx) + decorators = self.visit_decorators(node, newnode) else: decorators = None if PY3 and node.returns: - returns = self.visit(node.returns, newnode, assign_ctx) + returns = self.visit(node.returns, newnode) else: returns = None - newnode.postinit(self.visit(node.args, newnode, assign_ctx), - [self.visit(child, newnode, assign_ctx) + newnode.postinit(self.visit(node.args, newnode), + [self.visit(child, newnode) for child in node.body], decorators, returns) self._global_names.pop() return newnode - def visit_functiondef(self, node, parent, assign_ctx=None): - return self._visit_functiondef(nodes.FunctionDef, node, parent, - assign_ctx=assign_ctx) + def visit_functiondef(self, node, parent): + return self._visit_functiondef(nodes.FunctionDef, node, parent) - def visit_generatorexp(self, node, parent, assign_ctx=None): + def visit_generatorexp(self, node, parent): """visit a GeneratorExp node by returning a fresh instance of it""" newnode = nodes.GeneratorExp(node.lineno, node.col_offset, parent) - newnode.postinit(self.visit(node.elt, newnode, assign_ctx), - [self.visit(child, newnode, assign_ctx) + newnode.postinit(self.visit(node.elt, newnode), + [self.visit(child, newnode) for child in node.generators]) return newnode - def visit_attribute(self, node, parent, assign_ctx=None): + def visit_attribute(self, node, parent): """visit an Attribute node by returning a fresh instance of it""" - if assign_ctx == "Del": + context = _get_context(node) + if context == astroid.Del: # FIXME : maybe we should reintroduce and visit_delattr ? # for instance, deactivating assign_ctx newnode = nodes.DelAttr(node.attr, node.lineno, node.col_offset, parent) - elif assign_ctx == "Assign": + elif context == astroid.Store: newnode = nodes.AssignAttr(node.attr, node.lineno, node.col_offset, parent) else: newnode = nodes.Attribute(node.attr, node.lineno, node.col_offset, parent) - newnode.postinit(self.visit(node.value, newnode, None)) + newnode.postinit(self.visit(node.value, newnode)) return newnode - def visit_global(self, node, parent, assign_ctx=None): + def visit_global(self, node, parent): """visit a Global node to become astroid""" newnode = nodes.Global(node.names, getattr(node, 'lineno', None), getattr(node, 'col_offset', None), parent) @@ -495,74 +498,78 @@ def visit_global(self, node, parent, assign_ctx=None): self._global_names[-1].setdefault(name, []).append(newnode) return newnode - def visit_if(self, node, parent, assign_ctx=None): + def visit_if(self, node, parent): """visit an If node by returning a fresh instance of it""" newnode = nodes.If(node.lineno, node.col_offset, parent) - newnode.postinit(self.visit(node.test, newnode, assign_ctx), - [self.visit(child, newnode, assign_ctx) + newnode.postinit(self.visit(node.test, newnode), + [self.visit(child, newnode) for child in node.body], - [self.visit(child, newnode, assign_ctx) + [self.visit(child, newnode) for child in node.orelse]) return newnode - def visit_ifexp(self, node, parent, assign_ctx=None): + def visit_ifexp(self, node, parent): """visit a IfExp node by returning a fresh instance of it""" newnode = nodes.IfExp(node.lineno, node.col_offset, parent) - newnode.postinit(self.visit(node.test, newnode, assign_ctx), - self.visit(node.body, newnode, assign_ctx), - self.visit(node.orelse, newnode, assign_ctx)) + newnode.postinit(self.visit(node.test, newnode), + self.visit(node.body, newnode), + self.visit(node.orelse, newnode)) return newnode - def visit_import(self, node, parent, assign_ctx=None): + def visit_import(self, node, parent): """visit a Import node by returning a fresh instance of it""" names = [(alias.name, alias.asname) for alias in node.names] newnode = nodes.Import(names, getattr(node, 'lineno', None), getattr(node, 'col_offset', None), parent) return newnode - def visit_index(self, node, parent, assign_ctx=None): + def visit_index(self, node, parent): """visit a Index node by returning a fresh instance of it""" newnode = nodes.Index(parent=parent) - newnode.postinit(self.visit(node.value, newnode, assign_ctx)) + newnode.postinit(self.visit(node.value, newnode)) return newnode - def visit_keyword(self, node, parent, assign_ctx=None): + def visit_keyword(self, node, parent): """visit a Keyword node by returning a fresh instance of it""" newnode = nodes.Keyword(node.arg, parent=parent) - newnode.postinit(self.visit(node.value, newnode, assign_ctx)) + newnode.postinit(self.visit(node.value, newnode)) return newnode - def visit_lambda(self, node, parent, assign_ctx=None): + def visit_lambda(self, node, parent): """visit a Lambda node by returning a fresh instance of it""" newnode = nodes.Lambda(node.lineno, node.col_offset, parent) - newnode.postinit(self.visit(node.args, newnode, assign_ctx), - self.visit(node.body, newnode, assign_ctx)) + newnode.postinit(self.visit(node.args, newnode), + self.visit(node.body, newnode)) return newnode - def visit_list(self, node, parent, assign_ctx=None): + def visit_list(self, node, parent): """visit a List node by returning a fresh instance of it""" - newnode = nodes.List(node.lineno, node.col_offset, parent) - newnode.postinit([self.visit(child, newnode, assign_ctx) + context = _get_context(node) + newnode = nodes.List(ctx=context, + lineno=node.lineno, + col_offset=node.col_offset, + parent=parent) + newnode.postinit([self.visit(child, newnode) for child in node.elts]) return newnode - def visit_listcomp(self, node, parent, assign_ctx=None): + def visit_listcomp(self, node, parent): """visit a ListComp node by returning a fresh instance of it""" newnode = nodes.ListComp(node.lineno, node.col_offset, parent) - newnode.postinit(self.visit(node.elt, newnode, assign_ctx), - [self.visit(child, newnode, assign_ctx) + newnode.postinit(self.visit(node.elt, newnode), + [self.visit(child, newnode) for child in node.generators]) return newnode - def visit_name(self, node, parent, assign_ctx=None): + def visit_name(self, node, parent): """visit a Name node by returning a fresh instance of it""" + context = _get_context(node) # True and False can be assigned to something in py2x, so we have to - # check first the assign_ctx - if assign_ctx == "Del": + # check first the context. + if context == astroid.Del: newnode = nodes.DelName(node.id, node.lineno, node.col_offset, parent) - elif assign_ctx is not None: # Assign - assert assign_ctx == "Assign" + elif context == astroid.Store: newnode = nodes.AssignName(node.id, node.lineno, node.col_offset, parent) elif node.id in BUILTIN_NAMES: @@ -575,253 +582,254 @@ def visit_name(self, node, parent, assign_ctx=None): newnode = nodes.Name(node.id, node.lineno, node.col_offset, parent) return newnode - def visit_str(self, node, parent, assign_ctx=None): + def visit_str(self, node, parent): """visit a String/Bytes node by returning a fresh instance of Const""" return nodes.Const(node.s, getattr(node, 'lineno', None), getattr(node, 'col_offset', None), parent) visit_bytes = visit_str - def visit_num(self, node, parent, assign_ctx=None): + def visit_num(self, node, parent): """visit a Num node by returning a fresh instance of Const""" return nodes.Const(node.n, getattr(node, 'lineno', None), getattr(node, 'col_offset', None), parent) - def visit_pass(self, node, parent, assign_ctx=None): + def visit_pass(self, node, parent): """visit a Pass node by returning a fresh instance of it""" return nodes.Pass(node.lineno, node.col_offset, parent) - def visit_print(self, node, parent, assign_ctx=None): + def visit_print(self, node, parent): """visit a Print node by returning a fresh instance of it""" newnode = nodes.Print(node.nl, node.lineno, node.col_offset, parent) - newnode.postinit(_visit_or_none(node, 'dest', self, newnode, assign_ctx), - [self.visit(child, newnode, assign_ctx) + newnode.postinit(_visit_or_none(node, 'dest', self, newnode), + [self.visit(child, newnode) for child in node.values]) return newnode - def visit_raise(self, node, parent, assign_ctx=None): + def visit_raise(self, node, parent): """visit a Raise node by returning a fresh instance of it""" newnode = nodes.Raise(node.lineno, node.col_offset, parent) - newnode.postinit(_visit_or_none(node, 'type', self, newnode, assign_ctx), - _visit_or_none(node, 'inst', self, newnode, assign_ctx), - _visit_or_none(node, 'tback', self, newnode, - assign_ctx)) + newnode.postinit(_visit_or_none(node, 'type', self, newnode), + _visit_or_none(node, 'inst', self, newnode), + _visit_or_none(node, 'tback', self, newnode)) return newnode - def visit_return(self, node, parent, assign_ctx=None): + def visit_return(self, node, parent): """visit a Return node by returning a fresh instance of it""" newnode = nodes.Return(node.lineno, node.col_offset, parent) if node.value is not None: - newnode.postinit(self.visit(node.value, newnode, assign_ctx)) + newnode.postinit(self.visit(node.value, newnode)) return newnode - def visit_set(self, node, parent, assign_ctx=None): + def visit_set(self, node, parent): """visit a Set node by returning a fresh instance of it""" newnode = nodes.Set(node.lineno, node.col_offset, parent) - newnode.postinit([self.visit(child, newnode, assign_ctx) + newnode.postinit([self.visit(child, newnode) for child in node.elts]) return newnode - def visit_setcomp(self, node, parent, assign_ctx=None): + def visit_setcomp(self, node, parent): """visit a SetComp node by returning a fresh instance of it""" newnode = nodes.SetComp(node.lineno, node.col_offset, parent) - newnode.postinit(self.visit(node.elt, newnode, assign_ctx), - [self.visit(child, newnode, assign_ctx) + newnode.postinit(self.visit(node.elt, newnode), + [self.visit(child, newnode) for child in node.generators]) return newnode - def visit_slice(self, node, parent, assign_ctx=None): + def visit_slice(self, node, parent): """visit a Slice node by returning a fresh instance of it""" newnode = nodes.Slice(parent=parent) - newnode.postinit(_visit_or_none(node, 'lower', self, newnode, - assign_ctx), - _visit_or_none(node, 'upper', self, newnode, - assign_ctx), - _visit_or_none(node, 'step', self, newnode, assign_ctx)) + newnode.postinit(_visit_or_none(node, 'lower', self, newnode), + _visit_or_none(node, 'upper', self, newnode), + _visit_or_none(node, 'step', self, newnode)) return newnode - def visit_subscript(self, node, parent, assign_ctx=None): + def visit_subscript(self, node, parent): """visit a Subscript node by returning a fresh instance of it""" - newnode = nodes.Subscript(node.lineno, node.col_offset, parent) - newnode.postinit(self.visit(node.value, newnode, None), - self.visit(node.slice, newnode, None)) + context = _get_context(node) + newnode = nodes.Subscript(ctx=context, + lineno=node.lineno, + col_offset=node.col_offset, + parent=parent) + newnode.postinit(self.visit(node.value, newnode), + self.visit(node.slice, newnode)) return newnode - def visit_tryexcept(self, node, parent, assign_ctx=None): + def visit_tryexcept(self, node, parent): """visit a TryExcept node by returning a fresh instance of it""" newnode = nodes.TryExcept(node.lineno, node.col_offset, parent) - newnode.postinit([self.visit(child, newnode, assign_ctx) + newnode.postinit([self.visit(child, newnode) for child in node.body], - [self.visit(child, newnode, assign_ctx) + [self.visit(child, newnode) for child in node.handlers], - [self.visit(child, newnode, assign_ctx) + [self.visit(child, newnode) for child in node.orelse]) return newnode - def visit_tryfinally(self, node, parent, assign_ctx=None): + def visit_tryfinally(self, node, parent): """visit a TryFinally node by returning a fresh instance of it""" newnode = nodes.TryFinally(node.lineno, node.col_offset, parent) - newnode.postinit([self.visit(child, newnode, assign_ctx) + newnode.postinit([self.visit(child, newnode) for child in node.body], - [self.visit(n, newnode, assign_ctx) + [self.visit(n, newnode) for n in node.finalbody]) return newnode - def visit_tuple(self, node, parent, assign_ctx=None): + def visit_tuple(self, node, parent): """visit a Tuple node by returning a fresh instance of it""" - newnode = nodes.Tuple(node.lineno, node.col_offset, parent) - newnode.postinit([self.visit(child, newnode, assign_ctx) + context = _get_context(node) + newnode = nodes.Tuple(ctx=context, + lineno=node.lineno, + col_offset=node.col_offset, + parent=parent) + newnode.postinit([self.visit(child, newnode) for child in node.elts]) return newnode - def visit_unaryop(self, node, parent, assign_ctx=None): + def visit_unaryop(self, node, parent): """visit a UnaryOp node by returning a fresh instance of it""" newnode = nodes.UnaryOp(_UNARY_OP_CLASSES[node.op.__class__], node.lineno, node.col_offset, parent) - newnode.postinit(self.visit(node.operand, newnode, assign_ctx)) + newnode.postinit(self.visit(node.operand, newnode)) return newnode - def visit_while(self, node, parent, assign_ctx=None): + def visit_while(self, node, parent): """visit a While node by returning a fresh instance of it""" newnode = nodes.While(node.lineno, node.col_offset, parent) - newnode.postinit(self.visit(node.test, newnode, assign_ctx), - [self.visit(child, newnode, assign_ctx) + newnode.postinit(self.visit(node.test, newnode), + [self.visit(child, newnode) for child in node.body], - [self.visit(child, newnode, assign_ctx) + [self.visit(child, newnode) for child in node.orelse]) return newnode - def visit_with(self, node, parent, assign_ctx=None): + def visit_with(self, node, parent): newnode = nodes.With(node.lineno, node.col_offset, parent) - expr = self.visit(node.context_expr, newnode, assign_ctx) + expr = self.visit(node.context_expr, newnode) if node.optional_vars is not None: - optional_vars = self.visit(node.optional_vars, newnode, "Assign") + optional_vars = self.visit(node.optional_vars, newnode) else: optional_vars = None newnode.postinit([(expr, optional_vars)], - [self.visit(child, newnode, None) + [self.visit(child, newnode) for child in node.body]) return newnode - def visit_yield(self, node, parent, assign_ctx=None): + def visit_yield(self, node, parent): """visit a Yield node by returning a fresh instance of it""" newnode = nodes.Yield(node.lineno, node.col_offset, parent) if node.value is not None: - newnode.postinit(self.visit(node.value, newnode, assign_ctx)) + newnode.postinit(self.visit(node.value, newnode)) return newnode class TreeRebuilder3(TreeRebuilder): """extend and overwrite TreeRebuilder for python3k""" - def visit_arg(self, node, parent, assign_ctx=None): + def visit_arg(self, node, parent): """visit a arg node by returning a fresh AssName instance""" # TODO(cpopa): introduce an Arg node instead of using AssignName. - return self.visit_assignname(node, parent, assign_ctx, node.arg) + return self.visit_assignname(node, parent, node.arg) - def visit_nameconstant(self, node, parent, assign_ctx=None): + def visit_nameconstant(self, node, parent): # in Python 3.4 we have NameConstant for True / False / None return nodes.NameConstant(node.value, getattr(node, 'lineno', None), getattr(node, 'col_offset', None), parent) - def visit_excepthandler(self, node, parent, assign_ctx=None): + def visit_excepthandler(self, node, parent): """visit an ExceptHandler node by returning a fresh instance of it""" newnode = nodes.ExceptHandler(node.lineno, node.col_offset, parent) if node.name: - name = self.visit_assignname(node, newnode, assign_ctx, node.name) + name = self.visit_assignname(node, newnode, node.name) else: name = None - newnode.postinit(_visit_or_none(node, 'type', self, newnode, assign_ctx), + newnode.postinit(_visit_or_none(node, 'type', self, newnode), name, - [self.visit(child, newnode, assign_ctx) + [self.visit(child, newnode) for child in node.body]) return newnode - def visit_nonlocal(self, node, parent, assign_ctx=None): + def visit_nonlocal(self, node, parent): """visit a Nonlocal node and return a new instance of it""" return nodes.Nonlocal(node.names, getattr(node, 'lineno', None), getattr(node, 'col_offset', None), parent) - def visit_raise(self, node, parent, assign_ctx=None): + def visit_raise(self, node, parent): """visit a Raise node by returning a fresh instance of it""" newnode = nodes.Raise(node.lineno, node.col_offset, parent) # no traceback; anyway it is not used in Pylint - newnode.postinit(_visit_or_none(node, 'exc', self, newnode, assign_ctx), - _visit_or_none(node, 'cause', self, newnode, - assign_ctx)) + newnode.postinit(_visit_or_none(node, 'exc', self, newnode), + _visit_or_none(node, 'cause', self, newnode)) return newnode - def visit_starred(self, node, parent, assign_ctx=None): + def visit_starred(self, node, parent): """visit a Starred node and return a new instance of it""" - newnode = nodes.Starred(node.lineno, node.col_offset, parent) - newnode.postinit(self.visit(node.value, newnode, assign_ctx)) + context = _get_context(node) + newnode = nodes.Starred(ctx=context, lineno=node.lineno, + col_offset=node.col_offset, + parent=parent) + newnode.postinit(self.visit(node.value, newnode)) return newnode - def visit_try(self, node, parent, assign_ctx=None): + def visit_try(self, node, parent): # python 3.3 introduce a new Try node replacing # TryFinally/TryExcept nodes if node.finalbody: newnode = nodes.TryFinally(node.lineno, node.col_offset, parent) if node.handlers: - body = [self.visit_tryexcept(node, newnode, assign_ctx)] + body = [self.visit_tryexcept(node, newnode)] else: - body = [self.visit(child, newnode, assign_ctx) + body = [self.visit(child, newnode) for child in node.body] newnode.postinit(body, - [self.visit(n, newnode, assign_ctx) + [self.visit(n, newnode) for n in node.finalbody]) return newnode elif node.handlers: - return self.visit_tryexcept(node, parent, assign_ctx) + return self.visit_tryexcept(node, parent) - def _visit_with(self, cls, node, parent, assign_ctx=None): + def _visit_with(self, cls, node, parent): if 'items' not in node._fields: # python < 3.3 - return super(TreeRebuilder3, self).visit_with(node, parent, - assign_ctx) + return super(TreeRebuilder3, self).visit_with(node, parent) newnode = cls(node.lineno, node.col_offset, parent) def visit_child(child): - expr = self.visit(child.context_expr, newnode, assign_ctx) - var = _visit_or_none(child, 'optional_vars', self, newnode, - 'Assign') + expr = self.visit(child.context_expr, newnode) + var = _visit_or_none(child, 'optional_vars', self, newnode) return expr, var newnode.postinit([visit_child(child) for child in node.items], - [self.visit(child, newnode, None) + [self.visit(child, newnode) for child in node.body]) return newnode - def visit_with(self, node, parent, assign_ctx=None): - return self._visit_with(nodes.With, node, parent, assign_ctx=assign_ctx) + def visit_with(self, node, parent): + return self._visit_with(nodes.With, node, parent) - def visit_yieldfrom(self, node, parent, assign_ctx=None): + def visit_yieldfrom(self, node, parent): newnode = nodes.YieldFrom(node.lineno, node.col_offset, parent) if node.value is not None: - newnode.postinit(self.visit(node.value, newnode, assign_ctx)) + newnode.postinit(self.visit(node.value, newnode)) return newnode - def visit_classdef(self, node, parent, assign_ctx=None, newstyle=True): + def visit_classdef(self, node, parent, newstyle=True): return super(TreeRebuilder3, self).visit_classdef(node, parent, - assign_ctx, newstyle=newstyle) # Async structs added in Python 3.5 - def visit_asyncfunctiondef(self, node, parent, assign_ctx=None): - return self._visit_functiondef(nodes.AsyncFunctionDef, node, parent, - assign_ctx=assign_ctx) + def visit_asyncfunctiondef(self, node, parent): + return self._visit_functiondef(nodes.AsyncFunctionDef, node, parent) - def visit_asyncfor(self, node, parent, assign_ctx=None): - return self._visit_for(nodes.AsyncFor, node, parent, - assign_ctx=assign_ctx) + def visit_asyncfor(self, node, parent): + return self._visit_for(nodes.AsyncFor, node, parent) - def visit_await(self, node, parent, assign_ctx=None): + def visit_await(self, node, parent): newnode = nodes.Await(node.lineno, node.col_offset, parent) - newnode.postinit(value=self.visit(node.value, newnode, None)) + newnode.postinit(value=self.visit(node.value, newnode)) return newnode - def visit_asyncwith(self, node, parent, assign_ctx=None): - return self._visit_with(nodes.AsyncWith, node, parent, - assign_ctx=assign_ctx) + def visit_asyncwith(self, node, parent): + return self._visit_with(nodes.AsyncWith, node, parent) if sys.version_info >= (3, 0): From 08eeb6371f689d68a091144cf0e86beaff5da4a9 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Fri, 11 Dec 2015 00:02:57 +0200 Subject: [PATCH 123/312] Change the extension of the README file, so it gets rendered by github. --- README => README.rst | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename README => README.rst (100%) diff --git a/README b/README.rst similarity index 100% rename from README rename to README.rst From 34428dc9226b89a7c100e5c329f9a9581f918afc Mon Sep 17 00:00:00 2001 From: Aru Sahni Date: Thu, 10 Dec 2015 06:37:34 -0500 Subject: [PATCH 124/312] Port hgignore to gitignore. For future iterations, we should switch to using pre-canned ignore lists from https://gitignore.io. --- .gitignore | 9 +++++++++ .hgignore | 9 --------- 2 files changed, 9 insertions(+), 9 deletions(-) create mode 100644 .gitignore delete mode 100644 .hgignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..d74774df07 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +.svn/ +.hg/ +.hgtags/ +*.py[cod] +log +build +dist/ +astroid.egg-info/ +.tox diff --git a/.hgignore b/.hgignore deleted file mode 100644 index d2628e8911..0000000000 --- a/.hgignore +++ /dev/null @@ -1,9 +0,0 @@ -(^|/)\.svn($|/) -(^|/)\.hg($|/) -(^|/)\.hgtags($|/) -\.pyc$ -^log$ -^doc/build$ -^dist/ -^astroid\.egg-info/ -^\.tox/ From 4702a39f4f98cee089ff1c555a36d1e922174da6 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Fri, 11 Dec 2015 13:05:31 +0200 Subject: [PATCH 125/312] Enable the warnings in tests through a context manager Previously, we were enabling blindly all the warnings in tests files through `warnings.simplefilter`, which had the side effect of not taking into account the values of the -W flag. This meant that warnings which should have been silenced, weren't, leading to a huge output when running the tests with Python 3.5. --- astroid/test_utils.py | 13 +++++++++++++ astroid/tests/unittest_inference.py | 4 ++-- astroid/tests/unittest_nodes.py | 21 +++++++++++---------- astroid/tests/unittest_scoped_nodes.py | 4 ++-- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/astroid/test_utils.py b/astroid/test_utils.py index e16930786c..b15a14532e 100644 --- a/astroid/test_utils.py +++ b/astroid/test_utils.py @@ -1,6 +1,8 @@ """Utility functions for test code that uses astroid ASTs as input.""" +import contextlib import functools import sys +import warnings from astroid import builder from astroid import raw_building @@ -211,3 +213,14 @@ def bootstrap(astroid_builtin=None): # unittest_lookup.LookupTC.test_builtin_lookup fail depending on the # test order raw_building.ast_from_builtins() + + +@contextlib.contextmanager +def enable_warning(warning): + warnings.simplefilter('always', warning) + try: + yield + finally: + # Reset it to default value, so it will take + # into account the values from the -W flag. + warnings.simplefilter('default', warning) diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index edb46e3780..328b813b19 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -384,9 +384,9 @@ def f(f=1): ast = parse(code, __name__) a = ast['a'] - warnings.simplefilter('always') with warnings.catch_warnings(record=True) as w: - a.infered() + with test_utils.enable_warning(PendingDeprecationWarning): + a.infered() self.assertIsInstance(w[0].message, PendingDeprecationWarning) def test_exc_ancestors(self): diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index c3a6e463ba..56f705714e 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -682,17 +682,18 @@ class C: pass assign_type_mixin = module.body[1].targets[0] parent_assign_type_mixin = module.body[2] - warnings.simplefilter('always') - with warnings.catch_warnings(record=True) as w: - filter_stmts_mixin.ass_type() - self.assertIsInstance(w[0].message, PendingDeprecationWarning) + with test_utils.enable_warning(PendingDeprecationWarning): + filter_stmts_mixin.ass_type() + self.assertIsInstance(w[0].message, PendingDeprecationWarning) with warnings.catch_warnings(record=True) as w: - assign_type_mixin.ass_type() - self.assertIsInstance(w[0].message, PendingDeprecationWarning) + with test_utils.enable_warning(PendingDeprecationWarning): + assign_type_mixin.ass_type() + self.assertIsInstance(w[0].message, PendingDeprecationWarning) with warnings.catch_warnings(record=True) as w: - parent_assign_type_mixin.ass_type() - self.assertIsInstance(w[0].message, PendingDeprecationWarning) + with test_utils.enable_warning(PendingDeprecationWarning): + parent_assign_type_mixin.ass_type() + self.assertIsInstance(w[0].message, PendingDeprecationWarning) def test_isinstance_warnings(self): msg_format = ("%r is deprecated and slated for removal in astroid " @@ -700,8 +701,8 @@ def test_isinstance_warnings(self): for cls in (nodes.Discard, nodes.Backquote, nodes.AssName, nodes.AssAttr, nodes.Getattr, nodes.CallFunc, nodes.From): with warnings.catch_warnings(record=True) as w: - warnings.simplefilter('always') - isinstance(42, cls) + with test_utils.enable_warning(PendingDeprecationWarning): + isinstance(42, cls) self.assertIsInstance(w[0].message, PendingDeprecationWarning) actual_msg = msg_format % (cls.__class__.__name__, cls.__wrapped__.__name__) self.assertEqual(str(w[0].message), actual_msg) diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index 8467099cfd..ef7f3cdd97 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -234,8 +234,8 @@ def test_file_stream_api(self): # only Module.stream as the recommended way to retrieve # its file stream. with warnings.catch_warnings(record=True) as cm: - warnings.simplefilter("always") - self.assertIsNot(astroid.file_stream, astroid.file_stream) + with test_utils.enable_warning(PendingDeprecationWarning): + self.assertIsNot(astroid.file_stream, astroid.file_stream) self.assertGreater(len(cm), 1) self.assertEqual(cm[0].category, PendingDeprecationWarning) From 05316cc3effd38ea0911a71c1c0f0a693b8382bd Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Fri, 11 Dec 2015 18:21:15 +0200 Subject: [PATCH 126/312] Fix an undefined-variable, which was caught by pylint, but ignored lately. --- astroid/builder.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astroid/builder.py b/astroid/builder.py index 43a3bc4c98..b3a5d0f00f 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -132,7 +132,7 @@ def file_build(self, path, modname=None): # detect_encoding returns utf-8 if no encoding specified util.reraise(exceptions.AstroidBuildingError( 'Wrong ({encoding}) or no encoding specified for {filename}.', - encoding=encoding, filename=filename)) + encoding=encoding, filename=path)) with stream: # get module name if necessary if modname is None: From ea0425fdffaf00c71e8e1aa96bfba683c577179a Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Fri, 11 Dec 2015 19:16:41 +0200 Subject: [PATCH 127/312] Fix an undefined-variable in extract_node. --- astroid/test_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astroid/test_utils.py b/astroid/test_utils.py index b15a14532e..b15ab766b4 100644 --- a/astroid/test_utils.py +++ b/astroid/test_utils.py @@ -184,7 +184,7 @@ def parse(string, default=None): try: return tuple(int(v) for v in string.split('.')) except ValueError: - util.reraise(ValueError('%s is not a correct version : should be X.Y[.Z].' % version)) + util.reraise(ValueError('%s is not a correct version : should be X.Y[.Z].' % string)) def check_require_version(f): current = sys.version_info[:3] From d8a87cc5055abe7f8a8a41222632b640cf89ed7f Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Wed, 16 Dec 2015 11:06:27 +0200 Subject: [PATCH 128/312] 2.0 branch has version 2.0 now. --- astroid/__pkginfo__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astroid/__pkginfo__.py b/astroid/__pkginfo__.py index 3706d2b396..ee4712d713 100644 --- a/astroid/__pkginfo__.py +++ b/astroid/__pkginfo__.py @@ -22,7 +22,7 @@ modname = 'astroid' -numversion = (1, 4, 0) +numversion = (2, 0, 0) version = '.'.join([str(num) for num in numversion]) From c2590f59337ff742e503e4e2435467bea65c74f5 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 29 Nov 2015 22:11:22 +0200 Subject: [PATCH 129/312] Add support for handling Uninferable nodes when calling as_string Some object, for instance List or Tuple can have, after inference, Uninferable as their elements, happening when their components weren't couldn't be inferred properly. This means that as_string needs to cope with expecting Uninferable nodes part of the other nodes coming for a string transformation. The patch adds a visit method in AsString and ``accept`` on Yes / Uninferable nodes. Closes issue #270. --- astroid/as_string.py | 3 +++ astroid/tests/unittest_nodes.py | 10 ++++++++++ astroid/util.py | 6 ++++++ 3 files changed, 19 insertions(+) diff --git a/astroid/as_string.py b/astroid/as_string.py index 37dc77d559..d160b376ea 100644 --- a/astroid/as_string.py +++ b/astroid/as_string.py @@ -434,6 +434,9 @@ def visit_frozenset(self, node): def visit_super(self, node): return node.parent.accept(self) + def visit_uninferable(self, node): + return str(node) + class AsStringVisitor3(AsStringVisitor): """AsStringVisitor3 overwrites some AsStringVisitor methods""" diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index 56f705714e..ce8863b3fb 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -67,6 +67,16 @@ def test(a, b, c=42, *, x=42, **kwargs): node = parse(code) self.assertEqual(node.as_string().strip(), code.strip()) + def test_as_string_for_list_containing_uninferable(self): + node = test_utils.extract_node(''' + def foo(): + bar = [arg] * 1 + ''') + binop = node.body[0].value + inferred = next(binop.infer()) + self.assertEqual(inferred.as_string(), '[Uninferable]') + self.assertEqual(binop.as_string(), '([arg]) * (1)') + def test_frozenset_as_string(self): nodes = test_utils.extract_node(''' frozenset((1, 2, 3)) #@ diff --git a/astroid/util.py b/astroid/util.py index 9c20eb4b9d..6fd59cfb2c 100644 --- a/astroid/util.py +++ b/astroid/util.py @@ -76,17 +76,23 @@ class Uninferable(object): """Special inference object, which is returned when inference fails.""" def __repr__(self): return 'Uninferable' + __str__ = __repr__ def __getattribute__(self, name): if name == 'next': raise AttributeError('next method should not be called') if name.startswith('__') and name.endswith('__'): return object.__getattribute__(self, name) + if name == 'accept': + return object.__getattribute__(self, name) return self def __call__(self, *args, **kwargs): return self + def accept(self, visitor): + func = getattr(visitor, "visit_uninferable") + return func(self) class BadOperationMessage(object): """Object which describes a TypeError occurred somewhere in the inference chain From b93bef04c77b4dc5cb3e412eeb56685a2c0410af Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 1 Dec 2015 18:49:47 +0200 Subject: [PATCH 130/312] Use printf-style formatting in as_string, in order to avoid a potential problem with encodings when using .format. --- ChangeLog | 14 +++++++++++++- astroid/as_string.py | 12 +++++------- astroid/tests/unittest_regrtest.py | 17 +++++++++++++++++ 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index 56f1e90901..9fa0814710 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,7 +1,19 @@ Change log for the astroid package (used to be astng) ===================================================== --- + * Revert to using printf-style formatting in as_string, in order + to avoid a potential problem with encodings when using .format. + Closes issue #273. + + * Add support for handling Uninferable nodes when calling as_string + + Some object, for instance List or Tuple can have, after inference, + Uninferable as their elements, happening when their components + weren't couldn't be inferred properly. This means that as_string + needs to cope with expecting Uninferable nodes part of the other + nodes coming for a string transformation. The patch adds a visit + method in AsString and ``accept`` on Yes / Uninferable nodes. + Closes issue #270. * Some nodes got a new attribute, 'ctx', which tells in which context the said node was used. diff --git a/astroid/as_string.py b/astroid/as_string.py index d160b376ea..5b64e065eb 100644 --- a/astroid/as_string.py +++ b/astroid/as_string.py @@ -244,13 +244,11 @@ def visit_functiondef(self, node): trailer = return_annotation + ":" else: trailer = ":" - def_format = "\n{decorators}def {name}({args}){trailer}{docs}\n{body}" - return def_format.format(decorators=decorate, - name=node.name, - args=node.args.accept(self), - trailer=trailer, - docs=docs, - body=self._stmt_list(node.body)) + def_format = "\n%sdef %s(%s)%s%s\n%s" + return def_format % (decorate, node.name, + node.args.accept(self), + trailer, docs, + self._stmt_list(node.body)) def visit_generatorexp(self, node): """return an astroid.GeneratorExp node as string""" diff --git a/astroid/tests/unittest_regrtest.py b/astroid/tests/unittest_regrtest.py index e4e5f6ee0a..0206dd46d5 100644 --- a/astroid/tests/unittest_regrtest.py +++ b/astroid/tests/unittest_regrtest.py @@ -288,6 +288,23 @@ def gen(): yield ''') self.assertRaises(exceptions.InferenceError, next, node.infer()) + def test_unicode_in_docstring(self): + # Crashed for astroid==1.4.1 + # Test for https://bitbucket.org/logilab/astroid/issues/273/ + + # In a regular file, "coding: utf-8" would have been used. + node = extract_node(u''' + from __future__ import unicode_literals + + class MyClass(object): + def method(self): + "With unicode : %s " + + instance = MyClass() + ''' % u"\u2019") + + next(node.value.infer()).as_string() + class Whatever(object): a = property(lambda x: x, lambda x: x) From 7e270bcf3843fc775872305ba934463ad845b6e4 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 1 Dec 2015 18:54:53 +0200 Subject: [PATCH 131/312] Don't forget to give credit to the original author. --- ChangeLog | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 9fa0814710..c91de5207a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,7 +3,7 @@ Change log for the astroid package (used to be astng) * Revert to using printf-style formatting in as_string, in order to avoid a potential problem with encodings when using .format. - Closes issue #273. + Closes issue #273. Patch by notsqrt. * Add support for handling Uninferable nodes when calling as_string From e7107ae681c161f6cad0e43af510b3dbda30026f Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Fri, 11 Dec 2015 09:14:43 +0200 Subject: [PATCH 132/312] Include a travis.yml file for configuring Travis builds This patch also brings a couple of small changes to the setup.py file, in order to make it work installing from another directory. --- .travis.yml | 36 ++++++++++++++++++++++++++++++++++++ setup.py | 9 ++++++--- tox.ini | 4 ++-- 3 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000000..09af14a718 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,36 @@ +language: python + +matrix: + include: + - python: 2.7 + env: TOXENV=py27 + - python: 3.3 + env: TOXENV=py33 + - python: 3.4 + env: TOXENV=py34 + - python: 3.5 + env: TOXENV=py35 + - python: pypy + env: TOXENV=pypy + - python: 3.5 + env: TOXENV=pylint +before_install: + - python --version + - uname -a + - lsb_release -a +install: + - pip install tox + - virtualenv --version + - easy_install --version + - pip --version + - tox --version +script: + - tox -e $TOXENV +after_failure: + - more .tox/log/* | cat + - more .tox/*/log/* | cat +notifications: + email: + on_success: always + on_failure: always + diff --git a/setup.py b/setup.py index e03d64336d..e143a2b97c 100644 --- a/setup.py +++ b/setup.py @@ -22,17 +22,20 @@ from setuptools import setup, find_packages from setuptools.command import install_lib -pkginfo = 'astroid/__pkginfo__.py' + +real_path = os.path.realpath(__file__) +astroid_dir = os.path.dirname(real_path) +pkginfo = os.path.join(astroid_dir, 'astroid', '__pkginfo__.py') with open(pkginfo, 'rb') as fobj: exec(compile(fobj.read(), pkginfo, 'exec'), locals()) -with open('README') as fobj: +with open(os.path.join(astroid_dir, 'README.rst')) as fobj: long_description = fobj.read() class AstroidInstallLib(install_lib.install_lib): def byte_compile(self, files): - test_datadir = os.path.join('astroid', 'tests', 'testdata') + test_datadir = os.path.join(astroid_dir, 'tests', 'testdata') files = [f for f in files if test_datadir not in f] install_lib.install_lib.byte_compile(self, files) diff --git a/tox.ini b/tox.ini index c5c7924a32..46aad96f93 100644 --- a/tox.ini +++ b/tox.ini @@ -18,5 +18,5 @@ deps = py27,py33,pypy,jython: singledispatch six wrapt - pylint: hg+https://bitbucket.org/logilab/pylint -commands = python -m unittest discover {posargs} -s {envsitepackagesdir}/astroid/tests -p "unittest*.py" + pylint: git+https://github.com/pycqa/pylint@master +commands = python -Wi -m unittest discover -s {envsitepackagesdir}/astroid/tests -p "unittest*.py" From 58552c587707796bdb201c611ef2a859c59917de Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 11 Dec 2015 13:00:26 +0100 Subject: [PATCH 133/312] Delete .hgtags --- .hgtags | 61 --------------------------------------------------------- 1 file changed, 61 deletions(-) delete mode 100644 .hgtags diff --git a/.hgtags b/.hgtags deleted file mode 100644 index 0c1c0d6c8f..0000000000 --- a/.hgtags +++ /dev/null @@ -1,61 +0,0 @@ -3eb17cfa7620137fb7ff5f8e7ae03595ff96acaf logilab-astng-version-0_17_0 -fc9424aa3669f239d555ee7b8669a4ebb2f18488 logilab-astng-debian-version-0_17_0-1 -a8afb875d8065429b7d2cdef063a2ff9755693fb logilab-astng-debian-version-0_17_3-1 -26dff2cd8bb78fb2277a08327b57b10decc738a5 logilab-astng-version-0_17_4 -60b470837ff7c6f82f5f50fa13e39afa794fd2a1 logilab-astng-debian-version-0_17_4-1 -3f6b03d858303e4a2289c49dce10f5109cefa6e7 astng 0.18.0 -737d960676f3a27ae4118761e66b26da837c612f logilab-astng-debian-version-0_18_0-1 -b46f6536314c578d23b7521d1262d2b15ccdc0f4 logilab-astng-version-0.19.1 -9c4ae36166b7e7108fd28ad45207115b5eaa5303 logilab-astng-debian-version-0.19.1-1 -51b687ffb4d1a9fc8ede57ff47f74dac0d2919c8 logilab-astng-version-0.19.2 -b123c3e1c8c6af28f35191a1a379ac558448fc5b logilab-astng-debian-version-0.19.2-1 -cfafd4b0541e991765224127550e9666f2dc1164 logilab-astng-version-0.19.3 -2fce093c5db74ecee63bfe45bce91ce55f95c9b9 logilab-astng-debian-version-0.19.3-1 -482fe84433702c33b525cfdab3080a7d91584c1c logilab-astng-version-0.20.0 -a977509684549fc6f11306fe855ad9386574a3f7 logilab-astng-debian-version-0.20.0-1 -482fe84433702c33b525cfdab3080a7d91584c1c logilab-astng-version-0.20.0 -e00a23b3fd1a38c146bb43dcf127f64c89357a66 logilab-astng-version-0.20.0 -a977509684549fc6f11306fe855ad9386574a3f7 logilab-astng-debian-version-0.20.0-1 -e52fefc36cbc6d42f8cdddbb465f7be357de3efc logilab-astng-debian-version-0.20.0-1 -f7b51764cde673f8775522decb19f97a939c5b70 logilab-astng-version-0.20.1 -c863d9cc6c48a2588a18f75c4a3434a91aa121c7 logilab-astng-debian-version-0.20.1-1 -85f38bd9cd775d1916691d5953356a73dea488e3 logilab-astng-version-0.20.2 -83a9e1804d17ada2234b5cceac6a77cd867e645f logilab-astng-debian-version-0.20.2-1 -24cc9d8273ce8c15e7e73020aa3ed2d8c4d6b76d logilab-astng-version-0.20.3 -f2022f47efb0d0d9d41cd6f1e4445d51fac204dc logilab-astng-debian-version-0.20.3-1 -ba70ecabe1f5d7d87b908aaf33381d0a1c1dd11d logilab-astng-version-0.20.4 -b3681c63587356f66af36774e39453b0fb129475 logilab-astng-debian-version-0.20.4-1 -fd1a1cfd5d2779619787fa49100c033d700e3061 logilab-astng-version-0.21.0 -8c96858bbfa68c19f0fe8939d2f53f0747bf7042 logilab-astng-debian-version-0.21.0-1 -aef5848d64cc1202755502b3d1caf96d6745a2cf logilab-astng-version-0.21.1 -71d957477e3a4840b37ead8dd2d4ec38162427e5 logilab-astng-debian-version-0.21.1-1 -36dc61136d8be15b8f541d1fa529e60b612f82d5 logilab-astng-version-0.22.0 -688b0d8d1f06b3e8c1ccfbfa279ca8da76d5462a logilab-astng-debian-version-0.22.0-1 -fd80e67a98016c455479ac80bb387c08970aae57 logilab-astng-version-0.23.0 -8b510baa9baad004af644d464bd42e93ed695725 logilab-astng-debian-version-0.23.0-1 -652e0a150ddac980779e4cafa7da0aa1af98cbb1 logilab-astng-version-0.23.1 -abf75e6ca8ae4e48084ed1ee72a6cfa571c5db85 logilab-astng-debian-version-0.23.1-1 -e2ba7d936faf27fe8d680572265a99345f47faed logilab-astng-version-0.24.0 -d517d5a9bac963fac4777eecc34f09c1263c076c logilab-astng-debian-version-0.24.0-1 -604235790acecbc65ddffc0b32fbdda63b836b54 logilab-astng-version-0.24.1 -185d76f61c9c5436c0d8b4400d73468fd1e13ef0 logilab-astng-debian-version-0.24.1-1 -69f3515e4446a1b3d428763e1024e4910d35c57a logilab-astng-version-0.24.2 -c48aabd3865a866af3640cc33517d1e47e74db79 logilab-astng-debian-version-0.24.2-1 -a4a9ed0f8a0cec51305a0c7345d8f4c953eda4e8 logilab-astng-version-0.24.3 -c39e816972bc7740514a133040bf044623ef4368 logilab-astng-debian-version-0.24.3-1 -1d6914a1a1a0aff4496f6163e289ab61db705f2c astroid-version-1.0.0 -aeb398869afc7f395ca310272144fb7715f53be7 astroid-debian-version-1.0.0-1 -f67f24131b3a19f7a1f8ac59b7d18ad95a9c1d5b astroid-version-1.0.1 -0f3825899d581064c8dae86ade4afb44a7feb72f astroid-debian-version-1.0.1-1 -e003574ae51bff3a42fda97b2b7cc0e07b281a73 astroid-1.1 -59b1c5e1bd8dd43cc912b8d80ed817ac229a6bfe astroid-1.2 -315668c10333213025f8a0c88a28559131ccf59f astroid-1.2.1 -a92a2a2b7c0e20df8e5ef1a330c98c0c4663553b astroid-1.3 -6ad84c7537ab20661b623b8be1609d05805bcd56 astroid-1.3.1 -6ad84c7537ab20661b623b8be1609d05805bcd56 astroid-1.3.1 -c48847c4a515fb59286bd6cfeba4f725b2df1771 astroid-1.3.1 -16369edfbc893f005593076797f82672a8ee3936 astroid-1.3.2 -82ad34850236b094bdb2a370d1cdba12fb7a154a astroid-1.3.3 -912ee5c11020f045ec5ad44e01ef2e2cff074269 astroid-1.3.4 -21520283d496a2f77a63e9721ab070f1dbc16e3e astroid-1.3.5 -bae72378bead3418421f895bee95cc3d2b081dd6 astroid-1.3.6 From 977155b5fbdc2bf5a87312eaacbe891b801691a6 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Fri, 11 Dec 2015 14:28:50 +0200 Subject: [PATCH 134/312] Add Travis badge. --- README.rst | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index b19aca87da..f4dd58b83b 100644 --- a/README.rst +++ b/README.rst @@ -1,10 +1,11 @@ -.. image:: https://drone.io/bitbucket.org/logilab/astroid/status.png - :alt: drone.io Build Status - :target: https://drone.io/bitbucket.org/logilab/astroid - Astroid ======= +.. image:: https://travis-ci.org/PyCQA/astroid.svg?branch=master + :target: https://travis-ci.org/PyCQA/astroid + + + What's this? ------------ From 15e24726616094c7d5ebd0e04260a1a1569227c3 Mon Sep 17 00:00:00 2001 From: David Shea Date: Fri, 11 Dec 2015 15:04:21 -0500 Subject: [PATCH 135/312] Check for flags/enum types before checking for int gobject-introspection enum and flag types inherit from int, so the check for a unusable string represenation needs to happen before checking if the values can be used as integers. This fixes syntax errors in the generated stubs for gobject-introspection modules containing flag and enum types. --- astroid/brain/brain_gi.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/astroid/brain/brain_gi.py b/astroid/brain/brain_gi.py index d2c133d372..62b013c3ae 100644 --- a/astroid/brain/brain_gi.py +++ b/astroid/brain/brain_gi.py @@ -47,13 +47,13 @@ def _gi_build_stub(parent): elif (inspect.ismethod(obj) or inspect.ismethoddescriptor(obj)): methods[name] = obj - elif isinstance(obj, (int, str)): - constants[name] = obj elif (str(obj).startswith(" Date: Tue, 15 Dec 2015 15:13:18 +0200 Subject: [PATCH 136/312] Add support for not byte-compiling test files invalid by design. --- setup.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index e143a2b97c..2150d1c26e 100644 --- a/setup.py +++ b/setup.py @@ -20,6 +20,7 @@ """Setup script for astroid.""" import os from setuptools import setup, find_packages +from setuptools.command import easy_install from setuptools.command import install_lib @@ -35,11 +36,20 @@ class AstroidInstallLib(install_lib.install_lib): def byte_compile(self, files): - test_datadir = os.path.join(astroid_dir, 'tests', 'testdata') + test_datadir = os.path.join('astroid', 'tests', 'testdata') files = [f for f in files if test_datadir not in f] install_lib.install_lib.byte_compile(self, files) +class AstroidEasyInstallLib(easy_install.easy_install): + # override this since pip/easy_install attempt to byte compile + # test data files, some of them being syntactically wrong by design, + # and this scares the end-user + def byte_compile(self, files): + test_datadir = os.path.join('astroid', 'tests', 'testdata') + files = [f for f in files if test_datadir not in f] + easy_install.easy_install.byte_compile(self, files) + def install(): return setup(name = distname, @@ -54,7 +64,8 @@ def install(): include_package_data = True, install_requires = install_requires, packages = find_packages(), - cmdclass={'install_lib': AstroidInstallLib} + cmdclass={'install_lib': AstroidInstallLib, + 'easy_install': AstroidEasyInstallLib} ) From a001a1edb326e0ca096ff3c016c1b7800d221741 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 15 Dec 2015 18:06:44 +0200 Subject: [PATCH 137/312] Add lib_pypy into the list of standard library modules for PyPy Unfortunately it is not detected in any way by distutils.sysconfig.get_python_lib. This can detect datetime as a builtin module on PyPy. --- astroid/modutils.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/astroid/modutils.py b/astroid/modutils.py index 4c0a9ecb00..03382d7dc7 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -75,6 +75,9 @@ STD_LIB_DIRS.add(os.path.join(sys.real_prefix, 'dlls')) except AttributeError: pass + if platform.python_implementation() == 'PyPy': + _root = os.path.dirname(sys.executable) + STD_LIB_DIRS.add(os.path.join(_root, 'lib_pypy')) # get_python_lib(standard_lib=1) is not available on pypy, set STD_LIB_DIR to # non-valid path, see https://bugs.pypy.org/issue1164 except DistutilsPlatformError: From 89d793c443ad6c821329fc5efc6c7ac7f923cefe Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 15 Dec 2015 19:16:57 +0200 Subject: [PATCH 138/312] Improve the detection of lib_pypy, which was faulty. --- astroid/modutils.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/astroid/modutils.py b/astroid/modutils.py index 03382d7dc7..95e01f151f 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -76,8 +76,9 @@ except AttributeError: pass if platform.python_implementation() == 'PyPy': - _root = os.path.dirname(sys.executable) - STD_LIB_DIRS.add(os.path.join(_root, 'lib_pypy')) + _root = os.path.join(sys.prefix, 'lib_pypy') + STD_LIB_DIRS.add(_root) + del _root # get_python_lib(standard_lib=1) is not available on pypy, set STD_LIB_DIR to # non-valid path, see https://bugs.pypy.org/issue1164 except DistutilsPlatformError: From 60f4813e1a56294793e14c9de6a375f6d0a7a22e Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 15 Dec 2015 21:12:38 +0200 Subject: [PATCH 139/312] Move code which doesn't trigger DistutilsPlatformError outside of the try block. --- astroid/modutils.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/astroid/modutils.py b/astroid/modutils.py index 95e01f151f..7f0ff43fbc 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -68,22 +68,23 @@ # Take care of installations where exec_prefix != prefix. get_python_lib(standard_lib=True, prefix=sys.exec_prefix), get_python_lib(standard_lib=True)]) - if os.name == 'nt': - STD_LIB_DIRS.add(os.path.join(sys.prefix, 'dlls')) - try: - # real_prefix is defined when running inside virtualenv. - STD_LIB_DIRS.add(os.path.join(sys.real_prefix, 'dlls')) - except AttributeError: - pass - if platform.python_implementation() == 'PyPy': - _root = os.path.join(sys.prefix, 'lib_pypy') - STD_LIB_DIRS.add(_root) - del _root # get_python_lib(standard_lib=1) is not available on pypy, set STD_LIB_DIR to # non-valid path, see https://bugs.pypy.org/issue1164 except DistutilsPlatformError: STD_LIB_DIRS = set() +if os.name == 'nt': + STD_LIB_DIRS.add(os.path.join(sys.prefix, 'dlls')) + try: + # real_prefix is defined when running inside virtualenv. + STD_LIB_DIRS.add(os.path.join(sys.real_prefix, 'dlls')) + except AttributeError: + pass +if platform.python_implementation() == 'PyPy': + _root = os.path.join(sys.prefix, 'lib_pypy') + STD_LIB_DIRS.add(_root) + del _root + EXT_LIB_DIR = get_python_lib() BUILTIN_MODULES = dict.fromkeys(sys.builtin_module_names, True) From eb05a45d7e4d778bdf4f3f6e45e6a3ec4411ef0b Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 15 Dec 2015 21:41:30 +0200 Subject: [PATCH 140/312] Add lib_pypy from the real installation of Python This solves a problem with pypy and virtualenv, the latter deciding not to copy some of the modules into the virtual environment, such as datetime, which means that STD_LIB_DIRS needs to be updated in order to look there as well. --- astroid/modutils.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/astroid/modutils.py b/astroid/modutils.py index 7f0ff43fbc..dd512fae58 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -83,6 +83,11 @@ if platform.python_implementation() == 'PyPy': _root = os.path.join(sys.prefix, 'lib_pypy') STD_LIB_DIRS.add(_root) + try: + # real_prefix is defined when running inside virtualenv. + STD_LIB_DIRS.add(os.path.join(sys.real_prefix, 'lib_pypy')) + except AttributeError: + pass del _root EXT_LIB_DIR = get_python_lib() From 879fa477f4afff6d0631313b5c1d07223fc871df Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 14 Dec 2015 13:02:05 +0200 Subject: [PATCH 141/312] Setup a basic AppVeyor deployment. --- appveyor.yml | 34 ++++++++++++++++++++++++++++++++++ appveyor/install.ps1 | 27 +++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 appveyor.yml create mode 100644 appveyor/install.ps1 diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 0000000000..81f11a80d7 --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,34 @@ +version: '{branch}-{build}' +build: off +cache: + - 'C:\\tmp' +environment: + matrix: + - PYTHON: "C:\\Python27" + TOXENV: "py27" + + - PYTHON: "C:\\Python33" + TOXENV: "py33" + + - PYTHON: "C:\\Python34" + TOXENV: "py34" + + - PYTHON: "C:\\Python35" + TOXENV: "py35" + +init: + - ps: echo $env:TOXENV + - ps: ls C:\Python* +install: + - 'powershell ./appveyor/install.ps1' + - "python -m pip install tox" + - 'python -m pip install wheel' + - 'python -m pip --version' + - 'python -m tox --version' + +test_script: + - 'python -m tox' + +on_failure: + - ps: dir "env:" + - ps: get-content .tox\*\log\* diff --git a/appveyor/install.ps1 b/appveyor/install.ps1 new file mode 100644 index 0000000000..0ab6f085cc --- /dev/null +++ b/appveyor/install.ps1 @@ -0,0 +1,27 @@ +# Sample script to install pip under Windows +# Authors: Olivier Grisel, Jonathan Helmus and Kyle Kastner +# License: CC0 1.0 Universal: http://creativecommons.org/publicdomain/zero/1.0/ + +$GET_PIP_URL = "https://bootstrap.pypa.io/get-pip.py" +$GET_PIP_PATH = "C:\get-pip.py" +$ErrorActionPreference = "Stop" + +function InstallPip ($python_home) { + $pip_path = $python_home + "\Scripts\pip.exe" + $python_path = $python_home + "\python.exe" + if (-not(Test-Path $pip_path)) { + Write-Host "Installing pip..." + $webclient = New-Object System.Net.WebClient + $webclient.DownloadFile($GET_PIP_URL, $GET_PIP_PATH) + Write-Host "Executing:" $python_path $GET_PIP_PATH + Start-Process -FilePath "$python_path" -ArgumentList "$GET_PIP_PATH" -Wait -Passthru + } else { + Write-Host "pip already installed." + } +} + +function main () { + InstallPip $env:PYTHON +} + +main From e89985876a4770d46a42d038eb5c59c13f64ab24 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Wed, 16 Dec 2015 11:48:33 +0200 Subject: [PATCH 142/312] Add AppVeyor badge. --- README.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.rst b/README.rst index f4dd58b83b..f358204ac9 100644 --- a/README.rst +++ b/README.rst @@ -4,6 +4,10 @@ Astroid .. image:: https://travis-ci.org/PyCQA/astroid.svg?branch=master :target: https://travis-ci.org/PyCQA/astroid +.. image:: https://ci.appveyor.com/api/projects/status/github/PCManticore/astroid?branch=master&svg=true + :alt: AppVeyor Build Status + :target: https://ci.appveyor.com/project/PCManticore/astroid + What's this? From c17be75b735831c0affc1bb4c072884470a4ad8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laura=20M=C3=A9dioni?= Date: Wed, 16 Dec 2015 11:18:22 +0100 Subject: [PATCH 143/312] Add test for issue #265. --- astroid/tests/unittest_brain.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/astroid/tests/unittest_brain.py b/astroid/tests/unittest_brain.py index a36f385971..92d63a7a86 100644 --- a/astroid/tests/unittest_brain.py +++ b/astroid/tests/unittest_brain.py @@ -305,6 +305,15 @@ def test_multiprocessing_module_attributes(self): else: self.assertIsInstance(cpu_count, astroid.BoundMethod) + def test_module_name(self): + module = test_utils.extract_node(""" + import multiprocessing + multiprocessing.SyncManager() + """) + inferred_sync_mgr = next(module.infer()) + module = inferred_sync_mgr.root() + self.assertEqual(module.name, 'multiprocessing.managers') + def test_multiprocessing_manager(self): # Test that we have the proper attributes # for a multiprocessing.managers.SyncManager From 13e69de65d34d65035e1e6af4c9d67337b60f0a2 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Wed, 16 Dec 2015 16:06:07 +0200 Subject: [PATCH 144/312] Modify the AppVeyor badge link. --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index f358204ac9..86bd9d18d3 100644 --- a/README.rst +++ b/README.rst @@ -4,7 +4,7 @@ Astroid .. image:: https://travis-ci.org/PyCQA/astroid.svg?branch=master :target: https://travis-ci.org/PyCQA/astroid -.. image:: https://ci.appveyor.com/api/projects/status/github/PCManticore/astroid?branch=master&svg=true +.. image:: https://ci.appveyor.com/api/projects/status/co3u42kunguhbh6l/branch/master?svg=true :alt: AppVeyor Build Status :target: https://ci.appveyor.com/project/PCManticore/astroid From df0e1c23518f3bbbac584de5856dc53ac48c906c Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Fri, 18 Dec 2015 21:08:50 +0200 Subject: [PATCH 145/312] The slots() method conflates all the slots from the ancestors into a list of current and parent slots. We're doing this because this is the right semantics of slots, they get inherited, as long as each parent defines a __slots__ entry. --- ChangeLog | 7 +++++ astroid/tests/unittest_scoped_nodes.py | 27 +++++++++++++++++ astroid/tree/scoped_nodes.py | 40 +++++++++++++++++++------- 3 files changed, 64 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index c91de5207a..7259b1203d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -15,6 +15,13 @@ Change log for the astroid package (used to be astng) method in AsString and ``accept`` on Yes / Uninferable nodes. Closes issue #270. + * The slots() method conflates all the slots from the ancestors + into a list of current and parent slots. + + We're doing this because this is the right semantics of slots, + they get inherited, as long as each parent defines a __slots__ + entry. + * Some nodes got a new attribute, 'ctx', which tells in which context the said node was used. diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index ef7f3cdd97..e0e742d215 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -1232,6 +1232,33 @@ class Klass(object): cls = module['Klass'] self.assertEqual(cls.slots(), []) + def test_slots_taken_from_parents(self): + module = builder.parse(''' + class FirstParent(object): + __slots__ = ('a', 'b', 'c') + class SecondParent(FirstParent): + __slots__ = ('d', 'e') + class Third(SecondParent): + __slots__ = ('d', ) + ''') + cls = module['Third'] + slots = cls.slots() + self.assertEqual(sorted(set(slot.value for slot in slots)), + ['a', 'b', 'c', 'd', 'e']) + + def test_all_ancestors_need_slots(self): + module = builder.parse(''' + class A(object): + __slots__ = ('a', ) + class B(A): pass + class C(B): + __slots__ = ('a', ) + ''') + cls = module['C'] + self.assertIsNone(cls.slots()) + cls = module['B'] + self.assertIsNone(cls.slots()) + def assertEqualMro(self, klass, expected_mro): self.assertEqual( [member.name for member in klass.mro()], diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index ae8da65f8e..c11eb4ce82 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -2030,16 +2030,7 @@ def _islots(self): except exceptions.InferenceError: continue - # Cached, because inferring them all the time is expensive - @decorators_mod.cached - def slots(self): - """Get all the slots for this node. - - If the class doesn't define any slot, through `__slots__` - variable, then this function will return a None. - Also, it will return None in the case the slots weren't inferred. - Otherwise, it will return a list of slot names. - """ + def _slots(self): if not self.newstyle: raise TypeError( "The concept of slots is undefined for old-style classes.") @@ -2055,6 +2046,35 @@ def slots(self): # pylint: disable=unsupported-binary-operation; false positive return [first] + list(slots) + # Cached, because inferring them all the time is expensive + @decorators_mod.cached + def slots(self): + """Get all the slots for this node. + + If the class doesn't define any slot, through `__slots__` + variable, then this function will return a None. + Also, it will return None in the case the slots weren't inferred. + Otherwise, it will return a list of slot names. + """ + def grouped_slots(): + # Not interested in object, since it can't have slots. + for cls in self.mro()[:-1]: + try: + cls_slots = cls._slots() + except NotImplementedError: + continue + if cls_slots is not None: + for slot in cls_slots: + yield slot + else: + yield None + + slots = list(grouped_slots()) + if not all(slot is not None for slot in slots): + return None + + return sorted(slots, key=lambda item: item.value) + def _inferred_bases(self, context=None): # TODO(cpopa): really similar with .ancestors, # but the difference is when one base is inferred, From 623a8b862918f630f081b6b15ae86b1ff5c93158 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Fri, 18 Dec 2015 21:16:52 +0200 Subject: [PATCH 146/312] Trigger NotImplementedError with slots message before mro gets executed (will fail for old style classes) --- astroid/tree/scoped_nodes.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index c11eb4ce82..8c832b56cb 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -2069,6 +2069,10 @@ def grouped_slots(): else: yield None + if not self.newstyle: + raise NotImplementedError( + "The concept of slots is undefined for old-style classes.") + slots = list(grouped_slots()) if not all(slot is not None for slot in slots): return None From 3251cf20c381ae9e1332baf9b5a787833d25cd5a Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 21 Dec 2015 12:33:56 +0200 Subject: [PATCH 147/312] Make the pkg_resources brain tip a bit more specific The brain tip for pkg_resources was lacking proper returns, which led to an inadvertent assignment-from-no-return false positive, which assumed that the functions are indeed not returning anything useful. The bug was uncovered by 058a28. --- astroid/brain/brain_stdlib.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/astroid/brain/brain_stdlib.py b/astroid/brain/brain_stdlib.py index a9caf24bdb..ced4c4a202 100644 --- a/astroid/brain/brain_stdlib.py +++ b/astroid/brain/brain_stdlib.py @@ -97,30 +97,36 @@ def __delitem__(self, index): pass def pkg_resources_transform(): return AstroidBuilder(MANAGER).string_build(''' - def resource_exists(package_or_requirement, resource_name): - pass + return get_provider(package_or_requirement).has_resource(resource_name) def resource_isdir(package_or_requirement, resource_name): - pass + return get_provider(package_or_requirement).resource_isdir( + resource_name) def resource_filename(package_or_requirement, resource_name): - pass + return get_provider(package_or_requirement).get_resource_filename( + self, resource_name) def resource_stream(package_or_requirement, resource_name): - pass + return get_provider(package_or_requirement).get_resource_stream( + self, resource_name) def resource_string(package_or_requirement, resource_name): - pass + return get_provider(package_or_requirement).get_resource_string( + self, resource_name) def resource_listdir(package_or_requirement, resource_name): - pass + return get_provider(package_or_requirement).resource_listdir( + resource_name) def extraction_error(): pass def get_cache_path(archive_name, names=()): - pass + extract_path = self.extraction_path or get_default_cache() + target_path = os.path.join(extract_path, archive_name+'-tmp', *names) + return target_path def postprocess(tempname, filename): pass From 554b4df20669e0eaaa919223f3cdfa76c3e7308e Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sat, 19 Dec 2015 23:42:09 +0200 Subject: [PATCH 148/312] Add /usr/lib and /usr/lib64 to the list of stdlib paths --- astroid/modutils.py | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/astroid/modutils.py b/astroid/modutils.py index dd512fae58..3e1d556479 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -51,12 +51,7 @@ PY_SOURCE_EXTS = ('py',) PY_COMPILED_EXTS = ('so',) -# Notes about STD_LIB_DIRS -# Consider arch-specific installation for STD_LIB_DIRS definition -# :mod:`distutils.sysconfig` contains to much hardcoded values to rely on -# -# :see: `Problems with /usr/lib64 builds `_ -# :see: `FHS `_ + try: # The explicit sys.prefix is to work around a patch in virtualenv that # replaces the 'real' sys.prefix (i.e. the location of the binary) @@ -89,6 +84,28 @@ except AttributeError: pass del _root +if os.name == 'posix': + # Need the real prefix is we're under a virtualenv, otherwise + # the usual one will do. + try: + prefix = sys.real_prefix + except AttributeError: + prefix = sys.prefix + + def _posix_path(path): + base_python = 'python%d.%d' % sys.version_info[:2] + return os.path.join(prefix, path, base_python) + + STD_LIB_DIRS.add(_posix_path('lib')) + if sys.maxsize > 2**32: + # This tries to fix a problem with /usr/lib64 builds, + # where systems are running both 32-bit and 64-bit code + # on the same machine, which reflects into the places where + # standard library could be found. More details can be found + # here http://bugs.python.org/issue1294959. + # An easy reproducing case would be + # https://github.com/PyCQA/pylint/issues/712#issuecomment-163178753 + STD_LIB_DIRS.add(_posix_path('lib64')) EXT_LIB_DIR = get_python_lib() BUILTIN_MODULES = dict.fromkeys(sys.builtin_module_names, True) From 3bafe56ac9f03a28f4c5cc1664991a7bc4c2c7c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laura=20M=C3=A9dioni?= Date: Wed, 16 Dec 2015 11:30:53 +0100 Subject: [PATCH 149/312] Add support for inference on threading.Lock --- ChangeLog | 8 ++++++++ astroid/brain/brain_stdlib.py | 12 ++++++++++++ astroid/tests/unittest_brain.py | 15 +++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/ChangeLog b/ChangeLog index 7259b1203d..d0c9ca6274 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,10 +1,18 @@ Change log for the astroid package (used to be astng) ===================================================== +-- + * Revert to using printf-style formatting in as_string, in order to avoid a potential problem with encodings when using .format. Closes issue #273. Patch by notsqrt. + * Add support for inference on threading.Lock + + As a matter of fact, astroid can infer on threading.RLock, + threading.Semaphore, but can't do it on threading.Lock (because it comes + from an extension module). + * Add support for handling Uninferable nodes when calling as_string Some object, for instance List or Tuple can have, after inference, diff --git a/astroid/brain/brain_stdlib.py b/astroid/brain/brain_stdlib.py index ced4c4a202..c0e6ba81f4 100644 --- a/astroid/brain/brain_stdlib.py +++ b/astroid/brain/brain_stdlib.py @@ -453,6 +453,17 @@ def shutdown(self): pass ''')) +def thread_transform(): + return AstroidBuilder(MANAGER).string_build(''' +class lock(object): + def acquire(self, blocking=True): + pass + def release(self): + pass + +def Lock(): + return lock() +''') MANAGER.register_transform(nodes.Call, inference_tip(infer_namedtuple), _looks_like_namedtuple) @@ -466,3 +477,4 @@ def shutdown(self): register_module_extender(MANAGER, 'multiprocessing.managers', multiprocessing_managers_transform) register_module_extender(MANAGER, 'multiprocessing', multiprocessing_transform) +register_module_extender(MANAGER, 'threading', thread_transform) diff --git a/astroid/tests/unittest_brain.py b/astroid/tests/unittest_brain.py index 92d63a7a86..0bf2edddda 100644 --- a/astroid/tests/unittest_brain.py +++ b/astroid/tests/unittest_brain.py @@ -372,6 +372,21 @@ def test_multiprocessing_manager(self): self.assertTrue(manager.getattr('start')) self.assertTrue(manager.getattr('shutdown')) +class ThreadingBrainTest(unittest.TestCase): + + def test_threading(self): + module = test_utils.extract_node(""" + import threading + threading.Lock() + """) + inferred = next(module.infer()) + self.assertIsInstance(inferred, astroid.Instance) + self.assertEqual(inferred.root().name, 'threading') + self.assertIsInstance(inferred.getattr('acquire')[0], + astroid.FunctionDef) + self.assertIsInstance(inferred.getattr('release')[0], + astroid.FunctionDef) + @unittest.skipUnless(HAS_ENUM, 'The enum module was only added in Python 3.4. Support for ' From d3577d48d9e8c408a231ad9fad37a6ebda4bd809 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 21 Dec 2015 18:10:18 +0200 Subject: [PATCH 150/312] Add a new possible property-descriptor, lazy. Since we don't understand descriptors at all, we're relying on a list of possible property-descriptors, that is, decorators which transforms a method into its value, the exact process that a property does. While this is a bad approach, it works, at least until we can provide support for this high level of understanding. Closes #279 --- astroid/interpreter/objects.py | 2 +- astroid/tests/unittest_nodes.py | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/astroid/interpreter/objects.py b/astroid/interpreter/objects.py index 9d5d003841..7af5b6873d 100644 --- a/astroid/interpreter/objects.py +++ b/astroid/interpreter/objects.py @@ -61,7 +61,7 @@ POSSIBLE_PROPERTIES = {"cached_property", "cachedproperty", "lazyproperty", "lazy_property", "reify", "lazyattribute", "lazy_attribute", - "LazyProperty"} + "LazyProperty", "lazy"} def is_property(meth): diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index ce8863b3fb..62ecf301e1 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -540,6 +540,7 @@ def lazy_property(): pass def lazyproperty(): pass + def lazy(): pass class A(object): @property def builtin_property(self): @@ -556,6 +557,8 @@ def lazy_prop(self): return 42 @lazyproperty def lazyprop(self): return 42 def not_prop(self): pass + @lazy + def decorated_with_lazy(self): return 42 cls = A() builtin_property = cls.builtin_property @@ -565,9 +568,10 @@ def not_prop(self): pass not_prop = cls.not_prop lazy_prop = cls.lazy_prop lazyprop = cls.lazyprop + decorated_with_lazy = cls.decorated_with_lazy ''') for prop in ('builtin_property', 'abc_property', 'cached_p', 'reified', - 'lazy_prop', 'lazyprop'): + 'lazy_prop', 'lazyprop', 'decorated_with_lazy'): inferred = next(ast[prop].infer()) self.assertIsInstance(inferred, nodes.Const, prop) self.assertEqual(inferred.value, 42, prop) From 736e08ced56ed3bdd8b84337e36fad192ccce0ca Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 29 Dec 2015 17:08:06 +0200 Subject: [PATCH 151/312] We're raising TypeError in 2.0 for old-style class operations, not NotImplementedError. --- astroid/tree/scoped_nodes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 8c832b56cb..43c5cc060b 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -2070,7 +2070,7 @@ def grouped_slots(): yield None if not self.newstyle: - raise NotImplementedError( + raise TypeError( "The concept of slots is undefined for old-style classes.") slots = list(grouped_slots()) From 512fc01cd75b64b7e801f7e821a866b06086a31f Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 29 Dec 2015 18:32:49 +0200 Subject: [PATCH 152/312] Move lookup specific bits into interpreter.lookup This patch moves code which was targeted for implementing lookup support from scoped_nodes into interpreter.lookup, resulting in a more cohesive scoped_nodes module. The lookup functions were capable of using virtual base classes instead on relying on concrete classes offered by node_classes and scoped_nodes. The only minor complaint is that builtin_lookup still needs to depend on the manager, which is lazily loaded inside the function itself, at least until we'll get rid of this circular dependency. --- astroid/__init__.py | 2 +- astroid/interpreter/lookup.py | 313 +++++++++++++++++++++++++ astroid/tests/unittest_lookup.py | 5 +- astroid/tests/unittest_scoped_nodes.py | 3 +- astroid/tree/scoped_nodes.py | 291 +---------------------- 5 files changed, 330 insertions(+), 284 deletions(-) create mode 100644 astroid/interpreter/lookup.py diff --git a/astroid/__init__.py b/astroid/__init__.py index b18c9d2803..efae8eca8b 100644 --- a/astroid/__init__.py +++ b/astroid/__init__.py @@ -78,7 +78,7 @@ # Cache the builtins AST raw_building.ast_from_builtins() from astroid.interpreter.util import are_exclusive, unpack_infer -from astroid.tree.scoped_nodes import builtin_lookup +from astroid.interpreter.lookup import builtin_lookup from astroid.builder import parse from astroid.util import Uninferable, YES diff --git a/astroid/interpreter/lookup.py b/astroid/interpreter/lookup.py new file mode 100644 index 0000000000..02c75ff9a3 --- /dev/null +++ b/astroid/interpreter/lookup.py @@ -0,0 +1,313 @@ +# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. +# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr +# +# This file is part of astroid. +# +# astroid is free software: you can redistribute it and/or modify it +# under the terms of the GNU Lesser General Public License as published by the +# Free Software Foundation, either version 2.1 of the License, or (at your +# option) any later version. +# +# astroid is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License +# for more details. +# +# You should have received a copy of the GNU Lesser General Public License along +# with astroid. If not, see . + +import collections + +import six + +from astroid import exceptions +from astroid import mixins +from astroid.util import _singledispatch +from astroid.tree import treeabc +from astroid.tree import base as treebase + +try: + from types import MappingProxyType +except ImportError: + from dictproxyhack import dictproxy as MappingProxyType + + +class LocalsDictNode(mixins.LookupMixIn, + treebase.NodeNG): + """Provides locals handling common to Module, FunctionDef + and ClassDef nodes, including a dict like interface for direct access + to locals information + """ + + # attributes below are set by the builder module or by raw factories + + # dictionary of locals with name as key and node defining the local as + # value + + @property + def locals(self): + return MappingProxyType(get_locals(self)) + + def qname(self): + """return the 'qualified' name of the node, eg module.name, + module.class.name ... + """ + if self.parent is None: + return self.name + return '%s.%s' % (self.parent.frame().qname(), self.name) + + def frame(self): + """return the first parent frame node (i.e. Module, FunctionDef or + ClassDef) + + """ + return self + + def _scope_lookup(self, node, name, offset=0): + """XXX method for interfacing the scope lookup""" + try: + stmts = node._filter_stmts(self.locals[name], self, offset) + except KeyError: + stmts = () + if stmts: + return self, stmts + if self.parent: # i.e. not Module + # nested scope: if parent scope is a function, that's fine + # else jump to the module + pscope = self.parent.scope() + if not pscope.is_function: + pscope = pscope.root() + return pscope.scope_lookup(node, name) + return builtin_lookup(name) # Module + + def set_local(self, name, stmt): + raise Exception('Attempted locals mutation.') + + # def set_local(self, name, stmt): + # """define in locals ( is the node defining the name) + # if the node is a Module node (i.e. has globals), add the name to + # globals + + # if the name is already defined, ignore it + # """ + # #assert not stmt in self.locals.get(name, ()), (self, stmt) + # self.locals.setdefault(name, []).append(stmt) + + __setitem__ = set_local + + # def _append_node(self, child): + # """append a child, linking it in the tree""" + # self.body.append(child) + # child.parent = self + + # def add_local_node(self, child_node, name=None): + # """append a child which should alter locals to the given node""" + # 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) + + def __getitem__(self, item): + """method from the `dict` interface returning the first node + associated with the given name in the locals dictionary + + :type item: str + :param item: the name of the locally defined object + :raises KeyError: if the name is not defined + """ + return self.locals[item][0] + + def __iter__(self): + """method from the `dict` interface returning an iterator on + `self.keys()` + """ + return iter(self.locals) + + def keys(self): + """method from the `dict` interface returning a tuple containing + locally defined names + """ + return self.locals.keys() + + def values(self): + """method from the `dict` interface returning a tuple containing + locally defined nodes which are instance of `FunctionDef` or `ClassDef` + """ + return tuple(v[0] for v in self.locals.values()) + + def items(self): + """method from the `dict` interface returning a list of tuple + containing each locally defined name with its associated node, + which is an instance of `FunctionDef` or `ClassDef` + """ + return tuple((k, v[0]) for k, v in self.locals.items()) + + def __contains__(self, name): + return name in self.locals + + +def builtin_lookup(name): + """lookup a name into the builtin module + return the list of matching statements and the astroid for the builtin + module + """ + from astroid import MANAGER # TODO(cpopa) needs to be removed. + + builtin_astroid = MANAGER.ast_from_module(six.moves.builtins) + if name == '__dict__': + return builtin_astroid, () + stmts = builtin_astroid.locals.get(name, ()) + # Use inference to find what AssignName nodes point to in builtins. + stmts = [next(s.infer()) if isinstance(s, treeabc.AssignName) else s + for s in stmts] + return builtin_astroid, stmts + + +@_singledispatch +def get_locals(node): + '''Return the local variables for an appropriate node. + + For function nodes, this will be the local variables defined in + their scope, what would be returned by a locals() call in the + function body. For Modules, this will be all the global names + defined in the module, what would be returned by a locals() or + globals() call at the module level. For classes, this will be + class attributes defined in the class body, also what a locals() + call in the body would return. + + This function starts by recursing over its argument's children to + avoid incorrectly adding a class's, function's, or module's name + to its own local variables. + + Args: + node (LocalsDictNode): A node defining a scope to return locals for. + + Returns: + A defaultdict(list) mapping names (strings) to lists of nodes. + + Raises: + TypeError: When called on a node that doesn't represent a scope or a + non-node object. + ''' + raise TypeError("This isn't an astroid node: %s" % type(node)) + + +# pylint: disable=unused-variable; doesn't understand singledispatch +@get_locals.register(treeabc.NodeNG) +def not_scoped_node(node): + raise TypeError("This node doesn't have local variables: %s" % type(node)) + + +# pylint: disable=unused-variable; doesn't understand singledispatch +@get_locals.register(LocalsDictNode) +def scoped_node(node): + locals_ = collections.defaultdict(list) + for n in node.get_children(): + _get_locals(n, locals_) + return locals_ + + +@_singledispatch +def _get_locals(node, locals_): + '''Return the local variables for a node. + + This is the internal recursive generic function for gathering + nodes into a local variables mapping. The locals mapping is + passed down and mutated by each function. + + Args: + node (NodeNG): The node to inspect for assignments to locals. + locals_ (defaultdict(list)): A mapping of (strings) to lists of nodes. + + Raises: + TypeError: When called on a non-node object. + + ''' + + raise TypeError('Non-astroid object in an astroid AST: %s' % type(node)) + + +# pylint: disable=unused-variable; doesn't understand singledispatch +@_get_locals.register(treeabc.NodeNG) +def locals_generic(node, locals_): + '''Generic nodes don't create name bindings or scopes.''' + for n in node.get_children(): + _get_locals(n, locals_) + + +# # pylint: disable=unused-variable; doesn't understand singledispatch +@_get_locals.register(LocalsDictNode) +def locals_new_scope(node, locals_): + '''These nodes start a new scope, so terminate recursion here.''' + + +# pylint: disable=unused-variable; doesn't understand singledispatch +@_get_locals.register(treeabc.AssignName) +@_get_locals.register(treeabc.DelName) +@_get_locals.register(treeabc.FunctionDef) +@_get_locals.register(treeabc.ClassDef) +def locals_name(node, locals_): + '''These nodes add a name to the local variables. AssignName and + DelName have no children while FunctionDef and ClassDef start a + new scope so shouldn't be recursed into.''' + locals_[node.name].append(node) + + +@_get_locals.register(treeabc.InterpreterObject) +def locals_interpreter_object(node, locals_): + '''InterpreterObjects add an object to the local variables under a specified + name.''' + if node.name: + locals_[node.name].append(node) + + +@_get_locals.register(treeabc.ReservedName) +def locals_reserved_name(node, locals_): + '''InterpreterObjects add an object to the local variables under a specified + name.''' + locals_[node.name].append(node.value) + + +# pylint: disable=unused-variable; doesn't understand singledispatch +@_get_locals.register(treeabc.Arguments) +def locals_arguments(node, locals_): + '''Other names assigned by functions have AssignName nodes that are + children of an Arguments node.''' + if node.vararg: + locals_[node.vararg].append(node) + if node.kwarg: + locals_[node.kwarg].append(node) + for n in node.get_children(): + _get_locals(n, locals_) + + +# pylint: disable=unused-variable; doesn't understand singledispatch +@_get_locals.register(treeabc.Import) +def locals_import(node, locals_): + for name, asname in node.names: + name = asname or name + locals_[name.split('.')[0]].append(node) + + +# pylint: disable=unused-variable; doesn't understand singledispatch +@_get_locals.register(treeabc.ImportFrom) +def locals_import_from(node, locals_): + # Don't add future imports to locals. + if node.modname == '__future__': + return + # Inherited code, I don't know why this function sorts this list. + def sort_locals(my_list): + my_list.sort(key=lambda node: node.fromlineno) + + for name, asname in node.names: + if name == '*': + try: + imported = node.do_import_module() + except exceptions.AstroidBuildingError: + continue + for name in imported.wildcard_import_names(): + locals_[name].append(node) + sort_locals(locals_[name]) + else: + locals_[asname or name].append(node) + sort_locals(locals_[asname or name]) diff --git a/astroid/tests/unittest_lookup.py b/astroid/tests/unittest_lookup.py index ebd0aa85c0..5ec13245f9 100644 --- a/astroid/tests/unittest_lookup.py +++ b/astroid/tests/unittest_lookup.py @@ -23,6 +23,7 @@ from astroid import builder from astroid import exceptions +from astroid.interpreter import lookup from astroid import nodes from astroid.tree import scoped_nodes from astroid import test_utils @@ -262,8 +263,8 @@ def initialize(linter): self.assertEqual(len(path.lookup('__path__')[1]), 1) def test_builtin_lookup(self): - self.assertEqual(scoped_nodes.builtin_lookup('__dict__')[1], ()) - intstmts = scoped_nodes.builtin_lookup('int')[1] + self.assertEqual(lookup.builtin_lookup('__dict__')[1], ()) + intstmts = lookup.builtin_lookup('int')[1] self.assertEqual(len(intstmts), 1) self.assertIsInstance(intstmts[0], nodes.ClassDef) self.assertEqual(intstmts[0].name, 'int') diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index e0e742d215..25562f8d10 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -28,6 +28,7 @@ from astroid import builder from astroid import context +from astroid.interpreter import lookup from astroid import nodes from astroid.tree import scoped_nodes from astroid import util @@ -1414,7 +1415,7 @@ def test_implicit_metaclass(self): class A(object): pass """) - type_cls = scoped_nodes.builtin_lookup("type")[1][0] + type_cls = lookup.builtin_lookup("type")[1][0] self.assertEqual(cls.implicit_metaclass(), type_cls) def test_implicit_metaclass_lookup(self): diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 43c5cc060b..8226c281ed 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -38,6 +38,7 @@ from astroid import context as contextmod from astroid import exceptions from astroid import decorators as decorators_mod +from astroid.interpreter import lookup from astroid.interpreter import objects from astroid.interpreter import runtimeabc from astroid.interpreter.util import infer_stmts @@ -129,138 +130,8 @@ def std_special_attributes(self, name, add_locals=True): raise exceptions.AttributeInferenceError(target=self, attribute=name) -def builtin_lookup(name): - """lookup a name into the builtin module - return the list of matching statements and the astroid for the builtin - module - """ - builtin_astroid = MANAGER.ast_from_module(six.moves.builtins) - if name == '__dict__': - return builtin_astroid, () - stmts = builtin_astroid.locals.get(name, ()) - # Use inference to find what AssignName nodes point to in builtins. - stmts = [next(s.infer()) if isinstance(s, node_classes.AssignName) else s - for s in stmts] - return builtin_astroid, stmts - - -# TODO move this Mixin to mixins.py; problem: 'FunctionDef' in _scope_lookup -class LocalsDictNodeNG(mixins.LookupMixIn, - treebase.NodeNG): - """ this class provides locals handling common to Module, FunctionDef - and ClassDef nodes, including a dict like interface for direct access - to locals information - """ - - # attributes below are set by the builder module or by raw factories - - # dictionary of locals with name as key and node defining the local as - # value - - @property - def locals(self): - return MappingProxyType(get_locals(self)) - - def qname(self): - """return the 'qualified' name of the node, eg module.name, - module.class.name ... - """ - if self.parent is None: - return self.name - return '%s.%s' % (self.parent.frame().qname(), self.name) - - def frame(self): - """return the first parent frame node (i.e. Module, FunctionDef or - ClassDef) - - """ - return self - - def _scope_lookup(self, node, name, offset=0): - """XXX method for interfacing the scope lookup""" - try: - stmts = node._filter_stmts(self.locals[name], self, offset) - except KeyError: - stmts = () - if stmts: - return self, stmts - if self.parent: # i.e. not Module - # nested scope: if parent scope is a function, that's fine - # else jump to the module - pscope = self.parent.scope() - if not pscope.is_function: - pscope = pscope.root() - return pscope.scope_lookup(node, name) - return builtin_lookup(name) # Module - - def set_local(self, name, stmt): - raise Exception('Attempted locals mutation.') - - # def set_local(self, name, stmt): - # """define in locals ( is the node defining the name) - # if the node is a Module node (i.e. has globals), add the name to - # globals - - # if the name is already defined, ignore it - # """ - # #assert not stmt in self.locals.get(name, ()), (self, stmt) - # self.locals.setdefault(name, []).append(stmt) - - __setitem__ = set_local - - # def _append_node(self, child): - # """append a child, linking it in the tree""" - # self.body.append(child) - # child.parent = self - - # def add_local_node(self, child_node, name=None): - # """append a child which should alter locals to the given node""" - # 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) - - def __getitem__(self, item): - """method from the `dict` interface returning the first node - associated with the given name in the locals dictionary - - :type item: str - :param item: the name of the locally defined object - :raises KeyError: if the name is not defined - """ - return self.locals[item][0] - - def __iter__(self): - """method from the `dict` interface returning an iterator on - `self.keys()` - """ - return iter(self.locals) - - def keys(self): - """method from the `dict` interface returning a tuple containing - locally defined names - """ - return self.locals.keys() - - def values(self): - """method from the `dict` interface returning a tuple containing - locally defined nodes which are instance of `FunctionDef` or `ClassDef` - """ - return tuple(v[0] for v in self.locals.values()) - - def items(self): - """method from the `dict` interface returning a list of tuple - containing each locally defined name with its associated node, - which is an instance of `FunctionDef` or `ClassDef` - """ - return tuple((k, v[0]) for k, v in self.locals.items()) - - def __contains__(self, name): - return name in self.locals - - @util.register_implementation(treeabc.Module) -class Module(LocalsDictNodeNG): +class Module(lookup.LocalsDictNode): _astroid_fields = ('body',) fromlineno = 0 @@ -368,7 +239,7 @@ def files_bytes(self): @property def globals(self): - return MappingProxyType(get_locals(self)) + return MappingProxyType(lookup.get_locals(self)) @property def future_imports(self): @@ -612,11 +483,11 @@ def bool_value(self): return True -class ComprehensionScope(LocalsDictNodeNG): +class ComprehensionScope(lookup.LocalsDictNode): def frame(self): return self.parent.frame() - scope_lookup = LocalsDictNodeNG._scope_lookup + scope_lookup = lookup.LocalsDictNode._scope_lookup @util.register_implementation(treeabc.GeneratorExp) @@ -971,7 +842,7 @@ def infer_argument(self, name, context): @util.register_implementation(treeabc.Lambda) -class Lambda(mixins.FilterStmtsMixin, LocalsDictNodeNG): +class Lambda(mixins.FilterStmtsMixin, lookup.LocalsDictNode): _astroid_fields = ('args', 'body',) _other_other_fields = ('locals',) name = '' @@ -1431,7 +1302,7 @@ def get_wrapping_class(node): @util.register_implementation(treeabc.ClassDef) -class ClassDef(mixins.FilterStmtsMixin, LocalsDictNodeNG, +class ClassDef(mixins.FilterStmtsMixin, lookup.LocalsDictNode, node_classes.Statement): # some of the attributes below are set by the builder module or @@ -1476,7 +1347,7 @@ def postinit(self, bases, body, decorators, newstyle=None, metaclass=None): @property def locals(self): # return get_locals(self) - return MappingProxyType(get_locals(self)) + return MappingProxyType(lookup.get_locals(self)) # @property # def instance_attrs(self): @@ -1630,7 +1501,7 @@ def ancestors(self, recurs=True, context=None): context = contextmod.InferenceContext() if six.PY3: if not self.bases and self.qname() != 'builtins.object': - yield builtin_lookup("object")[1][0] + yield lookup.builtin_lookup("object")[1][0] return for stmt in self.bases: @@ -1900,7 +1771,7 @@ def implicit_metaclass(self): """ if self.newstyle: - return builtin_lookup('type')[1][0] + return lookup.builtin_lookup('type')[1][0] _metaclass = None def declared_metaclass(self): @@ -2098,7 +1969,7 @@ def _inferred_bases(self, context=None): context = contextmod.InferenceContext() if six.PY3: if not self.bases and self.qname() != 'builtins.object': - yield builtin_lookup("object")[1][0] + yield lookup.builtin_lookup("object")[1][0] return for stmt in self.bases: @@ -2150,146 +2021,6 @@ def bool_value(self): return True -@_singledispatch -def get_locals(node): - '''Return the local variables for an appropriate node. - - For function nodes, this will be the local variables defined in - their scope, what would be returned by a locals() call in the - function body. For Modules, this will be all the global names - defined in the module, what would be returned by a locals() or - globals() call at the module level. For classes, this will be - class attributes defined in the class body, also what a locals() - call in the body would return. - - This function starts by recursing over its argument's children to - avoid incorrectly adding a class's, function's, or module's name - to its own local variables. - - Args: - node (LocalsDictNodeNG): A node defining a scope to return locals for. - - Returns: - A defaultdict(list) mapping names (strings) to lists of nodes. - - Raises: - TypeError: When called on a node that doesn't represent a scope or a - non-node object. - ''' - raise TypeError("This isn't an astroid node: %s" % type(node)) - -# pylint: disable=unused-variable; doesn't understand singledispatch -@get_locals.register(base.NodeNG) -def not_scoped_node(node): - raise TypeError("This node doesn't have local variables: %s" % type(node)) - -# pylint: disable=unused-variable; doesn't understand singledispatch -@get_locals.register(LocalsDictNodeNG) -def scoped_node(node): - locals_ = collections.defaultdict(list) - for n in node.get_children(): - _get_locals(n, locals_) - return locals_ - - -@_singledispatch -def _get_locals(node, locals_): - '''Return the local variables for a node. - - This is the internal recursive generic function for gathering - nodes into a local variables mapping. The locals mapping is - passed down and mutated by each function. - - Args: - node (NodeNG): The node to inspect for assignments to locals. - locals_ (defaultdict(list)): A mapping of (strings) to lists of nodes. - - Raises: - TypeError: When called on a non-node object. - - ''' - - raise TypeError('Non-astroid object in an astroid AST: %s' % type(node)) - -# pylint: disable=unused-variable; doesn't understand singledispatch -@_get_locals.register(base.NodeNG) -def locals_generic(node, locals_): - '''Generic nodes don't create name bindings or scopes.''' - for n in node.get_children(): - _get_locals(n, locals_) - -# # pylint: disable=unused-variable; doesn't understand singledispatch -@_get_locals.register(LocalsDictNodeNG) -def locals_new_scope(node, locals_): - '''These nodes start a new scope, so terminate recursion here.''' - -# pylint: disable=unused-variable; doesn't understand singledispatch -@_get_locals.register(node_classes.AssignName) -@_get_locals.register(node_classes.DelName) -@_get_locals.register(FunctionDef) -@_get_locals.register(ClassDef) -def locals_name(node, locals_): - '''These nodes add a name to the local variables. AssignName and - DelName have no children while FunctionDef and ClassDef start a - new scope so shouldn't be recursed into.''' - locals_[node.name].append(node) - -@_get_locals.register(node_classes.InterpreterObject) -def locals_interpreter_object(node, locals_): - '''InterpreterObjects add an object to the local variables under a specified - name.''' - if node.name: - locals_[node.name].append(node) - -@_get_locals.register(node_classes.ReservedName) -def locals_reserved_name(node, locals_): - '''InterpreterObjects add an object to the local variables under a specified - name.''' - locals_[node.name].append(node.value) - -# pylint: disable=unused-variable; doesn't understand singledispatch -@_get_locals.register(node_classes.Arguments) -def locals_arguments(node, locals_): - '''Other names assigned by functions have AssignName nodes that are - children of an Arguments node.''' - if node.vararg: - locals_[node.vararg].append(node) - if node.kwarg: - locals_[node.kwarg].append(node) - for n in node.get_children(): - _get_locals(n, locals_) - -# pylint: disable=unused-variable; doesn't understand singledispatch -@_get_locals.register(node_classes.Import) -def locals_import(node, locals_): - for name, asname in node.names: - name = asname or name - locals_[name.split('.')[0]].append(node) - -# pylint: disable=unused-variable; doesn't understand singledispatch -@_get_locals.register(node_classes.ImportFrom) -def locals_import_from(node, locals_): - # Don't add future imports to locals. - if node.modname == '__future__': - return - # Inherited code, I don't know why this function sorts this list. - def sort_locals(my_list): - my_list.sort(key=lambda node: node.fromlineno) - - for name, asname in node.names: - if name == '*': - try: - imported = node.do_import_module() - except exceptions.AstroidBuildingError: - continue - for name in imported.wildcard_import_names(): - locals_[name].append(node) - sort_locals(locals_[name]) - else: - locals_[asname or name].append(node) - sort_locals(locals_[asname or name]) - - # Backwards-compatibility aliases Class = util.proxy_alias('Class', ClassDef) Function = util.proxy_alias('Function', FunctionDef) From 189b98bcdf8d2e5ec46d0dd3455adbaa516127ff Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Wed, 30 Dec 2015 10:57:33 +0200 Subject: [PATCH 153/312] Move qname() definition into a separate mixin The new mixin is used by ClassDef, Lambda, FunctionDef and Module, for defining a qname() method on them. This should fix a bug where the qname was defined on any subclass of LocalsDictNode, even though it wasn't possible to get a qualified name for the node in question. Closes #278. --- astroid/interpreter/lookup.py | 8 -------- astroid/tests/unittest_regrtest.py | 5 +++++ astroid/tree/scoped_nodes.py | 16 +++++++++++++--- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/astroid/interpreter/lookup.py b/astroid/interpreter/lookup.py index 02c75ff9a3..6f80eef205 100644 --- a/astroid/interpreter/lookup.py +++ b/astroid/interpreter/lookup.py @@ -48,14 +48,6 @@ class LocalsDictNode(mixins.LookupMixIn, def locals(self): return MappingProxyType(get_locals(self)) - def qname(self): - """return the 'qualified' name of the node, eg module.name, - module.class.name ... - """ - if self.parent is None: - return self.name - return '%s.%s' % (self.parent.frame().qname(), self.name) - def frame(self): """return the first parent frame node (i.e. Module, FunctionDef or ClassDef) diff --git a/astroid/tests/unittest_regrtest.py b/astroid/tests/unittest_regrtest.py index 0206dd46d5..9d36befcdd 100644 --- a/astroid/tests/unittest_regrtest.py +++ b/astroid/tests/unittest_regrtest.py @@ -305,6 +305,11 @@ def method(self): next(node.value.infer()).as_string() + def test_qname_not_on_generatorexp(self): + node = extract_node('''(i for i in range(10))''') + with self.assertRaises(AttributeError): + node.qname + class Whatever(object): a = property(lambda x: x, lambda x: x) diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 8226c281ed..906b4d6648 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -130,8 +130,17 @@ def std_special_attributes(self, name, add_locals=True): raise exceptions.AttributeInferenceError(target=self, attribute=name) +class QualifiedNameMixin(object): + + def qname(node): + """Return the 'qualified' name of the node.""" + if node.parent is None: + return node.name + return '%s.%s' % (node.parent.frame().qname(), node.name) + + @util.register_implementation(treeabc.Module) -class Module(lookup.LocalsDictNode): +class Module(QualifiedNameMixin, lookup.LocalsDictNode): _astroid_fields = ('body',) fromlineno = 0 @@ -842,7 +851,7 @@ def infer_argument(self, name, context): @util.register_implementation(treeabc.Lambda) -class Lambda(mixins.FilterStmtsMixin, lookup.LocalsDictNode): +class Lambda(QualifiedNameMixin, mixins.FilterStmtsMixin, lookup.LocalsDictNode): _astroid_fields = ('args', 'body',) _other_other_fields = ('locals',) name = '' @@ -1302,7 +1311,8 @@ def get_wrapping_class(node): @util.register_implementation(treeabc.ClassDef) -class ClassDef(mixins.FilterStmtsMixin, lookup.LocalsDictNode, +class ClassDef(QualifiedNameMixin, mixins.FilterStmtsMixin, + lookup.LocalsDictNode, node_classes.Statement): # some of the attributes below are set by the builder module or From 359118209ee5a458e83d5b137c2066447acdfd0d Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Wed, 30 Dec 2015 11:18:59 +0200 Subject: [PATCH 154/312] Support Jython on Travis This is a bit complicated for a couple of reasons. Jython is not available out of the box on Travis and needs to be installed manually, but the released version (2.7.0) doesn't play nice with pip, resulting in being completely unusable. Thus we're building Jython ourselves from the repository, which works, as long as tox can find where the jython executable is. Closes #288 --- .travis.yml | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 09af14a718..9075168a8b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,18 +14,28 @@ matrix: env: TOXENV=pypy - python: 3.5 env: TOXENV=pylint + - python: 2.7 + env: JYTHON=true; TOXENV=jython + before_install: + - if [ "$JYTHON" == "true" ]; then hg clone http://hg.python.org/jython; cd jython; ant; cd ..; chmod +x "$(pwd)/jython/dist/bin/jython"; fi + - if [ "$JYTHON" == "true" ]; then export PYTHON_EXE="$(pwd)/jython/dist/bin/jython"; else export PYTHON_EXE=python; fi + - if [ "$JYTHON" == "true" ]; then alias jython="$(pwd)/jython/dist/bin/jython"; export PATH="$(pwd)/jython/dist/bin:$PATH"; fi + - if [ "$JYTHON" == "true" ]; then wget https://bootstrap.pypa.io/get-pip.py; $PYTHON_EXE get-pip.py; fi - python --version - uname -a - lsb_release -a install: - - pip install tox - - virtualenv --version - - easy_install --version - - pip --version - - tox --version + - $PYTHON_EXE -m pip install pip -U + - python -m pip install tox + - python -m pip install pip -U + - $PYTHON_EXE -m pip install tox + - $PYTHON_EXE -m virtualenv --version + - $PYTHON_EXE -m easy_install --version + - $PYTHON_EXE -m pip --version + - $PYTHON_EXE -m tox --version script: - - tox -e $TOXENV + - python -m tox -e $TOXENV after_failure: - more .tox/log/* | cat - more .tox/*/log/* | cat From 1d70e6c956519e30909118c0ebc06bb8cb2c17e7 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sat, 2 Jan 2016 17:45:22 +0200 Subject: [PATCH 155/312] Remove do_import_module and real_name from ImportFrom and Import nodes. do_import_module was broken for Import, since it was relying on a missing attribute `modname`. do_import_module in fact required all the time for a name parameter to be passed as an argument. The inherent problem is that Import doesn't have an underlying module name, but more of them, in the .names attribute. Thus requiring for do_import_module to pick one name from .names or to return multiple modules was deemed inappropiate and the method became a function which always requires the import name to be given. do_import_module and real_name are now functions in interpreter.util. Closes #293 --- ChangeLog | 11 +++++++++ astroid/inference.py | 10 ++++---- astroid/interpreter/lookup.py | 3 ++- astroid/interpreter/util.py | 42 +++++++++++++++++++++++++++++++++ astroid/mixins.py | 40 ------------------------------- astroid/tests/unittest_brain.py | 3 ++- astroid/tests/unittest_nodes.py | 29 +++++++++++++++++------ astroid/tree/node_classes.py | 10 ++++++-- 8 files changed, 92 insertions(+), 56 deletions(-) diff --git a/ChangeLog b/ChangeLog index d0c9ca6274..6e964c1080 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,17 @@ Change log for the astroid package (used to be astng) ===================================================== -- + * Remove do_import_module and real_name from ImportFrom and Import nodes. + + do_import_module was broken for Import, since it was relying on a + missing attribute `modname`. do_import_module + in fact required all the time for a name parameter to be passed + as an argument. The inherent problem is that Import doesn't have + an underlying module name, but more of them, in the .names attribute. + Thus requiring for do_import_module to pick one name from .names or + to return multiple modules was deemed inappropiate and the method + became a function which always requires the import name to be given. + Closes issue #293. * Revert to using printf-style formatting in as_string, in order to avoid a potential problem with encodings when using .format. diff --git a/astroid/inference.py b/astroid/inference.py index eb050b784c..a4ee72e1c4 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -133,9 +133,10 @@ def infer_import(self, context=None, asname=True): raise exceptions.InferenceError(node=self, context=context) try: if asname: - yield self.do_import_module(self.real_name(name)) + real_name = inferenceutil.real_name(self, name) + yield inferenceutil.do_import_module(self, real_name) else: - yield self.do_import_module(name) + yield inferenceutil.do_import_module(self, name) except exceptions.AstroidBuildingError as exc: util.reraise(exceptions.InferenceError(node=self, error=exc, context=context)) @@ -149,10 +150,9 @@ def infer_import_from(self, context=None, asname=True): if name is None: raise exceptions.InferenceError(node=self, context=context) if asname: - name = self.real_name(name) - + name = inferenceutil.real_name(self, name) try: - module = self.do_import_module() + module = inferenceutil.do_import_module(self, self.modname) except exceptions.AstroidBuildingError as exc: util.reraise(exceptions.InferenceError(node=self, error=exc, context=context)) diff --git a/astroid/interpreter/lookup.py b/astroid/interpreter/lookup.py index 6f80eef205..78e08f1edb 100644 --- a/astroid/interpreter/lookup.py +++ b/astroid/interpreter/lookup.py @@ -25,6 +25,7 @@ from astroid.util import _singledispatch from astroid.tree import treeabc from astroid.tree import base as treebase +from astroid.interpreter import util try: from types import MappingProxyType @@ -294,7 +295,7 @@ def sort_locals(my_list): for name, asname in node.names: if name == '*': try: - imported = node.do_import_module() + imported = util.do_import_module(node, node.modname) except exceptions.AstroidBuildingError: continue for name in imported.wildcard_import_names(): diff --git a/astroid/interpreter/util.py b/astroid/interpreter/util.py index 4a2d5b46b5..b6613484ba 100644 --- a/astroid/interpreter/util.py +++ b/astroid/interpreter/util.py @@ -278,3 +278,45 @@ def object_type(node, context=None): if len(types) > 1 or not types: return util.Uninferable return list(types)[0] + + +def do_import_module(node, modname): + """Return the ast for a module whose name is imported by the given node.""" + + # handle special case where we are on a package node importing a module + # using the same name as the package, which may end in an infinite loop + # on relative imports + # XXX: no more needed ? + if not isinstance(node, (treeabc.Import, treeabc.ImportFrom)): + raise TypeError('Operation is undefined for node of type %s' + % type(node)) + + mymodule = node.root() + level = getattr(node, 'level', None) # Import as no level + # XXX we should investigate deeper if we really want to check + # importing itself: modname and mymodule.name be relative or absolute + if mymodule.relative_to_absolute_name(modname, level) == mymodule.name: + # FIXME: we used to raise InferenceError here, but why ? + return mymodule + + return mymodule.import_module(modname, level=level, + relative_only=level and level >= 1) + + +def real_name(node, asname): + """get name from 'as' name""" + if not isinstance(node, (treeabc.Import, treeabc.ImportFrom)): + raise TypeError('Operation is undefined for node of type %s' + % type(node)) + + for name, _asname in node.names: + if name == '*': + return asname + if not _asname: + name = name.split('.', 1)[0] + _asname = name + if asname == _asname: + return name + raise exceptions.AttributeInferenceError( + 'Could not find original name for {attribute} in {target!r}', + target=node, attribute=asname) \ No newline at end of file diff --git a/astroid/mixins.py b/astroid/mixins.py index 6e4f29fc0b..ad268a5d92 100644 --- a/astroid/mixins.py +++ b/astroid/mixins.py @@ -97,46 +97,6 @@ def ass_type(self): return self.assign_type() -class ImportFromMixin(FilterStmtsMixin): - """MixIn for From and Import Nodes""" - - def _infer_name(self, frame, name): - return name - - def do_import_module(self, modname=None): - """return the ast for a module whose name is imported by - """ - # handle special case where we are on a package node importing a module - # using the same name as the package, which may end in an infinite loop - # on relative imports - # XXX: no more needed ? - mymodule = self.root() - level = getattr(self, 'level', None) # Import as no level - if modname is None: - modname = self.modname - # XXX we should investigate deeper if we really want to check - # importing itself: modname and mymodule.name be relative or absolute - if mymodule.relative_to_absolute_name(modname, level) == mymodule.name: - # FIXME: we used to raise InferenceError here, but why ? - return mymodule - - return mymodule.import_module(modname, level=level, - relative_only=level and level >= 1) - - def real_name(self, asname): - """get name from 'as' name""" - for name, _asname in self.names: - if name == '*': - return asname - if not _asname: - name = name.split('.', 1)[0] - _asname = name - if asname == _asname: - return name - raise exceptions.AttributeInferenceError( - 'Could not find original name for {attribute} in {target!r}', - target=self, attribute=asname) - class LookupMixIn(object): """Mixin looking up a name in the right scope """ diff --git a/astroid/tests/unittest_brain.py b/astroid/tests/unittest_brain.py index 0bf2edddda..85f6f04c72 100644 --- a/astroid/tests/unittest_brain.py +++ b/astroid/tests/unittest_brain.py @@ -24,6 +24,7 @@ from astroid import builder from astroid import nodes from astroid.interpreter import objects +from astroid.interpreter import util as interpreterutil from astroid import test_utils from astroid import util import astroid @@ -298,7 +299,7 @@ def test_multiprocessing_module_attributes(self): module = test_utils.extract_node(""" import multiprocessing """) - module = module.do_import_module('multiprocessing') + module = interpreterutil.do_import_module(module, 'multiprocessing') cpu_count = next(module.igetattr('cpu_count')) if sys.version_info < (3, 4): self.assertIsInstance(cpu_count, nodes.FunctionDef) diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index 62ecf301e1..4a3d4405ad 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -322,6 +322,18 @@ def setUp(self): self.module = resources.build_file('data/module.py', 'data.module') self.module2 = resources.build_file('data/module2.py', 'data.module2') + def test_do_import_module_works_for_all(self): + import_from, import_ = test_utils.extract_node(''' + from collections import deque #@ + import collections #@ + ''') + inferred = inferenceutil.do_import_module(import_from, 'collections') + self.assertIsInstance(inferred, nodes.Module) + self.assertEqual(inferred.name, 'collections') + inferred = inferenceutil.do_import_module(import_, 'collections') + self.assertIsInstance(inferred, nodes.Module) + self.assertEqual(inferred.name, 'collections') + def test_import_self_resolve(self): myos = next(self.module2.igetattr('myos')) self.assertTrue(isinstance(myos, nodes.Module), myos) @@ -343,16 +355,19 @@ def test_from_self_resolve(self): def test_real_name(self): from_ = self.module['NameNode'] - self.assertEqual(from_.real_name('NameNode'), 'Name') + self.assertEqual(inferenceutil.real_name(from_, 'NameNode'), 'Name') imp_ = self.module['os'] - self.assertEqual(imp_.real_name('os'), 'os') - self.assertRaises(exceptions.AttributeInferenceError, imp_.real_name, 'os.path') + self.assertEqual(inferenceutil.real_name(imp_, 'os'), 'os') + self.assertRaises(exceptions.AttributeInferenceError, + inferenceutil.real_name, imp_, 'os.path') imp_ = self.module['NameNode'] - self.assertEqual(imp_.real_name('NameNode'), 'Name') - self.assertRaises(exceptions.AttributeInferenceError, imp_.real_name, 'Name') + self.assertEqual(inferenceutil.real_name(imp_, 'NameNode'), 'Name') + self.assertRaises(exceptions.AttributeInferenceError, + inferenceutil.real_name, imp_, 'Name') imp_ = self.module2['YO'] - self.assertEqual(imp_.real_name('YO'), 'YO') - self.assertRaises(exceptions.AttributeInferenceError, imp_.real_name, 'data') + self.assertEqual(inferenceutil.real_name(imp_, 'YO'), 'YO') + self.assertRaises(exceptions.AttributeInferenceError, + inferenceutil.real_name, imp_, 'data') def test_as_string(self): ast = self.module['modutils'] diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index f29885956a..597316fa17 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -815,7 +815,7 @@ def postinit(self, value=None): @util.register_implementation(treeabc.ImportFrom) -class ImportFrom(mixins.ImportFromMixin, Statement): +class ImportFrom(mixins.FilterStmtsMixin, Statement): """class representing a ImportFrom node""" _other_fields = ('modname', 'names', 'level') @@ -826,6 +826,9 @@ def __init__(self, fromname, names, level=0, lineno=None, self.level = level super(ImportFrom, self).__init__(lineno, col_offset, parent) + def _infer_name(self, frame, name): + return name + @util.register_implementation(treeabc.Attribute) class Attribute(base.NodeNG): @@ -897,7 +900,7 @@ def postinit(self, test=None, body=None, orelse=None): @util.register_implementation(treeabc.Import) -class Import(mixins.ImportFromMixin, Statement): +class Import(mixins.FilterStmtsMixin, Statement): """class representing an Import node""" _other_fields = ('names',) @@ -910,6 +913,9 @@ def infer_name_module(self, name): context.lookupname = name return self.infer(context, asname=False) + def _infer_name(self, frame, name): + return name + @util.register_implementation(treeabc.Index) class Index(base.NodeNG): From 0d82bf7d7912e729f5368f01ac92687e9fda3b1e Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sat, 2 Jan 2016 18:07:27 +0200 Subject: [PATCH 156/312] Move mixins into tree.base. The mixins are better off in tree.base, rather than having their own module. They are also used only by the AST nodes. Close #292 --- ChangeLog | 2 + astroid/interpreter/lookup.py | 3 +- astroid/mixins.py | 243 ---------------------------------- astroid/tree/base.py | 220 +++++++++++++++++++++++++++++- astroid/tree/node_classes.py | 39 +++--- astroid/tree/scoped_nodes.py | 5 +- 6 files changed, 242 insertions(+), 270 deletions(-) delete mode 100644 astroid/mixins.py diff --git a/ChangeLog b/ChangeLog index 6e964c1080..9a9b264b9e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,8 @@ Change log for the astroid package (used to be astng) ===================================================== -- + * mixins are now found in tree.base. + * Remove do_import_module and real_name from ImportFrom and Import nodes. do_import_module was broken for Import, since it was relying on a diff --git a/astroid/interpreter/lookup.py b/astroid/interpreter/lookup.py index 78e08f1edb..11991eb527 100644 --- a/astroid/interpreter/lookup.py +++ b/astroid/interpreter/lookup.py @@ -21,7 +21,6 @@ import six from astroid import exceptions -from astroid import mixins from astroid.util import _singledispatch from astroid.tree import treeabc from astroid.tree import base as treebase @@ -33,7 +32,7 @@ from dictproxyhack import dictproxy as MappingProxyType -class LocalsDictNode(mixins.LookupMixIn, +class LocalsDictNode(treebase.LookupMixIn, treebase.NodeNG): """Provides locals handling common to Module, FunctionDef and ClassDef nodes, including a dict like interface for direct access diff --git a/astroid/mixins.py b/astroid/mixins.py deleted file mode 100644 index ad268a5d92..0000000000 --- a/astroid/mixins.py +++ /dev/null @@ -1,243 +0,0 @@ -# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . -"""This module contains some mixins for the different nodes. -""" - -import warnings - -from astroid import context -from astroid import decorators -from astroid import exceptions -from astroid.interpreter import util as interpreterutil -from astroid import util -from astroid.tree import treeabc - - -class BlockRangeMixIn(object): - """override block range """ - - @decorators.cachedproperty - def blockstart_tolineno(self): - return self.lineno - - def _elsed_block_range(self, lineno, orelse, last=None): - """handle block line numbers range for try/finally, for, if and while - statements - """ - if lineno == self.fromlineno: - return lineno, lineno - if orelse: - if lineno >= orelse[0].fromlineno: - return lineno, orelse[-1].tolineno - return lineno, orelse[0].fromlineno - 1 - return lineno, last or self.tolineno - - -class FilterStmtsMixin(object): - """Mixin for statement filtering and assignment type""" - - def _get_filtered_stmts(self, _, node, _stmts, mystmt): - """method used in _filter_stmts to get statemtents and trigger break""" - if self.statement() is mystmt: - # original node's statement is the assignment, only keep - # current node (gen exp, list comp) - return [node], True - return _stmts, False - - def assign_type(self): - return self - - def ass_type(self): - util.rename_warning((type(self).__name__, type(self).__name__)) - return self.assign_type() - - -class AssignTypeMixin(object): - - def assign_type(self): - return self - - def ass_type(self): - util.rename_warning((type(self).__name__, type(self).__name__)) - return self.assign_type() - - def _get_filtered_stmts(self, lookup_node, node, _stmts, mystmt): - """method used in filter_stmts""" - if self is mystmt: - return _stmts, True - if self.statement() is mystmt: - # original node's statement is the assignment, only keep - # current node (gen exp, list comp) - return [node], True - return _stmts, False - - -class ParentAssignTypeMixin(AssignTypeMixin): - - def assign_type(self): - return self.parent.assign_type() - - def ass_type(self): - util.rename_warning((type(self).__name__, type(self).__name__)) - return self.assign_type() - - -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) - return interpreterutil.infer_stmts(stmts, context.InferenceContext(), 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() - if not myframe is frame or self is frame: - return stmts - 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 assigment is hidding previous - # assigment - _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 - # assignement 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 assigments if any (hence the test on - # optional_assign) - if not (optional_assign or interpreterutil.are_exclusive(_stmts[pindex], node)): - del _stmt_parents[pindex] - del _stmts[pindex] - if isinstance(node, treeabc.AssignName): - if not optional_assign and stmt.parent is mystmt.parent: - _stmts = [] - _stmt_parents = [] - elif isinstance(node, treeabc.DelName): - _stmts = [] - _stmt_parents = [] - continue - if not interpreterutil.are_exclusive(self, node): - _stmts.append(node) - _stmt_parents.append(stmt.parent) - return _stmts diff --git a/astroid/tree/base.py b/astroid/tree/base.py index 8a3b73805b..061f9a11fb 100644 --- a/astroid/tree/base.py +++ b/astroid/tree/base.py @@ -23,10 +23,11 @@ import six from astroid import as_string +from astroid import context from astroid import decorators from astroid import exceptions from astroid.interpreter import scope -from astroid import mixins +from astroid.interpreter import util as interpreterutil from astroid.tree import treeabc from astroid import util @@ -489,8 +490,223 @@ def bool_value(self): return util.Uninferable +class BlockRangeMixIn(object): + """override block range """ + + @decorators.cachedproperty + def blockstart_tolineno(self): + return self.lineno + + def _elsed_block_range(self, lineno, orelse, last=None): + """handle block line numbers range for try/finally, for, if and while + statements + """ + if lineno == self.fromlineno: + return lineno, lineno + if orelse: + if lineno >= orelse[0].fromlineno: + return lineno, orelse[-1].tolineno + return lineno, orelse[0].fromlineno - 1 + return lineno, last or self.tolineno + + +class FilterStmtsMixin(object): + """Mixin for statement filtering and assignment type""" + + def _get_filtered_stmts(self, _, node, _stmts, mystmt): + """method used in _filter_stmts to get statemtents and trigger break""" + if self.statement() is mystmt: + # original node's statement is the assignment, only keep + # current node (gen exp, list comp) + return [node], True + return _stmts, False + + def assign_type(self): + return self + + def ass_type(self): + util.rename_warning((type(self).__name__, type(self).__name__)) + return self.assign_type() + + +class AssignTypeMixin(object): + + def assign_type(self): + return self + + def ass_type(self): + util.rename_warning((type(self).__name__, type(self).__name__)) + return self.assign_type() + + def _get_filtered_stmts(self, lookup_node, node, _stmts, mystmt): + """method used in filter_stmts""" + if self is mystmt: + return _stmts, True + if self.statement() is mystmt: + # original node's statement is the assignment, only keep + # current node (gen exp, list comp) + return [node], True + return _stmts, False + + +class ParentAssignTypeMixin(AssignTypeMixin): + + def assign_type(self): + return self.parent.assign_type() + + def ass_type(self): + util.rename_warning((type(self).__name__, type(self).__name__)) + return self.assign_type() + + +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) + return interpreterutil.infer_stmts(stmts, context.InferenceContext(), 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() + if not myframe is frame or self is frame: + return stmts + 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 assigment is hidding previous + # assigment + _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 + # assignement 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 assigments if any (hence the test on + # optional_assign) + if not (optional_assign or interpreterutil.are_exclusive(_stmts[pindex], node)): + del _stmt_parents[pindex] + del _stmts[pindex] + if isinstance(node, treeabc.AssignName): + if not optional_assign and stmt.parent is mystmt.parent: + _stmts = [] + _stmt_parents = [] + elif isinstance(node, treeabc.DelName): + _stmts = [] + _stmt_parents = [] + continue + if not interpreterutil.are_exclusive(self, node): + _stmts.append(node) + _stmt_parents.append(stmt.parent) + return _stmts + + @six.add_metaclass(abc.ABCMeta) -class BaseContainer(mixins.ParentAssignTypeMixin, NodeNG): +class BaseContainer(ParentAssignTypeMixin, NodeNG): """Base class for Set, FrozenSet, Tuple and List.""" _astroid_fields = ('elts',) diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index 597316fa17..4d87c17471 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -31,7 +31,6 @@ from astroid.interpreter import runtimeabc from astroid.interpreter import objects from astroid import manager -from astroid import mixins from astroid import protocols from astroid.tree import base from astroid.tree import treeabc @@ -100,7 +99,7 @@ def assigned_stmts(self, node=None, context=None, asspath=None): # Name classes @util.register_implementation(treeabc.AssignName) -class AssignName(mixins.LookupMixIn, mixins.ParentAssignTypeMixin, +class AssignName(base.LookupMixIn, base.ParentAssignTypeMixin, AssignedStmtsMixin, base.NodeNG): """class representing an AssignName node""" _other_fields = ('name',) @@ -113,7 +112,7 @@ def __init__(self, name=None, lineno=None, col_offset=None, parent=None): @util.register_implementation(treeabc.DelName) -class DelName(mixins.LookupMixIn, mixins.ParentAssignTypeMixin, base.NodeNG): +class DelName(base.LookupMixIn, base.ParentAssignTypeMixin, base.NodeNG): """class representing a DelName node""" _other_fields = ('name',) @@ -123,7 +122,7 @@ def __init__(self, name=None, lineno=None, col_offset=None, parent=None): @util.register_implementation(treeabc.Name) -class Name(mixins.LookupMixIn, base.NodeNG): +class Name(base.LookupMixIn, base.NodeNG): """class representing a Name node""" _other_fields = ('name',) @@ -133,7 +132,7 @@ def __init__(self, name=None, lineno=None, col_offset=None, parent=None): @util.register_implementation(treeabc.Arguments) -class Arguments(mixins.AssignTypeMixin, AssignedStmtsMixin, base.NodeNG): +class Arguments(base.AssignTypeMixin, AssignedStmtsMixin, base.NodeNG): """class representing an Arguments node""" if six.PY3: # Python 3.4+ uses a different approach regarding annotations, @@ -293,7 +292,7 @@ def infer(self, context=None, **kwargs): @util.register_implementation(treeabc.AssignAttr) -class AssignAttr(mixins.ParentAssignTypeMixin, +class AssignAttr(base.ParentAssignTypeMixin, AssignedStmtsMixin, base.NodeNG): """class representing an AssignAttr node""" _astroid_fields = ('expr',) @@ -323,7 +322,7 @@ def postinit(self, test=None, fail=None): @util.register_implementation(treeabc.Assign) -class Assign(mixins.AssignTypeMixin, AssignedStmtsMixin, Statement): +class Assign(base.AssignTypeMixin, AssignedStmtsMixin, Statement): """class representing an Assign node""" _astroid_fields = ('targets', 'value',) targets = None @@ -335,7 +334,7 @@ def postinit(self, targets=None, value=None): @util.register_implementation(treeabc.AugAssign) -class AugAssign(mixins.AssignTypeMixin, AssignedStmtsMixin, Statement): +class AugAssign(base.AssignTypeMixin, AssignedStmtsMixin, Statement): """class representing an AugAssign node""" _astroid_fields = ('target', 'value') _other_fields = ('op',) @@ -603,7 +602,7 @@ def postinit(self, nodes): @util.register_implementation(treeabc.DelAttr) -class DelAttr(mixins.ParentAssignTypeMixin, base.NodeNG): +class DelAttr(base.ParentAssignTypeMixin, base.NodeNG): """class representing a DelAttr node""" _astroid_fields = ('expr',) _other_fields = ('attrname',) @@ -618,7 +617,7 @@ def postinit(self, expr=None): @util.register_implementation(treeabc.Delete) -class Delete(mixins.AssignTypeMixin, Statement): +class Delete(base.AssignTypeMixin, Statement): """class representing a Delete node""" _astroid_fields = ('targets',) targets = None @@ -723,7 +722,7 @@ def has_underlying_object(self): @util.register_implementation(treeabc.ExceptHandler) -class ExceptHandler(mixins.AssignTypeMixin, AssignedStmtsMixin, Statement): +class ExceptHandler(base.AssignTypeMixin, AssignedStmtsMixin, Statement): """class representing an ExceptHandler node""" _astroid_fields = ('type', 'name', 'body',) type = None @@ -777,7 +776,7 @@ def postinit(self, dims=None): @util.register_implementation(treeabc.For) -class For(mixins.BlockRangeMixIn, mixins.AssignTypeMixin, +class For(base.BlockRangeMixIn, base.AssignTypeMixin, AssignedStmtsMixin, Statement): """class representing a For node""" _astroid_fields = ('target', 'iter', 'body', 'orelse',) @@ -815,7 +814,7 @@ def postinit(self, value=None): @util.register_implementation(treeabc.ImportFrom) -class ImportFrom(mixins.FilterStmtsMixin, Statement): +class ImportFrom(base.FilterStmtsMixin, Statement): """class representing a ImportFrom node""" _other_fields = ('modname', 'names', 'level') @@ -859,7 +858,7 @@ def _infer_name(self, frame, name): @util.register_implementation(treeabc.If) -class If(mixins.BlockRangeMixIn, Statement): +class If(base.BlockRangeMixIn, Statement): """class representing an If node""" _astroid_fields = ('test', 'body', 'orelse') test = None @@ -900,7 +899,7 @@ def postinit(self, test=None, body=None, orelse=None): @util.register_implementation(treeabc.Import) -class Import(mixins.FilterStmtsMixin, Statement): +class Import(base.FilterStmtsMixin, Statement): """class representing an Import node""" _other_fields = ('names',) @@ -1096,7 +1095,7 @@ def getattr(self, attrname, context=None): @util.register_implementation(treeabc.Starred) -class Starred(mixins.ParentAssignTypeMixin, AssignedStmtsMixin, base.NodeNG): +class Starred(base.ParentAssignTypeMixin, AssignedStmtsMixin, base.NodeNG): """class representing a Starred node""" _astroid_fields = ('value',) _other_fields = ('ctx', ) @@ -1132,7 +1131,7 @@ def postinit(self, value=None, slice=None): @util.register_implementation(treeabc.TryExcept) -class TryExcept(mixins.BlockRangeMixIn, Statement): +class TryExcept(base.BlockRangeMixIn, Statement): """class representing a TryExcept node""" _astroid_fields = ('body', 'handlers', 'orelse',) body = None @@ -1161,7 +1160,7 @@ def block_range(self, lineno): @util.register_implementation(treeabc.TryFinally) -class TryFinally(mixins.BlockRangeMixIn, Statement): +class TryFinally(base.BlockRangeMixIn, Statement): """class representing a TryFinally node""" _astroid_fields = ('body', 'finalbody',) body = None @@ -1238,7 +1237,7 @@ def type_errors(self, context=None): @util.register_implementation(treeabc.While) -class While(mixins.BlockRangeMixIn, Statement): +class While(base.BlockRangeMixIn, Statement): """class representing a While node""" _astroid_fields = ('test', 'body', 'orelse',) test = None @@ -1260,7 +1259,7 @@ def block_range(self, lineno): @util.register_implementation(treeabc.With) -class With(mixins.BlockRangeMixIn, mixins.AssignTypeMixin, +class With(base.BlockRangeMixIn, base.AssignTypeMixin, AssignedStmtsMixin, Statement): """class representing a With node""" _astroid_fields = ('items', 'body') diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 906b4d6648..0509eb95ee 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -43,7 +43,6 @@ from astroid.interpreter import runtimeabc from astroid.interpreter.util import infer_stmts from astroid import manager -from astroid import mixins from astroid.tree import base as treebase from astroid.tree import node_classes from astroid.tree import treeabc @@ -851,7 +850,7 @@ def infer_argument(self, name, context): @util.register_implementation(treeabc.Lambda) -class Lambda(QualifiedNameMixin, mixins.FilterStmtsMixin, lookup.LocalsDictNode): +class Lambda(QualifiedNameMixin, base.FilterStmtsMixin, lookup.LocalsDictNode): _astroid_fields = ('args', 'body',) _other_other_fields = ('locals',) name = '' @@ -1311,7 +1310,7 @@ def get_wrapping_class(node): @util.register_implementation(treeabc.ClassDef) -class ClassDef(QualifiedNameMixin, mixins.FilterStmtsMixin, +class ClassDef(QualifiedNameMixin, base.FilterStmtsMixin, lookup.LocalsDictNode, node_classes.Statement): From c92e421ef92854cbb8bfa89a7018a74fa07ad0d5 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sat, 2 Jan 2016 18:20:24 +0200 Subject: [PATCH 157/312] file_stream is no more, using .stream() is the recommended way to obtain a module stream. --- astroid/tests/unittest_scoped_nodes.py | 28 ++++---------------------- astroid/tree/scoped_nodes.py | 15 -------------- 2 files changed, 4 insertions(+), 39 deletions(-) diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index 25562f8d10..d96003651a 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -41,7 +41,6 @@ from astroid.interpreter.objects import ( Instance, BoundMethod, UnboundMethod, Generator ) -from astroid import __pkginfo__ from astroid import test_utils from astroid.tests import resources @@ -211,34 +210,15 @@ def test_file_stream_in_memory(self): data = '''irrelevant_variable is irrelevant''' astroid = builder.parse(data, 'in_memory') with warnings.catch_warnings(record=True): - self.assertEqual(astroid.file_stream.read().decode(), data) + with astroid.stream() as stream: + self.assertEqual(stream.read().decode(), data) def test_file_stream_physical(self): path = resources.find('data/all.py') astroid = builder.AstroidBuilder().file_build(path, 'all') with open(path, 'rb') as file_io: - with warnings.catch_warnings(record=True): - self.assertEqual(astroid.file_stream.read(), file_io.read()) - - def test_file_stream_api(self): - path = resources.find('data/all.py') - astroid = builder.AstroidBuilder().file_build(path, 'all') - if __pkginfo__.numversion >= (1, 6): - # file_stream is slated for removal in astroid 1.6. - with self.assertRaises(AttributeError): - # pylint: disable=pointless-statement - astroid.file_stream - else: - # Until astroid 1.6, Module.file_stream will emit - # PendingDeprecationWarning in 1.4, DeprecationWarning - # in 1.5 and finally it will be removed in 1.6, leaving - # only Module.stream as the recommended way to retrieve - # its file stream. - with warnings.catch_warnings(record=True) as cm: - with test_utils.enable_warning(PendingDeprecationWarning): - self.assertIsNot(astroid.file_stream, astroid.file_stream) - self.assertGreater(len(cm), 1) - self.assertEqual(cm[0].category, PendingDeprecationWarning) + with astroid.stream() as stream: + self.assertEqual(stream.read(), file_io.read()) def test_stream_api(self): path = resources.find('data/all.py') diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 0509eb95ee..92be900a14 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -282,25 +282,10 @@ def _get_stream(self): return stream return None - @property - def file_stream(self): - util.attr_to_method_warning(('file_stream', type(self).__name__)) - return self._get_stream() - def stream(self): """Get a stream to the underlying file or bytes.""" return self._get_stream() - def close(self): - """Close the underlying file streams.""" - warnings.warn("The close method is deprecated and is " - "slated for removal in astroid 1.6, along " - "with 'file_stream'. " - "Its behavior is replaced by managing each " - "file stream returned by the 'stream' method.", - PendingDeprecationWarning, - stacklevel=2) - def block_range(self, lineno): """return block line numbers. From 478e05b9b20c726d3ed7c46dc25155aef45aacd2 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 3 Jan 2016 09:43:04 +0200 Subject: [PATCH 158/312] Remove the old aliases for ass_type / file_bytes / file and nodes. --- astroid/brain/brain_builtin_inference.py | 4 +- astroid/nodes.py | 4 - astroid/tests/unittest_nodes.py | 142 ----------------------- astroid/tree/base.py | 12 -- astroid/tree/node_classes.py | 15 --- astroid/tree/scoped_nodes.py | 46 -------- 6 files changed, 2 insertions(+), 221 deletions(-) diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index 4b5236cc3b..0d1e890d22 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -554,8 +554,8 @@ def infer_type_dunder_new(caller, context=None): # All the bases needs to be Classes raise UseInferenceDefault - cls = nodes.Class(name=name.value, lineno=caller.lineno, - col_offset=caller.col_offset, parent=caller) + cls = nodes.ClassDef(name=name.value, lineno=caller.lineno, + col_offset=caller.col_offset, parent=caller) # Verify the attributes. attrs = next(caller.args[3].infer(context=context)) diff --git a/astroid/nodes.py b/astroid/nodes.py index 4317352ea4..e2fb1e0c5e 100644 --- a/astroid/nodes.py +++ b/astroid/nodes.py @@ -45,8 +45,6 @@ List, Name, NameConstant, Nonlocal, Pass, Print, Raise, Return, Set, Slice, Starred, Subscript, TryExcept, TryFinally, Tuple, UnaryOp, While, With, Yield, YieldFrom, AsyncFor, Await, AsyncWith, - # Backwards-compatibility aliases - Backquote, Discard, AssName, AssAttr, Getattr, CallFunc, From, # Node not present in the builtin ast module. DictUnpack, # Special nodes for building from live objects. @@ -56,8 +54,6 @@ Module, GeneratorExp, Lambda, DictComp, ListComp, SetComp, FunctionDef, ClassDef, AsyncFunctionDef, - # Backwards-compatibility aliases - Class, Function, GenExpr, ) diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index 4a3d4405ad..a8a86682e5 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -595,148 +595,6 @@ def decorated_with_lazy(self): return 42 self.assertIsInstance(inferred, objects.BoundMethod) -class AliasesTest(unittest.TestCase): - - def setUp(self): - self.transformer = transforms.TransformVisitor() - - def parse_transform(self, code): - module = parse(code, apply_transforms=False) - return self.transformer.visit(module) - - def test_aliases(self): - def test_from(node): - node.names = node.names + [('absolute_import', None)] - return node - - def test_class(node): - node.name = 'Bar' - return node - - def test_function(node): - node.name = 'another_test' - return node - - def test_callfunc(node): - if node.func.name == 'Foo': - node.func.name = 'Bar' - return node - - def test_assname(node): - if node.name == 'foo': - return nodes.AssignName('bar', node.lineno, node.col_offset, - node.parent) - def test_assattr(node): - if node.attrname == 'a': - node.attrname = 'b' - return node - - def test_getattr(node): - if node.attrname == 'a': - node.attrname = 'b' - return node - - def test_genexpr(node): - if node.elt.value == 1: - node.elt = nodes.Const(2, node.lineno, node.col_offset, - node.parent) - return node - - self.transformer.register_transform(nodes.From, test_from) - self.transformer.register_transform(nodes.Class, test_class) - self.transformer.register_transform(nodes.Function, test_function) - self.transformer.register_transform(nodes.CallFunc, test_callfunc) - self.transformer.register_transform(nodes.AssName, test_assname) - self.transformer.register_transform(nodes.AssAttr, test_assattr) - self.transformer.register_transform(nodes.Getattr, test_getattr) - self.transformer.register_transform(nodes.GenExpr, test_genexpr) - - string = ''' - from __future__ import print_function - - class Foo: pass - - def test(a): return a - - foo = Foo() - foo.a = test(42) - foo.a - (1 for _ in range(0, 42)) - ''' - - module = self.parse_transform(string) - - self.assertEqual(len(module.body[0].names), 2) - self.assertIsInstance(module.body[0], nodes.ImportFrom) - self.assertEqual(module.body[1].name, 'Bar') - self.assertIsInstance(module.body[1], nodes.ClassDef) - self.assertEqual(module.body[2].name, 'another_test') - self.assertIsInstance(module.body[2], nodes.FunctionDef) - self.assertEqual(module.body[3].targets[0].name, 'bar') - self.assertIsInstance(module.body[3].targets[0], nodes.AssignName) - self.assertEqual(module.body[3].value.func.name, 'Bar') - self.assertIsInstance(module.body[3].value, nodes.Call) - self.assertEqual(module.body[4].targets[0].attrname, 'b') - self.assertIsInstance(module.body[4].targets[0], nodes.AssignAttr) - self.assertIsInstance(module.body[5], nodes.Expr) - self.assertEqual(module.body[5].value.attrname, 'b') - self.assertIsInstance(module.body[5].value, nodes.Attribute) - self.assertEqual(module.body[6].value.elt.value, 2) - self.assertIsInstance(module.body[6].value, nodes.GeneratorExp) - - @unittest.skipIf(six.PY3, "Python 3 doesn't have Repr nodes.") - def test_repr(self): - def test_backquote(node): - node.value.name = 'bar' - return node - - self.transformer.register_transform(nodes.Backquote, test_backquote) - - module = self.parse_transform('`foo`') - - self.assertEqual(module.body[0].value.value.name, 'bar') - self.assertIsInstance(module.body[0].value, nodes.Repr) - - -class DeprecationWarningsTest(unittest.TestCase): - def test_asstype_warnings(self): - string = ''' - class C: pass - c = C() - with warnings.catch_warnings(record=True) as w: - pass - ''' - module = parse(string) - filter_stmts_mixin = module.body[0] - assign_type_mixin = module.body[1].targets[0] - parent_assign_type_mixin = module.body[2] - - with warnings.catch_warnings(record=True) as w: - with test_utils.enable_warning(PendingDeprecationWarning): - filter_stmts_mixin.ass_type() - self.assertIsInstance(w[0].message, PendingDeprecationWarning) - with warnings.catch_warnings(record=True) as w: - with test_utils.enable_warning(PendingDeprecationWarning): - assign_type_mixin.ass_type() - self.assertIsInstance(w[0].message, PendingDeprecationWarning) - with warnings.catch_warnings(record=True) as w: - with test_utils.enable_warning(PendingDeprecationWarning): - parent_assign_type_mixin.ass_type() - self.assertIsInstance(w[0].message, PendingDeprecationWarning) - - def test_isinstance_warnings(self): - msg_format = ("%r is deprecated and slated for removal in astroid " - "2.0, use %r instead") - for cls in (nodes.Discard, nodes.Backquote, nodes.AssName, - nodes.AssAttr, nodes.Getattr, nodes.CallFunc, nodes.From): - with warnings.catch_warnings(record=True) as w: - with test_utils.enable_warning(PendingDeprecationWarning): - isinstance(42, cls) - self.assertIsInstance(w[0].message, PendingDeprecationWarning) - actual_msg = msg_format % (cls.__class__.__name__, cls.__wrapped__.__name__) - self.assertEqual(str(w[0].message), actual_msg) - - @test_utils.require_version('3.5') class Python35AsyncTest(unittest.TestCase): diff --git a/astroid/tree/base.py b/astroid/tree/base.py index 061f9a11fb..8b2a95e8a1 100644 --- a/astroid/tree/base.py +++ b/astroid/tree/base.py @@ -524,20 +524,12 @@ def _get_filtered_stmts(self, _, node, _stmts, mystmt): def assign_type(self): return self - def ass_type(self): - util.rename_warning((type(self).__name__, type(self).__name__)) - return self.assign_type() - class AssignTypeMixin(object): def assign_type(self): return self - def ass_type(self): - util.rename_warning((type(self).__name__, type(self).__name__)) - return self.assign_type() - def _get_filtered_stmts(self, lookup_node, node, _stmts, mystmt): """method used in filter_stmts""" if self is mystmt: @@ -554,10 +546,6 @@ class ParentAssignTypeMixin(AssignTypeMixin): def assign_type(self): return self.parent.assign_type() - def ass_type(self): - util.rename_warning((type(self).__name__, type(self).__name__)) - return self.assign_type() - class LookupMixIn(object): """Mixin looking up a name in the right scope diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index 4d87c17471..072373847f 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -499,10 +499,6 @@ def postinit(self, target=None, iter=None, ifs=None): def assign_type(self): return self - def ass_type(self): - util.rename_warning((type(self).__name__, type(self).__name__)) - return self.assign_type() - def _get_filtered_stmts(self, lookup_node, node, stmts, mystmt): """method used in filter_stmts""" if self is mystmt: @@ -1308,17 +1304,6 @@ class DictUnpack(base.NodeNG): """Represents the unpacking of dicts into dicts using PEP 448.""" -# 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) - - # Register additional inference dispatched functions. We do # this here, since we need to pass this module as an argument # to these functions, in order to avoid circular dependencies diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 92be900a14..86e09e9256 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -205,46 +205,6 @@ def __init__(self, name, doc, package=None, parent=None, def postinit(self, body=None): self.body = body - # Legacy API aliases - @property - def file(self): - util.rename_warning(('file', 'source_file')) - return self.source_file - @file.setter - def file(self, source_file): - util.rename_warning(('file', 'source_file')) - self.source_file = source_file - @file.deleter - def file(self): - util.rename_warning(('file', 'source_file')) - del self.source_file - - @property - def path(self): - util.rename_warning(('path', 'source_file')) - return self.source_file - @path.setter - def path(self, source_file): - util.rename_warning(('path', 'source_file')) - self.source_file = source_file - @path.deleter - def path(self): - util.rename_warning(('path', 'source_file')) - del self.source_file - - @property - def files_bytes(self): - util.rename_warning(('files_bytes', 'source_code')) - return self.source_code - @files_bytes.setter - def files_bytes(self, source_code): - util.rename_warning(('files_bytes', 'source_code')) - self.source_code = source_code - @files_bytes.deleter - def files_bytes(self): - util.rename_warning(('files_bytes', 'source_code')) - del self.source_code - @property def globals(self): return MappingProxyType(lookup.get_locals(self)) @@ -2013,9 +1973,3 @@ def mro(self, context=None): def bool_value(self): return True - - -# Backwards-compatibility aliases -Class = util.proxy_alias('Class', ClassDef) -Function = util.proxy_alias('Function', FunctionDef) -GenExpr = util.proxy_alias('GenExpr', GeneratorExp) From 3ed16dc880ade848ec987c4b5b0fef87cf980b4d Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 3 Jan 2016 10:40:55 +0200 Subject: [PATCH 159/312] Add specific checking for features instead of catching AttributeError in inference Certain blocks were protected with an AttributeError handler in order to ignore certain nodes which didn't provide an expected feature, such as .getattr and .getitem. This was replaced with an attribute check instead, in order to limit the scope of the exception handling, which could have concealed other bugs. We could check, in the future, for protocol classes instead, but in short term, this is the best approach. Closes #294 --- astroid/inference.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/astroid/inference.py b/astroid/inference.py index a4ee72e1c4..f7f0df5703 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -182,9 +182,6 @@ def infer_attribute(self, context=None): context.boundnode = None except (exceptions.AttributeInferenceError, exceptions.InferenceError): context.boundnode = None - except AttributeError: - # XXX method / function - context.boundnode = None # Explicit StopIteration to return error information, see comment # in raise_if_nothing_inferred. raise StopIteration(dict(node=self, context=context)) @@ -251,6 +248,10 @@ def infer_subscript(self, context=None): yield util.Uninferable return + if not hasattr(value, 'getitem'): + # TODO: we could have a Sequence protocol class or something similar. + raise exceptions.InferenceError(node=self, context=context) + index = next(self.slice.infer(context)) if index is util.Uninferable: yield util.Uninferable @@ -281,7 +282,7 @@ def infer_subscript(self, context=None): try: assigned = value.getitem(index_value, context) - except (IndexError, TypeError, AttributeError) as exc: + except (IndexError, TypeError) as exc: util.reraise(exceptions.InferenceError(node=self, error=exc, context=context)) @@ -438,6 +439,11 @@ def _is_not_implemented(const): def _invoke_binop_inference(instance, op, other, context, method_name, nodes): """Invoke binary operation inference on the given instance.""" + if not hasattr(instance, 'getattr'): + # The operation is undefined for the given node. We can stop + # the inference at this point. + raise exceptions.BinaryOperationNotSupportedError + method = instance.getattr(method_name)[0] inferred = next(method.infer(context=context)) return protocols.infer_binary_op(instance, op, other, context, inferred, nodes) @@ -573,7 +579,7 @@ def _infer_binary_operation(left, right, op, context, flow_factory, nodes): results = list(method()) except exceptions.BinaryOperationNotSupportedError: continue - except (AttributeError, exceptions.AttributeInferenceError): + except exceptions.AttributeInferenceError: continue except exceptions.InferenceError: yield util.Uninferable From 95f8a878bd496af39b700de2748fc315289a11cc Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 3 Jan 2016 17:13:30 +0200 Subject: [PATCH 160/312] Lambda is no longer at the top of the FunctionDef. This means that checks such as ``isinstance(node, Lambda)`` will not hold true anymore for Functions. Closes #291 --- ChangeLog | 6 ++ astroid/tests/unittest_nodes.py | 13 +++- astroid/tree/scoped_nodes.py | 110 +++++++++++++++++--------------- 3 files changed, 77 insertions(+), 52 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9a9b264b9e..758ea35e82 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,12 @@ Change log for the astroid package (used to be astng) ===================================================== -- + * Lambda is no longer at the top of the FunctionDef. + + This means that checks such as ``isinstance(node, Lambda)`` will not + hold true anymore for Functions. + Closes issue #291. + * mixins are now found in tree.base. * Remove do_import_module and real_name from ImportFrom and Import nodes. diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index a8a86682e5..8dd388033a 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -827,6 +827,17 @@ def test_starred_store(self): starred = node.targets[0].elts[1] self.assertIs(starred.ctx, astroid.Store) - + +class FunctionTest(unittest.TestCase): + + def test_function_not_on_top_of_lambda(self): + lambda_, function_ = test_utils.extract_node(''' + lambda x: x #@ + def func(): pass #@ + ''') + self.assertNotIsInstance(lambda_, astroid.FunctionDef) + self.assertNotIsInstance(function_, astroid.Lambda) + + if __name__ == '__main__': unittest.main() diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 86e09e9256..6e53dc3311 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -794,8 +794,53 @@ def infer_argument(self, name, context): func=self._funcnode, arg=name, context=context) +class LambdaFunctionMixin(QualifiedNameMixin, base.FilterStmtsMixin): + """Common code for lambda and functions.""" + + def called_with(self, args, keywords): + """Get a CallSite object with the given arguments + + Given these arguments, this will return an object + which considers them as being passed into the current function, + which can then be used to infer their values. + `args` needs to be a list of arguments, while `keywords` + needs to be a list of tuples, where each tuple is formed + by a keyword name and a keyword value. + """ + return CallSite(self, args, keywords) + + def scope_lookup(self, node, name, offset=0): + if node in self.args.defaults or node in self.args.kw_defaults: + frame = self.parent.frame() + # line offset to avoid that def func(f=func) resolve the default + # value to the defined function + offset = -1 + else: + # check this is not used in function decorators + frame = self + return frame._scope_lookup(node, name, offset) + + def argnames(self): + """return a list of argument names""" + if self.args.args: # maybe None with builtin functions + names = _rec_get_names(self.args.args) + else: + names = [] + if self.args.vararg: + names.append(self.args.vararg) + if self.args.kwarg: + names.append(self.args.kwarg) + return names + + def callable(self): + return True + + def bool_value(self): + return True + + @util.register_implementation(treeabc.Lambda) -class Lambda(QualifiedNameMixin, base.FilterStmtsMixin, lookup.LocalsDictNode): +class Lambda(LambdaFunctionMixin, lookup.LocalsDictNode): _astroid_fields = ('args', 'body',) _other_other_fields = ('locals',) name = '' @@ -818,63 +863,19 @@ def postinit(self, args, body): # return MappingProxyType(get_external_assignments(self, collections.defaultdict(list))) def pytype(self): - if 'method' in self.type: - return '%s.instancemethod' % BUILTINS return '%s.function' % BUILTINS def display_type(self): - if 'method' in self.type: - return 'Method' return 'Function' - def callable(self): - return True - - def argnames(self): - """return a list of argument names""" - if self.args.args: # maybe None with builtin functions - names = _rec_get_names(self.args.args) - else: - names = [] - if self.args.vararg: - names.append(self.args.vararg) - if self.args.kwarg: - names.append(self.args.kwarg) - return names - def infer_call_result(self, caller, context=None): """infer what a function is returning when called""" return self.body.infer(context) - def scope_lookup(self, node, name, offset=0): - if node in self.args.defaults or node in self.args.kw_defaults: - frame = self.parent.frame() - # line offset to avoid that def func(f=func) resolve the default - # value to the defined function - offset = -1 - else: - # check this is not used in function decorators - frame = self - return frame._scope_lookup(node, name, offset) - - def bool_value(self): - return True - - def called_with(self, args, keywords): - """Get a CallSite object with the given arguments - - Given these arguments, this will return an object - which considers them as being passed into the current function, - which can then be used to infer their values. - `args` needs to be a list of arguments, while `keywords` - needs to be a list of tuples, where each tuple is formed - by a keyword name and a keyword value. - """ - return CallSite(self, args, keywords) - @util.register_implementation(treeabc.FunctionDef) -class FunctionDef(node_classes.Statement, Lambda): +class FunctionDef(LambdaFunctionMixin, lookup.LocalsDictNode, + node_classes.Statement): '''Setting FunctionDef.args to Unknown, rather than an Arguments node, means that the corresponding function's arguments are unknown, probably because it represents a function implemented in C or that @@ -904,7 +905,7 @@ def __init__(self, name=None, doc=None, lineno=None, col_offset=None, parent=None): self.name = name self.doc = doc - self.instance_attrs = {} + self.instance_attrs = collections.defaultdict(list) super(FunctionDef, self).__init__(lineno, col_offset, parent) # pylint: disable=arguments-differ; different than Lambdas @@ -914,6 +915,16 @@ def postinit(self, args, body, decorators=None, returns=None): self.decorators = decorators self.returns = returns + def pytype(self): + if 'method' in self.type: + return '%s.instancemethod' % BUILTINS + return '%s.function' % BUILTINS + + def display_type(self): + if 'method' in self.type: + return 'Method' + return 'Function' + @decorators_mod.cachedproperty def extra_decorators(self): """Get the extra decorators that this function can haves @@ -1148,9 +1159,6 @@ def infer_call_result(self, caller, context=None): except exceptions.InferenceError: yield util.Uninferable - def bool_value(self): - return True - @util.register_implementation(treeabc.AsyncFunctionDef) class AsyncFunctionDef(FunctionDef): From 0a55e866e32548ea2de39db529802c22f2041b7f Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 4 Jan 2016 10:43:36 +0200 Subject: [PATCH 161/312] Add .builtins() method to the manager, for retrieving the builtins module. Close #300 --- astroid/brain/brain_builtin_inference.py | 2 +- astroid/interpreter/lookup.py | 2 +- astroid/interpreter/objects.py | 6 +++--- astroid/interpreter/util.py | 2 +- astroid/manager.py | 7 +++++++ astroid/tests/unittest_manager.py | 4 ++++ astroid/tree/node_classes.py | 14 +++++++------- 7 files changed, 24 insertions(+), 13 deletions(-) diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index 0d1e890d22..fef4183c3b 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -82,7 +82,7 @@ def split(self, *args): method.parent = class_node def extend_builtins(class_transforms): - builtin_ast = MANAGER.astroid_cache[BUILTINS] + builtin_ast = MANAGER.builtins() for class_name, transform in class_transforms.items(): transform(builtin_ast[class_name]) diff --git a/astroid/interpreter/lookup.py b/astroid/interpreter/lookup.py index 11991eb527..d8e5931aa9 100644 --- a/astroid/interpreter/lookup.py +++ b/astroid/interpreter/lookup.py @@ -145,7 +145,7 @@ def builtin_lookup(name): """ from astroid import MANAGER # TODO(cpopa) needs to be removed. - builtin_astroid = MANAGER.ast_from_module(six.moves.builtins) + builtin_astroid = MANAGER.builtins() if name == '__dict__': return builtin_astroid, () stmts = builtin_astroid.locals.get(name, ()) diff --git a/astroid/interpreter/objects.py b/astroid/interpreter/objects.py index 7af5b6873d..b097273baf 100644 --- a/astroid/interpreter/objects.py +++ b/astroid/interpreter/objects.py @@ -363,7 +363,7 @@ def __str__(self): @decorators.cachedproperty def _proxied(self): - builtins = MANAGER.astroid_cache[BUILTINS] + builtins = MANAGER.builtins() return builtins.getattr(types.GeneratorType.__name__)[0] @@ -376,7 +376,7 @@ def pytype(self): @decorators.cachedproperty def _proxied(self): - builtins = MANAGER.astroid_cache[BUILTINS] + builtins = MANAGER.builtins() return builtins.getattr('frozenset')[0] @@ -441,7 +441,7 @@ def super_mro(self): @decorators.cachedproperty def _proxied(self): - builtins = MANAGER.astroid_cache[BUILTINS] + builtins = MANAGER.builtins() return builtins.getattr('super')[0] def pytype(self): diff --git a/astroid/interpreter/util.py b/astroid/interpreter/util.py index b6613484ba..93ff831be6 100644 --- a/astroid/interpreter/util.py +++ b/astroid/interpreter/util.py @@ -227,7 +227,7 @@ def is_supertype(type1, type2): def _object_type(node, context=None): context = context or contextmod.InferenceContext() - builtins_ast = MANAGER.astroid_cache[BUILTINS] + builtins_ast = MANAGER.builtins() for inferred in node.infer(context=context): if isinstance(inferred, treeabc.ClassDef): diff --git a/astroid/manager.py b/astroid/manager.py index ed1c27393b..533d102adb 100644 --- a/astroid/manager.py +++ b/astroid/manager.py @@ -205,6 +205,13 @@ def ast_from_module(self, module, modname=None): self.astroid_cache[modname] = mock_ast return mock_ast + def builtins(self): + """Get the builtins module + + This module is special since it's always built. + """ + return self.astroid_cache[six.moves.builtins.__name__] + def register_failed_import_hook(self, hook): """Registers a hook to resolve imports that cannot be found otherwise. diff --git a/astroid/tests/unittest_manager.py b/astroid/tests/unittest_manager.py index cdc03e6499..12674516b2 100644 --- a/astroid/tests/unittest_manager.py +++ b/astroid/tests/unittest_manager.py @@ -173,6 +173,10 @@ def hook(modname): self.manager.ast_from_module_name('foo.bar.baz') del self.manager._failed_import_hooks[0] + def test_builtins(self): + builtins_module = self.manager.builtins() + self.assertEqual(builtins_module.name, BUILTINS) + class BorgAstroidManagerTC(unittest.TestCase): diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index 072373847f..f22614501c 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -550,7 +550,7 @@ def bool_value(self): @decorators.cachedproperty def _proxied(self): - builtins = MANAGER.astroid_cache[BUILTINS] + builtins = MANAGER.builtins() return builtins.getattr(type(self.value).__name__)[0] @@ -564,7 +564,7 @@ class NameConstant(Const): # @decorators.cachedproperty # def _proxied(self): # return self - # # builtins = MANAGER.astroid_cache[BUILTINS] + # # builtins = MANAGER.builtins() # # return builtins.getattr(str(self.value))[0] @@ -677,7 +677,7 @@ def bool_value(self): @decorators.cachedproperty def _proxied(self): - builtins = MANAGER.astroid_cache[BUILTINS] + builtins = MANAGER.builtins() return builtins.getattr('dict')[0] @@ -956,7 +956,7 @@ def getitem(self, index, context=None): @decorators.cachedproperty def _proxied(self): - builtins = MANAGER.astroid_cache[BUILTINS] + builtins = MANAGER.builtins() return builtins.getattr('list')[0] @@ -1044,7 +1044,7 @@ def pytype(self): @decorators.cachedproperty def _proxied(self): - builtins = MANAGER.astroid_cache[BUILTINS] + builtins = MANAGER.builtins() return builtins.getattr('set')[0] @@ -1069,7 +1069,7 @@ def _wrap_attribute(self, attr): @decorators.cachedproperty def _proxied(self): - builtins = MANAGER.astroid_cache[BUILTINS] + builtins = MANAGER.builtins() return builtins.getattr('slice')[0] def pytype(self): @@ -1196,7 +1196,7 @@ def getitem(self, index, context=None): @decorators.cachedproperty def _proxied(self): - builtins = MANAGER.astroid_cache[BUILTINS] + builtins = MANAGER.builtins() return builtins.getattr('tuple')[0] From d39369f422ca344782aa2356a91e25f9db4e8192 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 4 Jan 2016 14:46:43 +0200 Subject: [PATCH 162/312] Use keys and values as separate arguments for nodes.Dict Dict was a bit different that the corresponding class from the builtin ast module with respect to how it was initialized. Instead of accepting a list of pairs, the initializer accepts two arguments, one for keys, the other for values. For backward compatibility, the class gained a new .items property. --- ChangeLog | 5 +++- astroid/brain/brain_builtin_inference.py | 2 +- astroid/raw_building.py | 34 +++++++++++++++++------- astroid/tests/unittest_nodes.py | 12 +++++++++ astroid/tree/node_classes.py | 26 +++++++++++------- astroid/tree/rebuilder.py | 6 ++++- astroid/tree/scoped_nodes.py | 7 +++-- 7 files changed, 67 insertions(+), 25 deletions(-) diff --git a/ChangeLog b/ChangeLog index 758ea35e82..1b32b0883c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,8 +2,11 @@ Change log for the astroid package (used to be astng) ===================================================== -- - * Lambda is no longer at the top of the FunctionDef. + * Dict nodes are constructed with two arguments, keys and values, + in the same way that ast.Dict is built. + * Lambda is no longer at the top of the FunctionDef. + This means that checks such as ``isinstance(node, Lambda)`` will not hold true anymore for Functions. Closes issue #291. diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index fef4183c3b..a3c95370bc 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -300,7 +300,7 @@ def infer_dict(node, context=None): value = nodes.Dict(col_offset=node.col_offset, lineno=node.lineno, parent=node.parent) - value.postinit(items) + value.postinit(*zip(*items)) return value diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 2505ba5c05..f387114317 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -474,6 +474,22 @@ def ast_from_builtin_container(container, built_objects, module, name=None, return (node,) +def _build_from_dictionary(dictionary, built_objects, module, parent): + # This explicit list construction works around an obscure + # interaction: if _ast_from_dict is called on sys.modules and + # sys.modules contains a module that alters sys.modules upon + # import, as pytest.AliasModule does, then the recursive call + # can alter sys.modules's items() while it's being iterated + # over, causing a RuntimeError. + for key, value in list(dictionary.items()): + built_key_objects = _ast_from_object(key, built_objects, + module, parent=parent) + built_value_objects = _ast_from_object(value, built_objects, + module, parent=parent) + for built_key, built_value in zip(built_key_objects, built_value_objects): + yield built_key, built_value + + # pylint: disable=unused-variable; doesn't understand singledispatch @_ast_from_object.register(dict) def ast_from_dict(dictionary, built_objects, module, name=None, @@ -494,16 +510,14 @@ def ast_from_dict(dictionary, built_objects, module, name=None, node = dict_node if name is not None: built_objects[id(dictionary)] = _NameAST(name, node) - dict_node.postinit(items=[ - # This explicit list construction works around an obscure - # interaction: if _ast_from_dict is called on sys.modules and - # sys.modules contains a module that alters sys.modules upon - # import, as pytest.AliasModule does, then the recursive call - # can alter sys.modules's items() while it's being iterated - # over, causing a RuntimeError. - (x, y) for k, v in list(dictionary.items()) - for x, y in zip(_ast_from_object(k, built_objects, module, parent=node), - _ast_from_object(v, built_objects, module, parent=node))]) + + items = list(_build_from_dictionary(dictionary, built_objects, module, node)) + if items: + keys, values = zip(*items) + else: + keys, values = [], [] + + dict_node.postinit(keys, values) if name: parent.postinit(targets=[name_node], value=dict_node) return (node,) diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index 8dd388033a..aeab26e27e 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -839,5 +839,17 @@ def func(): pass #@ self.assertNotIsInstance(function_, astroid.Lambda) +class DictTest(unittest.TestCase): + + def test_keys_values_items(self): + node = test_utils.extract_node(''' + {1: 2, 2:3} + ''') + self.assertEqual([key.value for key in node.keys], [1, 2]) + self.assertEqual([value.value for value in node.values], [2, 3]) + self.assertEqual([(key.value, value.value) for (key, value) in node.items], + [(1, 2), (2, 3)]) + + if __name__ == '__main__': unittest.main() diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index f22614501c..c470761484 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -626,14 +626,20 @@ def postinit(self, targets=None): @util.register_implementation(runtimeabc.BuiltinInstance) class Dict(base.NodeNG, objects.BaseInstance): """class representing a Dict node""" - _astroid_fields = ('items',) + _astroid_fields = ('keys', 'values') def __init__(self, lineno=None, col_offset=None, parent=None): - self.items = [] + self.keys = [] + self.values = [] super(Dict, self).__init__(lineno, col_offset, parent) - def postinit(self, items): - self.items = items + def postinit(self, keys, values): + self.keys = keys + self.values = values + + @property + def items(self): + return list(zip(self.keys, self.values)) def pytype(self): return '%s.dict' % BUILTINS @@ -641,21 +647,21 @@ def pytype(self): def get_children(self): """get children of a Dict node""" # overrides get_children - for key, value in self.items: + for key, value in zip(self.keys, self.values): yield key yield value def last_child(self): """override last_child""" - if self.items: - return self.items[-1][1] + if self.values: + return self.values[-1] return None def itered(self): - return self.items[::2] + return self.keys def getitem(self, lookup_key, context=None): - for key, value in self.items: + for key, value in zip(self.keys, self.values): # TODO(cpopa): no support for overriding yet, {1:2, **{1: 3}}. if isinstance(key, DictUnpack): try: @@ -673,7 +679,7 @@ def getitem(self, lookup_key, context=None): raise IndexError(lookup_key) def bool_value(self): - return bool(self.items) + return bool(self.keys) @decorators.cachedproperty def _proxied(self): diff --git a/astroid/tree/rebuilder.py b/astroid/tree/rebuilder.py index 81ef48d442..1d99487637 100644 --- a/astroid/tree/rebuilder.py +++ b/astroid/tree/rebuilder.py @@ -370,7 +370,11 @@ def visit_dict(self, node, parent): """visit a Dict node by returning a fresh instance of it""" newnode = nodes.Dict(node.lineno, node.col_offset, parent) items = list(self._visit_dict_items(node, parent, newnode)) - newnode.postinit(items) + if items: + keys, values = zip(*items) + else: + keys, values = [], [] + newnode.postinit(keys, values) return newnode def visit_dictcomp(self, node, parent): diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 6e53dc3311..d04b549b08 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -763,9 +763,12 @@ def infer_argument(self, name, context): kwarg = node_classes.Dict(lineno=self._funcnode.args.lineno, col_offset=self._funcnode.args.col_offset, parent=self._funcnode.args) - kwarg.postinit([(node_classes.Const(key, parent=kwarg), value) - for key, value in kwargs.items()]) + items = [(node_classes.Const(key, parent=kwarg), value) + for key, value in kwargs.items()] + keys, values = zip(*items) + kwarg.postinit(keys, values) return iter((kwarg, )) + elif self._funcnode.args.vararg == name: # It wants all the args that were passed into # the call site. From 85160d73ba53a265c445c92981db35b7b1a96698 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Fri, 8 Jan 2016 12:09:43 -0500 Subject: [PATCH 163/312] Refactor Compare nodes to have two sequences, ops and comparators, corresponding to the stdlib Compare node. --- astroid/as_string.py | 2 +- astroid/tests/unittest_transforms.py | 2 +- astroid/tree/node_classes.py | 21 ++++++++++++--------- astroid/tree/rebuilder.py | 9 +++++---- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/astroid/as_string.py b/astroid/as_string.py index 5b64e065eb..eefc3c4755 100644 --- a/astroid/as_string.py +++ b/astroid/as_string.py @@ -123,7 +123,7 @@ def visit_classdef(self, node): def visit_compare(self, node): """return an astroid.Compare node as string""" rhs_str = ' '.join(['%s %s' % (op, expr.accept(self)) - for op, expr in node.ops]) + for op, expr in zip(node.ops, node.comparators)]) return '%s %s' % (node.left.accept(self), rhs_str) def visit_comprehension(self, node): diff --git a/astroid/tests/unittest_transforms.py b/astroid/tests/unittest_transforms.py index ae2691e6ec..6bcb1db0ae 100644 --- a/astroid/tests/unittest_transforms.py +++ b/astroid/tests/unittest_transforms.py @@ -69,7 +69,7 @@ def test_recursive_transforms_into_astroid_fields(self): # by going recursively into the _astroid_fields per each node. def transform_compare(node): # Let's check the values of the ops - _, right = node.ops[0] + right = node.comparators[0] # Assume they are Consts and they were transformed before # us. return nodes.Const(node.left.value < right.value) diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index c470761484..0638622621 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -458,25 +458,28 @@ def kwargs(self): @util.register_implementation(treeabc.Compare) class Compare(base.NodeNG): """class representing a Compare node""" - _astroid_fields = ('left', 'ops',) + _astroid_fields = ('left', 'comparators') + _other_fields = ('ops',) left = None - ops = None - def postinit(self, left=None, ops=None): - self.left = left + def __init__(self, ops, lineno=None, col_offset=None, parent=None): + self.comparators = [] self.ops = ops + super(Compare, self).__init__(lineno, col_offset, parent) + + def postinit(self, left=None, comparators=None): + self.left = left + self.comparators = comparators 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' + for comparator in self.comparators: + yield comparator def last_child(self): """override last_child""" - # XXX maybe if self.ops: - return self.ops[-1][1] - #return self.left + return self.comparators[-1] @util.register_implementation(treeabc.Comprehension) diff --git a/astroid/tree/rebuilder.py b/astroid/tree/rebuilder.py index 1d99487637..0221b1900b 100644 --- a/astroid/tree/rebuilder.py +++ b/astroid/tree/rebuilder.py @@ -322,11 +322,12 @@ def visit_continue(self, node, parent): def visit_compare(self, node, parent): """visit a Compare node by returning a fresh instance of it""" - newnode = nodes.Compare(node.lineno, node.col_offset, parent) + newnode = nodes.Compare([_CMP_OP_CLASSES[type(op)] for op in + node.ops], node.lineno, + node.col_offset, parent) newnode.postinit(self.visit(node.left, newnode), - [(_CMP_OP_CLASSES[op.__class__], - self.visit(expr, newnode)) - for (op, expr) in zip(node.ops, node.comparators)]) + [self.visit(expr, newnode) + for expr in node.comparators]) return newnode def visit_comprehension(self, node, parent): From 5273294a60d0313a2f7889caca83985181a3f8ae Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Sat, 9 Jan 2016 17:53:26 -0500 Subject: [PATCH 164/312] Refactor With nodes to have subnodes, WithItems, corresponding to the stdlib 3 With node. --- astroid/as_string.py | 8 ++++--- astroid/nodes.py | 4 ++-- astroid/protocols.py | 12 +++++++++- astroid/tree/node_classes.py | 26 +++++++++++++--------- astroid/tree/rebuilder.py | 43 ++++++++++++++++-------------------- astroid/tree/treeabc.py | 4 ++++ 6 files changed, 57 insertions(+), 40 deletions(-) diff --git a/astroid/as_string.py b/astroid/as_string.py index eefc3c4755..7f59f88c08 100644 --- a/astroid/as_string.py +++ b/astroid/as_string.py @@ -406,11 +406,13 @@ def visit_while(self, node): def visit_with(self, node): # 'with' without 'as' is possible """return an astroid.With node as string""" - items = ', '.join(('(%s)' % expr.accept(self)) + - (vars and ' as (%s)' % (vars.accept(self)) or '') - for expr, vars in node.items) + items = ', '.join(item.accept(self) for item in node.items) return 'with %s:\n%s' % (items, self._stmt_list(node.body)) + def visit_withitem(self, node): + return ('(%s)' % node.context_expr.accept(self) + ' as (%s)' % + (optional_vars.accept(self)) if optional_vars else '') + def visit_yield(self, node): """yield an ast.Yield node as string""" yi_val = node.value and (" " + node.value.accept(self)) or "" diff --git a/astroid/nodes.py b/astroid/nodes.py index e2fb1e0c5e..1aea53f275 100644 --- a/astroid/nodes.py +++ b/astroid/nodes.py @@ -44,7 +44,7 @@ ImportFrom, Attribute, Global, If, IfExp, Import, Index, Keyword, List, Name, NameConstant, Nonlocal, Pass, Print, Raise, Return, Set, Slice, Starred, Subscript, TryExcept, TryFinally, Tuple, UnaryOp, While, With, - Yield, YieldFrom, AsyncFor, Await, AsyncWith, + WithItem, Yield, YieldFrom, AsyncFor, Await, AsyncWith, # Node not present in the builtin ast module. DictUnpack, # Special nodes for building from live objects. @@ -79,6 +79,6 @@ Set, SetComp, Slice, Starred, Subscript, TryExcept, TryFinally, Tuple, UnaryOp, Unknown, - While, With, + While, With, WithItem, Yield, YieldFrom, ) diff --git a/astroid/protocols.py b/astroid/protocols.py index c9e2842d5e..0464f7e78c 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -469,6 +469,12 @@ def _infer_context_manager(self, mgr, context, nodes): yield result +@assigned_stmts.register(treeabc.WithItem) +def withitem_assigned_stmts(self, nodes, node=None, context=None, asspath=None): + print(node, self, self.parent) + return self.parent.assigned_stmts(nodes, node=node, asspath=asspath) + + @assigned_stmts.register(treeabc.With) @decorators.raise_if_nothing_inferred def with_assigned_stmts(self, nodes, node=None, context=None, asspath=None): @@ -495,7 +501,11 @@ def __enter__(self): context: TODO asspath: TODO """ - mgr = next(mgr for (mgr, vars) in self.items if vars == node) + print('SELF', self) + print('NODES', nodes) + print('NODE', node) + mgr = next(item.context_expr for item in self.items if + item.optional_vars == node) if asspath is None: for result in _infer_context_manager(self, mgr, context, nodes): yield result diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index 0638622621..ed7576326f 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -1268,8 +1268,11 @@ class With(base.BlockRangeMixIn, base.AssignTypeMixin, AssignedStmtsMixin, Statement): """class representing a With node""" _astroid_fields = ('items', 'body') - items = None - body = None + + def __init__(self, lineno=None, col_offset=None, parent=None): + self.items = [] + self.body = [] + super(With, self).__init__(lineno, col_offset, parent) def postinit(self, items=None, body=None): self.items = items @@ -1277,15 +1280,18 @@ def postinit(self, items=None, body=None): @decorators.cachedproperty def blockstart_tolineno(self): - return self.items[-1][0].tolineno + return self.items[-1].context_expr.tolineno - def get_children(self): - for expr, var in self.items: - yield expr - if var: - yield var - for elt in self.body: - yield elt + +@util.register_implementation(treeabc.WithItem) +class WithItem(base.NodeNG, base.ParentAssignTypeMixin, AssignedStmtsMixin): + _astroid_fields = ('context_expr', 'optional_vars') + context_expr = None + optional_vars = None + + def postinit(self, context_expr=None, optional_vars=None): + self.context_expr = context_expr + self.optional_vars = optional_vars @util.register_implementation(treeabc.AsyncWith) diff --git a/astroid/tree/rebuilder.py b/astroid/tree/rebuilder.py index 0221b1900b..12b9388ce0 100644 --- a/astroid/tree/rebuilder.py +++ b/astroid/tree/rebuilder.py @@ -709,14 +709,13 @@ def visit_while(self, node, parent): def visit_with(self, node, parent): newnode = nodes.With(node.lineno, node.col_offset, parent) - expr = self.visit(node.context_expr, newnode) - if node.optional_vars is not None: - optional_vars = self.visit(node.optional_vars, newnode) - else: - optional_vars = None - newnode.postinit([(expr, optional_vars)], - [self.visit(child, newnode) - for child in node.body]) + with_item = nodes.WithItem(node.context_expr.lineno, + node.context_expr.col_offset, newnode) + context_expr = self.visit(node.context_expr, with_item) + optional_vars = _visit_or_none(node, 'optional_vars', self, with_item) + with_item.postinit(context_expr, optional_vars) + newnode.postinit([with_item], + [self.visit(child, newnode) for child in node.body]) return newnode def visit_yield(self, node, parent): @@ -793,23 +792,19 @@ def visit_try(self, node, parent): elif node.handlers: return self.visit_tryexcept(node, parent) - def _visit_with(self, cls, node, parent): - if 'items' not in node._fields: - # python < 3.3 - return super(TreeRebuilder3, self).visit_with(node, parent) - - newnode = cls(node.lineno, node.col_offset, parent) - def visit_child(child): - expr = self.visit(child.context_expr, newnode) - var = _visit_or_none(child, 'optional_vars', self, newnode) - return expr, var - newnode.postinit([visit_child(child) for child in node.items], - [self.visit(child, newnode) - for child in node.body]) + def visit_with(self, node, parent): + newnode = nodes.With(node.lineno, node.col_offset, parent) + newnode.postinit([self.visit(item, newnode) for item in node.items], + [self.visit(child, newnode) for child in node.body]) return newnode - def visit_with(self, node, parent): - return self._visit_with(nodes.With, node, parent) + def visit_withitem(self, node, parent): + newnode = nodes.WithItem(node.context_expr.lineno, + node.context_expr.col_offset, parent) + context_expr = self.visit(node.context_expr, newnode) + optional_vars = _visit_or_none(node, 'optional_vars', self, newnode) + newnode.postinit(context_expr=context_expr, optional_vars=optional_vars) + return newnode def visit_yieldfrom(self, node, parent): newnode = nodes.YieldFrom(node.lineno, node.col_offset, parent) @@ -834,7 +829,7 @@ def visit_await(self, node, parent): return newnode def visit_asyncwith(self, node, parent): - return self._visit_with(nodes.AsyncWith, node, parent) + return self.visit_with(nodes.AsyncWith, node, parent) if sys.version_info >= (3, 0): diff --git a/astroid/tree/treeabc.py b/astroid/tree/treeabc.py index c9fa2269f7..6ad96ba81d 100644 --- a/astroid/tree/treeabc.py +++ b/astroid/tree/treeabc.py @@ -273,6 +273,10 @@ class With(Statement): """Class representing a With node""" +class WithItem(NodeNG): + """Class representing a WithItem node""" + + class AsyncWith(With): """Asynchronous `with` built with the `async` keyword.""" From fb9a7380fd526bbd5ff49c473fd20fa82b234873 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Sat, 9 Jan 2016 18:34:13 -0500 Subject: [PATCH 165/312] Fix forwarding for withitem_assigned_stmts() --- astroid/protocols.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/astroid/protocols.py b/astroid/protocols.py index 0464f7e78c..c554c69b1b 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -471,8 +471,7 @@ def _infer_context_manager(self, mgr, context, nodes): @assigned_stmts.register(treeabc.WithItem) def withitem_assigned_stmts(self, nodes, node=None, context=None, asspath=None): - print(node, self, self.parent) - return self.parent.assigned_stmts(nodes, node=node, asspath=asspath) + return assigned_stmts(self.parent, nodes=nodes, node=node, context=context, asspath=asspath) @assigned_stmts.register(treeabc.With) @@ -501,9 +500,6 @@ def __enter__(self): context: TODO asspath: TODO """ - print('SELF', self) - print('NODES', nodes) - print('NODE', node) mgr = next(item.context_expr for item in self.items if item.optional_vars == node) if asspath is None: From 4d22d97f3604465c1e40105b0d3e47185cbaf290 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Sat, 9 Jan 2016 19:06:34 -0500 Subject: [PATCH 166/312] Rename asspath to assign_ppath --- astroid/protocols.py | 82 ++++++++++++++++++------------------ astroid/tree/node_classes.py | 8 ++-- 2 files changed, 45 insertions(+), 45 deletions(-) diff --git a/astroid/protocols.py b/astroid/protocols.py index c554c69b1b..eada976481 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -224,14 +224,14 @@ def instance_infer_binary_op(self, operator, other, context, method, nodes): @util.singledispatch -def assigned_stmts(self, nodes, node=None, context=None, asspath=None): +def assigned_stmts(self, nodes, node=None, context=None, assign_path=None): raise exceptions.NotSupportedError -def _resolve_looppart(parts, asspath, context): +def _resolve_looppart(parts, assign_path, context): """recursive function to resolve multiple assignments on loops""" - asspath = asspath[:] - index = asspath.pop(0) + assign_path = assign_path[:] + index = assign_path.pop(0) for part in parts: if part is util.Uninferable: continue @@ -249,7 +249,7 @@ def _resolve_looppart(parts, asspath, context): continue except TypeError: # stmt is unsubscriptable Const continue - if not asspath: + if not assign_path: # we achieved to resolved the assignment path, # don't infer the last part yield assigned @@ -260,7 +260,7 @@ def _resolve_looppart(parts, asspath, context): # search on each possibly inferred value try: for inferred in _resolve_looppart(assigned.infer(context), - asspath, context): + assign_path, context): yield inferred except exceptions.InferenceError: break @@ -269,42 +269,42 @@ def _resolve_looppart(parts, asspath, context): @assigned_stmts.register(treeabc.For) @assigned_stmts.register(treeabc.Comprehension) @decorators.raise_if_nothing_inferred -def for_assigned_stmts(self, nodes, node=None, context=None, asspath=None): - if asspath is None: +def for_assigned_stmts(self, nodes, node=None, context=None, assign_path=None): + if assign_path is None: for lst in self.iter.infer(context): if isinstance(lst, (treeabc.Tuple, treeabc.List)): for item in lst.elts: yield item else: for inferred in _resolve_looppart(self.iter.infer(context), - asspath, context): + assign_path, context): yield inferred # Explicit StopIteration to return error information, see comment # in raise_if_nothing_inferred. raise StopIteration(dict(node=self, unknown=node, - assign_path=asspath, context=context)) + assign_path=assign_path, context=context)) @assigned_stmts.register(treeabc.Tuple) @assigned_stmts.register(treeabc.List) -def mulass_assigned_stmts(self, nodes, node=None, context=None, asspath=None): - if asspath is None: - asspath = [] +def mulass_assigned_stmts(self, nodes, node=None, context=None, assign_path=None): + if assign_path is None: + assign_path = [] try: index = self.elts.index(node) except ValueError: util.reraise(exceptions.InferenceError( 'Tried to retrieve a node {node!r} which does not exist', - node=self, assign_path=asspath, context=context)) + node=self, assign_path=assign_path, context=context)) - asspath.insert(0, index) - return self.parent.assigned_stmts(node=self, context=context, asspath=asspath) + assign_path.insert(0, index) + return self.parent.assigned_stmts(node=self, context=context, assign_path=assign_path) @assigned_stmts.register(treeabc.AssignName) @assigned_stmts.register(treeabc.AssignAttr) -def assend_assigned_stmts(self, nodes, node=None, context=None, asspath=None): +def assend_assigned_stmts(self, nodes, node=None, context=None, assign_path=None): return self.parent.assigned_stmts(self, context=context) @@ -354,7 +354,7 @@ def _arguments_infer_argname(self, name, context, nodes): @assigned_stmts.register(treeabc.Arguments) -def arguments_assigned_stmts(self, nodes, node=None, context=None, asspath=None): +def arguments_assigned_stmts(self, nodes, node=None, context=None, assign_path=None): if context.callcontext: # reset call context/name callcontext = context.callcontext @@ -369,23 +369,23 @@ def arguments_assigned_stmts(self, nodes, node=None, context=None, asspath=None) @assigned_stmts.register(treeabc.Assign) @assigned_stmts.register(treeabc.AugAssign) @decorators.raise_if_nothing_inferred -def assign_assigned_stmts(self, nodes, node=None, context=None, asspath=None): - if not asspath: +def assign_assigned_stmts(self, nodes, node=None, context=None, assign_path=None): + if not assign_path: yield self.value return - for inferred in _resolve_asspart(self.value.infer(context), asspath, context): + for inferred in _resolve_asspart(self.value.infer(context), assign_path, context): yield inferred # Explicit StopIteration to return error information, see comment # in raise_if_nothing_inferred. raise StopIteration(dict(node=self, unknown=node, - assign_path=asspath, context=context)) + assign_path=assign_path, context=context)) -def _resolve_asspart(parts, asspath, context): +def _resolve_asspart(parts, assign_path, context): """recursive function to resolve multiple assignments""" - asspath = asspath[:] - index = asspath.pop(0) + assign_path = assign_path[:] + index = assign_path.pop(0) for part in parts: if hasattr(part, 'getitem'): try: @@ -394,7 +394,7 @@ def _resolve_asspart(parts, asspath, context): # unexpected exception ? except (TypeError, IndexError): return - if not asspath: + if not assign_path: # we achieved to resolved the assignment path, don't infer the # last part yield assigned @@ -405,7 +405,7 @@ def _resolve_asspart(parts, asspath, context): # possibly inferred value try: for inferred in _resolve_asspart(assigned.infer(context), - asspath, context): + assign_path, context): yield inferred except exceptions.InferenceError: return @@ -413,7 +413,7 @@ def _resolve_asspart(parts, asspath, context): @assigned_stmts.register(treeabc.ExceptHandler) @decorators.raise_if_nothing_inferred -def excepthandler_assigned_stmts(self, nodes, node=None, context=None, asspath=None): +def excepthandler_assigned_stmts(self, nodes, node=None, context=None, assign_path=None): for assigned in inferenceutil.unpack_infer(self.type): if isinstance(assigned, treeabc.ClassDef): assigned = assigned.instantiate_class() @@ -422,7 +422,7 @@ def excepthandler_assigned_stmts(self, nodes, node=None, context=None, asspath=N # Explicit StopIteration to return error information, see comment # in raise_if_nothing_inferred. raise StopIteration(dict(node=self, unknown=node, - assign_path=asspath, context=context)) + assign_path=assign_path, context=context)) def _infer_context_manager(self, mgr, context, nodes): @@ -470,13 +470,13 @@ def _infer_context_manager(self, mgr, context, nodes): @assigned_stmts.register(treeabc.WithItem) -def withitem_assigned_stmts(self, nodes, node=None, context=None, asspath=None): - return assigned_stmts(self.parent, nodes=nodes, node=node, context=context, asspath=asspath) +def withitem_assigned_stmts(self, nodes, node=None, context=None, assign_path=None): + return assigned_stmts(self.parent, nodes=nodes, node=node, context=context, assign_path=assign_path) @assigned_stmts.register(treeabc.With) @decorators.raise_if_nothing_inferred -def with_assigned_stmts(self, nodes, node=None, context=None, asspath=None): +def with_assigned_stmts(self, nodes, node=None, context=None, assign_path=None): """Infer names and other nodes from a *with* statement. This enables only inference for name binding in a *with* statement. @@ -498,22 +498,22 @@ def __enter__(self): self: nodes.With node: The target of the assignment, `as (a, b)` in `with foo as (a, b)`. context: TODO - asspath: TODO + assign_path: TODO """ mgr = next(item.context_expr for item in self.items if item.optional_vars == node) - if asspath is None: + if assign_path is None: for result in _infer_context_manager(self, mgr, context, nodes): yield result else: for result in _infer_context_manager(self, mgr, context, nodes): - # Walk the asspath and get the item at the final index. + # Walk the assign_path and get the item at the final index. obj = result - for index in asspath: + for index in assign_path: if not hasattr(obj, 'elts'): raise exceptions.InferenceError( 'Wrong type ({targets!r}) for {node!r} assignment', - node=self, targets=node, assign_path=asspath, + node=self, targets=node, assign_path=assign_path, context=context) try: @@ -522,24 +522,24 @@ def __enter__(self): util.reraise(exceptions.InferenceError( 'Tried to infer a nonexistent target with index {index} ' 'in {node!r}.', node=self, targets=node, - assign_path=asspath, context=context)) + assign_path=assign_path, context=context)) yield obj # Explicit StopIteration to return error information, see comment # in raise_if_nothing_inferred. raise StopIteration(dict(node=self, unknown=node, - assign_path=asspath, context=context)) + assign_path=assign_path, context=context)) @assigned_stmts.register(treeabc.Starred) @decorators.yes_if_nothing_inferred -def starred_assigned_stmts(self, nodes, node=None, context=None, asspath=None): +def starred_assigned_stmts(self, nodes, node=None, context=None, assign_path=None): """ Arguments: self: nodes.Starred node: TODO context: TODO - asspath: TODO + assign_path: TODO """ stmt = self.statement() if not isinstance(stmt, (treeabc.Assign, treeabc.For)): diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index ed7576326f..4816fd2ea2 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -78,12 +78,12 @@ def previous_sibling(self): class AssignedStmtsMixin(object): """Provide an `assigned_stmts` method to classes which inherits it.""" - def assigned_stmts(self, node=None, context=None, asspath=None): + def assigned_stmts(self, node=None, context=None, assign_path=None): """Responsible to return the assigned statement (e.g. not inferred) according to the assignment type. - The `asspath` parameter is used to record the lhs path of the original node. - For instance if we want assigned statements for 'c' in 'a, (b,c)', asspath + The `assign_path` parameter is used to record the lhs path of the original node. + For instance if we want assigned statements for 'c' in 'a, (b,c)', assign_path will be [1, 1] once arrived to the Assign node. The `context` parameter is the current inference context which should be given @@ -93,7 +93,7 @@ def assigned_stmts(self, node=None, context=None, asspath=None): # circular dependencies between these modules. return protocols.assigned_stmts(self, sys.modules[__name__], node=node, context=context, - asspath=asspath) + assign_path=assign_path) # Name classes From 67844bb2a13627b267c3e60fa7eadc93cd1dad06 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Sat, 9 Jan 2016 21:44:15 -0500 Subject: [PATCH 167/312] Put mixins before NodeNG for WithItem --- astroid/tree/node_classes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index 4816fd2ea2..78a9afc8da 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -1284,7 +1284,7 @@ def blockstart_tolineno(self): @util.register_implementation(treeabc.WithItem) -class WithItem(base.NodeNG, base.ParentAssignTypeMixin, AssignedStmtsMixin): +class WithItem(base.ParentAssignTypeMixin, AssignedStmtsMixin, base.NodeNG): _astroid_fields = ('context_expr', 'optional_vars') context_expr = None optional_vars = None From 245a7584435288ba8196f7ba59071dc875945a9d Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 12 Jan 2016 00:50:49 +0200 Subject: [PATCH 168/312] Enforce strong updates per frames. When looking up a name in a scope, Scope.lookup will return only the values which will be reachable after execution, as seen in the following code: a = 1 a = 2 In this case it doesn't make sense to return two values, but only the last one. --- ChangeLog | 13 +++++++++++++ astroid/tests/unittest_lookup.py | 6 +++--- astroid/tree/base.py | 3 +-- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1b32b0883c..623725e2aa 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,7 @@ Change log for the astroid package (used to be astng) ===================================================== -- + * Dict nodes are constructed with two arguments, keys and values, in the same way that ast.Dict is built. @@ -29,6 +30,18 @@ Change log for the astroid package (used to be astng) to avoid a potential problem with encodings when using .format. Closes issue #273. Patch by notsqrt. + * Enforce strong updates per frames. + + When looking up a name in a scope, Scope.lookup will return + only the values which will be reachable after execution, as seen + in the following code: + + a = 1 + a = 2 + + In this case it doesn't make sense to return two values, but + only the last one. + * Add support for inference on threading.Lock As a matter of fact, astroid can infer on threading.RLock, diff --git a/astroid/tests/unittest_lookup.py b/astroid/tests/unittest_lookup.py index 5ec13245f9..2bdf569118 100644 --- a/astroid/tests/unittest_lookup.py +++ b/astroid/tests/unittest_lookup.py @@ -56,12 +56,12 @@ def func(): a = next(astroid.nodes_of_class(nodes.Name)) self.assertEqual(a.lineno, 2) if sys.version_info < (3, 0): - self.assertEqual(len(astroid.lookup('b')[1]), 2) - self.assertEqual(len(astroid.lookup('a')[1]), 3) + self.assertEqual(len(astroid.lookup('b')[1]), 1) + self.assertEqual(len(astroid.lookup('a')[1]), 1) b = astroid.locals['b'][1] else: self.assertEqual(len(astroid.lookup('b')[1]), 1) - self.assertEqual(len(astroid.lookup('a')[1]), 2) + self.assertEqual(len(astroid.lookup('a')[1]), 1) b = astroid.locals['b'][0] stmts = a.lookup('a')[1] self.assertEqual(len(stmts), 1) diff --git a/astroid/tree/base.py b/astroid/tree/base.py index 8b2a95e8a1..43f77d121e 100644 --- a/astroid/tree/base.py +++ b/astroid/tree/base.py @@ -603,8 +603,7 @@ def _filter_stmts(self, stmts, frame, offset): if self.statement() is myframe and myframe.parent: myframe = myframe.parent.frame() - if not myframe is frame or self is frame: - return stmts + mystmt = self.statement() # line filtering if we are in the same frame # From 371857b81fe1f4141c651df303a923bc9bad904a Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Tue, 12 Jan 2016 22:33:29 -0500 Subject: [PATCH 169/312] Fix an unprintable InferenceError by raising the class rather than an instance --- astroid/interpreter/objects.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astroid/interpreter/objects.py b/astroid/interpreter/objects.py index b097273baf..43b419a427 100644 --- a/astroid/interpreter/objects.py +++ b/astroid/interpreter/objects.py @@ -268,7 +268,7 @@ def getitem(self, index, context=None): try: return next(method.infer_call_result(self, new_context)) except StopIteration: - util.reraise(exceptions.InferenceError()) + util.reraise(exceptions.InferenceError) @util.register_implementation(runtimeabc.UnboundMethod) From 3ed5f67e0b2bd77783ed112389878cfc12e688b2 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Wed, 13 Jan 2016 15:36:46 -0500 Subject: [PATCH 170/312] Add message to the InferenceError for the base implementation of .infer() --- astroid/brain/brain_stdlib.py | 4 ++-- astroid/inference.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/astroid/brain/brain_stdlib.py b/astroid/brain/brain_stdlib.py index c0e6ba81f4..2072bb012e 100644 --- a/astroid/brain/brain_stdlib.py +++ b/astroid/brain/brain_stdlib.py @@ -22,7 +22,7 @@ def infer_first(node, context): if node is util.Uninferable: - util.reraise(InferenceError()) + util.reraise(InferenceError) try: value = next(node.infer(context=context)) @@ -31,7 +31,7 @@ def infer_first(node, context): else: return value except StopIteration: - util.reraise(InferenceError()) + util.reraise(InferenceError) # module specific transformation functions ##################################### diff --git a/astroid/inference.py b/astroid/inference.py index f7f0df5703..015fcb2096 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -39,7 +39,7 @@ @util.singledispatch def infer(self, context=None): - raise exceptions.InferenceError + raise exceptions.InferenceError('No inference implementation found for {node!r}', node=self, context=context) @infer.register(treeabc.Module) From 0814cff7e02d99ac477b3f44e06e3c070c688885 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Wed, 13 Jan 2016 15:45:22 -0500 Subject: [PATCH 171/312] Edit base implementation inference message to be consistent with master --- astroid/inference.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astroid/inference.py b/astroid/inference.py index 015fcb2096..1a7ce413a5 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -39,7 +39,7 @@ @util.singledispatch def infer(self, context=None): - raise exceptions.InferenceError('No inference implementation found for {node!r}', node=self, context=context) + raise exceptions.InferenceError('No inference function for {node!r}', node=self, context=context) @infer.register(treeabc.Module) From e6bcc81ac9eb2b9e3fd384b0ba3d0ea574a5eeef Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Fri, 15 Jan 2016 13:46:18 +0200 Subject: [PATCH 172/312] Raise InferenceError with a proper context. --- astroid/brain/brain_stdlib.py | 17 ++++++++--------- astroid/interpreter/objects.py | 8 ++++++-- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/astroid/brain/brain_stdlib.py b/astroid/brain/brain_stdlib.py index 2072bb012e..3a5ef341b1 100644 --- a/astroid/brain/brain_stdlib.py +++ b/astroid/brain/brain_stdlib.py @@ -22,16 +22,15 @@ def infer_first(node, context): if node is util.Uninferable: - util.reraise(InferenceError) + raise exceptions.InferenceError( + 'Could not infer an Uninferable node.', + node=node, context=context) - try: - value = next(node.infer(context=context)) - if value is util.Uninferable: - raise UseInferenceDefault() - else: - return value - except StopIteration: - util.reraise(InferenceError) + value = next(node.infer(context=context), None) + if value in (util.Uninferable, None): + raise UseInferenceDefault() + else: + return value # module specific transformation functions ##################################### diff --git a/astroid/interpreter/objects.py b/astroid/interpreter/objects.py index 43b419a427..2459e230a6 100644 --- a/astroid/interpreter/objects.py +++ b/astroid/interpreter/objects.py @@ -263,12 +263,16 @@ def getitem(self, index, context=None): method = next(self.igetattr('__getitem__', context=context)) if not isinstance(method, BoundMethod): - raise exceptions.InferenceError + raise exceptions.InferenceError( + 'Could not find __getitem__ for {node!r}.', + node=self, context=context) try: return next(method.infer_call_result(self, new_context)) except StopIteration: - util.reraise(exceptions.InferenceError) + util.reraise(exceptions.InferenceError( + message='Inference for {node!r}[{index!s}] failed.', + node=self, index=index, context=context)) @util.register_implementation(runtimeabc.UnboundMethod) From 168042eecfdfac6a143f942a22c2a01ffadc3401 Mon Sep 17 00:00:00 2001 From: Mateusz Bysiek Date: Thu, 14 Jan 2016 19:47:11 +0900 Subject: [PATCH 173/312] added 3 pkg_resources functions to brain [Those 3 are] methods of WorkingSet objects [that] are also available as module- level functions in pkg_resources that apply to the default working_set instance. Thus, you can use e.g. pkg_resources.require() as an abbreviation for pkg_resources.working_set.require() above taken from: https://pythonhosted.org/setuptools/pkg_resources.html#basic-workingset-methods this fixes https://github.com/PyCQA/pylint/issues/780 Signed-off-by: Mateusz Bysiek --- astroid/brain/brain_stdlib.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/astroid/brain/brain_stdlib.py b/astroid/brain/brain_stdlib.py index 3a5ef341b1..0791d93fbc 100644 --- a/astroid/brain/brain_stdlib.py +++ b/astroid/brain/brain_stdlib.py @@ -96,6 +96,15 @@ def __delitem__(self, index): pass def pkg_resources_transform(): return AstroidBuilder(MANAGER).string_build(''' +def require(*requirements): + return pkg_resources.working_set.require(*requirements) + +def run_script(requires, script_name): + return pkg_resources.working_set.run_script(requires, script_name) + +def iter_entry_points(group, name=None): + return pkg_resources.working_set.iter_entry_points(group, name) + def resource_exists(package_or_requirement, resource_name): return get_provider(package_or_requirement).has_resource(resource_name) From 935cba9e4d467d192891d18d5af5a977c7a303f5 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Thu, 14 Jan 2016 23:11:24 +0200 Subject: [PATCH 174/312] Support accessing properties using super(). --- ChangeLog | 2 ++ astroid/interpreter/objects.py | 4 ++++ astroid/tests/unittest_objects.py | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/ChangeLog b/ChangeLog index 623725e2aa..a73899e67e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -30,6 +30,8 @@ Change log for the astroid package (used to be astng) to avoid a potential problem with encodings when using .format. Closes issue #273. Patch by notsqrt. + * Support accessing properties with super(). + * Enforce strong updates per frames. When looking up a name in a scope, Scope.lookup will return diff --git a/astroid/interpreter/objects.py b/astroid/interpreter/objects.py index 2459e230a6..2c570a1e3a 100644 --- a/astroid/interpreter/objects.py +++ b/astroid/interpreter/objects.py @@ -506,6 +506,10 @@ def igetattr(self, name, context=None): yield inferred elif self._class_based or inferred.type == 'staticmethod': yield inferred + elif is_property(inferred): + # TODO: support other descriptors as well. + for value in inferred.infer_call_result(self, context): + yield value else: yield BoundMethod(inferred, cls) diff --git a/astroid/tests/unittest_objects.py b/astroid/tests/unittest_objects.py index 54fbc9a43e..1f5d078067 100644 --- a/astroid/tests/unittest_objects.py +++ b/astroid/tests/unittest_objects.py @@ -499,6 +499,24 @@ def __init__(self): with self.assertRaises(exceptions.SuperError): inferred.super_mro() + def test_super_properties(self): + node = test_utils.extract_node(''' + class Foo(object): + @property + def dict(self): + return 42 + + class Bar(Foo): + @property + def dict(self): + return super(Bar, self).dict + + Bar().dict + ''') + inferred = next(node.infer()) + self.assertIsInstance(inferred, nodes.Const) + self.assertEqual(inferred.value, 42) + if __name__ == '__main__': unittest.main() From 99a3870d4ba212a036dd629ad9ee020333009425 Mon Sep 17 00:00:00 2001 From: Dave Baum Date: Thu, 14 Jan 2016 11:14:39 -0600 Subject: [PATCH 175/312] Fix unpack_infer to fail if results are empty This should prevent a StopIteration leaking when next is called over unpack_infer. --- ChangeLog | 3 +++ astroid/interpreter/util.py | 2 ++ astroid/tests/unittest_utils.py | 10 +++++++++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index a73899e67e..3836022a16 100644 --- a/ChangeLog +++ b/ChangeLog @@ -30,6 +30,9 @@ Change log for the astroid package (used to be astng) to avoid a potential problem with encodings when using .format. Closes issue #273. Patch by notsqrt. + * unpack_infer raises InferenceError if it can't operate + with the given sequences of nodes. + * Support accessing properties with super(). * Enforce strong updates per frames. diff --git a/astroid/interpreter/util.py b/astroid/interpreter/util.py index 93ff831be6..c83330b016 100644 --- a/astroid/interpreter/util.py +++ b/astroid/interpreter/util.py @@ -22,6 +22,7 @@ import six from astroid import context as contextmod +from astroid import decorators from astroid import exceptions from astroid.interpreter import runtimeabc from astroid import manager @@ -63,6 +64,7 @@ def infer_stmts(stmts, context, frame=None): raise exceptions.InferenceError(str(stmt)) +@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 diff --git a/astroid/tests/unittest_utils.py b/astroid/tests/unittest_utils.py index 68ddc979f7..81777e376c 100644 --- a/astroid/tests/unittest_utils.py +++ b/astroid/tests/unittest_utils.py @@ -18,6 +18,7 @@ import unittest from astroid import builder +from astroid import InferenceError from astroid import nodes from astroid.interpreter import util from astroid import test_utils @@ -110,7 +111,14 @@ def test_unpack_infer_uninferable_nodes(self): self.assertTrue(all(elt is astroid_util.Uninferable for elt in unpacked)) + def test_unpack_infer_empty_tuple(self): + node = test_utils.extract_node(''' + () + ''') + inferred = next(node.infer()) + with self.assertRaises(InferenceError) as cm: + next(util.unpack_infer(inferred)) + if __name__ == '__main__': unittest.main() - From c83f62df489fb9bb6918de51001e62badfc2f536 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Fri, 15 Jan 2016 11:34:47 -0500 Subject: [PATCH 176/312] Fix typos for visit_withitem in as_string, forward visit_asyncwith correctly in rebuilder --- astroid/as_string.py | 2 +- astroid/tree/rebuilder.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/astroid/as_string.py b/astroid/as_string.py index 7f59f88c08..54cd29acdb 100644 --- a/astroid/as_string.py +++ b/astroid/as_string.py @@ -411,7 +411,7 @@ def visit_with(self, node): # 'with' without 'as' is possible def visit_withitem(self, node): return ('(%s)' % node.context_expr.accept(self) + ' as (%s)' % - (optional_vars.accept(self)) if optional_vars else '') + (node.optional_vars.accept(self)) if node.optional_vars else '') def visit_yield(self, node): """yield an ast.Yield node as string""" diff --git a/astroid/tree/rebuilder.py b/astroid/tree/rebuilder.py index 12b9388ce0..8c6baa9a75 100644 --- a/astroid/tree/rebuilder.py +++ b/astroid/tree/rebuilder.py @@ -829,7 +829,7 @@ def visit_await(self, node, parent): return newnode def visit_asyncwith(self, node, parent): - return self.visit_with(nodes.AsyncWith, node, parent) + return self.visit_with(node, parent) if sys.version_info >= (3, 0): From 86935f0437ad4ab5971353dda5cb46ae9017ed5a Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Fri, 15 Jan 2016 12:52:42 -0500 Subject: [PATCH 177/312] Propagate error information to @raise_if_nothing_inferred in unpack_infer() --- astroid/interpreter/util.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/astroid/interpreter/util.py b/astroid/interpreter/util.py index c83330b016..18e199b88e 100644 --- a/astroid/interpreter/util.py +++ b/astroid/interpreter/util.py @@ -76,12 +76,14 @@ def unpack_infer(stmt, context=None): continue for inferred_elt in unpack_infer(elt, context): yield inferred_elt - return + # 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 - return + raise StopIteration(dict(node=stmt, context=context)) # else, infer recursivly, except Uninferable object that should be returned as is for inferred in stmt.infer(context): if inferred is util.Uninferable: @@ -89,7 +91,7 @@ def unpack_infer(stmt, context=None): 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): """return true if the two given statements are mutually exclusive @@ -321,4 +323,4 @@ def real_name(node, asname): return name raise exceptions.AttributeInferenceError( 'Could not find original name for {attribute} in {target!r}', - target=node, attribute=asname) \ No newline at end of file + target=node, attribute=asname) From 3eb2202dbdbb9e29e0ab066dfa7f7de5e7c47714 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Fri, 15 Jan 2016 13:26:13 -0500 Subject: [PATCH 178/312] Build AsyncWith nodes correctly --- astroid/tree/rebuilder.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/astroid/tree/rebuilder.py b/astroid/tree/rebuilder.py index 8c6baa9a75..90a02f7354 100644 --- a/astroid/tree/rebuilder.py +++ b/astroid/tree/rebuilder.py @@ -792,8 +792,8 @@ def visit_try(self, node, parent): elif node.handlers: return self.visit_tryexcept(node, parent) - def visit_with(self, node, parent): - newnode = nodes.With(node.lineno, node.col_offset, parent) + def visit_with(self, node, parent, constructor=nodes.With): + newnode = constructor(node.lineno, node.col_offset, parent) newnode.postinit([self.visit(item, newnode) for item in node.items], [self.visit(child, newnode) for child in node.body]) return newnode @@ -829,7 +829,7 @@ def visit_await(self, node, parent): return newnode def visit_asyncwith(self, node, parent): - return self.visit_with(node, parent) + return self.visit_with(node, parent, constructor=nodes.AsyncWith) if sys.version_info >= (3, 0): From 5971b00727f50d3d971334f68d03a446acd1650d Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Fri, 15 Jan 2016 15:30:35 -0500 Subject: [PATCH 179/312] Fix typo in as_string.visit_withitem --- astroid/as_string.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/astroid/as_string.py b/astroid/as_string.py index 54cd29acdb..d99aee0aab 100644 --- a/astroid/as_string.py +++ b/astroid/as_string.py @@ -410,8 +410,9 @@ def visit_with(self, node): # 'with' without 'as' is possible return 'with %s:\n%s' % (items, self._stmt_list(node.body)) def visit_withitem(self, node): - return ('(%s)' % node.context_expr.accept(self) + ' as (%s)' % - (node.optional_vars.accept(self)) if node.optional_vars else '') + return ('(%s)' % node.context_expr.accept(self) + + (' as (%s)' % node.optional_vars.accept(self) + if node.optional_vars else '')) def visit_yield(self, node): """yield an ast.Yield node as string""" From c71e16725af822b3b96e0417ad087b33931273d0 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Mon, 18 Jan 2016 17:01:45 -0500 Subject: [PATCH 180/312] Add support for coverage to tox --- tox.ini | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/tox.ini b/tox.ini index 46aad96f93..1a7cb7a232 100644 --- a/tox.ini +++ b/tox.ini @@ -1,12 +1,21 @@ [tox] -envlist = py27, py33, py34, py35, pypy, jython, pylint +envlist = coverage-clean, py27, py33, py34, py35, pypy, jython, pylint, coverage-stats skip_missing_interpreters = true [testenv:pylint] commands = pylint -rn --rcfile={toxinidir}/pylintrc {envsitepackagesdir}/astroid +[testenv:coverage-clean] +commands = coverage erase + +[testenv:coverage-stats] +commands = + coverage report --ignore-errors --include="astroid*" + coverage html --ignore-errors --include="astroid*" + [testenv] deps = + coverage # Temporary hack, ignore this. dictproxyhack py27,py33,pypy,jython: enum34 @@ -19,4 +28,7 @@ deps = six wrapt pylint: git+https://github.com/pycqa/pylint@master -commands = python -Wi -m unittest discover -s {envsitepackagesdir}/astroid/tests -p "unittest*.py" +# Disable warnings because they overflow the Travis log on 3.5 +setenv = PYTHONWARNINGS = i +# {posargs} allows passing command line arguments to unittest +commands = coverage run --append --branch -m unittest discover {posargs} -s {envsitepackagesdir}/astroid/tests -p "unittest*.py" From 72eeb62f5fe54b646cd7f733ed3c62e8fc12327c Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 24 Jan 2016 23:20:42 +0200 Subject: [PATCH 181/312] Clarify why we are sorting locals. --- astroid/interpreter/lookup.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/astroid/interpreter/lookup.py b/astroid/interpreter/lookup.py index d8e5931aa9..b47c11938e 100644 --- a/astroid/interpreter/lookup.py +++ b/astroid/interpreter/lookup.py @@ -287,7 +287,9 @@ def locals_import_from(node, locals_): # Don't add future imports to locals. if node.modname == '__future__': return - # Inherited code, I don't know why this function sorts this list. + + # Sort the list for having the locals ordered by their first + # appearance. def sort_locals(my_list): my_list.sort(key=lambda node: node.fromlineno) From eb48cf362e413e29c0ddaa7c7d0c88a7db0be95a Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 25 Jan 2016 00:11:33 +0200 Subject: [PATCH 182/312] NodeNG.nearest was removed. --- ChangeLog | 3 +++ astroid/tree/base.py | 18 ------------------ 2 files changed, 3 insertions(+), 18 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3836022a16..251986c13d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,9 @@ Change log for the astroid package (used to be astng) -- + * NodeNG.nearest was removed. It's not an API that we were using + and it was buggy. + * Dict nodes are constructed with two arguments, keys and values, in the same way that ast.Dict is built. diff --git a/astroid/tree/base.py b/astroid/tree/base.py index 43f77d121e..755e3b8bb7 100644 --- a/astroid/tree/base.py +++ b/astroid/tree/base.py @@ -226,24 +226,6 @@ 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 From 35ba13cf470ffca09cf97e65ce2c7fa75c0025e5 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 25 Jan 2016 00:24:38 +0200 Subject: [PATCH 183/312] Don't set the fromlineno of parents or children for nodes without it set If a node had a .fromlineno as None, then a function would be invoked which retrieved the first line number of the children or the parents which wasn't none. This is wrong, because it's not a guarantee that all nodes should have it set, especially those coming from raw_building, where it can be close to impossible to determine the line number of an object coming from an extension module. The functionality was removed and certain guards were added in a couple of places in order to protect operations against None. Close #310 and #195 --- astroid/interpreter/lookup.py | 2 +- astroid/tests/unittest_nodes.py | 8 -------- astroid/tests/unittest_regrtest.py | 8 ++++++++ astroid/tree/base.py | 31 ++---------------------------- astroid/tree/node_classes.py | 7 +++++-- 5 files changed, 16 insertions(+), 40 deletions(-) diff --git a/astroid/interpreter/lookup.py b/astroid/interpreter/lookup.py index b47c11938e..8a8eb2a08b 100644 --- a/astroid/interpreter/lookup.py +++ b/astroid/interpreter/lookup.py @@ -291,7 +291,7 @@ def locals_import_from(node, locals_): # Sort the list for having the locals ordered by their first # appearance. def sort_locals(my_list): - my_list.sort(key=lambda node: node.fromlineno) + my_list.sort(key=lambda node: node.fromlineno or 0) for name, asname in node.names: if name == '*': diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index aeab26e27e..fab82eef79 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -509,14 +509,6 @@ def func(a, self.skipTest('FIXME http://bugs.python.org/issue10445 ' '(no line number on function args)') - def test_builtin_fromlineno_missing(self): - cls = test_utils.extract_node(''' - class Foo(Exception): #@ - pass - ''') - new = cls.getattr('__new__')[-1] - self.assertEqual(new.args.fromlineno, 0) - class UnboundMethodNodeTest(unittest.TestCase): diff --git a/astroid/tests/unittest_regrtest.py b/astroid/tests/unittest_regrtest.py index 9d36befcdd..21b93dc838 100644 --- a/astroid/tests/unittest_regrtest.py +++ b/astroid/tests/unittest_regrtest.py @@ -24,6 +24,7 @@ from astroid import MANAGER, Instance, nodes from astroid.builder import AstroidBuilder from astroid import exceptions +from astroid.interpreter import lookup from astroid.manager import AstroidManager from astroid import raw_building from astroid.test_utils import require_version, extract_node, bootstrap @@ -310,6 +311,13 @@ def test_qname_not_on_generatorexp(self): with self.assertRaises(AttributeError): node.qname + def test_null_fromlineno_does_not_crash_lookup(self): + node = nodes.ImportFrom('test', [('a', 'a')]) + function = nodes.FunctionDef() + locals_ = {'a': [function]} + lookup._get_locals(node, locals_) + + class Whatever(object): a = property(lambda x: x, lambda x: x) diff --git a/astroid/tree/base.py b/astroid/tree/base.py index 755e3b8bb7..de1375601f 100644 --- a/astroid/tree/base.py +++ b/astroid/tree/base.py @@ -231,10 +231,7 @@ def previous_sibling(self): @decorators.cachedproperty def fromlineno(self): - if self.lineno is None: - return self._fixed_source_line() - else: - return self.lineno + return self.lineno @decorators.cachedproperty def tolineno(self): @@ -248,29 +245,6 @@ def tolineno(self): else: return lastchild.tolineno - # TODO / FIXME: - assert self.fromlineno is not None, self - assert self.tolineno is not None, self - - 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 """ @@ -592,7 +566,6 @@ def _filter_stmts(self, stmts, frame, offset): # 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 @@ -602,7 +575,7 @@ def _filter_stmts(self, stmts, frame, offset): 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: + if mylineno > 0 and stmt.fromlineno and stmt.fromlineno > mylineno: break assert hasattr(node, 'assign_type'), (node, node.scope(), node.scope().locals) diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index 78a9afc8da..722e2b75ed 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -185,8 +185,11 @@ def _infer_name(self, frame, name): @decorators.cachedproperty def fromlineno(self): - lineno = super(Arguments, self).fromlineno - return max(lineno, self.parent.fromlineno or 0) + # Let the Function's lineno be the lineno for this. + if self.parent.fromlineno: + return self.parent.fromlineno + + return super(Arguments, self).fromlineno def format_args(self): """return arguments formatted as string""" From d403c33f5ae8a42d964d184641611e1d7fcf6339 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 25 Jan 2016 00:38:22 +0200 Subject: [PATCH 184/312] Look in keyword-only annotations when determining the scope of annotations. --- astroid/interpreter/scope.py | 1 + 1 file changed, 1 insertion(+) diff --git a/astroid/interpreter/scope.py b/astroid/interpreter/scope.py index 4cca5c5c56..0d092ce2df 100644 --- a/astroid/interpreter/scope.py +++ b/astroid/interpreter/scope.py @@ -50,6 +50,7 @@ def _scope_by_argument_parent(parent, node): look_for = itertools.chain( (args.kwargannotation, ), (args.varargannotation, ), + args.kwonly_annotations, args.annotations) if node in look_for: return args.parent.parent.scope() From a062dd23e3415a827cf586b10f540f5daa8fb8e6 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Sun, 24 Jan 2016 22:22:14 -0500 Subject: [PATCH 185/312] Move testdata/ to the top level; closes #312 --- astroid/tests/resources.py | 3 ++- .../python2/data/MyPyPa-0.1.0-py2.5.egg | Bin .../python2/data/MyPyPa-0.1.0-py2.5.zip | Bin .../python2/data/SSL1/Connection1.py | 0 .../python2/data/SSL1/__init__.py | 0 .../testdata => testdata}/python2/data/__init__.py | 0 .../python2/data/absimp/__init__.py | 0 .../python2/data/absimp/sidepackage/__init__.py | 0 .../python2/data/absimp/string.py | 0 .../testdata => testdata}/python2/data/absimport.py | 0 .../tests/testdata => testdata}/python2/data/all.py | 0 .../python2/data/appl/__init__.py | 0 .../python2/data/appl/myConnection.py | 0 .../python2/data/descriptor_crash.py | 0 .../testdata => testdata}/python2/data/email.py | 0 .../python2/data/find_test/__init__.py | 0 .../python2/data/find_test/module.py | 0 .../python2/data/find_test/module2.py | 0 .../python2/data/find_test/noendingnewline.py | 0 .../python2/data/find_test/nonregr.py | 0 .../testdata => testdata}/python2/data/format.py | 0 .../python2/data/invalid_encoding.py | 0 .../python2/data/lmfp/__init__.py | 0 .../testdata => testdata}/python2/data/lmfp/foo.py | 0 .../testdata => testdata}/python2/data/module.py | 0 .../python2/data/module1abs/__init__.py | 0 .../python2/data/module1abs/core.py | 0 .../testdata => testdata}/python2/data/module2.py | 0 .../python2/data/noendingnewline.py | 0 .../testdata => testdata}/python2/data/nonregr.py | 0 .../testdata => testdata}/python2/data/notall.py | 0 .../python2/data/notamodule/file.py | 0 .../python2/data/package/__init__.py | 0 .../python2/data/package/absimport.py | 0 .../python2/data/package/hello.py | 0 .../package/import_package_subpackage_module.py | 0 .../python2/data/package/subpackage/__init__.py | 0 .../python2/data/package/subpackage/module.py | 0 .../testdata => testdata}/python2/data/recursion.py | 0 .../python2/data/unicode_package/__init__.py | 0 .../python2/data/unicode_package/core/__init__.py | 0 .../python3/data/MyPyPa-0.1.0-py2.5.egg | Bin .../python3/data/MyPyPa-0.1.0-py2.5.zip | Bin .../python3/data/SSL1/Connection1.py | 0 .../python3/data/SSL1/__init__.py | 0 .../testdata => testdata}/python3/data/__init__.py | 0 .../python3/data/absimp/__init__.py | 0 .../python3/data/absimp/sidepackage/__init__.py | 0 .../python3/data/absimp/string.py | 0 .../testdata => testdata}/python3/data/absimport.py | 0 .../tests/testdata => testdata}/python3/data/all.py | 0 .../python3/data/appl/__init__.py | 0 .../python3/data/appl/myConnection.py | 0 .../python3/data/descriptor_crash.py | 0 .../testdata => testdata}/python3/data/email.py | 0 .../python3/data/find_test/__init__.py | 0 .../python3/data/find_test/module.py | 0 .../python3/data/find_test/module2.py | 0 .../python3/data/find_test/noendingnewline.py | 0 .../python3/data/find_test/nonregr.py | 0 .../testdata => testdata}/python3/data/format.py | 0 .../python3/data/invalid_encoding.py | 0 .../python3/data/lmfp/__init__.py | 0 .../testdata => testdata}/python3/data/lmfp/foo.py | 0 .../testdata => testdata}/python3/data/module.py | 0 .../python3/data/module1abs/__init__.py | 0 .../python3/data/module1abs/core.py | 0 .../testdata => testdata}/python3/data/module2.py | 0 .../python3/data/noendingnewline.py | 0 .../testdata => testdata}/python3/data/nonregr.py | 0 .../testdata => testdata}/python3/data/notall.py | 0 .../python3/data/notamodule/file.py | 0 .../python3/data/package/__init__.py | 0 .../python3/data/package/absimport.py | 0 .../python3/data/package/hello.py | 0 .../package/import_package_subpackage_module.py | 0 .../python3/data/package/subpackage/__init__.py | 0 .../python3/data/package/subpackage/module.py | 0 .../testdata => testdata}/python3/data/recursion.py | 0 .../python3/data/unicode_package/__init__.py | 0 .../python3/data/unicode_package/core/__init__.py | 0 81 files changed, 2 insertions(+), 1 deletion(-) rename {astroid/tests/testdata => testdata}/python2/data/MyPyPa-0.1.0-py2.5.egg (100%) rename {astroid/tests/testdata => testdata}/python2/data/MyPyPa-0.1.0-py2.5.zip (100%) rename {astroid/tests/testdata => testdata}/python2/data/SSL1/Connection1.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/SSL1/__init__.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/__init__.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/absimp/__init__.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/absimp/sidepackage/__init__.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/absimp/string.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/absimport.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/all.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/appl/__init__.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/appl/myConnection.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/descriptor_crash.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/email.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/find_test/__init__.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/find_test/module.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/find_test/module2.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/find_test/noendingnewline.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/find_test/nonregr.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/format.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/invalid_encoding.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/lmfp/__init__.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/lmfp/foo.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/module.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/module1abs/__init__.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/module1abs/core.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/module2.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/noendingnewline.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/nonregr.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/notall.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/notamodule/file.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/package/__init__.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/package/absimport.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/package/hello.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/package/import_package_subpackage_module.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/package/subpackage/__init__.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/package/subpackage/module.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/recursion.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/unicode_package/__init__.py (100%) rename {astroid/tests/testdata => testdata}/python2/data/unicode_package/core/__init__.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/MyPyPa-0.1.0-py2.5.egg (100%) rename {astroid/tests/testdata => testdata}/python3/data/MyPyPa-0.1.0-py2.5.zip (100%) rename {astroid/tests/testdata => testdata}/python3/data/SSL1/Connection1.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/SSL1/__init__.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/__init__.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/absimp/__init__.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/absimp/sidepackage/__init__.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/absimp/string.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/absimport.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/all.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/appl/__init__.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/appl/myConnection.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/descriptor_crash.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/email.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/find_test/__init__.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/find_test/module.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/find_test/module2.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/find_test/noendingnewline.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/find_test/nonregr.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/format.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/invalid_encoding.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/lmfp/__init__.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/lmfp/foo.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/module.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/module1abs/__init__.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/module1abs/core.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/module2.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/noendingnewline.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/nonregr.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/notall.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/notamodule/file.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/package/__init__.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/package/absimport.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/package/hello.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/package/import_package_subpackage_module.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/package/subpackage/__init__.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/package/subpackage/module.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/recursion.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/unicode_package/__init__.py (100%) rename {astroid/tests/testdata => testdata}/python3/data/unicode_package/core/__init__.py (100%) diff --git a/astroid/tests/resources.py b/astroid/tests/resources.py index 6a29b54ef0..175b9d1734 100644 --- a/astroid/tests/resources.py +++ b/astroid/tests/resources.py @@ -30,7 +30,8 @@ def find(name): return pkg_resources.resource_filename( - 'astroid.tests', os.path.normpath(os.path.join(DATA_DIR, name))) + pkg_resources.Requirement.parse('astroid'), + os.path.normpath(os.path.join(DATA_DIR, name))) def build_file(path, modname=None): diff --git a/astroid/tests/testdata/python2/data/MyPyPa-0.1.0-py2.5.egg b/testdata/python2/data/MyPyPa-0.1.0-py2.5.egg similarity index 100% rename from astroid/tests/testdata/python2/data/MyPyPa-0.1.0-py2.5.egg rename to testdata/python2/data/MyPyPa-0.1.0-py2.5.egg diff --git a/astroid/tests/testdata/python2/data/MyPyPa-0.1.0-py2.5.zip b/testdata/python2/data/MyPyPa-0.1.0-py2.5.zip similarity index 100% rename from astroid/tests/testdata/python2/data/MyPyPa-0.1.0-py2.5.zip rename to testdata/python2/data/MyPyPa-0.1.0-py2.5.zip diff --git a/astroid/tests/testdata/python2/data/SSL1/Connection1.py b/testdata/python2/data/SSL1/Connection1.py similarity index 100% rename from astroid/tests/testdata/python2/data/SSL1/Connection1.py rename to testdata/python2/data/SSL1/Connection1.py diff --git a/astroid/tests/testdata/python2/data/SSL1/__init__.py b/testdata/python2/data/SSL1/__init__.py similarity index 100% rename from astroid/tests/testdata/python2/data/SSL1/__init__.py rename to testdata/python2/data/SSL1/__init__.py diff --git a/astroid/tests/testdata/python2/data/__init__.py b/testdata/python2/data/__init__.py similarity index 100% rename from astroid/tests/testdata/python2/data/__init__.py rename to testdata/python2/data/__init__.py diff --git a/astroid/tests/testdata/python2/data/absimp/__init__.py b/testdata/python2/data/absimp/__init__.py similarity index 100% rename from astroid/tests/testdata/python2/data/absimp/__init__.py rename to testdata/python2/data/absimp/__init__.py diff --git a/astroid/tests/testdata/python2/data/absimp/sidepackage/__init__.py b/testdata/python2/data/absimp/sidepackage/__init__.py similarity index 100% rename from astroid/tests/testdata/python2/data/absimp/sidepackage/__init__.py rename to testdata/python2/data/absimp/sidepackage/__init__.py diff --git a/astroid/tests/testdata/python2/data/absimp/string.py b/testdata/python2/data/absimp/string.py similarity index 100% rename from astroid/tests/testdata/python2/data/absimp/string.py rename to testdata/python2/data/absimp/string.py diff --git a/astroid/tests/testdata/python2/data/absimport.py b/testdata/python2/data/absimport.py similarity index 100% rename from astroid/tests/testdata/python2/data/absimport.py rename to testdata/python2/data/absimport.py diff --git a/astroid/tests/testdata/python2/data/all.py b/testdata/python2/data/all.py similarity index 100% rename from astroid/tests/testdata/python2/data/all.py rename to testdata/python2/data/all.py diff --git a/astroid/tests/testdata/python2/data/appl/__init__.py b/testdata/python2/data/appl/__init__.py similarity index 100% rename from astroid/tests/testdata/python2/data/appl/__init__.py rename to testdata/python2/data/appl/__init__.py diff --git a/astroid/tests/testdata/python2/data/appl/myConnection.py b/testdata/python2/data/appl/myConnection.py similarity index 100% rename from astroid/tests/testdata/python2/data/appl/myConnection.py rename to testdata/python2/data/appl/myConnection.py diff --git a/astroid/tests/testdata/python2/data/descriptor_crash.py b/testdata/python2/data/descriptor_crash.py similarity index 100% rename from astroid/tests/testdata/python2/data/descriptor_crash.py rename to testdata/python2/data/descriptor_crash.py diff --git a/astroid/tests/testdata/python2/data/email.py b/testdata/python2/data/email.py similarity index 100% rename from astroid/tests/testdata/python2/data/email.py rename to testdata/python2/data/email.py diff --git a/astroid/tests/testdata/python2/data/find_test/__init__.py b/testdata/python2/data/find_test/__init__.py similarity index 100% rename from astroid/tests/testdata/python2/data/find_test/__init__.py rename to testdata/python2/data/find_test/__init__.py diff --git a/astroid/tests/testdata/python2/data/find_test/module.py b/testdata/python2/data/find_test/module.py similarity index 100% rename from astroid/tests/testdata/python2/data/find_test/module.py rename to testdata/python2/data/find_test/module.py diff --git a/astroid/tests/testdata/python2/data/find_test/module2.py b/testdata/python2/data/find_test/module2.py similarity index 100% rename from astroid/tests/testdata/python2/data/find_test/module2.py rename to testdata/python2/data/find_test/module2.py diff --git a/astroid/tests/testdata/python2/data/find_test/noendingnewline.py b/testdata/python2/data/find_test/noendingnewline.py similarity index 100% rename from astroid/tests/testdata/python2/data/find_test/noendingnewline.py rename to testdata/python2/data/find_test/noendingnewline.py diff --git a/astroid/tests/testdata/python2/data/find_test/nonregr.py b/testdata/python2/data/find_test/nonregr.py similarity index 100% rename from astroid/tests/testdata/python2/data/find_test/nonregr.py rename to testdata/python2/data/find_test/nonregr.py diff --git a/astroid/tests/testdata/python2/data/format.py b/testdata/python2/data/format.py similarity index 100% rename from astroid/tests/testdata/python2/data/format.py rename to testdata/python2/data/format.py diff --git a/astroid/tests/testdata/python2/data/invalid_encoding.py b/testdata/python2/data/invalid_encoding.py similarity index 100% rename from astroid/tests/testdata/python2/data/invalid_encoding.py rename to testdata/python2/data/invalid_encoding.py diff --git a/astroid/tests/testdata/python2/data/lmfp/__init__.py b/testdata/python2/data/lmfp/__init__.py similarity index 100% rename from astroid/tests/testdata/python2/data/lmfp/__init__.py rename to testdata/python2/data/lmfp/__init__.py diff --git a/astroid/tests/testdata/python2/data/lmfp/foo.py b/testdata/python2/data/lmfp/foo.py similarity index 100% rename from astroid/tests/testdata/python2/data/lmfp/foo.py rename to testdata/python2/data/lmfp/foo.py diff --git a/astroid/tests/testdata/python2/data/module.py b/testdata/python2/data/module.py similarity index 100% rename from astroid/tests/testdata/python2/data/module.py rename to testdata/python2/data/module.py diff --git a/astroid/tests/testdata/python2/data/module1abs/__init__.py b/testdata/python2/data/module1abs/__init__.py similarity index 100% rename from astroid/tests/testdata/python2/data/module1abs/__init__.py rename to testdata/python2/data/module1abs/__init__.py diff --git a/astroid/tests/testdata/python2/data/module1abs/core.py b/testdata/python2/data/module1abs/core.py similarity index 100% rename from astroid/tests/testdata/python2/data/module1abs/core.py rename to testdata/python2/data/module1abs/core.py diff --git a/astroid/tests/testdata/python2/data/module2.py b/testdata/python2/data/module2.py similarity index 100% rename from astroid/tests/testdata/python2/data/module2.py rename to testdata/python2/data/module2.py diff --git a/astroid/tests/testdata/python2/data/noendingnewline.py b/testdata/python2/data/noendingnewline.py similarity index 100% rename from astroid/tests/testdata/python2/data/noendingnewline.py rename to testdata/python2/data/noendingnewline.py diff --git a/astroid/tests/testdata/python2/data/nonregr.py b/testdata/python2/data/nonregr.py similarity index 100% rename from astroid/tests/testdata/python2/data/nonregr.py rename to testdata/python2/data/nonregr.py diff --git a/astroid/tests/testdata/python2/data/notall.py b/testdata/python2/data/notall.py similarity index 100% rename from astroid/tests/testdata/python2/data/notall.py rename to testdata/python2/data/notall.py diff --git a/astroid/tests/testdata/python2/data/notamodule/file.py b/testdata/python2/data/notamodule/file.py similarity index 100% rename from astroid/tests/testdata/python2/data/notamodule/file.py rename to testdata/python2/data/notamodule/file.py diff --git a/astroid/tests/testdata/python2/data/package/__init__.py b/testdata/python2/data/package/__init__.py similarity index 100% rename from astroid/tests/testdata/python2/data/package/__init__.py rename to testdata/python2/data/package/__init__.py diff --git a/astroid/tests/testdata/python2/data/package/absimport.py b/testdata/python2/data/package/absimport.py similarity index 100% rename from astroid/tests/testdata/python2/data/package/absimport.py rename to testdata/python2/data/package/absimport.py diff --git a/astroid/tests/testdata/python2/data/package/hello.py b/testdata/python2/data/package/hello.py similarity index 100% rename from astroid/tests/testdata/python2/data/package/hello.py rename to testdata/python2/data/package/hello.py diff --git a/astroid/tests/testdata/python2/data/package/import_package_subpackage_module.py b/testdata/python2/data/package/import_package_subpackage_module.py similarity index 100% rename from astroid/tests/testdata/python2/data/package/import_package_subpackage_module.py rename to testdata/python2/data/package/import_package_subpackage_module.py diff --git a/astroid/tests/testdata/python2/data/package/subpackage/__init__.py b/testdata/python2/data/package/subpackage/__init__.py similarity index 100% rename from astroid/tests/testdata/python2/data/package/subpackage/__init__.py rename to testdata/python2/data/package/subpackage/__init__.py diff --git a/astroid/tests/testdata/python2/data/package/subpackage/module.py b/testdata/python2/data/package/subpackage/module.py similarity index 100% rename from astroid/tests/testdata/python2/data/package/subpackage/module.py rename to testdata/python2/data/package/subpackage/module.py diff --git a/astroid/tests/testdata/python2/data/recursion.py b/testdata/python2/data/recursion.py similarity index 100% rename from astroid/tests/testdata/python2/data/recursion.py rename to testdata/python2/data/recursion.py diff --git a/astroid/tests/testdata/python2/data/unicode_package/__init__.py b/testdata/python2/data/unicode_package/__init__.py similarity index 100% rename from astroid/tests/testdata/python2/data/unicode_package/__init__.py rename to testdata/python2/data/unicode_package/__init__.py diff --git a/astroid/tests/testdata/python2/data/unicode_package/core/__init__.py b/testdata/python2/data/unicode_package/core/__init__.py similarity index 100% rename from astroid/tests/testdata/python2/data/unicode_package/core/__init__.py rename to testdata/python2/data/unicode_package/core/__init__.py diff --git a/astroid/tests/testdata/python3/data/MyPyPa-0.1.0-py2.5.egg b/testdata/python3/data/MyPyPa-0.1.0-py2.5.egg similarity index 100% rename from astroid/tests/testdata/python3/data/MyPyPa-0.1.0-py2.5.egg rename to testdata/python3/data/MyPyPa-0.1.0-py2.5.egg diff --git a/astroid/tests/testdata/python3/data/MyPyPa-0.1.0-py2.5.zip b/testdata/python3/data/MyPyPa-0.1.0-py2.5.zip similarity index 100% rename from astroid/tests/testdata/python3/data/MyPyPa-0.1.0-py2.5.zip rename to testdata/python3/data/MyPyPa-0.1.0-py2.5.zip diff --git a/astroid/tests/testdata/python3/data/SSL1/Connection1.py b/testdata/python3/data/SSL1/Connection1.py similarity index 100% rename from astroid/tests/testdata/python3/data/SSL1/Connection1.py rename to testdata/python3/data/SSL1/Connection1.py diff --git a/astroid/tests/testdata/python3/data/SSL1/__init__.py b/testdata/python3/data/SSL1/__init__.py similarity index 100% rename from astroid/tests/testdata/python3/data/SSL1/__init__.py rename to testdata/python3/data/SSL1/__init__.py diff --git a/astroid/tests/testdata/python3/data/__init__.py b/testdata/python3/data/__init__.py similarity index 100% rename from astroid/tests/testdata/python3/data/__init__.py rename to testdata/python3/data/__init__.py diff --git a/astroid/tests/testdata/python3/data/absimp/__init__.py b/testdata/python3/data/absimp/__init__.py similarity index 100% rename from astroid/tests/testdata/python3/data/absimp/__init__.py rename to testdata/python3/data/absimp/__init__.py diff --git a/astroid/tests/testdata/python3/data/absimp/sidepackage/__init__.py b/testdata/python3/data/absimp/sidepackage/__init__.py similarity index 100% rename from astroid/tests/testdata/python3/data/absimp/sidepackage/__init__.py rename to testdata/python3/data/absimp/sidepackage/__init__.py diff --git a/astroid/tests/testdata/python3/data/absimp/string.py b/testdata/python3/data/absimp/string.py similarity index 100% rename from astroid/tests/testdata/python3/data/absimp/string.py rename to testdata/python3/data/absimp/string.py diff --git a/astroid/tests/testdata/python3/data/absimport.py b/testdata/python3/data/absimport.py similarity index 100% rename from astroid/tests/testdata/python3/data/absimport.py rename to testdata/python3/data/absimport.py diff --git a/astroid/tests/testdata/python3/data/all.py b/testdata/python3/data/all.py similarity index 100% rename from astroid/tests/testdata/python3/data/all.py rename to testdata/python3/data/all.py diff --git a/astroid/tests/testdata/python3/data/appl/__init__.py b/testdata/python3/data/appl/__init__.py similarity index 100% rename from astroid/tests/testdata/python3/data/appl/__init__.py rename to testdata/python3/data/appl/__init__.py diff --git a/astroid/tests/testdata/python3/data/appl/myConnection.py b/testdata/python3/data/appl/myConnection.py similarity index 100% rename from astroid/tests/testdata/python3/data/appl/myConnection.py rename to testdata/python3/data/appl/myConnection.py diff --git a/astroid/tests/testdata/python3/data/descriptor_crash.py b/testdata/python3/data/descriptor_crash.py similarity index 100% rename from astroid/tests/testdata/python3/data/descriptor_crash.py rename to testdata/python3/data/descriptor_crash.py diff --git a/astroid/tests/testdata/python3/data/email.py b/testdata/python3/data/email.py similarity index 100% rename from astroid/tests/testdata/python3/data/email.py rename to testdata/python3/data/email.py diff --git a/astroid/tests/testdata/python3/data/find_test/__init__.py b/testdata/python3/data/find_test/__init__.py similarity index 100% rename from astroid/tests/testdata/python3/data/find_test/__init__.py rename to testdata/python3/data/find_test/__init__.py diff --git a/astroid/tests/testdata/python3/data/find_test/module.py b/testdata/python3/data/find_test/module.py similarity index 100% rename from astroid/tests/testdata/python3/data/find_test/module.py rename to testdata/python3/data/find_test/module.py diff --git a/astroid/tests/testdata/python3/data/find_test/module2.py b/testdata/python3/data/find_test/module2.py similarity index 100% rename from astroid/tests/testdata/python3/data/find_test/module2.py rename to testdata/python3/data/find_test/module2.py diff --git a/astroid/tests/testdata/python3/data/find_test/noendingnewline.py b/testdata/python3/data/find_test/noendingnewline.py similarity index 100% rename from astroid/tests/testdata/python3/data/find_test/noendingnewline.py rename to testdata/python3/data/find_test/noendingnewline.py diff --git a/astroid/tests/testdata/python3/data/find_test/nonregr.py b/testdata/python3/data/find_test/nonregr.py similarity index 100% rename from astroid/tests/testdata/python3/data/find_test/nonregr.py rename to testdata/python3/data/find_test/nonregr.py diff --git a/astroid/tests/testdata/python3/data/format.py b/testdata/python3/data/format.py similarity index 100% rename from astroid/tests/testdata/python3/data/format.py rename to testdata/python3/data/format.py diff --git a/astroid/tests/testdata/python3/data/invalid_encoding.py b/testdata/python3/data/invalid_encoding.py similarity index 100% rename from astroid/tests/testdata/python3/data/invalid_encoding.py rename to testdata/python3/data/invalid_encoding.py diff --git a/astroid/tests/testdata/python3/data/lmfp/__init__.py b/testdata/python3/data/lmfp/__init__.py similarity index 100% rename from astroid/tests/testdata/python3/data/lmfp/__init__.py rename to testdata/python3/data/lmfp/__init__.py diff --git a/astroid/tests/testdata/python3/data/lmfp/foo.py b/testdata/python3/data/lmfp/foo.py similarity index 100% rename from astroid/tests/testdata/python3/data/lmfp/foo.py rename to testdata/python3/data/lmfp/foo.py diff --git a/astroid/tests/testdata/python3/data/module.py b/testdata/python3/data/module.py similarity index 100% rename from astroid/tests/testdata/python3/data/module.py rename to testdata/python3/data/module.py diff --git a/astroid/tests/testdata/python3/data/module1abs/__init__.py b/testdata/python3/data/module1abs/__init__.py similarity index 100% rename from astroid/tests/testdata/python3/data/module1abs/__init__.py rename to testdata/python3/data/module1abs/__init__.py diff --git a/astroid/tests/testdata/python3/data/module1abs/core.py b/testdata/python3/data/module1abs/core.py similarity index 100% rename from astroid/tests/testdata/python3/data/module1abs/core.py rename to testdata/python3/data/module1abs/core.py diff --git a/astroid/tests/testdata/python3/data/module2.py b/testdata/python3/data/module2.py similarity index 100% rename from astroid/tests/testdata/python3/data/module2.py rename to testdata/python3/data/module2.py diff --git a/astroid/tests/testdata/python3/data/noendingnewline.py b/testdata/python3/data/noendingnewline.py similarity index 100% rename from astroid/tests/testdata/python3/data/noendingnewline.py rename to testdata/python3/data/noendingnewline.py diff --git a/astroid/tests/testdata/python3/data/nonregr.py b/testdata/python3/data/nonregr.py similarity index 100% rename from astroid/tests/testdata/python3/data/nonregr.py rename to testdata/python3/data/nonregr.py diff --git a/astroid/tests/testdata/python3/data/notall.py b/testdata/python3/data/notall.py similarity index 100% rename from astroid/tests/testdata/python3/data/notall.py rename to testdata/python3/data/notall.py diff --git a/astroid/tests/testdata/python3/data/notamodule/file.py b/testdata/python3/data/notamodule/file.py similarity index 100% rename from astroid/tests/testdata/python3/data/notamodule/file.py rename to testdata/python3/data/notamodule/file.py diff --git a/astroid/tests/testdata/python3/data/package/__init__.py b/testdata/python3/data/package/__init__.py similarity index 100% rename from astroid/tests/testdata/python3/data/package/__init__.py rename to testdata/python3/data/package/__init__.py diff --git a/astroid/tests/testdata/python3/data/package/absimport.py b/testdata/python3/data/package/absimport.py similarity index 100% rename from astroid/tests/testdata/python3/data/package/absimport.py rename to testdata/python3/data/package/absimport.py diff --git a/astroid/tests/testdata/python3/data/package/hello.py b/testdata/python3/data/package/hello.py similarity index 100% rename from astroid/tests/testdata/python3/data/package/hello.py rename to testdata/python3/data/package/hello.py diff --git a/astroid/tests/testdata/python3/data/package/import_package_subpackage_module.py b/testdata/python3/data/package/import_package_subpackage_module.py similarity index 100% rename from astroid/tests/testdata/python3/data/package/import_package_subpackage_module.py rename to testdata/python3/data/package/import_package_subpackage_module.py diff --git a/astroid/tests/testdata/python3/data/package/subpackage/__init__.py b/testdata/python3/data/package/subpackage/__init__.py similarity index 100% rename from astroid/tests/testdata/python3/data/package/subpackage/__init__.py rename to testdata/python3/data/package/subpackage/__init__.py diff --git a/astroid/tests/testdata/python3/data/package/subpackage/module.py b/testdata/python3/data/package/subpackage/module.py similarity index 100% rename from astroid/tests/testdata/python3/data/package/subpackage/module.py rename to testdata/python3/data/package/subpackage/module.py diff --git a/astroid/tests/testdata/python3/data/recursion.py b/testdata/python3/data/recursion.py similarity index 100% rename from astroid/tests/testdata/python3/data/recursion.py rename to testdata/python3/data/recursion.py diff --git a/astroid/tests/testdata/python3/data/unicode_package/__init__.py b/testdata/python3/data/unicode_package/__init__.py similarity index 100% rename from astroid/tests/testdata/python3/data/unicode_package/__init__.py rename to testdata/python3/data/unicode_package/__init__.py diff --git a/astroid/tests/testdata/python3/data/unicode_package/core/__init__.py b/testdata/python3/data/unicode_package/core/__init__.py similarity index 100% rename from astroid/tests/testdata/python3/data/unicode_package/core/__init__.py rename to testdata/python3/data/unicode_package/core/__init__.py From 682b87f5b9832bb55d35d1c3a589ba8695495481 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Mon, 25 Jan 2016 18:32:38 -0500 Subject: [PATCH 186/312] Try to fix the testdata issue --- MANIFEST.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MANIFEST.in b/MANIFEST.in index 4837adf108..bce4565033 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -3,5 +3,5 @@ include README include COPYING include COPYING.LESSER include tox.ini -recursive-include astroid/tests/testdata *.py *.zip *.egg +recursive-include testdata *.py *.zip *.egg recursive-include astroid/brain *.py From aa7f1ea24e9c96764ca745e643c85de0f4a071f8 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Mon, 25 Jan 2016 20:34:56 -0500 Subject: [PATCH 187/312] Try rewriting MANIFEST.in --- MANIFEST.in | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MANIFEST.in b/MANIFEST.in index bce4565033..2108e789cc 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,7 +1,7 @@ include ChangeLog -include README +include README.rst include COPYING include COPYING.LESSER include tox.ini recursive-include testdata *.py *.zip *.egg -recursive-include astroid/brain *.py + From 18fa724c04c2393b134d57d4fe4cebe38472bad8 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Thu, 28 Jan 2016 12:31:39 -0500 Subject: [PATCH 188/312] Try graft instead of recursive-include --- MANIFEST.in | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/MANIFEST.in b/MANIFEST.in index 2108e789cc..26bd65712a 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,7 +1,6 @@ +graft testdata include ChangeLog include README.rst include COPYING include COPYING.LESSER include tox.ini -recursive-include testdata *.py *.zip *.egg - From c510c17f63c2df01b8c9d5e2fd97ac963f737c7f Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Fri, 5 Feb 2016 12:10:19 -0500 Subject: [PATCH 189/312] setup.py install now installs testdata/, but pip doesn't --- setup.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 2150d1c26e..4b2a48eb59 100644 --- a/setup.py +++ b/setup.py @@ -65,7 +65,9 @@ def install(): install_requires = install_requires, packages = find_packages(), cmdclass={'install_lib': AstroidInstallLib, - 'easy_install': AstroidEasyInstallLib} + 'easy_install': AstroidEasyInstallLib}, + data_files=[(p, [os.path.join(p, n) for n in ns]) for p, _, ns in os.walk('testdata/')] + # [('testdata', [os.path.join(p, n) for p, _, ns in os.walk('testdata/') for n in ns])] ) From d37b81de4f1e64abc2f222c487785d816ab469ea Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Mon, 8 Feb 2016 15:32:46 -0500 Subject: [PATCH 190/312] Try to solve the packaging issues again --- MANIFEST.in | 2 +- astroid/{ => astroid}/__init__.py | 0 astroid/{ => astroid}/as_string.py | 0 .../{ => astroid}/brain/brain_builtin_inference.py | 0 astroid/{ => astroid}/brain/brain_dateutil.py | 0 astroid/{ => astroid}/brain/brain_gi.py | 0 astroid/{ => astroid}/brain/brain_mechanize.py | 0 astroid/{ => astroid}/brain/brain_nose.py | 0 astroid/{ => astroid}/brain/brain_numpy.py | 0 astroid/{ => astroid}/brain/brain_pytest.py | 0 astroid/{ => astroid}/brain/brain_qt.py | 0 astroid/{ => astroid}/brain/brain_six.py | 0 astroid/{ => astroid}/brain/brain_stdlib.py | 0 astroid/{ => astroid}/builder.py | 0 astroid/{ => astroid}/context.py | 0 astroid/{ => astroid}/decorators.py | 0 astroid/{ => astroid}/exceptions.py | 0 astroid/{ => astroid}/inference.py | 0 astroid/{ => astroid}/interpreter/__init__.py | 0 astroid/{ => astroid}/interpreter/lookup.py | 0 astroid/{ => astroid}/interpreter/objects.py | 0 astroid/{ => astroid}/interpreter/runtimeabc.py | 0 astroid/{ => astroid}/interpreter/scope.py | 0 astroid/{ => astroid}/interpreter/util.py | 0 astroid/{ => astroid}/manager.py | 0 astroid/{ => astroid}/modutils.py | 0 astroid/{ => astroid}/nodes.py | 0 astroid/{ => astroid}/protocols.py | 0 astroid/{ => astroid}/raw_building.py | 0 astroid/{ => astroid}/test_utils.py | 0 astroid/{ => astroid}/tests/__init__.py | 0 astroid/{ => astroid}/tests/resources.py | 2 +- astroid/{ => astroid}/tests/unittest_brain.py | 0 astroid/{ => astroid}/tests/unittest_builder.py | 0 astroid/{ => astroid}/tests/unittest_helpers.py | 0 astroid/{ => astroid}/tests/unittest_inference.py | 0 astroid/{ => astroid}/tests/unittest_lookup.py | 0 astroid/{ => astroid}/tests/unittest_manager.py | 0 astroid/{ => astroid}/tests/unittest_modutils.py | 0 astroid/{ => astroid}/tests/unittest_nodes.py | 0 astroid/{ => astroid}/tests/unittest_objects.py | 0 astroid/{ => astroid}/tests/unittest_protocols.py | 0 astroid/{ => astroid}/tests/unittest_python3.py | 0 .../{ => astroid}/tests/unittest_raw_building.py | 0 astroid/{ => astroid}/tests/unittest_regrtest.py | 0 .../{ => astroid}/tests/unittest_scoped_nodes.py | 0 astroid/{ => astroid}/tests/unittest_transforms.py | 0 astroid/{ => astroid}/tests/unittest_utils.py | 0 astroid/{ => astroid}/transforms.py | 0 astroid/{ => astroid}/tree/__init__.py | 0 astroid/{ => astroid}/tree/base.py | 0 astroid/{ => astroid}/tree/node_classes.py | 0 astroid/{ => astroid}/tree/rebuilder.py | 0 astroid/{ => astroid}/tree/scoped_nodes.py | 0 astroid/{ => astroid}/tree/treeabc.py | 0 astroid/{ => astroid}/util.py | 0 .../testdata}/python2/data/MyPyPa-0.1.0-py2.5.egg | Bin .../testdata}/python2/data/MyPyPa-0.1.0-py2.5.zip | Bin .../testdata}/python2/data/SSL1/Connection1.py | 0 .../testdata}/python2/data/SSL1/__init__.py | 0 .../testdata}/python2/data/__init__.py | 0 .../testdata}/python2/data/absimp/__init__.py | 0 .../python2/data/absimp/sidepackage/__init__.py | 0 .../testdata}/python2/data/absimp/string.py | 0 .../testdata}/python2/data/absimport.py | 0 {testdata => astroid/testdata}/python2/data/all.py | 0 .../testdata}/python2/data/appl/__init__.py | 0 .../testdata}/python2/data/appl/myConnection.py | 0 .../testdata}/python2/data/descriptor_crash.py | 0 .../testdata}/python2/data/email.py | 0 .../testdata}/python2/data/find_test/__init__.py | 0 .../testdata}/python2/data/find_test/module.py | 0 .../testdata}/python2/data/find_test/module2.py | 0 .../python2/data/find_test/noendingnewline.py | 0 .../testdata}/python2/data/find_test/nonregr.py | 0 .../testdata}/python2/data/format.py | 0 .../testdata}/python2/data/invalid_encoding.py | 0 .../testdata}/python2/data/lmfp/__init__.py | 0 .../testdata}/python2/data/lmfp/foo.py | 0 .../testdata}/python2/data/module.py | 0 .../testdata}/python2/data/module1abs/__init__.py | 0 .../testdata}/python2/data/module1abs/core.py | 0 .../testdata}/python2/data/module2.py | 0 .../testdata}/python2/data/noendingnewline.py | 0 .../testdata}/python2/data/nonregr.py | 0 .../testdata}/python2/data/notall.py | 0 .../testdata}/python2/data/notamodule/file.py | 0 .../testdata}/python2/data/package/__init__.py | 0 .../testdata}/python2/data/package/absimport.py | 0 .../testdata}/python2/data/package/hello.py | 0 .../package/import_package_subpackage_module.py | 0 .../python2/data/package/subpackage/__init__.py | 0 .../python2/data/package/subpackage/module.py | 0 .../testdata}/python2/data/recursion.py | 0 .../python2/data/unicode_package/__init__.py | 0 .../python2/data/unicode_package/core/__init__.py | 0 .../testdata}/python3/data/MyPyPa-0.1.0-py2.5.egg | Bin .../testdata}/python3/data/MyPyPa-0.1.0-py2.5.zip | Bin .../testdata}/python3/data/SSL1/Connection1.py | 0 .../testdata}/python3/data/SSL1/__init__.py | 0 .../testdata}/python3/data/__init__.py | 0 .../testdata}/python3/data/absimp/__init__.py | 0 .../python3/data/absimp/sidepackage/__init__.py | 0 .../testdata}/python3/data/absimp/string.py | 0 .../testdata}/python3/data/absimport.py | 0 {testdata => astroid/testdata}/python3/data/all.py | 0 .../testdata}/python3/data/appl/__init__.py | 0 .../testdata}/python3/data/appl/myConnection.py | 0 .../testdata}/python3/data/descriptor_crash.py | 0 .../testdata}/python3/data/email.py | 0 .../testdata}/python3/data/find_test/__init__.py | 0 .../testdata}/python3/data/find_test/module.py | 0 .../testdata}/python3/data/find_test/module2.py | 0 .../python3/data/find_test/noendingnewline.py | 0 .../testdata}/python3/data/find_test/nonregr.py | 0 .../testdata}/python3/data/format.py | 0 .../testdata}/python3/data/invalid_encoding.py | 0 .../testdata}/python3/data/lmfp/__init__.py | 0 .../testdata}/python3/data/lmfp/foo.py | 0 .../testdata}/python3/data/module.py | 0 .../testdata}/python3/data/module1abs/__init__.py | 0 .../testdata}/python3/data/module1abs/core.py | 0 .../testdata}/python3/data/module2.py | 0 .../testdata}/python3/data/noendingnewline.py | 0 .../testdata}/python3/data/nonregr.py | 0 .../testdata}/python3/data/notall.py | 0 .../testdata}/python3/data/notamodule/file.py | 0 .../testdata}/python3/data/package/__init__.py | 0 .../testdata}/python3/data/package/absimport.py | 0 .../testdata}/python3/data/package/hello.py | 0 .../package/import_package_subpackage_module.py | 0 .../python3/data/package/subpackage/__init__.py | 0 .../python3/data/package/subpackage/module.py | 0 .../testdata}/python3/data/recursion.py | 0 .../python3/data/unicode_package/__init__.py | 0 .../python3/data/unicode_package/core/__init__.py | 0 setup.py | 6 ++---- tox.ini | 7 ++++--- 138 files changed, 8 insertions(+), 9 deletions(-) rename astroid/{ => astroid}/__init__.py (100%) rename astroid/{ => astroid}/as_string.py (100%) rename astroid/{ => astroid}/brain/brain_builtin_inference.py (100%) rename astroid/{ => astroid}/brain/brain_dateutil.py (100%) rename astroid/{ => astroid}/brain/brain_gi.py (100%) rename astroid/{ => astroid}/brain/brain_mechanize.py (100%) rename astroid/{ => astroid}/brain/brain_nose.py (100%) rename astroid/{ => astroid}/brain/brain_numpy.py (100%) rename astroid/{ => astroid}/brain/brain_pytest.py (100%) rename astroid/{ => astroid}/brain/brain_qt.py (100%) rename astroid/{ => astroid}/brain/brain_six.py (100%) rename astroid/{ => astroid}/brain/brain_stdlib.py (100%) rename astroid/{ => astroid}/builder.py (100%) rename astroid/{ => astroid}/context.py (100%) rename astroid/{ => astroid}/decorators.py (100%) rename astroid/{ => astroid}/exceptions.py (100%) rename astroid/{ => astroid}/inference.py (100%) rename astroid/{ => astroid}/interpreter/__init__.py (100%) rename astroid/{ => astroid}/interpreter/lookup.py (100%) rename astroid/{ => astroid}/interpreter/objects.py (100%) rename astroid/{ => astroid}/interpreter/runtimeabc.py (100%) rename astroid/{ => astroid}/interpreter/scope.py (100%) rename astroid/{ => astroid}/interpreter/util.py (100%) rename astroid/{ => astroid}/manager.py (100%) rename astroid/{ => astroid}/modutils.py (100%) rename astroid/{ => astroid}/nodes.py (100%) rename astroid/{ => astroid}/protocols.py (100%) rename astroid/{ => astroid}/raw_building.py (100%) rename astroid/{ => astroid}/test_utils.py (100%) rename astroid/{ => astroid}/tests/__init__.py (100%) rename astroid/{ => astroid}/tests/resources.py (97%) rename astroid/{ => astroid}/tests/unittest_brain.py (100%) rename astroid/{ => astroid}/tests/unittest_builder.py (100%) rename astroid/{ => astroid}/tests/unittest_helpers.py (100%) rename astroid/{ => astroid}/tests/unittest_inference.py (100%) rename astroid/{ => astroid}/tests/unittest_lookup.py (100%) rename astroid/{ => astroid}/tests/unittest_manager.py (100%) rename astroid/{ => astroid}/tests/unittest_modutils.py (100%) rename astroid/{ => astroid}/tests/unittest_nodes.py (100%) rename astroid/{ => astroid}/tests/unittest_objects.py (100%) rename astroid/{ => astroid}/tests/unittest_protocols.py (100%) rename astroid/{ => astroid}/tests/unittest_python3.py (100%) rename astroid/{ => astroid}/tests/unittest_raw_building.py (100%) rename astroid/{ => astroid}/tests/unittest_regrtest.py (100%) rename astroid/{ => astroid}/tests/unittest_scoped_nodes.py (100%) rename astroid/{ => astroid}/tests/unittest_transforms.py (100%) rename astroid/{ => astroid}/tests/unittest_utils.py (100%) rename astroid/{ => astroid}/transforms.py (100%) rename astroid/{ => astroid}/tree/__init__.py (100%) rename astroid/{ => astroid}/tree/base.py (100%) rename astroid/{ => astroid}/tree/node_classes.py (100%) rename astroid/{ => astroid}/tree/rebuilder.py (100%) rename astroid/{ => astroid}/tree/scoped_nodes.py (100%) rename astroid/{ => astroid}/tree/treeabc.py (100%) rename astroid/{ => astroid}/util.py (100%) rename {testdata => astroid/testdata}/python2/data/MyPyPa-0.1.0-py2.5.egg (100%) rename {testdata => astroid/testdata}/python2/data/MyPyPa-0.1.0-py2.5.zip (100%) rename {testdata => astroid/testdata}/python2/data/SSL1/Connection1.py (100%) rename {testdata => astroid/testdata}/python2/data/SSL1/__init__.py (100%) rename {testdata => astroid/testdata}/python2/data/__init__.py (100%) rename {testdata => astroid/testdata}/python2/data/absimp/__init__.py (100%) rename {testdata => astroid/testdata}/python2/data/absimp/sidepackage/__init__.py (100%) rename {testdata => astroid/testdata}/python2/data/absimp/string.py (100%) rename {testdata => astroid/testdata}/python2/data/absimport.py (100%) rename {testdata => astroid/testdata}/python2/data/all.py (100%) rename {testdata => astroid/testdata}/python2/data/appl/__init__.py (100%) rename {testdata => astroid/testdata}/python2/data/appl/myConnection.py (100%) rename {testdata => astroid/testdata}/python2/data/descriptor_crash.py (100%) rename {testdata => astroid/testdata}/python2/data/email.py (100%) rename {testdata => astroid/testdata}/python2/data/find_test/__init__.py (100%) rename {testdata => astroid/testdata}/python2/data/find_test/module.py (100%) rename {testdata => astroid/testdata}/python2/data/find_test/module2.py (100%) rename {testdata => astroid/testdata}/python2/data/find_test/noendingnewline.py (100%) rename {testdata => astroid/testdata}/python2/data/find_test/nonregr.py (100%) rename {testdata => astroid/testdata}/python2/data/format.py (100%) rename {testdata => astroid/testdata}/python2/data/invalid_encoding.py (100%) rename {testdata => astroid/testdata}/python2/data/lmfp/__init__.py (100%) rename {testdata => astroid/testdata}/python2/data/lmfp/foo.py (100%) rename {testdata => astroid/testdata}/python2/data/module.py (100%) rename {testdata => astroid/testdata}/python2/data/module1abs/__init__.py (100%) rename {testdata => astroid/testdata}/python2/data/module1abs/core.py (100%) rename {testdata => astroid/testdata}/python2/data/module2.py (100%) rename {testdata => astroid/testdata}/python2/data/noendingnewline.py (100%) rename {testdata => astroid/testdata}/python2/data/nonregr.py (100%) rename {testdata => astroid/testdata}/python2/data/notall.py (100%) rename {testdata => astroid/testdata}/python2/data/notamodule/file.py (100%) rename {testdata => astroid/testdata}/python2/data/package/__init__.py (100%) rename {testdata => astroid/testdata}/python2/data/package/absimport.py (100%) rename {testdata => astroid/testdata}/python2/data/package/hello.py (100%) rename {testdata => astroid/testdata}/python2/data/package/import_package_subpackage_module.py (100%) rename {testdata => astroid/testdata}/python2/data/package/subpackage/__init__.py (100%) rename {testdata => astroid/testdata}/python2/data/package/subpackage/module.py (100%) rename {testdata => astroid/testdata}/python2/data/recursion.py (100%) rename {testdata => astroid/testdata}/python2/data/unicode_package/__init__.py (100%) rename {testdata => astroid/testdata}/python2/data/unicode_package/core/__init__.py (100%) rename {testdata => astroid/testdata}/python3/data/MyPyPa-0.1.0-py2.5.egg (100%) rename {testdata => astroid/testdata}/python3/data/MyPyPa-0.1.0-py2.5.zip (100%) rename {testdata => astroid/testdata}/python3/data/SSL1/Connection1.py (100%) rename {testdata => astroid/testdata}/python3/data/SSL1/__init__.py (100%) rename {testdata => astroid/testdata}/python3/data/__init__.py (100%) rename {testdata => astroid/testdata}/python3/data/absimp/__init__.py (100%) rename {testdata => astroid/testdata}/python3/data/absimp/sidepackage/__init__.py (100%) rename {testdata => astroid/testdata}/python3/data/absimp/string.py (100%) rename {testdata => astroid/testdata}/python3/data/absimport.py (100%) rename {testdata => astroid/testdata}/python3/data/all.py (100%) rename {testdata => astroid/testdata}/python3/data/appl/__init__.py (100%) rename {testdata => astroid/testdata}/python3/data/appl/myConnection.py (100%) rename {testdata => astroid/testdata}/python3/data/descriptor_crash.py (100%) rename {testdata => astroid/testdata}/python3/data/email.py (100%) rename {testdata => astroid/testdata}/python3/data/find_test/__init__.py (100%) rename {testdata => astroid/testdata}/python3/data/find_test/module.py (100%) rename {testdata => astroid/testdata}/python3/data/find_test/module2.py (100%) rename {testdata => astroid/testdata}/python3/data/find_test/noendingnewline.py (100%) rename {testdata => astroid/testdata}/python3/data/find_test/nonregr.py (100%) rename {testdata => astroid/testdata}/python3/data/format.py (100%) rename {testdata => astroid/testdata}/python3/data/invalid_encoding.py (100%) rename {testdata => astroid/testdata}/python3/data/lmfp/__init__.py (100%) rename {testdata => astroid/testdata}/python3/data/lmfp/foo.py (100%) rename {testdata => astroid/testdata}/python3/data/module.py (100%) rename {testdata => astroid/testdata}/python3/data/module1abs/__init__.py (100%) rename {testdata => astroid/testdata}/python3/data/module1abs/core.py (100%) rename {testdata => astroid/testdata}/python3/data/module2.py (100%) rename {testdata => astroid/testdata}/python3/data/noendingnewline.py (100%) rename {testdata => astroid/testdata}/python3/data/nonregr.py (100%) rename {testdata => astroid/testdata}/python3/data/notall.py (100%) rename {testdata => astroid/testdata}/python3/data/notamodule/file.py (100%) rename {testdata => astroid/testdata}/python3/data/package/__init__.py (100%) rename {testdata => astroid/testdata}/python3/data/package/absimport.py (100%) rename {testdata => astroid/testdata}/python3/data/package/hello.py (100%) rename {testdata => astroid/testdata}/python3/data/package/import_package_subpackage_module.py (100%) rename {testdata => astroid/testdata}/python3/data/package/subpackage/__init__.py (100%) rename {testdata => astroid/testdata}/python3/data/package/subpackage/module.py (100%) rename {testdata => astroid/testdata}/python3/data/recursion.py (100%) rename {testdata => astroid/testdata}/python3/data/unicode_package/__init__.py (100%) rename {testdata => astroid/testdata}/python3/data/unicode_package/core/__init__.py (100%) diff --git a/MANIFEST.in b/MANIFEST.in index 26bd65712a..1c177eb8cb 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,4 +1,4 @@ -graft testdata +graft astroid include ChangeLog include README.rst include COPYING diff --git a/astroid/__init__.py b/astroid/astroid/__init__.py similarity index 100% rename from astroid/__init__.py rename to astroid/astroid/__init__.py diff --git a/astroid/as_string.py b/astroid/astroid/as_string.py similarity index 100% rename from astroid/as_string.py rename to astroid/astroid/as_string.py diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/astroid/brain/brain_builtin_inference.py similarity index 100% rename from astroid/brain/brain_builtin_inference.py rename to astroid/astroid/brain/brain_builtin_inference.py diff --git a/astroid/brain/brain_dateutil.py b/astroid/astroid/brain/brain_dateutil.py similarity index 100% rename from astroid/brain/brain_dateutil.py rename to astroid/astroid/brain/brain_dateutil.py diff --git a/astroid/brain/brain_gi.py b/astroid/astroid/brain/brain_gi.py similarity index 100% rename from astroid/brain/brain_gi.py rename to astroid/astroid/brain/brain_gi.py diff --git a/astroid/brain/brain_mechanize.py b/astroid/astroid/brain/brain_mechanize.py similarity index 100% rename from astroid/brain/brain_mechanize.py rename to astroid/astroid/brain/brain_mechanize.py diff --git a/astroid/brain/brain_nose.py b/astroid/astroid/brain/brain_nose.py similarity index 100% rename from astroid/brain/brain_nose.py rename to astroid/astroid/brain/brain_nose.py diff --git a/astroid/brain/brain_numpy.py b/astroid/astroid/brain/brain_numpy.py similarity index 100% rename from astroid/brain/brain_numpy.py rename to astroid/astroid/brain/brain_numpy.py diff --git a/astroid/brain/brain_pytest.py b/astroid/astroid/brain/brain_pytest.py similarity index 100% rename from astroid/brain/brain_pytest.py rename to astroid/astroid/brain/brain_pytest.py diff --git a/astroid/brain/brain_qt.py b/astroid/astroid/brain/brain_qt.py similarity index 100% rename from astroid/brain/brain_qt.py rename to astroid/astroid/brain/brain_qt.py diff --git a/astroid/brain/brain_six.py b/astroid/astroid/brain/brain_six.py similarity index 100% rename from astroid/brain/brain_six.py rename to astroid/astroid/brain/brain_six.py diff --git a/astroid/brain/brain_stdlib.py b/astroid/astroid/brain/brain_stdlib.py similarity index 100% rename from astroid/brain/brain_stdlib.py rename to astroid/astroid/brain/brain_stdlib.py diff --git a/astroid/builder.py b/astroid/astroid/builder.py similarity index 100% rename from astroid/builder.py rename to astroid/astroid/builder.py diff --git a/astroid/context.py b/astroid/astroid/context.py similarity index 100% rename from astroid/context.py rename to astroid/astroid/context.py diff --git a/astroid/decorators.py b/astroid/astroid/decorators.py similarity index 100% rename from astroid/decorators.py rename to astroid/astroid/decorators.py diff --git a/astroid/exceptions.py b/astroid/astroid/exceptions.py similarity index 100% rename from astroid/exceptions.py rename to astroid/astroid/exceptions.py diff --git a/astroid/inference.py b/astroid/astroid/inference.py similarity index 100% rename from astroid/inference.py rename to astroid/astroid/inference.py diff --git a/astroid/interpreter/__init__.py b/astroid/astroid/interpreter/__init__.py similarity index 100% rename from astroid/interpreter/__init__.py rename to astroid/astroid/interpreter/__init__.py diff --git a/astroid/interpreter/lookup.py b/astroid/astroid/interpreter/lookup.py similarity index 100% rename from astroid/interpreter/lookup.py rename to astroid/astroid/interpreter/lookup.py diff --git a/astroid/interpreter/objects.py b/astroid/astroid/interpreter/objects.py similarity index 100% rename from astroid/interpreter/objects.py rename to astroid/astroid/interpreter/objects.py diff --git a/astroid/interpreter/runtimeabc.py b/astroid/astroid/interpreter/runtimeabc.py similarity index 100% rename from astroid/interpreter/runtimeabc.py rename to astroid/astroid/interpreter/runtimeabc.py diff --git a/astroid/interpreter/scope.py b/astroid/astroid/interpreter/scope.py similarity index 100% rename from astroid/interpreter/scope.py rename to astroid/astroid/interpreter/scope.py diff --git a/astroid/interpreter/util.py b/astroid/astroid/interpreter/util.py similarity index 100% rename from astroid/interpreter/util.py rename to astroid/astroid/interpreter/util.py diff --git a/astroid/manager.py b/astroid/astroid/manager.py similarity index 100% rename from astroid/manager.py rename to astroid/astroid/manager.py diff --git a/astroid/modutils.py b/astroid/astroid/modutils.py similarity index 100% rename from astroid/modutils.py rename to astroid/astroid/modutils.py diff --git a/astroid/nodes.py b/astroid/astroid/nodes.py similarity index 100% rename from astroid/nodes.py rename to astroid/astroid/nodes.py diff --git a/astroid/protocols.py b/astroid/astroid/protocols.py similarity index 100% rename from astroid/protocols.py rename to astroid/astroid/protocols.py diff --git a/astroid/raw_building.py b/astroid/astroid/raw_building.py similarity index 100% rename from astroid/raw_building.py rename to astroid/astroid/raw_building.py diff --git a/astroid/test_utils.py b/astroid/astroid/test_utils.py similarity index 100% rename from astroid/test_utils.py rename to astroid/astroid/test_utils.py diff --git a/astroid/tests/__init__.py b/astroid/astroid/tests/__init__.py similarity index 100% rename from astroid/tests/__init__.py rename to astroid/astroid/tests/__init__.py diff --git a/astroid/tests/resources.py b/astroid/astroid/tests/resources.py similarity index 97% rename from astroid/tests/resources.py rename to astroid/astroid/tests/resources.py index 175b9d1734..c08e56b60c 100644 --- a/astroid/tests/resources.py +++ b/astroid/astroid/tests/resources.py @@ -25,7 +25,7 @@ from astroid import MANAGER -DATA_DIR = 'testdata/python{}/'.format(sys.version_info[0]) +DATA_DIR = 'astroid/testdata/python{}/'.format(sys.version_info[0]) BUILTINS = six.moves.builtins.__name__ def find(name): diff --git a/astroid/tests/unittest_brain.py b/astroid/astroid/tests/unittest_brain.py similarity index 100% rename from astroid/tests/unittest_brain.py rename to astroid/astroid/tests/unittest_brain.py diff --git a/astroid/tests/unittest_builder.py b/astroid/astroid/tests/unittest_builder.py similarity index 100% rename from astroid/tests/unittest_builder.py rename to astroid/astroid/tests/unittest_builder.py diff --git a/astroid/tests/unittest_helpers.py b/astroid/astroid/tests/unittest_helpers.py similarity index 100% rename from astroid/tests/unittest_helpers.py rename to astroid/astroid/tests/unittest_helpers.py diff --git a/astroid/tests/unittest_inference.py b/astroid/astroid/tests/unittest_inference.py similarity index 100% rename from astroid/tests/unittest_inference.py rename to astroid/astroid/tests/unittest_inference.py diff --git a/astroid/tests/unittest_lookup.py b/astroid/astroid/tests/unittest_lookup.py similarity index 100% rename from astroid/tests/unittest_lookup.py rename to astroid/astroid/tests/unittest_lookup.py diff --git a/astroid/tests/unittest_manager.py b/astroid/astroid/tests/unittest_manager.py similarity index 100% rename from astroid/tests/unittest_manager.py rename to astroid/astroid/tests/unittest_manager.py diff --git a/astroid/tests/unittest_modutils.py b/astroid/astroid/tests/unittest_modutils.py similarity index 100% rename from astroid/tests/unittest_modutils.py rename to astroid/astroid/tests/unittest_modutils.py diff --git a/astroid/tests/unittest_nodes.py b/astroid/astroid/tests/unittest_nodes.py similarity index 100% rename from astroid/tests/unittest_nodes.py rename to astroid/astroid/tests/unittest_nodes.py diff --git a/astroid/tests/unittest_objects.py b/astroid/astroid/tests/unittest_objects.py similarity index 100% rename from astroid/tests/unittest_objects.py rename to astroid/astroid/tests/unittest_objects.py diff --git a/astroid/tests/unittest_protocols.py b/astroid/astroid/tests/unittest_protocols.py similarity index 100% rename from astroid/tests/unittest_protocols.py rename to astroid/astroid/tests/unittest_protocols.py diff --git a/astroid/tests/unittest_python3.py b/astroid/astroid/tests/unittest_python3.py similarity index 100% rename from astroid/tests/unittest_python3.py rename to astroid/astroid/tests/unittest_python3.py diff --git a/astroid/tests/unittest_raw_building.py b/astroid/astroid/tests/unittest_raw_building.py similarity index 100% rename from astroid/tests/unittest_raw_building.py rename to astroid/astroid/tests/unittest_raw_building.py diff --git a/astroid/tests/unittest_regrtest.py b/astroid/astroid/tests/unittest_regrtest.py similarity index 100% rename from astroid/tests/unittest_regrtest.py rename to astroid/astroid/tests/unittest_regrtest.py diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/astroid/tests/unittest_scoped_nodes.py similarity index 100% rename from astroid/tests/unittest_scoped_nodes.py rename to astroid/astroid/tests/unittest_scoped_nodes.py diff --git a/astroid/tests/unittest_transforms.py b/astroid/astroid/tests/unittest_transforms.py similarity index 100% rename from astroid/tests/unittest_transforms.py rename to astroid/astroid/tests/unittest_transforms.py diff --git a/astroid/tests/unittest_utils.py b/astroid/astroid/tests/unittest_utils.py similarity index 100% rename from astroid/tests/unittest_utils.py rename to astroid/astroid/tests/unittest_utils.py diff --git a/astroid/transforms.py b/astroid/astroid/transforms.py similarity index 100% rename from astroid/transforms.py rename to astroid/astroid/transforms.py diff --git a/astroid/tree/__init__.py b/astroid/astroid/tree/__init__.py similarity index 100% rename from astroid/tree/__init__.py rename to astroid/astroid/tree/__init__.py diff --git a/astroid/tree/base.py b/astroid/astroid/tree/base.py similarity index 100% rename from astroid/tree/base.py rename to astroid/astroid/tree/base.py diff --git a/astroid/tree/node_classes.py b/astroid/astroid/tree/node_classes.py similarity index 100% rename from astroid/tree/node_classes.py rename to astroid/astroid/tree/node_classes.py diff --git a/astroid/tree/rebuilder.py b/astroid/astroid/tree/rebuilder.py similarity index 100% rename from astroid/tree/rebuilder.py rename to astroid/astroid/tree/rebuilder.py diff --git a/astroid/tree/scoped_nodes.py b/astroid/astroid/tree/scoped_nodes.py similarity index 100% rename from astroid/tree/scoped_nodes.py rename to astroid/astroid/tree/scoped_nodes.py diff --git a/astroid/tree/treeabc.py b/astroid/astroid/tree/treeabc.py similarity index 100% rename from astroid/tree/treeabc.py rename to astroid/astroid/tree/treeabc.py diff --git a/astroid/util.py b/astroid/astroid/util.py similarity index 100% rename from astroid/util.py rename to astroid/astroid/util.py diff --git a/testdata/python2/data/MyPyPa-0.1.0-py2.5.egg b/astroid/testdata/python2/data/MyPyPa-0.1.0-py2.5.egg similarity index 100% rename from testdata/python2/data/MyPyPa-0.1.0-py2.5.egg rename to astroid/testdata/python2/data/MyPyPa-0.1.0-py2.5.egg diff --git a/testdata/python2/data/MyPyPa-0.1.0-py2.5.zip b/astroid/testdata/python2/data/MyPyPa-0.1.0-py2.5.zip similarity index 100% rename from testdata/python2/data/MyPyPa-0.1.0-py2.5.zip rename to astroid/testdata/python2/data/MyPyPa-0.1.0-py2.5.zip diff --git a/testdata/python2/data/SSL1/Connection1.py b/astroid/testdata/python2/data/SSL1/Connection1.py similarity index 100% rename from testdata/python2/data/SSL1/Connection1.py rename to astroid/testdata/python2/data/SSL1/Connection1.py diff --git a/testdata/python2/data/SSL1/__init__.py b/astroid/testdata/python2/data/SSL1/__init__.py similarity index 100% rename from testdata/python2/data/SSL1/__init__.py rename to astroid/testdata/python2/data/SSL1/__init__.py diff --git a/testdata/python2/data/__init__.py b/astroid/testdata/python2/data/__init__.py similarity index 100% rename from testdata/python2/data/__init__.py rename to astroid/testdata/python2/data/__init__.py diff --git a/testdata/python2/data/absimp/__init__.py b/astroid/testdata/python2/data/absimp/__init__.py similarity index 100% rename from testdata/python2/data/absimp/__init__.py rename to astroid/testdata/python2/data/absimp/__init__.py diff --git a/testdata/python2/data/absimp/sidepackage/__init__.py b/astroid/testdata/python2/data/absimp/sidepackage/__init__.py similarity index 100% rename from testdata/python2/data/absimp/sidepackage/__init__.py rename to astroid/testdata/python2/data/absimp/sidepackage/__init__.py diff --git a/testdata/python2/data/absimp/string.py b/astroid/testdata/python2/data/absimp/string.py similarity index 100% rename from testdata/python2/data/absimp/string.py rename to astroid/testdata/python2/data/absimp/string.py diff --git a/testdata/python2/data/absimport.py b/astroid/testdata/python2/data/absimport.py similarity index 100% rename from testdata/python2/data/absimport.py rename to astroid/testdata/python2/data/absimport.py diff --git a/testdata/python2/data/all.py b/astroid/testdata/python2/data/all.py similarity index 100% rename from testdata/python2/data/all.py rename to astroid/testdata/python2/data/all.py diff --git a/testdata/python2/data/appl/__init__.py b/astroid/testdata/python2/data/appl/__init__.py similarity index 100% rename from testdata/python2/data/appl/__init__.py rename to astroid/testdata/python2/data/appl/__init__.py diff --git a/testdata/python2/data/appl/myConnection.py b/astroid/testdata/python2/data/appl/myConnection.py similarity index 100% rename from testdata/python2/data/appl/myConnection.py rename to astroid/testdata/python2/data/appl/myConnection.py diff --git a/testdata/python2/data/descriptor_crash.py b/astroid/testdata/python2/data/descriptor_crash.py similarity index 100% rename from testdata/python2/data/descriptor_crash.py rename to astroid/testdata/python2/data/descriptor_crash.py diff --git a/testdata/python2/data/email.py b/astroid/testdata/python2/data/email.py similarity index 100% rename from testdata/python2/data/email.py rename to astroid/testdata/python2/data/email.py diff --git a/testdata/python2/data/find_test/__init__.py b/astroid/testdata/python2/data/find_test/__init__.py similarity index 100% rename from testdata/python2/data/find_test/__init__.py rename to astroid/testdata/python2/data/find_test/__init__.py diff --git a/testdata/python2/data/find_test/module.py b/astroid/testdata/python2/data/find_test/module.py similarity index 100% rename from testdata/python2/data/find_test/module.py rename to astroid/testdata/python2/data/find_test/module.py diff --git a/testdata/python2/data/find_test/module2.py b/astroid/testdata/python2/data/find_test/module2.py similarity index 100% rename from testdata/python2/data/find_test/module2.py rename to astroid/testdata/python2/data/find_test/module2.py diff --git a/testdata/python2/data/find_test/noendingnewline.py b/astroid/testdata/python2/data/find_test/noendingnewline.py similarity index 100% rename from testdata/python2/data/find_test/noendingnewline.py rename to astroid/testdata/python2/data/find_test/noendingnewline.py diff --git a/testdata/python2/data/find_test/nonregr.py b/astroid/testdata/python2/data/find_test/nonregr.py similarity index 100% rename from testdata/python2/data/find_test/nonregr.py rename to astroid/testdata/python2/data/find_test/nonregr.py diff --git a/testdata/python2/data/format.py b/astroid/testdata/python2/data/format.py similarity index 100% rename from testdata/python2/data/format.py rename to astroid/testdata/python2/data/format.py diff --git a/testdata/python2/data/invalid_encoding.py b/astroid/testdata/python2/data/invalid_encoding.py similarity index 100% rename from testdata/python2/data/invalid_encoding.py rename to astroid/testdata/python2/data/invalid_encoding.py diff --git a/testdata/python2/data/lmfp/__init__.py b/astroid/testdata/python2/data/lmfp/__init__.py similarity index 100% rename from testdata/python2/data/lmfp/__init__.py rename to astroid/testdata/python2/data/lmfp/__init__.py diff --git a/testdata/python2/data/lmfp/foo.py b/astroid/testdata/python2/data/lmfp/foo.py similarity index 100% rename from testdata/python2/data/lmfp/foo.py rename to astroid/testdata/python2/data/lmfp/foo.py diff --git a/testdata/python2/data/module.py b/astroid/testdata/python2/data/module.py similarity index 100% rename from testdata/python2/data/module.py rename to astroid/testdata/python2/data/module.py diff --git a/testdata/python2/data/module1abs/__init__.py b/astroid/testdata/python2/data/module1abs/__init__.py similarity index 100% rename from testdata/python2/data/module1abs/__init__.py rename to astroid/testdata/python2/data/module1abs/__init__.py diff --git a/testdata/python2/data/module1abs/core.py b/astroid/testdata/python2/data/module1abs/core.py similarity index 100% rename from testdata/python2/data/module1abs/core.py rename to astroid/testdata/python2/data/module1abs/core.py diff --git a/testdata/python2/data/module2.py b/astroid/testdata/python2/data/module2.py similarity index 100% rename from testdata/python2/data/module2.py rename to astroid/testdata/python2/data/module2.py diff --git a/testdata/python2/data/noendingnewline.py b/astroid/testdata/python2/data/noendingnewline.py similarity index 100% rename from testdata/python2/data/noendingnewline.py rename to astroid/testdata/python2/data/noendingnewline.py diff --git a/testdata/python2/data/nonregr.py b/astroid/testdata/python2/data/nonregr.py similarity index 100% rename from testdata/python2/data/nonregr.py rename to astroid/testdata/python2/data/nonregr.py diff --git a/testdata/python2/data/notall.py b/astroid/testdata/python2/data/notall.py similarity index 100% rename from testdata/python2/data/notall.py rename to astroid/testdata/python2/data/notall.py diff --git a/testdata/python2/data/notamodule/file.py b/astroid/testdata/python2/data/notamodule/file.py similarity index 100% rename from testdata/python2/data/notamodule/file.py rename to astroid/testdata/python2/data/notamodule/file.py diff --git a/testdata/python2/data/package/__init__.py b/astroid/testdata/python2/data/package/__init__.py similarity index 100% rename from testdata/python2/data/package/__init__.py rename to astroid/testdata/python2/data/package/__init__.py diff --git a/testdata/python2/data/package/absimport.py b/astroid/testdata/python2/data/package/absimport.py similarity index 100% rename from testdata/python2/data/package/absimport.py rename to astroid/testdata/python2/data/package/absimport.py diff --git a/testdata/python2/data/package/hello.py b/astroid/testdata/python2/data/package/hello.py similarity index 100% rename from testdata/python2/data/package/hello.py rename to astroid/testdata/python2/data/package/hello.py diff --git a/testdata/python2/data/package/import_package_subpackage_module.py b/astroid/testdata/python2/data/package/import_package_subpackage_module.py similarity index 100% rename from testdata/python2/data/package/import_package_subpackage_module.py rename to astroid/testdata/python2/data/package/import_package_subpackage_module.py diff --git a/testdata/python2/data/package/subpackage/__init__.py b/astroid/testdata/python2/data/package/subpackage/__init__.py similarity index 100% rename from testdata/python2/data/package/subpackage/__init__.py rename to astroid/testdata/python2/data/package/subpackage/__init__.py diff --git a/testdata/python2/data/package/subpackage/module.py b/astroid/testdata/python2/data/package/subpackage/module.py similarity index 100% rename from testdata/python2/data/package/subpackage/module.py rename to astroid/testdata/python2/data/package/subpackage/module.py diff --git a/testdata/python2/data/recursion.py b/astroid/testdata/python2/data/recursion.py similarity index 100% rename from testdata/python2/data/recursion.py rename to astroid/testdata/python2/data/recursion.py diff --git a/testdata/python2/data/unicode_package/__init__.py b/astroid/testdata/python2/data/unicode_package/__init__.py similarity index 100% rename from testdata/python2/data/unicode_package/__init__.py rename to astroid/testdata/python2/data/unicode_package/__init__.py diff --git a/testdata/python2/data/unicode_package/core/__init__.py b/astroid/testdata/python2/data/unicode_package/core/__init__.py similarity index 100% rename from testdata/python2/data/unicode_package/core/__init__.py rename to astroid/testdata/python2/data/unicode_package/core/__init__.py diff --git a/testdata/python3/data/MyPyPa-0.1.0-py2.5.egg b/astroid/testdata/python3/data/MyPyPa-0.1.0-py2.5.egg similarity index 100% rename from testdata/python3/data/MyPyPa-0.1.0-py2.5.egg rename to astroid/testdata/python3/data/MyPyPa-0.1.0-py2.5.egg diff --git a/testdata/python3/data/MyPyPa-0.1.0-py2.5.zip b/astroid/testdata/python3/data/MyPyPa-0.1.0-py2.5.zip similarity index 100% rename from testdata/python3/data/MyPyPa-0.1.0-py2.5.zip rename to astroid/testdata/python3/data/MyPyPa-0.1.0-py2.5.zip diff --git a/testdata/python3/data/SSL1/Connection1.py b/astroid/testdata/python3/data/SSL1/Connection1.py similarity index 100% rename from testdata/python3/data/SSL1/Connection1.py rename to astroid/testdata/python3/data/SSL1/Connection1.py diff --git a/testdata/python3/data/SSL1/__init__.py b/astroid/testdata/python3/data/SSL1/__init__.py similarity index 100% rename from testdata/python3/data/SSL1/__init__.py rename to astroid/testdata/python3/data/SSL1/__init__.py diff --git a/testdata/python3/data/__init__.py b/astroid/testdata/python3/data/__init__.py similarity index 100% rename from testdata/python3/data/__init__.py rename to astroid/testdata/python3/data/__init__.py diff --git a/testdata/python3/data/absimp/__init__.py b/astroid/testdata/python3/data/absimp/__init__.py similarity index 100% rename from testdata/python3/data/absimp/__init__.py rename to astroid/testdata/python3/data/absimp/__init__.py diff --git a/testdata/python3/data/absimp/sidepackage/__init__.py b/astroid/testdata/python3/data/absimp/sidepackage/__init__.py similarity index 100% rename from testdata/python3/data/absimp/sidepackage/__init__.py rename to astroid/testdata/python3/data/absimp/sidepackage/__init__.py diff --git a/testdata/python3/data/absimp/string.py b/astroid/testdata/python3/data/absimp/string.py similarity index 100% rename from testdata/python3/data/absimp/string.py rename to astroid/testdata/python3/data/absimp/string.py diff --git a/testdata/python3/data/absimport.py b/astroid/testdata/python3/data/absimport.py similarity index 100% rename from testdata/python3/data/absimport.py rename to astroid/testdata/python3/data/absimport.py diff --git a/testdata/python3/data/all.py b/astroid/testdata/python3/data/all.py similarity index 100% rename from testdata/python3/data/all.py rename to astroid/testdata/python3/data/all.py diff --git a/testdata/python3/data/appl/__init__.py b/astroid/testdata/python3/data/appl/__init__.py similarity index 100% rename from testdata/python3/data/appl/__init__.py rename to astroid/testdata/python3/data/appl/__init__.py diff --git a/testdata/python3/data/appl/myConnection.py b/astroid/testdata/python3/data/appl/myConnection.py similarity index 100% rename from testdata/python3/data/appl/myConnection.py rename to astroid/testdata/python3/data/appl/myConnection.py diff --git a/testdata/python3/data/descriptor_crash.py b/astroid/testdata/python3/data/descriptor_crash.py similarity index 100% rename from testdata/python3/data/descriptor_crash.py rename to astroid/testdata/python3/data/descriptor_crash.py diff --git a/testdata/python3/data/email.py b/astroid/testdata/python3/data/email.py similarity index 100% rename from testdata/python3/data/email.py rename to astroid/testdata/python3/data/email.py diff --git a/testdata/python3/data/find_test/__init__.py b/astroid/testdata/python3/data/find_test/__init__.py similarity index 100% rename from testdata/python3/data/find_test/__init__.py rename to astroid/testdata/python3/data/find_test/__init__.py diff --git a/testdata/python3/data/find_test/module.py b/astroid/testdata/python3/data/find_test/module.py similarity index 100% rename from testdata/python3/data/find_test/module.py rename to astroid/testdata/python3/data/find_test/module.py diff --git a/testdata/python3/data/find_test/module2.py b/astroid/testdata/python3/data/find_test/module2.py similarity index 100% rename from testdata/python3/data/find_test/module2.py rename to astroid/testdata/python3/data/find_test/module2.py diff --git a/testdata/python3/data/find_test/noendingnewline.py b/astroid/testdata/python3/data/find_test/noendingnewline.py similarity index 100% rename from testdata/python3/data/find_test/noendingnewline.py rename to astroid/testdata/python3/data/find_test/noendingnewline.py diff --git a/testdata/python3/data/find_test/nonregr.py b/astroid/testdata/python3/data/find_test/nonregr.py similarity index 100% rename from testdata/python3/data/find_test/nonregr.py rename to astroid/testdata/python3/data/find_test/nonregr.py diff --git a/testdata/python3/data/format.py b/astroid/testdata/python3/data/format.py similarity index 100% rename from testdata/python3/data/format.py rename to astroid/testdata/python3/data/format.py diff --git a/testdata/python3/data/invalid_encoding.py b/astroid/testdata/python3/data/invalid_encoding.py similarity index 100% rename from testdata/python3/data/invalid_encoding.py rename to astroid/testdata/python3/data/invalid_encoding.py diff --git a/testdata/python3/data/lmfp/__init__.py b/astroid/testdata/python3/data/lmfp/__init__.py similarity index 100% rename from testdata/python3/data/lmfp/__init__.py rename to astroid/testdata/python3/data/lmfp/__init__.py diff --git a/testdata/python3/data/lmfp/foo.py b/astroid/testdata/python3/data/lmfp/foo.py similarity index 100% rename from testdata/python3/data/lmfp/foo.py rename to astroid/testdata/python3/data/lmfp/foo.py diff --git a/testdata/python3/data/module.py b/astroid/testdata/python3/data/module.py similarity index 100% rename from testdata/python3/data/module.py rename to astroid/testdata/python3/data/module.py diff --git a/testdata/python3/data/module1abs/__init__.py b/astroid/testdata/python3/data/module1abs/__init__.py similarity index 100% rename from testdata/python3/data/module1abs/__init__.py rename to astroid/testdata/python3/data/module1abs/__init__.py diff --git a/testdata/python3/data/module1abs/core.py b/astroid/testdata/python3/data/module1abs/core.py similarity index 100% rename from testdata/python3/data/module1abs/core.py rename to astroid/testdata/python3/data/module1abs/core.py diff --git a/testdata/python3/data/module2.py b/astroid/testdata/python3/data/module2.py similarity index 100% rename from testdata/python3/data/module2.py rename to astroid/testdata/python3/data/module2.py diff --git a/testdata/python3/data/noendingnewline.py b/astroid/testdata/python3/data/noendingnewline.py similarity index 100% rename from testdata/python3/data/noendingnewline.py rename to astroid/testdata/python3/data/noendingnewline.py diff --git a/testdata/python3/data/nonregr.py b/astroid/testdata/python3/data/nonregr.py similarity index 100% rename from testdata/python3/data/nonregr.py rename to astroid/testdata/python3/data/nonregr.py diff --git a/testdata/python3/data/notall.py b/astroid/testdata/python3/data/notall.py similarity index 100% rename from testdata/python3/data/notall.py rename to astroid/testdata/python3/data/notall.py diff --git a/testdata/python3/data/notamodule/file.py b/astroid/testdata/python3/data/notamodule/file.py similarity index 100% rename from testdata/python3/data/notamodule/file.py rename to astroid/testdata/python3/data/notamodule/file.py diff --git a/testdata/python3/data/package/__init__.py b/astroid/testdata/python3/data/package/__init__.py similarity index 100% rename from testdata/python3/data/package/__init__.py rename to astroid/testdata/python3/data/package/__init__.py diff --git a/testdata/python3/data/package/absimport.py b/astroid/testdata/python3/data/package/absimport.py similarity index 100% rename from testdata/python3/data/package/absimport.py rename to astroid/testdata/python3/data/package/absimport.py diff --git a/testdata/python3/data/package/hello.py b/astroid/testdata/python3/data/package/hello.py similarity index 100% rename from testdata/python3/data/package/hello.py rename to astroid/testdata/python3/data/package/hello.py diff --git a/testdata/python3/data/package/import_package_subpackage_module.py b/astroid/testdata/python3/data/package/import_package_subpackage_module.py similarity index 100% rename from testdata/python3/data/package/import_package_subpackage_module.py rename to astroid/testdata/python3/data/package/import_package_subpackage_module.py diff --git a/testdata/python3/data/package/subpackage/__init__.py b/astroid/testdata/python3/data/package/subpackage/__init__.py similarity index 100% rename from testdata/python3/data/package/subpackage/__init__.py rename to astroid/testdata/python3/data/package/subpackage/__init__.py diff --git a/testdata/python3/data/package/subpackage/module.py b/astroid/testdata/python3/data/package/subpackage/module.py similarity index 100% rename from testdata/python3/data/package/subpackage/module.py rename to astroid/testdata/python3/data/package/subpackage/module.py diff --git a/testdata/python3/data/recursion.py b/astroid/testdata/python3/data/recursion.py similarity index 100% rename from testdata/python3/data/recursion.py rename to astroid/testdata/python3/data/recursion.py diff --git a/testdata/python3/data/unicode_package/__init__.py b/astroid/testdata/python3/data/unicode_package/__init__.py similarity index 100% rename from testdata/python3/data/unicode_package/__init__.py rename to astroid/testdata/python3/data/unicode_package/__init__.py diff --git a/testdata/python3/data/unicode_package/core/__init__.py b/astroid/testdata/python3/data/unicode_package/core/__init__.py similarity index 100% rename from testdata/python3/data/unicode_package/core/__init__.py rename to astroid/testdata/python3/data/unicode_package/core/__init__.py diff --git a/setup.py b/setup.py index 4b2a48eb59..95f23759ee 100644 --- a/setup.py +++ b/setup.py @@ -64,10 +64,8 @@ def install(): include_package_data = True, install_requires = install_requires, packages = find_packages(), - cmdclass={'install_lib': AstroidInstallLib, - 'easy_install': AstroidEasyInstallLib}, - data_files=[(p, [os.path.join(p, n) for n in ns]) for p, _, ns in os.walk('testdata/')] - # [('testdata', [os.path.join(p, n) for p, _, ns in os.walk('testdata/') for n in ns])] + cmdclass = {'install_lib': AstroidInstallLib, + 'easy_install': AstroidEasyInstallLib}, ) diff --git a/tox.ini b/tox.ini index 1a7cb7a232..9f23d7ce85 100644 --- a/tox.ini +++ b/tox.ini @@ -10,8 +10,8 @@ commands = coverage erase [testenv:coverage-stats] commands = - coverage report --ignore-errors --include="astroid*" - coverage html --ignore-errors --include="astroid*" + coverage report --ignore-errors --include="*astroid*" + coverage html --ignore-errors --include="*astroid*" [testenv] deps = @@ -31,4 +31,5 @@ deps = # Disable warnings because they overflow the Travis log on 3.5 setenv = PYTHONWARNINGS = i # {posargs} allows passing command line arguments to unittest -commands = coverage run --append --branch -m unittest discover {posargs} -s {envsitepackagesdir}/astroid/tests -p "unittest*.py" +commands = coverage run --append --branch -m unittest discover {posargs} -t {envsitepackagesdir}/astroid/ -s {envsitepackagesdir}/astroid/astroid/tests -p "unittest*.py" + From 4596201f9146e64c18cfcb0f841618328e953910 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Mon, 8 Feb 2016 16:25:27 -0500 Subject: [PATCH 191/312] Modify tox.ini to properly exclude files not part of astroid --- tox.ini | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tox.ini b/tox.ini index 9f23d7ce85..50169c8d3e 100644 --- a/tox.ini +++ b/tox.ini @@ -10,8 +10,8 @@ commands = coverage erase [testenv:coverage-stats] commands = - coverage report --ignore-errors --include="*astroid*" - coverage html --ignore-errors --include="*astroid*" + coverage report --ignore-errors + coverage html --ignore-errors [testenv] deps = @@ -31,5 +31,4 @@ deps = # Disable warnings because they overflow the Travis log on 3.5 setenv = PYTHONWARNINGS = i # {posargs} allows passing command line arguments to unittest -commands = coverage run --append --branch -m unittest discover {posargs} -t {envsitepackagesdir}/astroid/ -s {envsitepackagesdir}/astroid/astroid/tests -p "unittest*.py" - +commands = coverage run --source {envsitepackagesdir}/astroid --append --branch -m unittest discover {posargs} -t {envsitepackagesdir}/astroid/ -s {envsitepackagesdir}/astroid/astroid/tests -p "unittest*.py" From ef1906924604fabf3396c0aa7d4f43d786169e52 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Mon, 8 Feb 2016 17:28:41 -0500 Subject: [PATCH 192/312] Try running tox as a non-module --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 9075168a8b..e3a7da100b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -35,7 +35,7 @@ install: - $PYTHON_EXE -m pip --version - $PYTHON_EXE -m tox --version script: - - python -m tox -e $TOXENV + - tox -e $TOXENV after_failure: - more .tox/log/* | cat - more .tox/*/log/* | cat From 70367203846947c3b9a3682f06e22f24fab2c684 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Mon, 8 Feb 2016 17:56:05 -0500 Subject: [PATCH 193/312] Get travis directory listing --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index e3a7da100b..0827887d09 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,6 +25,9 @@ before_install: - python --version - uname -a - lsb_release -a +before_script: + - ls ~ + - ls .tox install: - $PYTHON_EXE -m pip install pip -U - python -m pip install tox From d990138351b436002da7f7d7eaa7919141af4425 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Mon, 8 Feb 2016 18:18:51 -0500 Subject: [PATCH 194/312] Get travis directory listing --- .travis.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0827887d09..01e73bbca4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,8 +26,7 @@ before_install: - uname -a - lsb_release -a before_script: - - ls ~ - - ls .tox + - ls -a ~ | more install: - $PYTHON_EXE -m pip install pip -U - python -m pip install tox From d1f8e0743011d0d764d2a75d5417cd59e4864f1b Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Mon, 8 Feb 2016 18:26:40 -0500 Subject: [PATCH 195/312] Get travis directory listing --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 01e73bbca4..bbe5e58855 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,7 +26,7 @@ before_install: - uname -a - lsb_release -a before_script: - - ls -a ~ | more + - ls -a ~ install: - $PYTHON_EXE -m pip install pip -U - python -m pip install tox From 2c8a50feae9caaa0aaf42da0e38d7f3ee45060ad Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Mon, 8 Feb 2016 18:43:45 -0500 Subject: [PATCH 196/312] Get travis directory listing --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index bbe5e58855..080197ca70 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,7 +26,7 @@ before_install: - uname -a - lsb_release -a before_script: - - ls -a ~ + - ls -a ~ | echo install: - $PYTHON_EXE -m pip install pip -U - python -m pip install tox From 1707d827a2e3d02254c205751a0c4dcd1b4bcc21 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Mon, 8 Feb 2016 19:41:08 -0500 Subject: [PATCH 197/312] Get travis directory listing --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 080197ca70..d2281d351a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,7 +26,7 @@ before_install: - uname -a - lsb_release -a before_script: - - ls -a ~ | echo + - find ~ -name '*.*' | cat install: - $PYTHON_EXE -m pip install pip -U - python -m pip install tox From 68fe76dec77d3f64ec370bb539617c11c7a77fcc Mon Sep 17 00:00:00 2001 From: Julien Cristau Date: Wed, 10 Feb 2016 15:15:02 +0100 Subject: [PATCH 198/312] doc: fix build Stop hardcoding an outdated version number, don't reference nonexistent _static directory, and fix "inference" module reference. Signed-off-by: Julien Cristau --- doc/source/conf.py | 6 +++--- doc/source/inference.rst | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/source/conf.py b/doc/source/conf.py index 980fd31eac..f4ceb2a89e 100644 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -48,9 +48,9 @@ # built documents. # # The short X.Y version. -version = '1.3.4' +from astroid.__pkginfo__ import version # The full version, including alpha/beta/rc tags. -release = '1.3.4' +release = version # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. @@ -125,7 +125,7 @@ # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] +html_static_path = [] # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, # using the given strftime format. diff --git a/doc/source/inference.rst b/doc/source/inference.rst index bf67993f03..7fed194f93 100644 --- a/doc/source/inference.rst +++ b/doc/source/inference.rst @@ -81,7 +81,7 @@ of the :mod:`inference`. .. todo:: actually annotate the doc to structure its approach -.. automodule:: inference +.. automodule:: astroid.inference :members: :undoc-members: .. :special-members: in autodoc/sphinx 1.1 From 6f1fcd8763ba1ad4c96318676348ba6512165644 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Thu, 11 Feb 2016 14:17:07 +0000 Subject: [PATCH 199/312] Revert "Try to fix the testdata issue" This reverts commit 682b87f5b9832bb55d35d1c3a589ba8695495481. --- MANIFEST.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MANIFEST.in b/MANIFEST.in index 1c177eb8cb..843bdd721c 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -3,4 +3,4 @@ include ChangeLog include README.rst include COPYING include COPYING.LESSER -include tox.ini +include tox.ini \ No newline at end of file From 5259a4db2822a1926b66e7b15f059a874bee324d Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Thu, 11 Feb 2016 14:18:00 +0000 Subject: [PATCH 200/312] Revert "Try graft instead of recursive-include" This reverts commit 18fa724c04c2393b134d57d4fe4cebe38472bad8. --- MANIFEST.in | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/MANIFEST.in b/MANIFEST.in index 843bdd721c..642d7a6542 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,6 +1,5 @@ -graft astroid include ChangeLog include README.rst include COPYING include COPYING.LESSER -include tox.ini \ No newline at end of file +include tox.ini From 8aae5f078b5c837ba6829e8042453c12dfe2421f Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Thu, 11 Feb 2016 14:25:36 +0000 Subject: [PATCH 201/312] Revert "Try to solve the packaging issues again" This reverts commit d37b81de4f1e64abc2f222c487785d816ab469ea. --- astroid/{astroid => }/__init__.py | 0 astroid/{astroid => }/as_string.py | 0 .../{astroid => }/brain/brain_builtin_inference.py | 0 astroid/{astroid => }/brain/brain_dateutil.py | 0 astroid/{astroid => }/brain/brain_gi.py | 0 astroid/{astroid => }/brain/brain_mechanize.py | 0 astroid/{astroid => }/brain/brain_nose.py | 0 astroid/{astroid => }/brain/brain_numpy.py | 0 astroid/{astroid => }/brain/brain_pytest.py | 0 astroid/{astroid => }/brain/brain_qt.py | 0 astroid/{astroid => }/brain/brain_six.py | 0 astroid/{astroid => }/brain/brain_stdlib.py | 0 astroid/{astroid => }/builder.py | 0 astroid/{astroid => }/context.py | 0 astroid/{astroid => }/decorators.py | 0 astroid/{astroid => }/exceptions.py | 0 astroid/{astroid => }/inference.py | 0 astroid/{astroid => }/interpreter/__init__.py | 0 astroid/{astroid => }/interpreter/lookup.py | 0 astroid/{astroid => }/interpreter/objects.py | 0 astroid/{astroid => }/interpreter/runtimeabc.py | 0 astroid/{astroid => }/interpreter/scope.py | 0 astroid/{astroid => }/interpreter/util.py | 0 astroid/{astroid => }/manager.py | 0 astroid/{astroid => }/modutils.py | 0 astroid/{astroid => }/nodes.py | 0 astroid/{astroid => }/protocols.py | 0 astroid/{astroid => }/raw_building.py | 0 astroid/{astroid => }/test_utils.py | 0 astroid/{astroid => }/tests/__init__.py | 0 astroid/{astroid => }/tests/resources.py | 2 +- astroid/{astroid => }/tests/unittest_brain.py | 0 astroid/{astroid => }/tests/unittest_builder.py | 0 astroid/{astroid => }/tests/unittest_helpers.py | 0 astroid/{astroid => }/tests/unittest_inference.py | 0 astroid/{astroid => }/tests/unittest_lookup.py | 0 astroid/{astroid => }/tests/unittest_manager.py | 0 astroid/{astroid => }/tests/unittest_modutils.py | 0 astroid/{astroid => }/tests/unittest_nodes.py | 0 astroid/{astroid => }/tests/unittest_objects.py | 0 astroid/{astroid => }/tests/unittest_protocols.py | 0 astroid/{astroid => }/tests/unittest_python3.py | 0 .../{astroid => }/tests/unittest_raw_building.py | 0 astroid/{astroid => }/tests/unittest_regrtest.py | 0 .../{astroid => }/tests/unittest_scoped_nodes.py | 0 astroid/{astroid => }/tests/unittest_transforms.py | 0 astroid/{astroid => }/tests/unittest_utils.py | 0 astroid/{astroid => }/transforms.py | 0 astroid/{astroid => }/tree/__init__.py | 0 astroid/{astroid => }/tree/base.py | 0 astroid/{astroid => }/tree/node_classes.py | 0 astroid/{astroid => }/tree/rebuilder.py | 0 astroid/{astroid => }/tree/scoped_nodes.py | 0 astroid/{astroid => }/tree/treeabc.py | 0 astroid/{astroid => }/util.py | 0 setup.py | 6 ++++-- .../python2/data/MyPyPa-0.1.0-py2.5.egg | Bin .../python2/data/MyPyPa-0.1.0-py2.5.zip | Bin .../python2/data/SSL1/Connection1.py | 0 .../python2/data/SSL1/__init__.py | 0 .../testdata => testdata}/python2/data/__init__.py | 0 .../python2/data/absimp/__init__.py | 0 .../python2/data/absimp/sidepackage/__init__.py | 0 .../python2/data/absimp/string.py | 0 .../testdata => testdata}/python2/data/absimport.py | 0 {astroid/testdata => testdata}/python2/data/all.py | 0 .../python2/data/appl/__init__.py | 0 .../python2/data/appl/myConnection.py | 0 .../python2/data/descriptor_crash.py | 0 .../testdata => testdata}/python2/data/email.py | 0 .../python2/data/find_test/__init__.py | 0 .../python2/data/find_test/module.py | 0 .../python2/data/find_test/module2.py | 0 .../python2/data/find_test/noendingnewline.py | 0 .../python2/data/find_test/nonregr.py | 0 .../testdata => testdata}/python2/data/format.py | 0 .../python2/data/invalid_encoding.py | 0 .../python2/data/lmfp/__init__.py | 0 .../testdata => testdata}/python2/data/lmfp/foo.py | 0 .../testdata => testdata}/python2/data/module.py | 0 .../python2/data/module1abs/__init__.py | 0 .../python2/data/module1abs/core.py | 0 .../testdata => testdata}/python2/data/module2.py | 0 .../python2/data/noendingnewline.py | 0 .../testdata => testdata}/python2/data/nonregr.py | 0 .../testdata => testdata}/python2/data/notall.py | 0 .../python2/data/notamodule/file.py | 0 .../python2/data/package/__init__.py | 0 .../python2/data/package/absimport.py | 0 .../python2/data/package/hello.py | 0 .../package/import_package_subpackage_module.py | 0 .../python2/data/package/subpackage/__init__.py | 0 .../python2/data/package/subpackage/module.py | 0 .../testdata => testdata}/python2/data/recursion.py | 0 .../python2/data/unicode_package/__init__.py | 0 .../python2/data/unicode_package/core/__init__.py | 0 .../python3/data/MyPyPa-0.1.0-py2.5.egg | Bin .../python3/data/MyPyPa-0.1.0-py2.5.zip | Bin .../python3/data/SSL1/Connection1.py | 0 .../python3/data/SSL1/__init__.py | 0 .../testdata => testdata}/python3/data/__init__.py | 0 .../python3/data/absimp/__init__.py | 0 .../python3/data/absimp/sidepackage/__init__.py | 0 .../python3/data/absimp/string.py | 0 .../testdata => testdata}/python3/data/absimport.py | 0 {astroid/testdata => testdata}/python3/data/all.py | 0 .../python3/data/appl/__init__.py | 0 .../python3/data/appl/myConnection.py | 0 .../python3/data/descriptor_crash.py | 0 .../testdata => testdata}/python3/data/email.py | 0 .../python3/data/find_test/__init__.py | 0 .../python3/data/find_test/module.py | 0 .../python3/data/find_test/module2.py | 0 .../python3/data/find_test/noendingnewline.py | 0 .../python3/data/find_test/nonregr.py | 0 .../testdata => testdata}/python3/data/format.py | 0 .../python3/data/invalid_encoding.py | 0 .../python3/data/lmfp/__init__.py | 0 .../testdata => testdata}/python3/data/lmfp/foo.py | 0 .../testdata => testdata}/python3/data/module.py | 0 .../python3/data/module1abs/__init__.py | 0 .../python3/data/module1abs/core.py | 0 .../testdata => testdata}/python3/data/module2.py | 0 .../python3/data/noendingnewline.py | 0 .../testdata => testdata}/python3/data/nonregr.py | 0 .../testdata => testdata}/python3/data/notall.py | 0 .../python3/data/notamodule/file.py | 0 .../python3/data/package/__init__.py | 0 .../python3/data/package/absimport.py | 0 .../python3/data/package/hello.py | 0 .../package/import_package_subpackage_module.py | 0 .../python3/data/package/subpackage/__init__.py | 0 .../python3/data/package/subpackage/module.py | 0 .../testdata => testdata}/python3/data/recursion.py | 0 .../python3/data/unicode_package/__init__.py | 0 .../python3/data/unicode_package/core/__init__.py | 0 tox.ini | 1 + 137 files changed, 6 insertions(+), 3 deletions(-) rename astroid/{astroid => }/__init__.py (100%) rename astroid/{astroid => }/as_string.py (100%) rename astroid/{astroid => }/brain/brain_builtin_inference.py (100%) rename astroid/{astroid => }/brain/brain_dateutil.py (100%) rename astroid/{astroid => }/brain/brain_gi.py (100%) rename astroid/{astroid => }/brain/brain_mechanize.py (100%) rename astroid/{astroid => }/brain/brain_nose.py (100%) rename astroid/{astroid => }/brain/brain_numpy.py (100%) rename astroid/{astroid => }/brain/brain_pytest.py (100%) rename astroid/{astroid => }/brain/brain_qt.py (100%) rename astroid/{astroid => }/brain/brain_six.py (100%) rename astroid/{astroid => }/brain/brain_stdlib.py (100%) rename astroid/{astroid => }/builder.py (100%) rename astroid/{astroid => }/context.py (100%) rename astroid/{astroid => }/decorators.py (100%) rename astroid/{astroid => }/exceptions.py (100%) rename astroid/{astroid => }/inference.py (100%) rename astroid/{astroid => }/interpreter/__init__.py (100%) rename astroid/{astroid => }/interpreter/lookup.py (100%) rename astroid/{astroid => }/interpreter/objects.py (100%) rename astroid/{astroid => }/interpreter/runtimeabc.py (100%) rename astroid/{astroid => }/interpreter/scope.py (100%) rename astroid/{astroid => }/interpreter/util.py (100%) rename astroid/{astroid => }/manager.py (100%) rename astroid/{astroid => }/modutils.py (100%) rename astroid/{astroid => }/nodes.py (100%) rename astroid/{astroid => }/protocols.py (100%) rename astroid/{astroid => }/raw_building.py (100%) rename astroid/{astroid => }/test_utils.py (100%) rename astroid/{astroid => }/tests/__init__.py (100%) rename astroid/{astroid => }/tests/resources.py (97%) rename astroid/{astroid => }/tests/unittest_brain.py (100%) rename astroid/{astroid => }/tests/unittest_builder.py (100%) rename astroid/{astroid => }/tests/unittest_helpers.py (100%) rename astroid/{astroid => }/tests/unittest_inference.py (100%) rename astroid/{astroid => }/tests/unittest_lookup.py (100%) rename astroid/{astroid => }/tests/unittest_manager.py (100%) rename astroid/{astroid => }/tests/unittest_modutils.py (100%) rename astroid/{astroid => }/tests/unittest_nodes.py (100%) rename astroid/{astroid => }/tests/unittest_objects.py (100%) rename astroid/{astroid => }/tests/unittest_protocols.py (100%) rename astroid/{astroid => }/tests/unittest_python3.py (100%) rename astroid/{astroid => }/tests/unittest_raw_building.py (100%) rename astroid/{astroid => }/tests/unittest_regrtest.py (100%) rename astroid/{astroid => }/tests/unittest_scoped_nodes.py (100%) rename astroid/{astroid => }/tests/unittest_transforms.py (100%) rename astroid/{astroid => }/tests/unittest_utils.py (100%) rename astroid/{astroid => }/transforms.py (100%) rename astroid/{astroid => }/tree/__init__.py (100%) rename astroid/{astroid => }/tree/base.py (100%) rename astroid/{astroid => }/tree/node_classes.py (100%) rename astroid/{astroid => }/tree/rebuilder.py (100%) rename astroid/{astroid => }/tree/scoped_nodes.py (100%) rename astroid/{astroid => }/tree/treeabc.py (100%) rename astroid/{astroid => }/util.py (100%) rename {astroid/testdata => testdata}/python2/data/MyPyPa-0.1.0-py2.5.egg (100%) rename {astroid/testdata => testdata}/python2/data/MyPyPa-0.1.0-py2.5.zip (100%) rename {astroid/testdata => testdata}/python2/data/SSL1/Connection1.py (100%) rename {astroid/testdata => testdata}/python2/data/SSL1/__init__.py (100%) rename {astroid/testdata => testdata}/python2/data/__init__.py (100%) rename {astroid/testdata => testdata}/python2/data/absimp/__init__.py (100%) rename {astroid/testdata => testdata}/python2/data/absimp/sidepackage/__init__.py (100%) rename {astroid/testdata => testdata}/python2/data/absimp/string.py (100%) rename {astroid/testdata => testdata}/python2/data/absimport.py (100%) rename {astroid/testdata => testdata}/python2/data/all.py (100%) rename {astroid/testdata => testdata}/python2/data/appl/__init__.py (100%) rename {astroid/testdata => testdata}/python2/data/appl/myConnection.py (100%) rename {astroid/testdata => testdata}/python2/data/descriptor_crash.py (100%) rename {astroid/testdata => testdata}/python2/data/email.py (100%) rename {astroid/testdata => testdata}/python2/data/find_test/__init__.py (100%) rename {astroid/testdata => testdata}/python2/data/find_test/module.py (100%) rename {astroid/testdata => testdata}/python2/data/find_test/module2.py (100%) rename {astroid/testdata => testdata}/python2/data/find_test/noendingnewline.py (100%) rename {astroid/testdata => testdata}/python2/data/find_test/nonregr.py (100%) rename {astroid/testdata => testdata}/python2/data/format.py (100%) rename {astroid/testdata => testdata}/python2/data/invalid_encoding.py (100%) rename {astroid/testdata => testdata}/python2/data/lmfp/__init__.py (100%) rename {astroid/testdata => testdata}/python2/data/lmfp/foo.py (100%) rename {astroid/testdata => testdata}/python2/data/module.py (100%) rename {astroid/testdata => testdata}/python2/data/module1abs/__init__.py (100%) rename {astroid/testdata => testdata}/python2/data/module1abs/core.py (100%) rename {astroid/testdata => testdata}/python2/data/module2.py (100%) rename {astroid/testdata => testdata}/python2/data/noendingnewline.py (100%) rename {astroid/testdata => testdata}/python2/data/nonregr.py (100%) rename {astroid/testdata => testdata}/python2/data/notall.py (100%) rename {astroid/testdata => testdata}/python2/data/notamodule/file.py (100%) rename {astroid/testdata => testdata}/python2/data/package/__init__.py (100%) rename {astroid/testdata => testdata}/python2/data/package/absimport.py (100%) rename {astroid/testdata => testdata}/python2/data/package/hello.py (100%) rename {astroid/testdata => testdata}/python2/data/package/import_package_subpackage_module.py (100%) rename {astroid/testdata => testdata}/python2/data/package/subpackage/__init__.py (100%) rename {astroid/testdata => testdata}/python2/data/package/subpackage/module.py (100%) rename {astroid/testdata => testdata}/python2/data/recursion.py (100%) rename {astroid/testdata => testdata}/python2/data/unicode_package/__init__.py (100%) rename {astroid/testdata => testdata}/python2/data/unicode_package/core/__init__.py (100%) rename {astroid/testdata => testdata}/python3/data/MyPyPa-0.1.0-py2.5.egg (100%) rename {astroid/testdata => testdata}/python3/data/MyPyPa-0.1.0-py2.5.zip (100%) rename {astroid/testdata => testdata}/python3/data/SSL1/Connection1.py (100%) rename {astroid/testdata => testdata}/python3/data/SSL1/__init__.py (100%) rename {astroid/testdata => testdata}/python3/data/__init__.py (100%) rename {astroid/testdata => testdata}/python3/data/absimp/__init__.py (100%) rename {astroid/testdata => testdata}/python3/data/absimp/sidepackage/__init__.py (100%) rename {astroid/testdata => testdata}/python3/data/absimp/string.py (100%) rename {astroid/testdata => testdata}/python3/data/absimport.py (100%) rename {astroid/testdata => testdata}/python3/data/all.py (100%) rename {astroid/testdata => testdata}/python3/data/appl/__init__.py (100%) rename {astroid/testdata => testdata}/python3/data/appl/myConnection.py (100%) rename {astroid/testdata => testdata}/python3/data/descriptor_crash.py (100%) rename {astroid/testdata => testdata}/python3/data/email.py (100%) rename {astroid/testdata => testdata}/python3/data/find_test/__init__.py (100%) rename {astroid/testdata => testdata}/python3/data/find_test/module.py (100%) rename {astroid/testdata => testdata}/python3/data/find_test/module2.py (100%) rename {astroid/testdata => testdata}/python3/data/find_test/noendingnewline.py (100%) rename {astroid/testdata => testdata}/python3/data/find_test/nonregr.py (100%) rename {astroid/testdata => testdata}/python3/data/format.py (100%) rename {astroid/testdata => testdata}/python3/data/invalid_encoding.py (100%) rename {astroid/testdata => testdata}/python3/data/lmfp/__init__.py (100%) rename {astroid/testdata => testdata}/python3/data/lmfp/foo.py (100%) rename {astroid/testdata => testdata}/python3/data/module.py (100%) rename {astroid/testdata => testdata}/python3/data/module1abs/__init__.py (100%) rename {astroid/testdata => testdata}/python3/data/module1abs/core.py (100%) rename {astroid/testdata => testdata}/python3/data/module2.py (100%) rename {astroid/testdata => testdata}/python3/data/noendingnewline.py (100%) rename {astroid/testdata => testdata}/python3/data/nonregr.py (100%) rename {astroid/testdata => testdata}/python3/data/notall.py (100%) rename {astroid/testdata => testdata}/python3/data/notamodule/file.py (100%) rename {astroid/testdata => testdata}/python3/data/package/__init__.py (100%) rename {astroid/testdata => testdata}/python3/data/package/absimport.py (100%) rename {astroid/testdata => testdata}/python3/data/package/hello.py (100%) rename {astroid/testdata => testdata}/python3/data/package/import_package_subpackage_module.py (100%) rename {astroid/testdata => testdata}/python3/data/package/subpackage/__init__.py (100%) rename {astroid/testdata => testdata}/python3/data/package/subpackage/module.py (100%) rename {astroid/testdata => testdata}/python3/data/recursion.py (100%) rename {astroid/testdata => testdata}/python3/data/unicode_package/__init__.py (100%) rename {astroid/testdata => testdata}/python3/data/unicode_package/core/__init__.py (100%) diff --git a/astroid/astroid/__init__.py b/astroid/__init__.py similarity index 100% rename from astroid/astroid/__init__.py rename to astroid/__init__.py diff --git a/astroid/astroid/as_string.py b/astroid/as_string.py similarity index 100% rename from astroid/astroid/as_string.py rename to astroid/as_string.py diff --git a/astroid/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py similarity index 100% rename from astroid/astroid/brain/brain_builtin_inference.py rename to astroid/brain/brain_builtin_inference.py diff --git a/astroid/astroid/brain/brain_dateutil.py b/astroid/brain/brain_dateutil.py similarity index 100% rename from astroid/astroid/brain/brain_dateutil.py rename to astroid/brain/brain_dateutil.py diff --git a/astroid/astroid/brain/brain_gi.py b/astroid/brain/brain_gi.py similarity index 100% rename from astroid/astroid/brain/brain_gi.py rename to astroid/brain/brain_gi.py diff --git a/astroid/astroid/brain/brain_mechanize.py b/astroid/brain/brain_mechanize.py similarity index 100% rename from astroid/astroid/brain/brain_mechanize.py rename to astroid/brain/brain_mechanize.py diff --git a/astroid/astroid/brain/brain_nose.py b/astroid/brain/brain_nose.py similarity index 100% rename from astroid/astroid/brain/brain_nose.py rename to astroid/brain/brain_nose.py diff --git a/astroid/astroid/brain/brain_numpy.py b/astroid/brain/brain_numpy.py similarity index 100% rename from astroid/astroid/brain/brain_numpy.py rename to astroid/brain/brain_numpy.py diff --git a/astroid/astroid/brain/brain_pytest.py b/astroid/brain/brain_pytest.py similarity index 100% rename from astroid/astroid/brain/brain_pytest.py rename to astroid/brain/brain_pytest.py diff --git a/astroid/astroid/brain/brain_qt.py b/astroid/brain/brain_qt.py similarity index 100% rename from astroid/astroid/brain/brain_qt.py rename to astroid/brain/brain_qt.py diff --git a/astroid/astroid/brain/brain_six.py b/astroid/brain/brain_six.py similarity index 100% rename from astroid/astroid/brain/brain_six.py rename to astroid/brain/brain_six.py diff --git a/astroid/astroid/brain/brain_stdlib.py b/astroid/brain/brain_stdlib.py similarity index 100% rename from astroid/astroid/brain/brain_stdlib.py rename to astroid/brain/brain_stdlib.py diff --git a/astroid/astroid/builder.py b/astroid/builder.py similarity index 100% rename from astroid/astroid/builder.py rename to astroid/builder.py diff --git a/astroid/astroid/context.py b/astroid/context.py similarity index 100% rename from astroid/astroid/context.py rename to astroid/context.py diff --git a/astroid/astroid/decorators.py b/astroid/decorators.py similarity index 100% rename from astroid/astroid/decorators.py rename to astroid/decorators.py diff --git a/astroid/astroid/exceptions.py b/astroid/exceptions.py similarity index 100% rename from astroid/astroid/exceptions.py rename to astroid/exceptions.py diff --git a/astroid/astroid/inference.py b/astroid/inference.py similarity index 100% rename from astroid/astroid/inference.py rename to astroid/inference.py diff --git a/astroid/astroid/interpreter/__init__.py b/astroid/interpreter/__init__.py similarity index 100% rename from astroid/astroid/interpreter/__init__.py rename to astroid/interpreter/__init__.py diff --git a/astroid/astroid/interpreter/lookup.py b/astroid/interpreter/lookup.py similarity index 100% rename from astroid/astroid/interpreter/lookup.py rename to astroid/interpreter/lookup.py diff --git a/astroid/astroid/interpreter/objects.py b/astroid/interpreter/objects.py similarity index 100% rename from astroid/astroid/interpreter/objects.py rename to astroid/interpreter/objects.py diff --git a/astroid/astroid/interpreter/runtimeabc.py b/astroid/interpreter/runtimeabc.py similarity index 100% rename from astroid/astroid/interpreter/runtimeabc.py rename to astroid/interpreter/runtimeabc.py diff --git a/astroid/astroid/interpreter/scope.py b/astroid/interpreter/scope.py similarity index 100% rename from astroid/astroid/interpreter/scope.py rename to astroid/interpreter/scope.py diff --git a/astroid/astroid/interpreter/util.py b/astroid/interpreter/util.py similarity index 100% rename from astroid/astroid/interpreter/util.py rename to astroid/interpreter/util.py diff --git a/astroid/astroid/manager.py b/astroid/manager.py similarity index 100% rename from astroid/astroid/manager.py rename to astroid/manager.py diff --git a/astroid/astroid/modutils.py b/astroid/modutils.py similarity index 100% rename from astroid/astroid/modutils.py rename to astroid/modutils.py diff --git a/astroid/astroid/nodes.py b/astroid/nodes.py similarity index 100% rename from astroid/astroid/nodes.py rename to astroid/nodes.py diff --git a/astroid/astroid/protocols.py b/astroid/protocols.py similarity index 100% rename from astroid/astroid/protocols.py rename to astroid/protocols.py diff --git a/astroid/astroid/raw_building.py b/astroid/raw_building.py similarity index 100% rename from astroid/astroid/raw_building.py rename to astroid/raw_building.py diff --git a/astroid/astroid/test_utils.py b/astroid/test_utils.py similarity index 100% rename from astroid/astroid/test_utils.py rename to astroid/test_utils.py diff --git a/astroid/astroid/tests/__init__.py b/astroid/tests/__init__.py similarity index 100% rename from astroid/astroid/tests/__init__.py rename to astroid/tests/__init__.py diff --git a/astroid/astroid/tests/resources.py b/astroid/tests/resources.py similarity index 97% rename from astroid/astroid/tests/resources.py rename to astroid/tests/resources.py index c08e56b60c..175b9d1734 100644 --- a/astroid/astroid/tests/resources.py +++ b/astroid/tests/resources.py @@ -25,7 +25,7 @@ from astroid import MANAGER -DATA_DIR = 'astroid/testdata/python{}/'.format(sys.version_info[0]) +DATA_DIR = 'testdata/python{}/'.format(sys.version_info[0]) BUILTINS = six.moves.builtins.__name__ def find(name): diff --git a/astroid/astroid/tests/unittest_brain.py b/astroid/tests/unittest_brain.py similarity index 100% rename from astroid/astroid/tests/unittest_brain.py rename to astroid/tests/unittest_brain.py diff --git a/astroid/astroid/tests/unittest_builder.py b/astroid/tests/unittest_builder.py similarity index 100% rename from astroid/astroid/tests/unittest_builder.py rename to astroid/tests/unittest_builder.py diff --git a/astroid/astroid/tests/unittest_helpers.py b/astroid/tests/unittest_helpers.py similarity index 100% rename from astroid/astroid/tests/unittest_helpers.py rename to astroid/tests/unittest_helpers.py diff --git a/astroid/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py similarity index 100% rename from astroid/astroid/tests/unittest_inference.py rename to astroid/tests/unittest_inference.py diff --git a/astroid/astroid/tests/unittest_lookup.py b/astroid/tests/unittest_lookup.py similarity index 100% rename from astroid/astroid/tests/unittest_lookup.py rename to astroid/tests/unittest_lookup.py diff --git a/astroid/astroid/tests/unittest_manager.py b/astroid/tests/unittest_manager.py similarity index 100% rename from astroid/astroid/tests/unittest_manager.py rename to astroid/tests/unittest_manager.py diff --git a/astroid/astroid/tests/unittest_modutils.py b/astroid/tests/unittest_modutils.py similarity index 100% rename from astroid/astroid/tests/unittest_modutils.py rename to astroid/tests/unittest_modutils.py diff --git a/astroid/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py similarity index 100% rename from astroid/astroid/tests/unittest_nodes.py rename to astroid/tests/unittest_nodes.py diff --git a/astroid/astroid/tests/unittest_objects.py b/astroid/tests/unittest_objects.py similarity index 100% rename from astroid/astroid/tests/unittest_objects.py rename to astroid/tests/unittest_objects.py diff --git a/astroid/astroid/tests/unittest_protocols.py b/astroid/tests/unittest_protocols.py similarity index 100% rename from astroid/astroid/tests/unittest_protocols.py rename to astroid/tests/unittest_protocols.py diff --git a/astroid/astroid/tests/unittest_python3.py b/astroid/tests/unittest_python3.py similarity index 100% rename from astroid/astroid/tests/unittest_python3.py rename to astroid/tests/unittest_python3.py diff --git a/astroid/astroid/tests/unittest_raw_building.py b/astroid/tests/unittest_raw_building.py similarity index 100% rename from astroid/astroid/tests/unittest_raw_building.py rename to astroid/tests/unittest_raw_building.py diff --git a/astroid/astroid/tests/unittest_regrtest.py b/astroid/tests/unittest_regrtest.py similarity index 100% rename from astroid/astroid/tests/unittest_regrtest.py rename to astroid/tests/unittest_regrtest.py diff --git a/astroid/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py similarity index 100% rename from astroid/astroid/tests/unittest_scoped_nodes.py rename to astroid/tests/unittest_scoped_nodes.py diff --git a/astroid/astroid/tests/unittest_transforms.py b/astroid/tests/unittest_transforms.py similarity index 100% rename from astroid/astroid/tests/unittest_transforms.py rename to astroid/tests/unittest_transforms.py diff --git a/astroid/astroid/tests/unittest_utils.py b/astroid/tests/unittest_utils.py similarity index 100% rename from astroid/astroid/tests/unittest_utils.py rename to astroid/tests/unittest_utils.py diff --git a/astroid/astroid/transforms.py b/astroid/transforms.py similarity index 100% rename from astroid/astroid/transforms.py rename to astroid/transforms.py diff --git a/astroid/astroid/tree/__init__.py b/astroid/tree/__init__.py similarity index 100% rename from astroid/astroid/tree/__init__.py rename to astroid/tree/__init__.py diff --git a/astroid/astroid/tree/base.py b/astroid/tree/base.py similarity index 100% rename from astroid/astroid/tree/base.py rename to astroid/tree/base.py diff --git a/astroid/astroid/tree/node_classes.py b/astroid/tree/node_classes.py similarity index 100% rename from astroid/astroid/tree/node_classes.py rename to astroid/tree/node_classes.py diff --git a/astroid/astroid/tree/rebuilder.py b/astroid/tree/rebuilder.py similarity index 100% rename from astroid/astroid/tree/rebuilder.py rename to astroid/tree/rebuilder.py diff --git a/astroid/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py similarity index 100% rename from astroid/astroid/tree/scoped_nodes.py rename to astroid/tree/scoped_nodes.py diff --git a/astroid/astroid/tree/treeabc.py b/astroid/tree/treeabc.py similarity index 100% rename from astroid/astroid/tree/treeabc.py rename to astroid/tree/treeabc.py diff --git a/astroid/astroid/util.py b/astroid/util.py similarity index 100% rename from astroid/astroid/util.py rename to astroid/util.py diff --git a/setup.py b/setup.py index 95f23759ee..4b2a48eb59 100644 --- a/setup.py +++ b/setup.py @@ -64,8 +64,10 @@ def install(): include_package_data = True, install_requires = install_requires, packages = find_packages(), - cmdclass = {'install_lib': AstroidInstallLib, - 'easy_install': AstroidEasyInstallLib}, + cmdclass={'install_lib': AstroidInstallLib, + 'easy_install': AstroidEasyInstallLib}, + data_files=[(p, [os.path.join(p, n) for n in ns]) for p, _, ns in os.walk('testdata/')] + # [('testdata', [os.path.join(p, n) for p, _, ns in os.walk('testdata/') for n in ns])] ) diff --git a/astroid/testdata/python2/data/MyPyPa-0.1.0-py2.5.egg b/testdata/python2/data/MyPyPa-0.1.0-py2.5.egg similarity index 100% rename from astroid/testdata/python2/data/MyPyPa-0.1.0-py2.5.egg rename to testdata/python2/data/MyPyPa-0.1.0-py2.5.egg diff --git a/astroid/testdata/python2/data/MyPyPa-0.1.0-py2.5.zip b/testdata/python2/data/MyPyPa-0.1.0-py2.5.zip similarity index 100% rename from astroid/testdata/python2/data/MyPyPa-0.1.0-py2.5.zip rename to testdata/python2/data/MyPyPa-0.1.0-py2.5.zip diff --git a/astroid/testdata/python2/data/SSL1/Connection1.py b/testdata/python2/data/SSL1/Connection1.py similarity index 100% rename from astroid/testdata/python2/data/SSL1/Connection1.py rename to testdata/python2/data/SSL1/Connection1.py diff --git a/astroid/testdata/python2/data/SSL1/__init__.py b/testdata/python2/data/SSL1/__init__.py similarity index 100% rename from astroid/testdata/python2/data/SSL1/__init__.py rename to testdata/python2/data/SSL1/__init__.py diff --git a/astroid/testdata/python2/data/__init__.py b/testdata/python2/data/__init__.py similarity index 100% rename from astroid/testdata/python2/data/__init__.py rename to testdata/python2/data/__init__.py diff --git a/astroid/testdata/python2/data/absimp/__init__.py b/testdata/python2/data/absimp/__init__.py similarity index 100% rename from astroid/testdata/python2/data/absimp/__init__.py rename to testdata/python2/data/absimp/__init__.py diff --git a/astroid/testdata/python2/data/absimp/sidepackage/__init__.py b/testdata/python2/data/absimp/sidepackage/__init__.py similarity index 100% rename from astroid/testdata/python2/data/absimp/sidepackage/__init__.py rename to testdata/python2/data/absimp/sidepackage/__init__.py diff --git a/astroid/testdata/python2/data/absimp/string.py b/testdata/python2/data/absimp/string.py similarity index 100% rename from astroid/testdata/python2/data/absimp/string.py rename to testdata/python2/data/absimp/string.py diff --git a/astroid/testdata/python2/data/absimport.py b/testdata/python2/data/absimport.py similarity index 100% rename from astroid/testdata/python2/data/absimport.py rename to testdata/python2/data/absimport.py diff --git a/astroid/testdata/python2/data/all.py b/testdata/python2/data/all.py similarity index 100% rename from astroid/testdata/python2/data/all.py rename to testdata/python2/data/all.py diff --git a/astroid/testdata/python2/data/appl/__init__.py b/testdata/python2/data/appl/__init__.py similarity index 100% rename from astroid/testdata/python2/data/appl/__init__.py rename to testdata/python2/data/appl/__init__.py diff --git a/astroid/testdata/python2/data/appl/myConnection.py b/testdata/python2/data/appl/myConnection.py similarity index 100% rename from astroid/testdata/python2/data/appl/myConnection.py rename to testdata/python2/data/appl/myConnection.py diff --git a/astroid/testdata/python2/data/descriptor_crash.py b/testdata/python2/data/descriptor_crash.py similarity index 100% rename from astroid/testdata/python2/data/descriptor_crash.py rename to testdata/python2/data/descriptor_crash.py diff --git a/astroid/testdata/python2/data/email.py b/testdata/python2/data/email.py similarity index 100% rename from astroid/testdata/python2/data/email.py rename to testdata/python2/data/email.py diff --git a/astroid/testdata/python2/data/find_test/__init__.py b/testdata/python2/data/find_test/__init__.py similarity index 100% rename from astroid/testdata/python2/data/find_test/__init__.py rename to testdata/python2/data/find_test/__init__.py diff --git a/astroid/testdata/python2/data/find_test/module.py b/testdata/python2/data/find_test/module.py similarity index 100% rename from astroid/testdata/python2/data/find_test/module.py rename to testdata/python2/data/find_test/module.py diff --git a/astroid/testdata/python2/data/find_test/module2.py b/testdata/python2/data/find_test/module2.py similarity index 100% rename from astroid/testdata/python2/data/find_test/module2.py rename to testdata/python2/data/find_test/module2.py diff --git a/astroid/testdata/python2/data/find_test/noendingnewline.py b/testdata/python2/data/find_test/noendingnewline.py similarity index 100% rename from astroid/testdata/python2/data/find_test/noendingnewline.py rename to testdata/python2/data/find_test/noendingnewline.py diff --git a/astroid/testdata/python2/data/find_test/nonregr.py b/testdata/python2/data/find_test/nonregr.py similarity index 100% rename from astroid/testdata/python2/data/find_test/nonregr.py rename to testdata/python2/data/find_test/nonregr.py diff --git a/astroid/testdata/python2/data/format.py b/testdata/python2/data/format.py similarity index 100% rename from astroid/testdata/python2/data/format.py rename to testdata/python2/data/format.py diff --git a/astroid/testdata/python2/data/invalid_encoding.py b/testdata/python2/data/invalid_encoding.py similarity index 100% rename from astroid/testdata/python2/data/invalid_encoding.py rename to testdata/python2/data/invalid_encoding.py diff --git a/astroid/testdata/python2/data/lmfp/__init__.py b/testdata/python2/data/lmfp/__init__.py similarity index 100% rename from astroid/testdata/python2/data/lmfp/__init__.py rename to testdata/python2/data/lmfp/__init__.py diff --git a/astroid/testdata/python2/data/lmfp/foo.py b/testdata/python2/data/lmfp/foo.py similarity index 100% rename from astroid/testdata/python2/data/lmfp/foo.py rename to testdata/python2/data/lmfp/foo.py diff --git a/astroid/testdata/python2/data/module.py b/testdata/python2/data/module.py similarity index 100% rename from astroid/testdata/python2/data/module.py rename to testdata/python2/data/module.py diff --git a/astroid/testdata/python2/data/module1abs/__init__.py b/testdata/python2/data/module1abs/__init__.py similarity index 100% rename from astroid/testdata/python2/data/module1abs/__init__.py rename to testdata/python2/data/module1abs/__init__.py diff --git a/astroid/testdata/python2/data/module1abs/core.py b/testdata/python2/data/module1abs/core.py similarity index 100% rename from astroid/testdata/python2/data/module1abs/core.py rename to testdata/python2/data/module1abs/core.py diff --git a/astroid/testdata/python2/data/module2.py b/testdata/python2/data/module2.py similarity index 100% rename from astroid/testdata/python2/data/module2.py rename to testdata/python2/data/module2.py diff --git a/astroid/testdata/python2/data/noendingnewline.py b/testdata/python2/data/noendingnewline.py similarity index 100% rename from astroid/testdata/python2/data/noendingnewline.py rename to testdata/python2/data/noendingnewline.py diff --git a/astroid/testdata/python2/data/nonregr.py b/testdata/python2/data/nonregr.py similarity index 100% rename from astroid/testdata/python2/data/nonregr.py rename to testdata/python2/data/nonregr.py diff --git a/astroid/testdata/python2/data/notall.py b/testdata/python2/data/notall.py similarity index 100% rename from astroid/testdata/python2/data/notall.py rename to testdata/python2/data/notall.py diff --git a/astroid/testdata/python2/data/notamodule/file.py b/testdata/python2/data/notamodule/file.py similarity index 100% rename from astroid/testdata/python2/data/notamodule/file.py rename to testdata/python2/data/notamodule/file.py diff --git a/astroid/testdata/python2/data/package/__init__.py b/testdata/python2/data/package/__init__.py similarity index 100% rename from astroid/testdata/python2/data/package/__init__.py rename to testdata/python2/data/package/__init__.py diff --git a/astroid/testdata/python2/data/package/absimport.py b/testdata/python2/data/package/absimport.py similarity index 100% rename from astroid/testdata/python2/data/package/absimport.py rename to testdata/python2/data/package/absimport.py diff --git a/astroid/testdata/python2/data/package/hello.py b/testdata/python2/data/package/hello.py similarity index 100% rename from astroid/testdata/python2/data/package/hello.py rename to testdata/python2/data/package/hello.py diff --git a/astroid/testdata/python2/data/package/import_package_subpackage_module.py b/testdata/python2/data/package/import_package_subpackage_module.py similarity index 100% rename from astroid/testdata/python2/data/package/import_package_subpackage_module.py rename to testdata/python2/data/package/import_package_subpackage_module.py diff --git a/astroid/testdata/python2/data/package/subpackage/__init__.py b/testdata/python2/data/package/subpackage/__init__.py similarity index 100% rename from astroid/testdata/python2/data/package/subpackage/__init__.py rename to testdata/python2/data/package/subpackage/__init__.py diff --git a/astroid/testdata/python2/data/package/subpackage/module.py b/testdata/python2/data/package/subpackage/module.py similarity index 100% rename from astroid/testdata/python2/data/package/subpackage/module.py rename to testdata/python2/data/package/subpackage/module.py diff --git a/astroid/testdata/python2/data/recursion.py b/testdata/python2/data/recursion.py similarity index 100% rename from astroid/testdata/python2/data/recursion.py rename to testdata/python2/data/recursion.py diff --git a/astroid/testdata/python2/data/unicode_package/__init__.py b/testdata/python2/data/unicode_package/__init__.py similarity index 100% rename from astroid/testdata/python2/data/unicode_package/__init__.py rename to testdata/python2/data/unicode_package/__init__.py diff --git a/astroid/testdata/python2/data/unicode_package/core/__init__.py b/testdata/python2/data/unicode_package/core/__init__.py similarity index 100% rename from astroid/testdata/python2/data/unicode_package/core/__init__.py rename to testdata/python2/data/unicode_package/core/__init__.py diff --git a/astroid/testdata/python3/data/MyPyPa-0.1.0-py2.5.egg b/testdata/python3/data/MyPyPa-0.1.0-py2.5.egg similarity index 100% rename from astroid/testdata/python3/data/MyPyPa-0.1.0-py2.5.egg rename to testdata/python3/data/MyPyPa-0.1.0-py2.5.egg diff --git a/astroid/testdata/python3/data/MyPyPa-0.1.0-py2.5.zip b/testdata/python3/data/MyPyPa-0.1.0-py2.5.zip similarity index 100% rename from astroid/testdata/python3/data/MyPyPa-0.1.0-py2.5.zip rename to testdata/python3/data/MyPyPa-0.1.0-py2.5.zip diff --git a/astroid/testdata/python3/data/SSL1/Connection1.py b/testdata/python3/data/SSL1/Connection1.py similarity index 100% rename from astroid/testdata/python3/data/SSL1/Connection1.py rename to testdata/python3/data/SSL1/Connection1.py diff --git a/astroid/testdata/python3/data/SSL1/__init__.py b/testdata/python3/data/SSL1/__init__.py similarity index 100% rename from astroid/testdata/python3/data/SSL1/__init__.py rename to testdata/python3/data/SSL1/__init__.py diff --git a/astroid/testdata/python3/data/__init__.py b/testdata/python3/data/__init__.py similarity index 100% rename from astroid/testdata/python3/data/__init__.py rename to testdata/python3/data/__init__.py diff --git a/astroid/testdata/python3/data/absimp/__init__.py b/testdata/python3/data/absimp/__init__.py similarity index 100% rename from astroid/testdata/python3/data/absimp/__init__.py rename to testdata/python3/data/absimp/__init__.py diff --git a/astroid/testdata/python3/data/absimp/sidepackage/__init__.py b/testdata/python3/data/absimp/sidepackage/__init__.py similarity index 100% rename from astroid/testdata/python3/data/absimp/sidepackage/__init__.py rename to testdata/python3/data/absimp/sidepackage/__init__.py diff --git a/astroid/testdata/python3/data/absimp/string.py b/testdata/python3/data/absimp/string.py similarity index 100% rename from astroid/testdata/python3/data/absimp/string.py rename to testdata/python3/data/absimp/string.py diff --git a/astroid/testdata/python3/data/absimport.py b/testdata/python3/data/absimport.py similarity index 100% rename from astroid/testdata/python3/data/absimport.py rename to testdata/python3/data/absimport.py diff --git a/astroid/testdata/python3/data/all.py b/testdata/python3/data/all.py similarity index 100% rename from astroid/testdata/python3/data/all.py rename to testdata/python3/data/all.py diff --git a/astroid/testdata/python3/data/appl/__init__.py b/testdata/python3/data/appl/__init__.py similarity index 100% rename from astroid/testdata/python3/data/appl/__init__.py rename to testdata/python3/data/appl/__init__.py diff --git a/astroid/testdata/python3/data/appl/myConnection.py b/testdata/python3/data/appl/myConnection.py similarity index 100% rename from astroid/testdata/python3/data/appl/myConnection.py rename to testdata/python3/data/appl/myConnection.py diff --git a/astroid/testdata/python3/data/descriptor_crash.py b/testdata/python3/data/descriptor_crash.py similarity index 100% rename from astroid/testdata/python3/data/descriptor_crash.py rename to testdata/python3/data/descriptor_crash.py diff --git a/astroid/testdata/python3/data/email.py b/testdata/python3/data/email.py similarity index 100% rename from astroid/testdata/python3/data/email.py rename to testdata/python3/data/email.py diff --git a/astroid/testdata/python3/data/find_test/__init__.py b/testdata/python3/data/find_test/__init__.py similarity index 100% rename from astroid/testdata/python3/data/find_test/__init__.py rename to testdata/python3/data/find_test/__init__.py diff --git a/astroid/testdata/python3/data/find_test/module.py b/testdata/python3/data/find_test/module.py similarity index 100% rename from astroid/testdata/python3/data/find_test/module.py rename to testdata/python3/data/find_test/module.py diff --git a/astroid/testdata/python3/data/find_test/module2.py b/testdata/python3/data/find_test/module2.py similarity index 100% rename from astroid/testdata/python3/data/find_test/module2.py rename to testdata/python3/data/find_test/module2.py diff --git a/astroid/testdata/python3/data/find_test/noendingnewline.py b/testdata/python3/data/find_test/noendingnewline.py similarity index 100% rename from astroid/testdata/python3/data/find_test/noendingnewline.py rename to testdata/python3/data/find_test/noendingnewline.py diff --git a/astroid/testdata/python3/data/find_test/nonregr.py b/testdata/python3/data/find_test/nonregr.py similarity index 100% rename from astroid/testdata/python3/data/find_test/nonregr.py rename to testdata/python3/data/find_test/nonregr.py diff --git a/astroid/testdata/python3/data/format.py b/testdata/python3/data/format.py similarity index 100% rename from astroid/testdata/python3/data/format.py rename to testdata/python3/data/format.py diff --git a/astroid/testdata/python3/data/invalid_encoding.py b/testdata/python3/data/invalid_encoding.py similarity index 100% rename from astroid/testdata/python3/data/invalid_encoding.py rename to testdata/python3/data/invalid_encoding.py diff --git a/astroid/testdata/python3/data/lmfp/__init__.py b/testdata/python3/data/lmfp/__init__.py similarity index 100% rename from astroid/testdata/python3/data/lmfp/__init__.py rename to testdata/python3/data/lmfp/__init__.py diff --git a/astroid/testdata/python3/data/lmfp/foo.py b/testdata/python3/data/lmfp/foo.py similarity index 100% rename from astroid/testdata/python3/data/lmfp/foo.py rename to testdata/python3/data/lmfp/foo.py diff --git a/astroid/testdata/python3/data/module.py b/testdata/python3/data/module.py similarity index 100% rename from astroid/testdata/python3/data/module.py rename to testdata/python3/data/module.py diff --git a/astroid/testdata/python3/data/module1abs/__init__.py b/testdata/python3/data/module1abs/__init__.py similarity index 100% rename from astroid/testdata/python3/data/module1abs/__init__.py rename to testdata/python3/data/module1abs/__init__.py diff --git a/astroid/testdata/python3/data/module1abs/core.py b/testdata/python3/data/module1abs/core.py similarity index 100% rename from astroid/testdata/python3/data/module1abs/core.py rename to testdata/python3/data/module1abs/core.py diff --git a/astroid/testdata/python3/data/module2.py b/testdata/python3/data/module2.py similarity index 100% rename from astroid/testdata/python3/data/module2.py rename to testdata/python3/data/module2.py diff --git a/astroid/testdata/python3/data/noendingnewline.py b/testdata/python3/data/noendingnewline.py similarity index 100% rename from astroid/testdata/python3/data/noendingnewline.py rename to testdata/python3/data/noendingnewline.py diff --git a/astroid/testdata/python3/data/nonregr.py b/testdata/python3/data/nonregr.py similarity index 100% rename from astroid/testdata/python3/data/nonregr.py rename to testdata/python3/data/nonregr.py diff --git a/astroid/testdata/python3/data/notall.py b/testdata/python3/data/notall.py similarity index 100% rename from astroid/testdata/python3/data/notall.py rename to testdata/python3/data/notall.py diff --git a/astroid/testdata/python3/data/notamodule/file.py b/testdata/python3/data/notamodule/file.py similarity index 100% rename from astroid/testdata/python3/data/notamodule/file.py rename to testdata/python3/data/notamodule/file.py diff --git a/astroid/testdata/python3/data/package/__init__.py b/testdata/python3/data/package/__init__.py similarity index 100% rename from astroid/testdata/python3/data/package/__init__.py rename to testdata/python3/data/package/__init__.py diff --git a/astroid/testdata/python3/data/package/absimport.py b/testdata/python3/data/package/absimport.py similarity index 100% rename from astroid/testdata/python3/data/package/absimport.py rename to testdata/python3/data/package/absimport.py diff --git a/astroid/testdata/python3/data/package/hello.py b/testdata/python3/data/package/hello.py similarity index 100% rename from astroid/testdata/python3/data/package/hello.py rename to testdata/python3/data/package/hello.py diff --git a/astroid/testdata/python3/data/package/import_package_subpackage_module.py b/testdata/python3/data/package/import_package_subpackage_module.py similarity index 100% rename from astroid/testdata/python3/data/package/import_package_subpackage_module.py rename to testdata/python3/data/package/import_package_subpackage_module.py diff --git a/astroid/testdata/python3/data/package/subpackage/__init__.py b/testdata/python3/data/package/subpackage/__init__.py similarity index 100% rename from astroid/testdata/python3/data/package/subpackage/__init__.py rename to testdata/python3/data/package/subpackage/__init__.py diff --git a/astroid/testdata/python3/data/package/subpackage/module.py b/testdata/python3/data/package/subpackage/module.py similarity index 100% rename from astroid/testdata/python3/data/package/subpackage/module.py rename to testdata/python3/data/package/subpackage/module.py diff --git a/astroid/testdata/python3/data/recursion.py b/testdata/python3/data/recursion.py similarity index 100% rename from astroid/testdata/python3/data/recursion.py rename to testdata/python3/data/recursion.py diff --git a/astroid/testdata/python3/data/unicode_package/__init__.py b/testdata/python3/data/unicode_package/__init__.py similarity index 100% rename from astroid/testdata/python3/data/unicode_package/__init__.py rename to testdata/python3/data/unicode_package/__init__.py diff --git a/astroid/testdata/python3/data/unicode_package/core/__init__.py b/testdata/python3/data/unicode_package/core/__init__.py similarity index 100% rename from astroid/testdata/python3/data/unicode_package/core/__init__.py rename to testdata/python3/data/unicode_package/core/__init__.py diff --git a/tox.ini b/tox.ini index 50169c8d3e..844a4cd0a3 100644 --- a/tox.ini +++ b/tox.ini @@ -32,3 +32,4 @@ deps = setenv = PYTHONWARNINGS = i # {posargs} allows passing command line arguments to unittest commands = coverage run --source {envsitepackagesdir}/astroid --append --branch -m unittest discover {posargs} -t {envsitepackagesdir}/astroid/ -s {envsitepackagesdir}/astroid/astroid/tests -p "unittest*.py" + From 5c6b19de694621b1c58852b9614615ae2401a671 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Thu, 11 Feb 2016 14:35:43 +0000 Subject: [PATCH 202/312] Revert "setup.py install now installs testdata/, but pip doesn't" This reverts commit c510c17f63c2df01b8c9d5e2fd97ac963f737c7f. --- setup.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/setup.py b/setup.py index 4b2a48eb59..2150d1c26e 100644 --- a/setup.py +++ b/setup.py @@ -65,9 +65,7 @@ def install(): install_requires = install_requires, packages = find_packages(), cmdclass={'install_lib': AstroidInstallLib, - 'easy_install': AstroidEasyInstallLib}, - data_files=[(p, [os.path.join(p, n) for n in ns]) for p, _, ns in os.walk('testdata/')] - # [('testdata', [os.path.join(p, n) for p, _, ns in os.walk('testdata/') for n in ns])] + 'easy_install': AstroidEasyInstallLib} ) From 41a24c207483ca77053f2adc7ecb4a950cf913a6 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Thu, 11 Feb 2016 14:42:33 +0000 Subject: [PATCH 203/312] Revert "Modify tox.ini to properly exclude files not part of astroid" This reverts commit 4596201f9146e64c18cfcb0f841618328e953910. --- tox.ini | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tox.ini b/tox.ini index 844a4cd0a3..9f23d7ce85 100644 --- a/tox.ini +++ b/tox.ini @@ -10,8 +10,8 @@ commands = coverage erase [testenv:coverage-stats] commands = - coverage report --ignore-errors - coverage html --ignore-errors + coverage report --ignore-errors --include="*astroid*" + coverage html --ignore-errors --include="*astroid*" [testenv] deps = @@ -31,5 +31,5 @@ deps = # Disable warnings because they overflow the Travis log on 3.5 setenv = PYTHONWARNINGS = i # {posargs} allows passing command line arguments to unittest -commands = coverage run --source {envsitepackagesdir}/astroid --append --branch -m unittest discover {posargs} -t {envsitepackagesdir}/astroid/ -s {envsitepackagesdir}/astroid/astroid/tests -p "unittest*.py" +commands = coverage run --append --branch -m unittest discover {posargs} -t {envsitepackagesdir}/astroid/ -s {envsitepackagesdir}/astroid/astroid/tests -p "unittest*.py" From a4a12428946b533dfebd5ecc38bcbef39288b499 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Thu, 11 Feb 2016 14:44:39 +0000 Subject: [PATCH 204/312] Revert change to tests command. --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 9f23d7ce85..3dca0d5b8f 100644 --- a/tox.ini +++ b/tox.ini @@ -31,5 +31,5 @@ deps = # Disable warnings because they overflow the Travis log on 3.5 setenv = PYTHONWARNINGS = i # {posargs} allows passing command line arguments to unittest -commands = coverage run --append --branch -m unittest discover {posargs} -t {envsitepackagesdir}/astroid/ -s {envsitepackagesdir}/astroid/astroid/tests -p "unittest*.py" +commands = coverage run --append --branch -m unittest discover {posargs} -s {envsitepackagesdir}/astroid/tests -p "unittest*.py" From 6d2529632bae545ff7564501cac14316d5ea9204 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 1 Feb 2016 11:53:36 +0000 Subject: [PATCH 205/312] Changed the way how parameters are being built The old way consisted in having the parameter names, their defaults and their annotations separated in different components of the Arguments node. We introduced a new Param node, which holds the name of a parameter, its default value and its annotation. If any of the last two values are missing, then that slot will be filled with a new node kind, Empty, which is used for specifying the lack of something (None could have been used instead, but that means having non-AST nodes in the Arguments node). We're also having support for positional only arguments, for the moment only in raw_building. Close #215 --- ChangeLog | 19 +++ astroid/inference.py | 1 + astroid/interpreter/lookup.py | 1 + astroid/interpreter/scope.py | 21 ++-- astroid/nodes.py | 6 +- astroid/protocols.py | 7 +- astroid/raw_building.py | 96 ++++++++------- astroid/test_utils.py | 13 +- astroid/tests/unittest_brain.py | 4 +- astroid/tests/unittest_nodes.py | 3 + astroid/tests/unittest_python3.py | 40 +++--- astroid/tests/unittest_scoped_nodes.py | 2 +- astroid/tree/base.py | 2 +- astroid/tree/node_classes.py | 157 ++++++++++++------------ astroid/tree/rebuilder.py | 162 ++++++++++++++++--------- astroid/tree/scoped_nodes.py | 30 +++-- astroid/tree/treeabc.py | 13 ++ 17 files changed, 349 insertions(+), 228 deletions(-) diff --git a/ChangeLog b/ChangeLog index 251986c13d..f405b4e4db 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,25 @@ Change log for the astroid package (used to be astng) ===================================================== -- + * Changed the way how parameters are being built + + The old way consisted in having the parameter names, their + defaults and their annotations separated in different components + of the Arguments node. We introduced a new Param node, which holds + the name of a parameter, its default value and its annotation. + If any of the last two values are missing, then that slot will be + filled with a new node kind, Empty, which is used for specifying the + lack of something (None could have been used instead, but that means having + non-AST nodes in the Arguments node). + We're also having support for positional only arguments, for the moment + only in raw_building. + + * We don't support nested arguments in functions in Python 2 + anymore. + + This was dropped in order to simplify the implementation. + When they are encountered, we'll unflatten them into a list + of parameters, as if they were not nested from the beginning. * NodeNG.nearest was removed. It's not an API that we were using and it was buggy. diff --git a/astroid/inference.py b/astroid/inference.py index 1a7ce413a5..e4074bc15f 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -698,6 +698,7 @@ def infer_arguments(self, context=None, nodes=None): @infer.register(treeabc.AssignName) @infer.register(treeabc.AssignAttr) +@infer.register(treeabc.Parameter) @decorators.path_wrapper def infer_assign(self, context=None): """infer a AssignName/AssignAttr: need to inspect the RHS part of the diff --git a/astroid/interpreter/lookup.py b/astroid/interpreter/lookup.py index 8a8eb2a08b..20fda77a81 100644 --- a/astroid/interpreter/lookup.py +++ b/astroid/interpreter/lookup.py @@ -238,6 +238,7 @@ def locals_new_scope(node, locals_): @_get_locals.register(treeabc.DelName) @_get_locals.register(treeabc.FunctionDef) @_get_locals.register(treeabc.ClassDef) +@_get_locals.register(treeabc.Parameter) def locals_name(node, locals_): '''These nodes add a name to the local variables. AssignName and DelName have no children while FunctionDef and ClassDef start a diff --git a/astroid/interpreter/scope.py b/astroid/interpreter/scope.py index 0d092ce2df..47fb3a0cd5 100644 --- a/astroid/interpreter/scope.py +++ b/astroid/interpreter/scope.py @@ -41,20 +41,23 @@ def _scope_by_parent(parent, node): # in order to decouple the implementation for the normal cases. +def _node_arguments(node): + for arg in itertools.chain(node.positional_and_keyword, node.keyword_only, + (node.vararg, ), (node.kwarg, )): + if arg and arg.annotation: + yield arg + + @_scope_by_parent.register(treeabc.Arguments) def _scope_by_argument_parent(parent, node): args = parent - if node in itertools.chain(args.defaults, args.kw_defaults): - return args.parent.parent.scope() - if six.PY3: - look_for = itertools.chain( - (args.kwargannotation, ), - (args.varargannotation, ), - args.kwonly_annotations, - args.annotations) - if node in look_for: + for param in itertools.chain(args.positional_and_keyword, args.keyword_only): + if param.default == node: return args.parent.parent.scope() + if six.PY3 and node in _node_arguments(args): + return args.parent.parent.scope() + @_scope_by_parent.register(treeabc.FunctionDef) def _scope_by_function_parent(parent, node): diff --git a/astroid/nodes.py b/astroid/nodes.py index 1aea53f275..6fb3765282 100644 --- a/astroid/nodes.py +++ b/astroid/nodes.py @@ -40,9 +40,9 @@ Arguments, AssignAttr, Assert, Assign, AssignName, AugAssign, Repr, BinOp, BoolOp, Break, Call, Compare, Comprehension, Const, Continue, Decorators, DelAttr, DelName, Delete, - Dict, Expr, Ellipsis, ExceptHandler, Exec, ExtSlice, For, + Dict, Empty, Expr, Ellipsis, ExceptHandler, Exec, ExtSlice, For, ImportFrom, Attribute, Global, If, IfExp, Import, Index, Keyword, - List, Name, NameConstant, Nonlocal, Pass, Print, Raise, Return, Set, Slice, + List, Name, NameConstant, Nonlocal, Pass, Parameter, Print, Raise, Return, Set, Slice, Starred, Subscript, TryExcept, TryFinally, Tuple, UnaryOp, While, With, WithItem, Yield, YieldFrom, AsyncFor, Await, AsyncWith, # Node not present in the builtin ast module. @@ -74,7 +74,7 @@ Lambda, List, ListComp, Name, NameConstant, Nonlocal, Module, - Pass, Print, + Parameter, Pass, Print, Raise, ReservedName, Return, Set, SetComp, Slice, Starred, Subscript, TryExcept, TryFinally, Tuple, diff --git a/astroid/protocols.py b/astroid/protocols.py index eada976481..7fdc6e291b 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -304,6 +304,7 @@ def mulass_assigned_stmts(self, nodes, node=None, context=None, assign_path=None @assigned_stmts.register(treeabc.AssignName) @assigned_stmts.register(treeabc.AssignAttr) +@assigned_stmts.register(treeabc.Parameter) def assend_assigned_stmts(self, nodes, node=None, context=None, assign_path=None): return self.parent.assigned_stmts(self, context=context) @@ -311,7 +312,7 @@ def assend_assigned_stmts(self, nodes, node=None, context=None, assign_path=None def _arguments_infer_argname(self, name, context, nodes): # arguments information may be missing, in which case we can't do anything # more - if not (self.args or self.vararg or self.kwarg): + if not self.args and (not self.vararg and not self.kwarg): yield util.Uninferable return # first argument of instance/class method @@ -336,10 +337,10 @@ def _arguments_infer_argname(self, name, context, nodes): return # TODO: just provide the type here, no need to have an empty Dict. - if name == self.vararg: + if self.vararg and name == self.vararg.name: yield nodes.Tuple(parent=self) return - if name == self.kwarg: + if self.kwarg and name == self.kwarg.name: yield nodes.Dict(parent=self) return # if there is a default value, yield it. And then yield Uninferable to reflect diff --git a/astroid/raw_building.py b/astroid/raw_building.py index f387114317..bccd83dafc 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -319,65 +319,71 @@ def ast_from_function(func, built_objects, module, name=None, parent=None): itertools.groupby(signature.parameters.values(), operator.attrgetter('kind'))} - def extract_args(parameters, parent): - '''Takes an iterator over Parameter objects and returns three - sequences, arg names, default values, and annotations. - - ''' - names = [] - defaults = [] - annotations = [] + def _extract_args(parameters, parent): + """Generate an iterator of Parameter nodes from a list of inspect.Parameter objects.""" for parameter in parameters: - names.append(parameter.name) + param = node_classes.Parameter(name=parameter.name, + col_offset=parent.col_offset, + lineno=parent.lineno, + parent=parent) + default = node_classes.Empty + annotation = node_classes.Empty if parameter.default is not _Parameter.empty: - defaults.extend(_ast_from_object(parameter.default, built_objects, module, parent=parent)) + default = _ast_from_object(parameter.default, built_objects, + module, parent=parent) + if parameter.annotation is not _Parameter.empty: - annotations.extend(_ast_from_object(parameter.annotation, built_objects, module, parent=parent)) - else: - annotations.append(None) - return names, defaults, annotations + annotation = _ast_from_object(parameter.annotation, built_objects, + module, parent=parent) - def extract_vararg(parameter): - '''Takes a single-element iterator possibly containing a Parameter and - returns a name and an annotation. + param.postinit(default=default, annotation=annotation) + yield param - ''' + def _extract_vararg(parameter, parent): + """Build a variadic Parameter node from an inspect.Parameter object.""" try: - return parameter[0].name + parameter = parameter[0] except IndexError: - return None - - vararg = parameters.get(_Parameter.VAR_POSITIONAL, ()) - kwarg = parameters.get(_Parameter.VAR_KEYWORD, ()) - vararg_name = extract_vararg(vararg) - kwarg_name = extract_vararg(kwarg) - args_node = node_classes.Arguments(vararg=vararg_name, kwarg=kwarg_name, parent=func_node) - - # This ignores POSITIONAL_ONLY args, because they only appear in - # functions implemented in C and can't be mimicked by any Python - # function. - names, defaults, annotations = extract_args(parameters.get(_Parameter.POSITIONAL_OR_KEYWORD, ()), args_node) - kwonlynames, kw_defaults, kwonly_annotations = extract_args(parameters.get(_Parameter.KEYWORD_ONLY, ()), args_node) - args = [node_classes.AssignName(name=n, parent=args_node) for n in names] - kwonlyargs = [node_classes.AssignName(name=n, parent=args_node) for n in kwonlynames] - if vararg_name and vararg[0].annotation is not _Parameter.empty: - varargannotation = vararg.annotation - else: - varargannotation = None - if kwarg_name and kwarg[0].annotation is not _Parameter.empty: - kwargannotation = kwarg.annotation - else: - kwargannotation = None + return node_classes.Empty + + if parameter.annotation is not _Parameter.empty: + annotation = _ast_from_object(parameter.annotation, + built_objects, module, parent=parent)[0] + else: + annotation = node_classes.Empty + + param = node_classes.Parameter(name=parameter.name, + lineno=parent.lineno, + col_offset=parent.col_offset, + parent=parent) + param.postinit(annotation=annotation, default=node_classes.Empty) + return param + + args_node = node_classes.Arguments(parent=func_node) + args = _extract_args(parameters.get(_Parameter.POSITIONAL_OR_KEYWORD, ()), + args_node) + keyword_only = _extract_args(parameters.get(_Parameter.KEYWORD_ONLY, ()), + args_node) + positional_only = _extract_args(parameters.get(_Parameter.POSITIONAL_ONLY, ()), + args_node) + python_vararg = parameters.get(_Parameter.VAR_POSITIONAL, ()) + python_kwarg = parameters.get(_Parameter.VAR_KEYWORD, ()) + vararg = _extract_vararg(python_vararg, args_node) + kwarg = _extract_vararg(python_kwarg, args_node) + returns = None if signature.return_annotation is not _Parameter.empty: returns = _ast_from_object(signature.return_annotation, built_objects, module, parent=func_node)[0] - args_node.postinit(args, defaults, kwonlyargs, kw_defaults, - annotations, kwonly_annotations, - varargannotation, kwargannotation) + args_node.postinit(args=list(args), + vararg=vararg, + kwarg=kwarg, + keyword_only=list(keyword_only), + positional_only=list(positional_only)) func_node.postinit(args=args_node, body=[], returns=returns) + for name in set(dir(func)) - set(dir(type(func))): # This checks against method special attributes because # methods are also dispatched through this function. diff --git a/astroid/test_utils.py b/astroid/test_utils.py index b15ab766b4..ff81cd59f8 100644 --- a/astroid/test_utils.py +++ b/astroid/test_utils.py @@ -19,6 +19,7 @@ # when calling extract_node. _STATEMENT_SELECTOR = '#@' + def _extract_expressions(node): """Find expressions in a call to _TRANSIENT_FUNCTION and extract them. @@ -46,8 +47,18 @@ def _extract_expressions(node): child = getattr(node.parent, name) if isinstance(child, (list, tuple)): for idx, compound_child in enumerate(child): - if compound_child is node: + + # Can't find a cleaner way to do this. + if isinstance(compound_child, nodes.Parameter): + if compound_child.default is node: + child[idx].default = real_expr + elif compound_child.annotation is node: + child[idx].annotation = real_expr + else: + child[idx] = real_expr + elif compound_child is node: child[idx] = real_expr + elif child is node: setattr(node.parent, name, real_expr) yield real_expr diff --git a/astroid/tests/unittest_brain.py b/astroid/tests/unittest_brain.py index 85f6f04c72..a12b242e8d 100644 --- a/astroid/tests/unittest_brain.py +++ b/astroid/tests/unittest_brain.py @@ -84,7 +84,9 @@ def test_hashlib(self): self.assertIn('block_size', class_obj) self.assertIn('digest_size', class_obj) self.assertEqual(len(class_obj['__init__'].args.args), 2) - self.assertEqual(len(class_obj['__init__'].args.defaults), 1) + default = class_obj['__init__'].args.args[1].default + self.assertIsInstance(default, nodes.Const) + self.assertEqual(default.value, '') self.assertEqual(len(class_obj['update'].args.args), 2) self.assertEqual(len(class_obj['digest'].args.args), 1) self.assertEqual(len(class_obj['hexdigest'].args.args), 1) diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index fab82eef79..5b89043a70 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -491,6 +491,9 @@ def hello(False): class ArgumentsNodeTC(unittest.TestCase): + + @unittest.skipIf(sys.version_info[:2] == (3, 3), + "Line numbering is broken on Python 3.3.") def test_linenumbering(self): ast = builder.parse(''' def func(a, diff --git a/astroid/tests/unittest_python3.py b/astroid/tests/unittest_python3.py index 1dd07c611e..3cd7b1f5de 100644 --- a/astroid/tests/unittest_python3.py +++ b/astroid/tests/unittest_python3.py @@ -20,6 +20,7 @@ from astroid import nodes from astroid.tree.node_classes import Assign, Expr, YieldFrom, Name, Const +from astroid import raw_building from astroid.builder import AstroidBuilder from astroid.tree.scoped_nodes import ClassDef, FunctionDef from astroid.test_utils import require_version, extract_node @@ -187,31 +188,31 @@ def test(a: int, b: str, c: None, d, e, pass """)) func = astroid['test'] - self.assertIsInstance(func.args.varargannotation, Name) - self.assertEqual(func.args.varargannotation.name, 'float') - self.assertIsInstance(func.args.kwargannotation, Name) - self.assertEqual(func.args.kwargannotation.name, 'int') + self.assertIsInstance(func.args.vararg.annotation, Name) + self.assertEqual(func.args.vararg.annotation.name, 'float') + self.assertIsInstance(func.args.kwarg.annotation, Name) + self.assertEqual(func.args.kwarg.annotation.name, 'int') self.assertIsInstance(func.returns, Name) self.assertEqual(func.returns.name, 'int') arguments = func.args - self.assertIsInstance(arguments.annotations[0], Name) - self.assertEqual(arguments.annotations[0].name, 'int') - self.assertIsInstance(arguments.annotations[1], Name) - self.assertEqual(arguments.annotations[1].name, 'str') - self.assertIsInstance(arguments.annotations[2], Const) - self.assertIsNone(arguments.annotations[2].value) - self.assertIsNone(arguments.annotations[3]) - self.assertIsNone(arguments.annotations[4]) + self.assertIsInstance(arguments.args[0].annotation, Name) + self.assertEqual(arguments.args[0].annotation.name, 'int') + self.assertIsInstance(arguments.args[1].annotation, Name) + self.assertEqual(arguments.args[1].annotation.name, 'str') + self.assertIsInstance(arguments.args[2].annotation, Const) + self.assertIsNone(arguments.args[2].annotation.value) + self.assertIs(arguments.args[3].annotation, nodes.Empty) + self.assertIs(arguments.args[4].annotation, nodes.Empty) astroid = self.builder.string_build(dedent(""" def test(a: int=1, b: str=2): pass """)) func = astroid['test'] - self.assertIsInstance(func.args.annotations[0], Name) - self.assertEqual(func.args.annotations[0].name, 'int') - self.assertIsInstance(func.args.annotations[1], Name) - self.assertEqual(func.args.annotations[1].name, 'str') + self.assertIsInstance(func.args.args[0].annotation, Name) + self.assertEqual(func.args.args[0].annotation.name, 'int') + self.assertIsInstance(func.args.args[1].annotation, Name) + self.assertEqual(func.args.args[1].annotation.name, 'str') self.assertIsNone(func.returns) @require_version('3.0') @@ -249,6 +250,13 @@ def test_unpacking_in_dict_getitem(self): self.assertIsInstance(value, nodes.Const) self.assertEqual(value.value, expected) + @require_version('3.4') + def test_positional_only_parameters(self): + ast = raw_building.ast_from_object(issubclass) + self.assertEqual(len(ast.args.positional_only), 2) + for name, arg in zip(('cls', 'class_or_tuple'), ast.args.positional_only): + self.assertEqual(arg.name, name) + if __name__ == '__main__': unittest.main() diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index d96003651a..462a1f202c 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -284,7 +284,7 @@ def nested_args(a, (b, c, d)): tree = builder.parse(code) func = tree['nested_args'] self.assertEqual(sorted(func.locals), ['a', 'b', 'c', 'd']) - self.assertEqual(func.args.format_args(), 'a, (b, c, d)') + self.assertEqual(func.args.format_args(), 'a, b, c, d') def test_four_args(self): func = self.module['four_args'] diff --git a/astroid/tree/base.py b/astroid/tree/base.py index de1375601f..58298b8d5c 100644 --- a/astroid/tree/base.py +++ b/astroid/tree/base.py @@ -633,7 +633,7 @@ def _filter_stmts(self, stmts, frame, offset): if not (optional_assign or interpreterutil.are_exclusive(_stmts[pindex], node)): del _stmt_parents[pindex] del _stmts[pindex] - if isinstance(node, treeabc.AssignName): + if isinstance(node, (treeabc.Parameter, treeabc.AssignName)): if not optional_assign and stmt.parent is mystmt.parent: _stmts = [] _stmt_parents = [] diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index 722e2b75ed..7189b068be 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -98,18 +98,36 @@ def assigned_stmts(self, node=None, context=None, assign_path=None): # Name classes -@util.register_implementation(treeabc.AssignName) -class AssignName(base.LookupMixIn, base.ParentAssignTypeMixin, - AssignedStmtsMixin, base.NodeNG): - """class representing an AssignName node""" + +class BaseAssignName(base.LookupMixIn, base.ParentAssignTypeMixin, + AssignedStmtsMixin, base.NodeNG): _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) + super(BaseAssignName, self).__init__(lineno, col_offset, parent) infer_lhs = inference.infer_name +@util.register_implementation(treeabc.AssignName) +class AssignName(BaseAssignName): + """class representing an AssignName node""" + + +@util.register_implementation(treeabc.Parameter) +class Parameter(BaseAssignName): + + _astroid_fields = ('default', 'annotation') + _other_fields = ('name', ) + + def __init__(self, name=None, lineno=None, col_offset=None, parent=None): + super(Parameter, self).__init__(name=name, lineno=lineno, + col_offset=col_offset, parent=parent) + + def postinit(self, default, annotation): + self.default = default + self.annotation = annotation + @util.register_implementation(treeabc.DelName) class DelName(base.LookupMixIn, base.ParentAssignTypeMixin, base.NodeNG): @@ -129,54 +147,25 @@ class Name(base.LookupMixIn, base.NodeNG): def __init__(self, name=None, lineno=None, col_offset=None, parent=None): self.name = name super(Name, self).__init__(lineno, col_offset, parent) - + @util.register_implementation(treeabc.Arguments) class Arguments(base.AssignTypeMixin, AssignedStmtsMixin, base.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', 'kwonly_annotations', - 'varargannotation', 'kwargannotation') - 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): + _astroid_fields = ('args', 'vararg', 'kwarg', 'keyword_only', 'positional_only') + + def __init__(self, parent=None): + # We don't want lineno and col_offset from the parent's __init__. + super(Arguments, self).__init__(parent=parent) + + def postinit(self, args, vararg, kwarg, keyword_only, positional_only): + self.args = args self.vararg = vararg self.kwarg = kwarg - self.parent = parent - self.args = [] - self.defaults = [] - self.kwonlyargs = [] - self.kw_defaults = [] - self.annotations = [] - self.kwonly_annotations = [] - - def postinit(self, args, defaults, kwonlyargs, kw_defaults, - annotations, kwonly_annotations, varargannotation=None, - kwargannotation=None): - self.args = args - self.defaults = defaults - self.kwonlyargs = kwonlyargs - self.kw_defaults = kw_defaults - self.annotations = annotations - self.varargannotation = varargannotation - self.kwargannotation = kwargannotation - self.kwonly_annotations = kwonly_annotations + self.keyword_only = keyword_only + self.positional_only = positional_only + self.positional_and_keyword = self.args + self.positional_only def _infer_name(self, frame, name): if self.parent is frame: @@ -194,19 +183,16 @@ def fromlineno(self): 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.positional_and_keyword: + result.append(_format_args(self.positional_and_keyword)) if self.vararg: - result.append('*%s' % self.vararg) - if self.kwonlyargs: + result.append('*%s' % _format_args((self.vararg, ))) + if self.keyword_only: if not self.vararg: result.append('*') - result.append(_format_args(self.kwonlyargs, self.kw_defaults)) + result.append(_format_args(self.keyword_only)) if self.kwarg: - result.append('**%s' % self.kwarg) + result.append('**%s' % _format_args((self.kwarg, ))) return ', '.join(result) def default_value(self, argname): @@ -214,28 +200,28 @@ def default_value(self, argname): :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] + for place in (self.positional_and_keyword, self.keyword_only): + i = _find_arg(argname, place)[0] + if i is not None: + value = place[i] + if not value.default: + continue + return value.default + 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: + if self.vararg and name == self.vararg.name: return True - if name == self.kwarg: + if self.kwarg and name == self.kwarg.name: return True return self.find_argname(name, 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) + if self.positional_and_keyword: # self.args may be None in some cases (builtin function) + return _find_arg(argname, self.positional_and_keyword, rec) return None, None def get_children(self): @@ -257,27 +243,24 @@ def _find_arg(argname, args, rec=False): return None, None -def _format_args(args, defaults=None, annotations=None): +def _format_args(args): values = [] - if args is None: + if not args: 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): + for i, arg in enumerate(args): if isinstance(arg, Tuple): values.append('(%s)' % _format_args(arg.elts)) else: argname = arg.name - if annotation is not None: + annotation = arg.annotation + if annotation: argname += ':' + annotation.as_string() values.append(argname) + + default = arg.default + if default: + values[-1] += '=' + default.as_string() - 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) @@ -1322,6 +1305,22 @@ class DictUnpack(base.NodeNG): """Represents the unpacking of dicts into dicts using PEP 448.""" +@object.__new__ +@util.register_implementation(treeabc.Empty) +class Empty(base.NodeNG): + """Empty nodes represents the lack of something + + For instance, they can be used to represent missing annotations + or defaults for arguments or anything where None is a valid + value. + """ + + def __bool__(self): + return False + + __nonzero__ = __bool__ + + # Register additional inference dispatched functions. We do # this here, since we need to pass this module as an argument # to these functions, in order to avoid circular dependencies diff --git a/astroid/tree/rebuilder.py b/astroid/tree/rebuilder.py index 90a02f7354..074aea1e8c 100644 --- a/astroid/tree/rebuilder.py +++ b/astroid/tree/rebuilder.py @@ -20,6 +20,7 @@ """ import ast +import collections import sys import astroid @@ -113,6 +114,53 @@ def _get_context(node): return CONTEXTS.get(type(node.ctx), astroid.Load) +class ParameterVisitor(object): + """A visitor which is used for building the components of Arguments node.""" + + def __init__(self, visitor): + self._visitor = visitor + + def visit(self, param_node, *args): + cls_name = param_node.__class__.__name__ + visit_name = 'visit_' + REDIRECT.get(cls_name, cls_name).lower() + visit_method = getattr(self, visit_name) + return visit_method(param_node, *args) + + def visit_arg(self, param_node, *args): + name = param_node.arg + return self._build_parameter(param_node, name, *args) + + def visit_name(self, param_node, *args): + name = param_node.id + return self._build_parameter(param_node, name, *args) + + def visit_tuple(self, param_node, parent, default): + # We're not supporting nested arguments anymore, but in order to + # simply not crash when running on Python 2, we're unpacking the elements + # before hand. We simply don't want to support this feature anymore, + # so it's possible to be broken. + converted_node = self._visitor.visit(param_node, parent) + for element in converted_node.elts: + param = nodes.Parameter(name=element.name, lineno=param_node.lineno, + col_offset=param_node.col_offset, + parent=parent) + param.postinit(default=default, annotation=nodes.Empty) + yield param + + def _build_parameter(self, param_node, name, parent, default): + param = nodes.Parameter(name=name, lineno=getattr(param_node, 'lineno', None), + col_offset=getattr(param_node, 'col_offset', None), + parent=parent) + annotation = nodes.Empty + param_annotation = getattr(param_node, 'annotation', None) + if param_annotation: + annotation = self._visitor.visit(param_annotation, param) + + param.postinit(default=default, annotation=annotation) + yield param + + + class TreeRebuilder(object): """Rebuilds the ast tree to become an Astroid tree""" @@ -141,57 +189,64 @@ def visit(self, node, parent): def visit_arguments(self, node, parent): """visit a Arguments node by returning a fresh instance of it""" - vararg, kwarg = node.vararg, node.kwarg - if PY34: - newnode = nodes.Arguments(vararg.arg if vararg else None, - kwarg.arg if kwarg else None, - parent) - else: - newnode = nodes.Arguments(vararg, kwarg, parent) - args = [self.visit(child, newnode) for child in node.args] - defaults = [self.visit(child, newnode) - for child in node.defaults] - varargannotation = None - kwargannotation = None - # change added in 82732 (7c5c678e4164), vararg and kwarg - # are instances of `ast.arg`, not strings - if vararg: - if PY34: - if node.vararg.annotation: - varargannotation = self.visit(node.vararg.annotation, - newnode) - vararg = vararg.arg - elif PY3 and node.varargannotation: - varargannotation = self.visit(node.varargannotation, - newnode) - if kwarg: - if PY34: - if node.kwarg.annotation: - kwargannotation = self.visit(node.kwarg.annotation, - newnode) - kwarg = kwarg.arg - elif PY3: - if node.kwargannotation: - kwargannotation = self.visit(node.kwargannotation, - newnode) - if PY3: - kwonlyargs = [self.visit(child, newnode) for child - in node.kwonlyargs] - kw_defaults = [self.visit(child, newnode) if child else - None for child in node.kw_defaults] - annotations = [self.visit(arg.annotation, newnode) if - arg.annotation else None for arg in node.args] - kwonly_annotations = [self.visit(arg.annotation, newnode) - if arg.annotation else None - for arg in node.kwonlyargs] - else: - kwonlyargs = [] - kw_defaults = [] - annotations = [] - kwonly_annotations = [] - newnode.postinit(args, defaults, kwonlyargs, kw_defaults, - annotations, kwonly_annotations, - varargannotation, kwargannotation) + def _build_variadic(field_name): + param = nodes.Empty + variadic = getattr(node, field_name) + + if variadic: + # Various places to get the name from. + try: + param_name = variadic.id + except AttributeError: + try: + param_name = variadic.arg + except AttributeError: + param_name = variadic + + param = nodes.Parameter(name=param_name, + lineno=newnode.lineno, + col_offset=newnode.col_offset, + parent=newnode) + # Get the annotation of the variadic node. + annotation = nodes.Empty + default = nodes.Empty + variadic_annotation = getattr(variadic, 'annotation', None) + if variadic_annotation is None: + # Support for Python 3.3. + variadic_annotation = getattr(node, field_name + 'annotation', None) + if variadic_annotation: + annotation = self.visit(variadic_annotation, param) + + param.postinit(default=default, annotation=annotation) + return param + + def _build_args(params, defaults): + # Pad the list of defaults so that each arguments gets a default. + defaults = collections.deque(defaults) + while len(defaults) != len(params): + defaults.appendleft(nodes.Empty) + + param_visitor = ParameterVisitor(self) + for parameter in params: + default = defaults.popleft() + if default: + default = self.visit(default, newnode) + + for param in param_visitor.visit(parameter, newnode, default): + yield param + + newnode = nodes.Arguments(parent=parent) + # Build the arguments list. + positional_args = list(_build_args(node.args, node.defaults)) + kwonlyargs = list(_build_args(getattr(node, 'kwonlyargs', ()), + getattr(node, 'kw_defaults', ()))) + # Build vararg and kwarg. + vararg = _build_variadic('vararg') + kwarg = _build_variadic('kwarg') + # Prepare the arguments new node. + newnode.postinit(args=positional_args, vararg=vararg, kwarg=kwarg, + keyword_only=kwonlyargs, + positional_only=[]) return newnode def visit_assert(self, node, parent): @@ -729,11 +784,6 @@ def visit_yield(self, node, parent): class TreeRebuilder3(TreeRebuilder): """extend and overwrite TreeRebuilder for python3k""" - def visit_arg(self, node, parent): - """visit a arg node by returning a fresh AssName instance""" - # TODO(cpopa): introduce an Arg node instead of using AssignName. - return self.visit_assignname(node, parent, node.arg) - def visit_nameconstant(self, node, parent): # in Python 3.4 we have NameConstant for True / False / None return nodes.NameConstant(node.value, getattr(node, 'lineno', None), diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index d04b549b08..6744ec8279 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -686,7 +686,7 @@ def infer_argument(self, name, context): pass # Too many arguments given and no variable arguments. - if len(self.positional_arguments) > len(self._funcnode.args.args): + if len(self.positional_arguments) > len(self._funcnode.args.positional_and_keyword): if not self._funcnode.args.vararg: raise exceptions.InferenceError('Too many positional arguments ' 'passed to {func!r} that does ' @@ -694,10 +694,10 @@ def infer_argument(self, name, context): call_site=self, func=self._funcnode, arg=name, context=context) - positional = self.positional_arguments[:len(self._funcnode.args.args)] - vararg = self.positional_arguments[len(self._funcnode.args.args):] + positional = self.positional_arguments[:len(self._funcnode.args.positional_and_keyword)] + vararg = self.positional_arguments[len(self._funcnode.args.positional_and_keyword):] argindex = self._funcnode.args.find_argname(name)[0] - kwonlyargs = set(arg.name for arg in self._funcnode.args.kwonlyargs) + kwonlyargs = set(arg.name for arg in self._funcnode.args.keyword_only) kwargs = { key: value for key, value in self.keyword_arguments.items() if key not in kwonlyargs @@ -707,8 +707,8 @@ def infer_argument(self, name, context): # if the missing positional arguments were passed # as keyword arguments and if so, place them into the # positional args list. - if len(positional) < len(self._funcnode.args.args): - for func_arg in self._funcnode.args.args: + if len(positional) < len(self._funcnode.args.positional_and_keyword): + for func_arg in self._funcnode.args.positional_and_keyword: if func_arg.name in kwargs: arg = kwargs.pop(func_arg.name) positional.append(arg) @@ -748,7 +748,7 @@ def infer_argument(self, name, context): except IndexError: pass - if self._funcnode.args.kwarg == name: + if self._funcnode.args.kwarg and self._funcnode.args.kwarg.name == name: # It wants all the keywords that were passed into # the call site. if self.has_invalid_keywords(): @@ -769,7 +769,7 @@ def infer_argument(self, name, context): kwarg.postinit(keys, values) return iter((kwarg, )) - elif self._funcnode.args.vararg == name: + if self._funcnode.args.vararg and self._funcnode.args.vararg.name == name: # It wants all the args that were passed into # the call site. if self.has_invalid_arguments(): @@ -813,7 +813,9 @@ def called_with(self, args, keywords): return CallSite(self, args, keywords) def scope_lookup(self, node, name, offset=0): - if node in self.args.defaults or node in self.args.kw_defaults: + + if node in itertools.chain((self.args.positional_and_keyword, + self.args.keyword_only)): frame = self.parent.frame() # line offset to avoid that def func(f=func) resolve the default # value to the defined function @@ -825,14 +827,16 @@ def scope_lookup(self, node, name, offset=0): def argnames(self): """return a list of argument names""" - if self.args.args: # maybe None with builtin functions - names = _rec_get_names(self.args.args) + if self.args.positional_and_keyword: # maybe None with builtin functions + names = _rec_get_names(self.args.positional_and_keyword) else: names = [] if self.args.vararg: - names.append(self.args.vararg) + names.append(self.args.vararg.name) if self.args.kwarg: - names.append(self.args.kwarg) + names.append(self.args.kwarg.name) + if self.args.keyword_only: + names.extend([arg.name for arg in self.keyword_only]) return names def callable(self): diff --git a/astroid/tree/treeabc.py b/astroid/tree/treeabc.py index 6ad96ba81d..5c00825584 100644 --- a/astroid/tree/treeabc.py +++ b/astroid/tree/treeabc.py @@ -67,6 +67,10 @@ class Name(NodeNG): """Class representing a Name node""" +class Parameter(NodeNG): + """Class representing a parameter node.""" + + class Arguments(NodeNG): """Class representing an Arguments node""" @@ -339,3 +343,12 @@ class ReservedName(NodeNG): class Unknown(NodeNG): pass + + +class Empty(NodeNG): + """Empty nodes represents the lack of something + + For instance, they can be used to represent missing annotations + or defaults for arguments or anything where None is a valid + value. + """ From ffa1ba873ca879ae2d259903b8add63f6e6eb2ac Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sat, 13 Feb 2016 15:01:03 +0000 Subject: [PATCH 206/312] Move testdata back into tests. --- .travis.yml | 2 - MANIFEST.in | 2 + astroid/as_string.py | 15 +- astroid/tests/resources.py | 26 +++- .../testdata}/data/MyPyPa-0.1.0-py2.5.egg | Bin .../testdata}/data/MyPyPa-0.1.0-py2.5.zip | Bin .../tests/testdata}/data/SSL1/Connection1.py | 0 .../tests/testdata}/data/SSL1/__init__.py | 0 .../tests/testdata}/data/__init__.py | 0 .../tests/testdata}/data/absimp/__init__.py | 0 .../data/absimp/sidepackage/__init__.py | 0 .../tests/testdata}/data/absimp/string.py | 0 .../tests/testdata}/data/absimport.py | 0 .../tests/testdata}/data/all.py | 2 +- .../tests/testdata}/data/appl/__init__.py | 0 .../tests/testdata}/data/appl/myConnection.py | 1 + .../tests/testdata}/data/descriptor_crash.py | 0 .../tests/testdata}/data/email.py | 0 .../testdata}/data/find_test/__init__.py | 0 .../tests/testdata}/data/find_test/module.py | 0 .../tests/testdata}/data/find_test/module2.py | 0 .../data/find_test/noendingnewline.py | 0 .../tests/testdata}/data/find_test/nonregr.py | 0 .../tests/testdata}/data/format.py | 0 .../tests/testdata}/data/lmfp/__init__.py | 0 .../tests/testdata}/data/lmfp/foo.py | 0 .../tests/testdata}/data/module.py | 6 +- .../testdata}/data/module1abs/__init__.py | 0 .../tests/testdata}/data/module1abs/core.py | 0 .../tests/testdata}/data/module2.py | 2 - .../tests/testdata}/data/noendingnewline.py | 0 .../tests/testdata}/data/nonregr.py | 0 .../tests/testdata}/data/notall.py | 0 .../tests/testdata}/data/notamodule/file.py | 0 .../tests/testdata}/data/package/__init__.py | 0 .../tests/testdata}/data/package/absimport.py | 0 .../tests/testdata}/data/package/hello.py | 0 .../import_package_subpackage_module.py | 0 .../data/package/subpackage/__init__.py | 0 .../data/package/subpackage/module.py | 0 .../tests/testdata}/data/recursion.py | 4 +- .../data/unicode_package/__init__.py | 0 .../data/unicode_package/core/__init__.py | 0 astroid/tests/unittest_builder.py | 51 +++++-- astroid/tests/unittest_modutils.py | 2 + astroid/tests/unittest_nodes.py | 1 + astroid/tests/unittest_scoped_nodes.py | 22 ++- testdata/python2/data/SSL1/Connection1.py | 14 -- testdata/python2/data/SSL1/__init__.py | 1 - testdata/python2/data/appl/myConnection.py | 12 -- testdata/python2/data/invalid_encoding.py | 1 - testdata/python2/data/module2.py | 143 ------------------ testdata/python2/data/noendingnewline.py | 36 ----- testdata/python2/data/notall.py | 7 - testdata/python3/data/MyPyPa-0.1.0-py2.5.egg | Bin 1222 -> 0 bytes testdata/python3/data/MyPyPa-0.1.0-py2.5.zip | Bin 1222 -> 0 bytes testdata/python3/data/__init__.py | 1 - testdata/python3/data/absimp/__init__.py | 5 - .../data/absimp/sidepackage/__init__.py | 3 - testdata/python3/data/absimp/string.py | 3 - testdata/python3/data/absimport.py | 3 - testdata/python3/data/all.py | 9 -- testdata/python3/data/appl/__init__.py | 3 - testdata/python3/data/descriptor_crash.py | 11 -- testdata/python3/data/email.py | 1 - testdata/python3/data/find_test/__init__.py | 0 testdata/python3/data/find_test/module.py | 0 testdata/python3/data/find_test/module2.py | 0 .../python3/data/find_test/noendingnewline.py | 0 testdata/python3/data/find_test/nonregr.py | 0 testdata/python3/data/format.py | 34 ----- testdata/python3/data/invalid_encoding.py | 1 - testdata/python3/data/lmfp/__init__.py | 2 - testdata/python3/data/lmfp/foo.py | 6 - testdata/python3/data/module.py | 87 ----------- testdata/python3/data/module1abs/__init__.py | 4 - testdata/python3/data/module1abs/core.py | 1 - testdata/python3/data/nonregr.py | 57 ------- testdata/python3/data/notamodule/file.py | 0 testdata/python3/data/package/__init__.py | 4 - testdata/python3/data/package/absimport.py | 6 - testdata/python3/data/package/hello.py | 2 - .../import_package_subpackage_module.py | 49 ------ .../data/package/subpackage/__init__.py | 1 - .../python3/data/package/subpackage/module.py | 1 - testdata/python3/data/recursion.py | 3 - .../python3/data/unicode_package/__init__.py | 1 - .../data/unicode_package/core/__init__.py | 0 88 files changed, 97 insertions(+), 551 deletions(-) rename {testdata/python2 => astroid/tests/testdata}/data/MyPyPa-0.1.0-py2.5.egg (100%) rename {testdata/python2 => astroid/tests/testdata}/data/MyPyPa-0.1.0-py2.5.zip (100%) rename {testdata/python3 => astroid/tests/testdata}/data/SSL1/Connection1.py (100%) rename {testdata/python3 => astroid/tests/testdata}/data/SSL1/__init__.py (100%) rename {testdata/python2 => astroid/tests/testdata}/data/__init__.py (100%) rename {testdata/python2 => astroid/tests/testdata}/data/absimp/__init__.py (100%) rename {testdata/python2 => astroid/tests/testdata}/data/absimp/sidepackage/__init__.py (100%) rename {testdata/python2 => astroid/tests/testdata}/data/absimp/string.py (100%) rename {testdata/python2 => astroid/tests/testdata}/data/absimport.py (100%) rename {testdata/python2 => astroid/tests/testdata}/data/all.py (78%) rename {testdata/python2 => astroid/tests/testdata}/data/appl/__init__.py (100%) rename {testdata/python3 => astroid/tests/testdata}/data/appl/myConnection.py (87%) rename {testdata/python2 => astroid/tests/testdata}/data/descriptor_crash.py (100%) rename {testdata/python2 => astroid/tests/testdata}/data/email.py (100%) rename {testdata/python2 => astroid/tests/testdata}/data/find_test/__init__.py (100%) rename {testdata/python2 => astroid/tests/testdata}/data/find_test/module.py (100%) rename {testdata/python2 => astroid/tests/testdata}/data/find_test/module2.py (100%) rename {testdata/python2 => astroid/tests/testdata}/data/find_test/noendingnewline.py (100%) rename {testdata/python2 => astroid/tests/testdata}/data/find_test/nonregr.py (100%) rename {testdata/python2 => astroid/tests/testdata}/data/format.py (100%) rename {testdata/python2 => astroid/tests/testdata}/data/lmfp/__init__.py (100%) rename {testdata/python2 => astroid/tests/testdata}/data/lmfp/foo.py (100%) rename {testdata/python2 => astroid/tests/testdata}/data/module.py (95%) rename {testdata/python2 => astroid/tests/testdata}/data/module1abs/__init__.py (100%) rename {testdata/python2 => astroid/tests/testdata}/data/module1abs/core.py (100%) rename {testdata/python3 => astroid/tests/testdata}/data/module2.py (98%) rename {testdata/python3 => astroid/tests/testdata}/data/noendingnewline.py (100%) rename {testdata/python2 => astroid/tests/testdata}/data/nonregr.py (100%) rename {testdata/python3 => astroid/tests/testdata}/data/notall.py (100%) rename {testdata/python2 => astroid/tests/testdata}/data/notamodule/file.py (100%) rename {testdata/python2 => astroid/tests/testdata}/data/package/__init__.py (100%) rename {testdata/python2 => astroid/tests/testdata}/data/package/absimport.py (100%) rename {testdata/python2 => astroid/tests/testdata}/data/package/hello.py (100%) rename {testdata/python2 => astroid/tests/testdata}/data/package/import_package_subpackage_module.py (100%) rename {testdata/python2 => astroid/tests/testdata}/data/package/subpackage/__init__.py (100%) rename {testdata/python2 => astroid/tests/testdata}/data/package/subpackage/module.py (100%) rename {testdata/python2 => astroid/tests/testdata}/data/recursion.py (96%) rename {testdata/python2 => astroid/tests/testdata}/data/unicode_package/__init__.py (100%) rename {testdata/python2 => astroid/tests/testdata}/data/unicode_package/core/__init__.py (100%) delete mode 100644 testdata/python2/data/SSL1/Connection1.py delete mode 100644 testdata/python2/data/SSL1/__init__.py delete mode 100644 testdata/python2/data/appl/myConnection.py delete mode 100644 testdata/python2/data/invalid_encoding.py delete mode 100644 testdata/python2/data/module2.py delete mode 100644 testdata/python2/data/noendingnewline.py delete mode 100644 testdata/python2/data/notall.py delete mode 100644 testdata/python3/data/MyPyPa-0.1.0-py2.5.egg delete mode 100644 testdata/python3/data/MyPyPa-0.1.0-py2.5.zip delete mode 100644 testdata/python3/data/__init__.py delete mode 100644 testdata/python3/data/absimp/__init__.py delete mode 100644 testdata/python3/data/absimp/sidepackage/__init__.py delete mode 100644 testdata/python3/data/absimp/string.py delete mode 100644 testdata/python3/data/absimport.py delete mode 100644 testdata/python3/data/all.py delete mode 100644 testdata/python3/data/appl/__init__.py delete mode 100644 testdata/python3/data/descriptor_crash.py delete mode 100644 testdata/python3/data/email.py delete mode 100644 testdata/python3/data/find_test/__init__.py delete mode 100644 testdata/python3/data/find_test/module.py delete mode 100644 testdata/python3/data/find_test/module2.py delete mode 100644 testdata/python3/data/find_test/noendingnewline.py delete mode 100644 testdata/python3/data/find_test/nonregr.py delete mode 100644 testdata/python3/data/format.py delete mode 100644 testdata/python3/data/invalid_encoding.py delete mode 100644 testdata/python3/data/lmfp/__init__.py delete mode 100644 testdata/python3/data/lmfp/foo.py delete mode 100644 testdata/python3/data/module.py delete mode 100644 testdata/python3/data/module1abs/__init__.py delete mode 100644 testdata/python3/data/module1abs/core.py delete mode 100644 testdata/python3/data/nonregr.py delete mode 100644 testdata/python3/data/notamodule/file.py delete mode 100644 testdata/python3/data/package/__init__.py delete mode 100644 testdata/python3/data/package/absimport.py delete mode 100644 testdata/python3/data/package/hello.py delete mode 100644 testdata/python3/data/package/import_package_subpackage_module.py delete mode 100644 testdata/python3/data/package/subpackage/__init__.py delete mode 100644 testdata/python3/data/package/subpackage/module.py delete mode 100644 testdata/python3/data/recursion.py delete mode 100644 testdata/python3/data/unicode_package/__init__.py delete mode 100644 testdata/python3/data/unicode_package/core/__init__.py diff --git a/.travis.yml b/.travis.yml index d2281d351a..e3a7da100b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,8 +25,6 @@ before_install: - python --version - uname -a - lsb_release -a -before_script: - - find ~ -name '*.*' | cat install: - $PYTHON_EXE -m pip install pip -U - python -m pip install tox diff --git a/MANIFEST.in b/MANIFEST.in index 642d7a6542..d185621c47 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -3,3 +3,5 @@ include README.rst include COPYING include COPYING.LESSER include tox.ini +recursive-include astroid/tests/testdata *.py *.zip *.egg +recursive-include astroid/brain *.py \ No newline at end of file diff --git a/astroid/as_string.py b/astroid/as_string.py index d99aee0aab..800617e79c 100644 --- a/astroid/as_string.py +++ b/astroid/as_string.py @@ -193,8 +193,8 @@ def visit_unknown(self, node): def visit_excepthandler(self, node): if node.type: if node.name: - excs = 'except %s, %s' % (node.type.accept(self), - node.name.accept(self)) + excs = 'except %s as %s' % (node.type.accept(self), + node.name.accept(self)) else: excs = 'except %s' % node.type.accept(self) else: @@ -442,17 +442,6 @@ def visit_uninferable(self, node): class AsStringVisitor3(AsStringVisitor): """AsStringVisitor3 overwrites some AsStringVisitor methods""" - def visit_excepthandler(self, node): - if node.type: - if node.name: - excs = 'except %s as %s' % (node.type.accept(self), - node.name.accept(self)) - else: - excs = 'except %s' % node.type.accept(self) - else: - excs = 'except' - return '%s:\n%s' % (excs, self._stmt_list(node.body)) - def visit_nonlocal(self, node): """return an astroid.Nonlocal node as string""" return 'nonlocal %s' % ', '.join(node.names) diff --git a/astroid/tests/resources.py b/astroid/tests/resources.py index 175b9d1734..1390088cd3 100644 --- a/astroid/tests/resources.py +++ b/astroid/tests/resources.py @@ -15,8 +15,12 @@ # # You should have received a copy of the GNU Lesser General Public License along # with astroid. If not, see . +import binascii +import contextlib import os import sys +import shutil +import tempfile import pkg_resources import six @@ -25,12 +29,30 @@ from astroid import MANAGER -DATA_DIR = 'testdata/python{}/'.format(sys.version_info[0]) +DATA_DIR = 'testdata' BUILTINS = six.moves.builtins.__name__ + +@contextlib.contextmanager +def _temporary_file(): + name = binascii.hexlify(os.urandom(5)).decode() + path = find(name) + try: + yield path + finally: + os.remove(path) + +@contextlib.contextmanager +def tempfile_with_content(content): + with _temporary_file() as tmp: + with open(tmp, 'wb') as stream: + stream.write(content) + yield tmp + + def find(name): return pkg_resources.resource_filename( - pkg_resources.Requirement.parse('astroid'), + 'astroid.tests', os.path.normpath(os.path.join(DATA_DIR, name))) diff --git a/testdata/python2/data/MyPyPa-0.1.0-py2.5.egg b/astroid/tests/testdata/data/MyPyPa-0.1.0-py2.5.egg similarity index 100% rename from testdata/python2/data/MyPyPa-0.1.0-py2.5.egg rename to astroid/tests/testdata/data/MyPyPa-0.1.0-py2.5.egg diff --git a/testdata/python2/data/MyPyPa-0.1.0-py2.5.zip b/astroid/tests/testdata/data/MyPyPa-0.1.0-py2.5.zip similarity index 100% rename from testdata/python2/data/MyPyPa-0.1.0-py2.5.zip rename to astroid/tests/testdata/data/MyPyPa-0.1.0-py2.5.zip diff --git a/testdata/python3/data/SSL1/Connection1.py b/astroid/tests/testdata/data/SSL1/Connection1.py similarity index 100% rename from testdata/python3/data/SSL1/Connection1.py rename to astroid/tests/testdata/data/SSL1/Connection1.py diff --git a/testdata/python3/data/SSL1/__init__.py b/astroid/tests/testdata/data/SSL1/__init__.py similarity index 100% rename from testdata/python3/data/SSL1/__init__.py rename to astroid/tests/testdata/data/SSL1/__init__.py diff --git a/testdata/python2/data/__init__.py b/astroid/tests/testdata/data/__init__.py similarity index 100% rename from testdata/python2/data/__init__.py rename to astroid/tests/testdata/data/__init__.py diff --git a/testdata/python2/data/absimp/__init__.py b/astroid/tests/testdata/data/absimp/__init__.py similarity index 100% rename from testdata/python2/data/absimp/__init__.py rename to astroid/tests/testdata/data/absimp/__init__.py diff --git a/testdata/python2/data/absimp/sidepackage/__init__.py b/astroid/tests/testdata/data/absimp/sidepackage/__init__.py similarity index 100% rename from testdata/python2/data/absimp/sidepackage/__init__.py rename to astroid/tests/testdata/data/absimp/sidepackage/__init__.py diff --git a/testdata/python2/data/absimp/string.py b/astroid/tests/testdata/data/absimp/string.py similarity index 100% rename from testdata/python2/data/absimp/string.py rename to astroid/tests/testdata/data/absimp/string.py diff --git a/testdata/python2/data/absimport.py b/astroid/tests/testdata/data/absimport.py similarity index 100% rename from testdata/python2/data/absimport.py rename to astroid/tests/testdata/data/absimport.py diff --git a/testdata/python2/data/all.py b/astroid/tests/testdata/data/all.py similarity index 78% rename from testdata/python2/data/all.py rename to astroid/tests/testdata/data/all.py index 23f7d2b66f..dd86abc560 100644 --- a/testdata/python2/data/all.py +++ b/astroid/tests/testdata/data/all.py @@ -4,6 +4,6 @@ other = 'o' class Aaa: pass -def func(): print 'yo' +def func(): pass __all__ = 'Aaa', '_bla', 'name' diff --git a/testdata/python2/data/appl/__init__.py b/astroid/tests/testdata/data/appl/__init__.py similarity index 100% rename from testdata/python2/data/appl/__init__.py rename to astroid/tests/testdata/data/appl/__init__.py diff --git a/testdata/python3/data/appl/myConnection.py b/astroid/tests/testdata/data/appl/myConnection.py similarity index 87% rename from testdata/python3/data/appl/myConnection.py rename to astroid/tests/testdata/data/appl/myConnection.py index 492695343f..398484ada0 100644 --- a/testdata/python3/data/appl/myConnection.py +++ b/astroid/tests/testdata/data/appl/myConnection.py @@ -1,3 +1,4 @@ +from __future__ import print_function from data import SSL1 class MyConnection(SSL1.Connection): diff --git a/testdata/python2/data/descriptor_crash.py b/astroid/tests/testdata/data/descriptor_crash.py similarity index 100% rename from testdata/python2/data/descriptor_crash.py rename to astroid/tests/testdata/data/descriptor_crash.py diff --git a/testdata/python2/data/email.py b/astroid/tests/testdata/data/email.py similarity index 100% rename from testdata/python2/data/email.py rename to astroid/tests/testdata/data/email.py diff --git a/testdata/python2/data/find_test/__init__.py b/astroid/tests/testdata/data/find_test/__init__.py similarity index 100% rename from testdata/python2/data/find_test/__init__.py rename to astroid/tests/testdata/data/find_test/__init__.py diff --git a/testdata/python2/data/find_test/module.py b/astroid/tests/testdata/data/find_test/module.py similarity index 100% rename from testdata/python2/data/find_test/module.py rename to astroid/tests/testdata/data/find_test/module.py diff --git a/testdata/python2/data/find_test/module2.py b/astroid/tests/testdata/data/find_test/module2.py similarity index 100% rename from testdata/python2/data/find_test/module2.py rename to astroid/tests/testdata/data/find_test/module2.py diff --git a/testdata/python2/data/find_test/noendingnewline.py b/astroid/tests/testdata/data/find_test/noendingnewline.py similarity index 100% rename from testdata/python2/data/find_test/noendingnewline.py rename to astroid/tests/testdata/data/find_test/noendingnewline.py diff --git a/testdata/python2/data/find_test/nonregr.py b/astroid/tests/testdata/data/find_test/nonregr.py similarity index 100% rename from testdata/python2/data/find_test/nonregr.py rename to astroid/tests/testdata/data/find_test/nonregr.py diff --git a/testdata/python2/data/format.py b/astroid/tests/testdata/data/format.py similarity index 100% rename from testdata/python2/data/format.py rename to astroid/tests/testdata/data/format.py diff --git a/testdata/python2/data/lmfp/__init__.py b/astroid/tests/testdata/data/lmfp/__init__.py similarity index 100% rename from testdata/python2/data/lmfp/__init__.py rename to astroid/tests/testdata/data/lmfp/__init__.py diff --git a/testdata/python2/data/lmfp/foo.py b/astroid/tests/testdata/data/lmfp/foo.py similarity index 100% rename from testdata/python2/data/lmfp/foo.py rename to astroid/tests/testdata/data/lmfp/foo.py diff --git a/testdata/python2/data/module.py b/astroid/tests/testdata/data/module.py similarity index 95% rename from testdata/python2/data/module.py rename to astroid/tests/testdata/data/module.py index 118b16f937..84185cfae1 100644 --- a/testdata/python2/data/module.py +++ b/astroid/tests/testdata/data/module.py @@ -29,7 +29,7 @@ class YO: def __init__(self): try: self.yo = 1 - except ValueError, ex: + except ValueError as ex: pass except (NameError, TypeError): raise XXXError() @@ -66,7 +66,7 @@ def static_method(): def class_method(cls): """class method test""" - exec a in b + pass class_method = classmethod(class_method) @@ -83,6 +83,6 @@ def four_args(a, b, c, d): d = ((a) and (b)) or (c) else: c = ((a) and (b)) or (d) - map(lambda x, y: (y, x), a) + list(map(lambda x, y: (y, x), a)) redirect = four_args diff --git a/testdata/python2/data/module1abs/__init__.py b/astroid/tests/testdata/data/module1abs/__init__.py similarity index 100% rename from testdata/python2/data/module1abs/__init__.py rename to astroid/tests/testdata/data/module1abs/__init__.py diff --git a/testdata/python2/data/module1abs/core.py b/astroid/tests/testdata/data/module1abs/core.py similarity index 100% rename from testdata/python2/data/module1abs/core.py rename to astroid/tests/testdata/data/module1abs/core.py diff --git a/testdata/python3/data/module2.py b/astroid/tests/testdata/data/module2.py similarity index 98% rename from testdata/python3/data/module2.py rename to astroid/tests/testdata/data/module2.py index 582ccd9824..72d08c5116 100644 --- a/testdata/python3/data/module2.py +++ b/astroid/tests/testdata/data/module2.py @@ -76,8 +76,6 @@ class Concrete23(Concrete1): bb = ((1) | (two)) | (6) ccc = ((one) & (two)) & (three) dddd = ((x) ^ (o)) ^ (r) -exec('c = 3') -exec('c = 3', {}, {}) def raise_string(a=2, *args, **kwargs): raise Exception('yo') diff --git a/testdata/python3/data/noendingnewline.py b/astroid/tests/testdata/data/noendingnewline.py similarity index 100% rename from testdata/python3/data/noendingnewline.py rename to astroid/tests/testdata/data/noendingnewline.py diff --git a/testdata/python2/data/nonregr.py b/astroid/tests/testdata/data/nonregr.py similarity index 100% rename from testdata/python2/data/nonregr.py rename to astroid/tests/testdata/data/nonregr.py diff --git a/testdata/python3/data/notall.py b/astroid/tests/testdata/data/notall.py similarity index 100% rename from testdata/python3/data/notall.py rename to astroid/tests/testdata/data/notall.py diff --git a/testdata/python2/data/notamodule/file.py b/astroid/tests/testdata/data/notamodule/file.py similarity index 100% rename from testdata/python2/data/notamodule/file.py rename to astroid/tests/testdata/data/notamodule/file.py diff --git a/testdata/python2/data/package/__init__.py b/astroid/tests/testdata/data/package/__init__.py similarity index 100% rename from testdata/python2/data/package/__init__.py rename to astroid/tests/testdata/data/package/__init__.py diff --git a/testdata/python2/data/package/absimport.py b/astroid/tests/testdata/data/package/absimport.py similarity index 100% rename from testdata/python2/data/package/absimport.py rename to astroid/tests/testdata/data/package/absimport.py diff --git a/testdata/python2/data/package/hello.py b/astroid/tests/testdata/data/package/hello.py similarity index 100% rename from testdata/python2/data/package/hello.py rename to astroid/tests/testdata/data/package/hello.py diff --git a/testdata/python2/data/package/import_package_subpackage_module.py b/astroid/tests/testdata/data/package/import_package_subpackage_module.py similarity index 100% rename from testdata/python2/data/package/import_package_subpackage_module.py rename to astroid/tests/testdata/data/package/import_package_subpackage_module.py diff --git a/testdata/python2/data/package/subpackage/__init__.py b/astroid/tests/testdata/data/package/subpackage/__init__.py similarity index 100% rename from testdata/python2/data/package/subpackage/__init__.py rename to astroid/tests/testdata/data/package/subpackage/__init__.py diff --git a/testdata/python2/data/package/subpackage/module.py b/astroid/tests/testdata/data/package/subpackage/module.py similarity index 100% rename from testdata/python2/data/package/subpackage/module.py rename to astroid/tests/testdata/data/package/subpackage/module.py diff --git a/testdata/python2/data/recursion.py b/astroid/tests/testdata/data/recursion.py similarity index 96% rename from testdata/python2/data/recursion.py rename to astroid/tests/testdata/data/recursion.py index a34dad32c9..85f6513493 100644 --- a/testdata/python2/data/recursion.py +++ b/astroid/tests/testdata/data/recursion.py @@ -1,3 +1,3 @@ -""" For issue #25 """ -class Base(object): +""" For issue #25 """ +class Base(object): pass \ No newline at end of file diff --git a/testdata/python2/data/unicode_package/__init__.py b/astroid/tests/testdata/data/unicode_package/__init__.py similarity index 100% rename from testdata/python2/data/unicode_package/__init__.py rename to astroid/tests/testdata/data/unicode_package/__init__.py diff --git a/testdata/python2/data/unicode_package/core/__init__.py b/astroid/tests/testdata/data/unicode_package/core/__init__.py similarity index 100% rename from testdata/python2/data/unicode_package/core/__init__.py rename to astroid/tests/testdata/data/unicode_package/core/__init__.py diff --git a/astroid/tests/unittest_builder.py b/astroid/tests/unittest_builder.py index 206c7da275..813c2e3dbd 100644 --- a/astroid/tests/unittest_builder.py +++ b/astroid/tests/unittest_builder.py @@ -39,7 +39,41 @@ class FromToLineNoTest(unittest.TestCase): def setUp(self): - self.astroid = resources.build_file('data/format.py') + self.astroid = builder.parse(''' + """A multiline string + """ + + function('aeozrijz\ + earzer', hop) + # XXX write test + x = [i for i in range(5) + if i % 4] + + fonction(1, + 2, + 3, + 4) + + def definition(a, + b, + c): + return a + b + c + + class debile(dict, + object): + pass + + if aaaa: pass + else: + aaaa,bbbb = 1,2 + aaaa,bbbb = bbbb,aaaa + # XXX write test + hop = \ + aaaa + + + __revision__.lower(); + ''') def test_callfunc_lineno(self): stmts = self.astroid.body @@ -48,16 +82,16 @@ def test_callfunc_lineno(self): # earzer', hop) discard = stmts[0] self.assertIsInstance(discard, nodes.Expr) - self.assertEqual(discard.fromlineno, 4) + self.assertEqual(discard.fromlineno, 5) self.assertEqual(discard.tolineno, 5) callfunc = discard.value self.assertIsInstance(callfunc, nodes.Call) - self.assertEqual(callfunc.fromlineno, 4) + self.assertEqual(callfunc.fromlineno, 5) self.assertEqual(callfunc.tolineno, 5) name = callfunc.func self.assertIsInstance(name, nodes.Name) - self.assertEqual(name.fromlineno, 4) - self.assertEqual(name.tolineno, 4) + self.assertEqual(name.fromlineno, 5) + self.assertEqual(name.tolineno, 5) strarg = callfunc.args[0] self.assertIsInstance(strarg, nodes.Const) if hasattr(sys, 'pypy_version_info'): @@ -263,10 +297,6 @@ def test_data_build_invalid_x_escape(self): with self.assertRaises(exceptions.AstroidSyntaxError): self.builder.string_build('"\\x1"') - def test_missing_newline(self): - """check that a file with no trailing new line is parseable""" - resources.build_file('data/noendingnewline.py') - def test_missing_file(self): with self.assertRaises(exceptions.AstroidBuildingError): resources.build_file('data/inexistant.py') @@ -711,7 +741,8 @@ def test_method_locals(self): def test_unknown_encoding(self): with self.assertRaises(exceptions.AstroidSyntaxError): - resources.build_file('data/invalid_encoding.py') + with resources.tempfile_with_content(b'# -*- coding: lala -*-') as tmp: + builder.AstroidBuilder().file_build(tmp) class ModuleBuildTest(resources.SysPathSetup, FileBuildTest): diff --git a/astroid/tests/unittest_modutils.py b/astroid/tests/unittest_modutils.py index 3740aa2639..6dfd3bd889 100644 --- a/astroid/tests/unittest_modutils.py +++ b/astroid/tests/unittest_modutils.py @@ -244,6 +244,7 @@ class GetModuleFilesTest(unittest.TestCase): def test_get_module_files_1(self): package = resources.find('data/find_test') modules = set(modutils.get_module_files(package, [])) + expected = ['__init__.py', 'module.py', 'module2.py', 'noendingnewline.py', 'nonregr.py'] self.assertEqual(modules, @@ -254,6 +255,7 @@ def test_get_all_files(self): """ non_package = resources.find('data/notamodule') modules = modutils.get_module_files(non_package, [], list_all=True) + self.assertEqual( modules, [os.path.join(non_package, 'file.py')], diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index fab82eef79..acfb817c01 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -106,6 +106,7 @@ def test_module_as_string(self): with open(resources.find('data/module.py'), 'r') as fobj: self.assertMultiLineEqual(module.as_string(), fobj.read()) + maxDiff = None def test_module2_as_string(self): """check as_string on a whole module prepared to be returned identically """ diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index d96003651a..edc72caab0 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -110,9 +110,23 @@ def test_getattr(self): self.assertRaises(InferenceError, self.nonregr.igetattr, 'YOAA') def test_wildcard_import_names(self): - m = resources.build_file('data/all.py', 'all') + m = builder.parse(''' + name = 'a' + _bla = 2 + other = 'o' + class Aaa: pass + def func(): print('yo') + __all__ = 'Aaa', '_bla', 'name' + ''') self.assertEqual(m.wildcard_import_names(), ['Aaa', '_bla', 'name']) - m = resources.build_file('data/notall.py', 'notall') + m = builder.parse(''' + name = 'a' + _bla = 2 + other = 'o' + class Aaa: pass + + def func(): return 'yo' + ''') res = sorted(m.wildcard_import_names()) self.assertEqual(res, ['Aaa', 'func', 'name', 'other']) @@ -214,14 +228,14 @@ def test_file_stream_in_memory(self): self.assertEqual(stream.read().decode(), data) def test_file_stream_physical(self): - path = resources.find('data/all.py') + path = resources.find('data/absimport.py') astroid = builder.AstroidBuilder().file_build(path, 'all') with open(path, 'rb') as file_io: with astroid.stream() as stream: self.assertEqual(stream.read(), file_io.read()) def test_stream_api(self): - path = resources.find('data/all.py') + path = resources.find('data/absimport.py') astroid = builder.AstroidBuilder().file_build(path, 'all') stream = astroid.stream() self.assertTrue(hasattr(stream, 'close')) diff --git a/testdata/python2/data/SSL1/Connection1.py b/testdata/python2/data/SSL1/Connection1.py deleted file mode 100644 index 6bbb13028d..0000000000 --- a/testdata/python2/data/SSL1/Connection1.py +++ /dev/null @@ -1,14 +0,0 @@ -"""M2Crypto.SSL.Connection - -Copyright (c) 1999-2004 Ng Pheng Siong. All rights reserved.""" -from __future__ import print_function -RCS_id='$Id: Connection1.py,v 1.1 2005-06-13 20:55:22 syt Exp $' - -#Some code deleted here - -class Connection: - - """An SSL connection.""" - - def __init__(self, ctx, sock=None): - print('init Connection') diff --git a/testdata/python2/data/SSL1/__init__.py b/testdata/python2/data/SSL1/__init__.py deleted file mode 100644 index a007b04932..0000000000 --- a/testdata/python2/data/SSL1/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from Connection1 import Connection diff --git a/testdata/python2/data/appl/myConnection.py b/testdata/python2/data/appl/myConnection.py deleted file mode 100644 index 5b24b259a8..0000000000 --- a/testdata/python2/data/appl/myConnection.py +++ /dev/null @@ -1,12 +0,0 @@ -from __future__ import print_function -from data import SSL1 -class MyConnection(SSL1.Connection): - - """An SSL connection.""" - - def __init__(self, dummy): - print('MyConnection init') - -if __name__ == '__main__': - myConnection = MyConnection(' ') - raw_input('Press Enter to continue...') diff --git a/testdata/python2/data/invalid_encoding.py b/testdata/python2/data/invalid_encoding.py deleted file mode 100644 index dddd208e6a..0000000000 --- a/testdata/python2/data/invalid_encoding.py +++ /dev/null @@ -1 +0,0 @@ -# -*- coding: lala -*- \ No newline at end of file diff --git a/testdata/python2/data/module2.py b/testdata/python2/data/module2.py deleted file mode 100644 index 0a1bd1adfa..0000000000 --- a/testdata/python2/data/module2.py +++ /dev/null @@ -1,143 +0,0 @@ -from data.module import YO, YOUPI -import data - - -class Specialization(YOUPI, YO): - pass - - - -class Metaclass(type): - pass - - - -class Interface: - pass - - - -class MyIFace(Interface): - pass - - - -class AnotherIFace(Interface): - pass - - - -class MyException(Exception): - pass - - - -class MyError(MyException): - pass - - - -class AbstractClass(object): - - def to_override(self, whatever): - raise NotImplementedError() - - def return_something(self, param): - if param: - return 'toto' - return - - - -class Concrete0: - __implements__ = MyIFace - - - -class Concrete1: - __implements__ = (MyIFace, AnotherIFace) - - - -class Concrete2: - __implements__ = (MyIFace, AnotherIFace) - - - -class Concrete23(Concrete1): - pass - -del YO.member -del YO -[SYN1, SYN2] = (Concrete0, Concrete1) -assert '1' -b = (1) | (((2) & (3)) ^ (8)) -bb = ((1) | (two)) | (6) -ccc = ((one) & (two)) & (three) -dddd = ((x) ^ (o)) ^ (r) -exec 'c = 3' -exec 'c = 3' in {}, {} - -def raise_string(a=2, *args, **kwargs): - raise Exception, 'yo' - yield 'coucou' - yield -a = (b) + (2) -c = (b) * (2) -c = (b) / (2) -c = (b) // (2) -c = (b) - (2) -c = (b) % (2) -c = (b) ** (2) -c = (b) << (2) -c = (b) >> (2) -c = ~b -c = not b -d = [c] -e = d[:] -e = d[a:b:c] -raise_string(*args, **kwargs) -print >> stream, 'bonjour' -print >> stream, 'salut', - -def make_class(any, base=data.module.YO, *args, **kwargs): - """check base is correctly resolved to Concrete0""" - - - class Aaaa(base): - """dynamic class""" - - - return Aaaa -from os.path import abspath -import os as myos - - -class A: - pass - - - -class A(A): - pass - - -def generator(): - """A generator.""" - yield - -def not_a_generator(): - """A function that contains generator, but is not one.""" - - def generator(): - yield - genl = lambda : (yield) - -def with_metaclass(meta, *bases): - return meta('NewBase', bases, {}) - - -class NotMetaclass(with_metaclass(Metaclass)): - pass - - diff --git a/testdata/python2/data/noendingnewline.py b/testdata/python2/data/noendingnewline.py deleted file mode 100644 index e1d6e4a186..0000000000 --- a/testdata/python2/data/noendingnewline.py +++ /dev/null @@ -1,36 +0,0 @@ -import unittest - - -class TestCase(unittest.TestCase): - - def setUp(self): - unittest.TestCase.setUp(self) - - - def tearDown(self): - unittest.TestCase.tearDown(self) - - def testIt(self): - self.a = 10 - self.xxx() - - - def xxx(self): - if False: - pass - print 'a' - - if False: - pass - pass - - if False: - pass - print 'rara' - - -if __name__ == '__main__': - print 'test2' - unittest.main() - - diff --git a/testdata/python2/data/notall.py b/testdata/python2/data/notall.py deleted file mode 100644 index 042491e016..0000000000 --- a/testdata/python2/data/notall.py +++ /dev/null @@ -1,7 +0,0 @@ -name = 'a' -_bla = 2 -other = 'o' -class Aaa: pass - -def func(): return 'yo' - diff --git a/testdata/python3/data/MyPyPa-0.1.0-py2.5.egg b/testdata/python3/data/MyPyPa-0.1.0-py2.5.egg deleted file mode 100644 index f62599c7b10469b9eadabb31e9a42128a769c7cd..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1222 zcmWIWW@Zs#U|`^2V0WyvGL3P1+yvw;0%Bnx&aEt{EJ)OkkI&4@EQycTE2vDq{ik!f z_N!x_=Tq1;5?GSh74)7l32E~11UgKbs2I`Asd9`*h35;;UlWH_UOO6HYHA|00t?gX z;%fG=d001TTw@jTTsqTI^OvXQ%%iGRmNP4titd`3CYJVV<;$19c1~rT%G&wsg1;w&+sQ$d~(!s_JietmCU zt#fD2clU>H2n{g5U>x$C3CR?Y$GxZZOpXTX?t_}->h7-V>F4IJAM76*_t<%}h{;@`zS&LcdYtZG(rN*BxefrA0=WeO(-#dQ{Rhs@f zH_wS}{_3UWW$+{@h&$+WP|)W|+K-EkK5y#YsHJsMzvH~8uJ>8Sljx37u41p@1UiHr zh(TV1JEkPRAU-FxEHww@oYQM{R_J&&P!l5%%OYz|Ni9gtOG(X3u8hyg z%*!qYneiB(Zb4+-Rhb34#ffRD7&;CGJDSu1Rc;4j6deKHkRbf*tLy3GspENt7ZMAb zgAA@1KltQ*#&>JbhqXK_cs!mootAjf_@v3ZxLCMbYpqDoC(%!zyp4=LU)pK&sW`Zl zTj+A*ET_MF{{A`qcZZC(x6!BW3qNg1;w&+sQ$d~(!s_JietmCU zt#fD2clU>H2n{g5U>x$C3CR?Y$GxZZOpXTX?t_}->h7-V>F4IJAM76*_t<%}h{;@`zS&LcdYtZG(rN*BxefrA0=WeO(-#dQ{Rhs@f zH_wS}{_3UWW$+{@h&$+WP|)W|+K-EkK5y#YsHJsMzvH~8uJ>8Sljx37u41p@1UiHr zh(TV1JEkPRAU-FxEHww@oYQM{R_J&&P!l5%%OYz|Ni9gtOG(X3u8hyg z%*!qYneiB(Zb4+-Rhb34#ffRD7&;CGJDSu1Rc;4j6deKHkRbf*tLy3GspENt7ZMAb zgAA@1KltQ*#&>JbhqXK_cs!mootAjf_@v3ZxLCMbYpqDoC(%!zyp4=LU)pK&sW`Zl zTj+A*ET_MF{{A`qcZZC(x6!BW3qN Date: Sat, 13 Feb 2016 18:29:16 -0500 Subject: [PATCH 207/312] Fix minor issues in packaging and testing files. I removed "recursive-include brain/" in MANIFEST.in because it doesn't do anything, probably because Python files in subdirectories of packages are automatically included. For some reason "graft astroid/tests/testdata" doesn't work, maybe because tests/ contains an `__init__.py`. I also altered coverage to include only astroid files by using --source in coverage run call, rather than --ignore in the reports. Note that when tox is called from the root of the repository at the moment, astroid is imported from the files in the *repository*, not the installed files in the virtualenvs in .tox/. Meanwhile, the tests themselves are run from the virtualenvs in .tox/, which accounts for why coverage shows 0% for all the unittest files in tests/. --- MANIFEST.in | 1 - setup.py | 4 ++-- tox.ini | 7 +++---- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/MANIFEST.in b/MANIFEST.in index d185621c47..eab1f75bb8 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -4,4 +4,3 @@ include COPYING include COPYING.LESSER include tox.ini recursive-include astroid/tests/testdata *.py *.zip *.egg -recursive-include astroid/brain *.py \ No newline at end of file diff --git a/setup.py b/setup.py index 2150d1c26e..3a3cc62159 100644 --- a/setup.py +++ b/setup.py @@ -64,8 +64,8 @@ def install(): include_package_data = True, install_requires = install_requires, packages = find_packages(), - cmdclass={'install_lib': AstroidInstallLib, - 'easy_install': AstroidEasyInstallLib} + cmdclass = {'install_lib': AstroidInstallLib, + 'easy_install': AstroidEasyInstallLib} ) diff --git a/tox.ini b/tox.ini index 3dca0d5b8f..888a955031 100644 --- a/tox.ini +++ b/tox.ini @@ -10,8 +10,8 @@ commands = coverage erase [testenv:coverage-stats] commands = - coverage report --ignore-errors --include="*astroid*" - coverage html --ignore-errors --include="*astroid*" + coverage report --ignore-errors + coverage html --ignore-errors [testenv] deps = @@ -31,5 +31,4 @@ deps = # Disable warnings because they overflow the Travis log on 3.5 setenv = PYTHONWARNINGS = i # {posargs} allows passing command line arguments to unittest -commands = coverage run --append --branch -m unittest discover {posargs} -s {envsitepackagesdir}/astroid/tests -p "unittest*.py" - +commands = coverage run --source astroid --append --branch -m unittest discover {posargs} -s {envsitepackagesdir}/astroid/tests -p "unittest*.py" From fd1f8c78be2b295405111f5db05e47457ee618a5 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 16 Feb 2016 18:44:53 +0000 Subject: [PATCH 208/312] Set the parent to the BinOp node when inferring nodes created during binary operations In order to do so, we had to change the signature of infer_binary_operation to receive the binary operation instead of the operator itself. --- astroid/inference.py | 64 +++++++++++++++--------------- astroid/protocols.py | 16 ++++---- astroid/tests/unittest_regrtest.py | 13 ++++++ 3 files changed, 53 insertions(+), 40 deletions(-) diff --git a/astroid/inference.py b/astroid/inference.py index e4074bc15f..7bf2361025 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -437,7 +437,7 @@ def _is_not_implemented(const): return isinstance(const, treeabc.Const) and const.value is NotImplemented -def _invoke_binop_inference(instance, op, other, context, method_name, nodes): +def _invoke_binop_inference(instance, opnode, op, other, context, method_name, nodes): """Invoke binary operation inference on the given instance.""" if not hasattr(instance, 'getattr'): # The operation is undefined for the given node. We can stop @@ -446,21 +446,21 @@ def _invoke_binop_inference(instance, op, other, context, method_name, nodes): method = instance.getattr(method_name)[0] inferred = next(method.infer(context=context)) - return protocols.infer_binary_op(instance, op, other, context, inferred, nodes) + return protocols.infer_binary_op(instance, opnode, op, other, context, inferred, nodes) -def _aug_op(instance, op, other, context, nodes, reverse=False): +def _aug_op(instance, opnode, op, other, context, nodes, reverse=False): """Get an inference callable for an augmented binary operation.""" method_name = protocols.AUGMENTED_OP_METHOD[op] return functools.partial(_invoke_binop_inference, instance=instance, - op=op, other=other, + op=op, opnode=opnode, other=other, context=context, method_name=method_name, nodes=nodes) -def _bin_op(instance, op, other, context, nodes, reverse=False): +def _bin_op(instance, opnode, op, other, context, nodes, reverse=False): """Get an inference callable for a normal binary operation. If *reverse* is True, then the reflected method will be used instead. @@ -471,7 +471,7 @@ def _bin_op(instance, op, other, context, nodes, reverse=False): method_name = protocols.BIN_OP_METHOD[op] return functools.partial(_invoke_binop_inference, instance=instance, - op=op, other=other, + op=op, opnode=opnode, other=other, context=context, method_name=method_name, nodes=nodes) @@ -498,7 +498,7 @@ def _same_type(type1, type2): return type1.qname() == type2.qname() -def _get_binop_flow(left, left_type, op, right, right_type, +def _get_binop_flow(left, left_type, binary_opnode, right, right_type, context, reverse_context, nodes): """Get the flow for binary operations. @@ -514,20 +514,21 @@ def _get_binop_flow(left, left_type, op, right, right_type, * if left is a supertype of right, then right.__rop__(left) is first tried and then left.__op__(right) """ + op = binary_opnode.op if _same_type(left_type, right_type): - methods = [_bin_op(left, op, right, context, nodes)] + methods = [_bin_op(left, binary_opnode, op, right, context, nodes)] elif inferenceutil.is_subtype(left_type, right_type): - methods = [_bin_op(left, op, right, context, nodes)] + methods = [_bin_op(left, binary_opnode, op, right, context, nodes)] elif inferenceutil.is_supertype(left_type, right_type): - methods = [_bin_op(right, op, left, reverse_context, nodes, reverse=True), - _bin_op(left, op, right, context, nodes)] + methods = [_bin_op(right, binary_opnode, op, left, reverse_context, nodes, reverse=True), + _bin_op(left, binary_opnode, op, right, context, nodes)] else: - methods = [_bin_op(left, op, right, context, nodes), - _bin_op(right, op, left, reverse_context, nodes, reverse=True)] + methods = [_bin_op(left, binary_opnode, op, right, context, nodes), + _bin_op(right, binary_opnode, op, left, reverse_context, nodes, reverse=True)] return methods -def _get_aug_flow(left, left_type, aug_op, right, right_type, +def _get_aug_flow(left, left_type, aug_opnode, right, right_type, context, reverse_context, nodes): """Get the flow for augmented binary operations. @@ -544,25 +545,26 @@ def _get_aug_flow(left, left_type, aug_op, right, right_type, is tried, then right.__rop__(left) and then left.__op__(right) """ - op = aug_op.strip("=") + bin_op = aug_opnode.op.strip("=") + aug_op = aug_opnode.op if _same_type(left_type, right_type): - methods = [_aug_op(left, aug_op, right, context, nodes), - _bin_op(left, op, right, context, nodes)] + methods = [_aug_op(left, aug_opnode, aug_op, right, context, nodes), + _bin_op(left, aug_opnode, bin_op, right, context, nodes)] elif inferenceutil.is_subtype(left_type, right_type): - methods = [_aug_op(left, aug_op, right, context, nodes), - _bin_op(left, op, right, context, nodes)] + methods = [_aug_op(left, aug_opnode, aug_op, right, context, nodes), + _bin_op(left, aug_opnode, bin_op, right, context, nodes)] elif inferenceutil.is_supertype(left_type, right_type): - methods = [_aug_op(left, aug_op, right, context, nodes), - _bin_op(right, op, left, reverse_context, nodes, reverse=True), - _bin_op(left, op, right, context, nodes)] + methods = [_aug_op(left, aug_opnode, aug_op, right, context, nodes), + _bin_op(right, aug_opnode, bin_op, left, reverse_context, nodes, reverse=True), + _bin_op(left, aug_opnode, bin_op, right, context, nodes)] else: - methods = [_aug_op(left, aug_op, right, context, nodes), - _bin_op(left, op, right, context, nodes), - _bin_op(right, op, left, reverse_context, nodes, reverse=True)] + methods = [_aug_op(left, aug_opnode, aug_op, right, context, nodes), + _bin_op(left, aug_opnode, bin_op, right, context, nodes), + _bin_op(right, aug_opnode, bin_op, left, reverse_context, nodes, reverse=True)] return methods -def _infer_binary_operation(left, right, op, context, flow_factory, nodes): +def _infer_binary_operation(left, right, binary_opnode, context, flow_factory, nodes): """Infer a binary operation between a left operand and a right operand This is used by both normal binary operations and augmented binary @@ -572,7 +574,7 @@ def _infer_binary_operation(left, right, op, context, flow_factory, nodes): context, reverse_context = _get_binop_contexts(context, left, right) left_type = inferenceutil.object_type(left) right_type = inferenceutil.object_type(right) - methods = flow_factory(left, left_type, op, right, right_type, + methods = flow_factory(left, left_type, binary_opnode, right, right_type, context, reverse_context, nodes) for method in methods: try: @@ -606,7 +608,7 @@ def _infer_binary_operation(left, right, op, context, flow_factory, nodes): return # TODO(cpopa): yield a BadBinaryOperationMessage here, # since the operation is not supported - yield util.BadBinaryOperationMessage(left_type, op, right_type) + yield util.BadBinaryOperationMessage(left_type, binary_opnode.op, right_type) def infer_binop(self, context, nodes): @@ -615,7 +617,6 @@ def infer_binop(self, context, nodes): context = contextmod.InferenceContext() left = self.left right = self.right - op = self.op # we use two separate contexts for evaluating lhs and rhs because # 1. evaluating lhs may leave some undesired entries in context.path @@ -635,7 +636,7 @@ def infer_binop(self, context, nodes): yield util.Uninferable return - results = _infer_binary_operation(lhs, rhs, op, context, + results = _infer_binary_operation(lhs, rhs, self, context, _get_binop_flow, nodes) for result in results: yield result @@ -653,7 +654,6 @@ def infer_augassign(self, context=None, nodes=None): """Inferrence logic for augmented binary operations.""" if context is None: context = contextmod.InferenceContext() - op = self.op for lhs in self.target.infer_lhs(context=context): if lhs is util.Uninferable: @@ -673,7 +673,7 @@ def infer_augassign(self, context=None, nodes=None): yield util.Uninferable return - results = _infer_binary_operation(lhs, rhs, op, + results = _infer_binary_operation(lhs, rhs, self, context, _get_aug_flow, nodes) for result in results: yield result diff --git a/astroid/protocols.py b/astroid/protocols.py index 7fdc6e291b..8ca9ee1ccd 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -148,7 +148,7 @@ def infer_binary_op(self, operator, other, context, method, nodes): @infer_binary_op.register(treeabc.Const) @decorators.yes_if_nothing_inferred -def const_infer_binary_op(self, operator, other, context, _, nodes): +def const_infer_binary_op(self, opnode, operator, other, context, _, nodes): not_implemented = nodes.NameConstant(NotImplemented) if isinstance(other, treeabc.Const): try: @@ -169,8 +169,8 @@ def const_infer_binary_op(self, operator, other, context, _, nodes): yield not_implemented -def _multiply_seq_by_int(self, other, context): - node = self.__class__() +def _multiply_seq_by_int(self, opnode, other, context): + node = self.__class__(parent=opnode) elts = [] for elt in self.elts: infered = inferenceutil.safe_infer(elt, context) @@ -193,10 +193,10 @@ def _filter_uninferable_nodes(elts, context): @infer_binary_op.register(treeabc.Tuple) @infer_binary_op.register(treeabc.List) @decorators.yes_if_nothing_inferred -def tl_infer_binary_op(self, operator, other, context, method, nodes): +def tl_infer_binary_op(self, opnode, operator, other, context, method, nodes): not_implemented = nodes.NameConstant(NotImplemented) if isinstance(other, self.__class__) and operator == '+': - node = self.__class__() + node = self.__class__(parent=opnode) elts = list(_filter_uninferable_nodes(self.elts, context)) elts += list(_filter_uninferable_nodes(other.elts, context)) node.elts = elts @@ -205,21 +205,21 @@ def tl_infer_binary_op(self, operator, other, context, method, nodes): if not isinstance(other.value, int): yield not_implemented return - yield _multiply_seq_by_int(self, other, context) + yield _multiply_seq_by_int(self, opnode, other, context) elif isinstance(other, runtimeabc.Instance) and operator == '*': # Verify if the instance supports __index__. as_index = inferenceutil.class_instance_as_index(other) if not as_index: yield util.Uninferable else: - yield _multiply_seq_by_int(self, as_index, context) + yield _multiply_seq_by_int(self, opnode, as_index, context) else: yield not_implemented @infer_binary_op.register(runtimeabc.Instance) @decorators.yes_if_nothing_inferred -def instance_infer_binary_op(self, operator, other, context, method, nodes): +def instance_infer_binary_op(self, opnode, operator, other, context, method, nodes): return method.infer_call_result(self, context) diff --git a/astroid/tests/unittest_regrtest.py b/astroid/tests/unittest_regrtest.py index 21b93dc838..52a45cf939 100644 --- a/astroid/tests/unittest_regrtest.py +++ b/astroid/tests/unittest_regrtest.py @@ -317,6 +317,19 @@ def test_null_fromlineno_does_not_crash_lookup(self): locals_ = {'a': [function]} lookup._get_locals(node, locals_) + def test_binop_generates_nodes_with_parents(self): + node = extract_node(''' + def no_op(*args): + pass + def foo(*args): + def inner(*more_args): + args + more_args #@ + return inner + ''') + inferred = next(node.infer()) + self.assertIsInstance(inferred, nodes.Tuple) + self.assertIsNotNone(inferred.parent) + self.assertIsInstance(inferred.parent, nodes.BinOp) class Whatever(object): From 5f9f8df7aa92981fbb41ea01535c5928ec8c2e08 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Thu, 18 Feb 2016 21:55:32 +0000 Subject: [PATCH 209/312] imported_names() got replaced by public_names() Our understanding of wildcard imports through __all__ was half baked to say at least, since we couldn't account for modifications of the list, which results in tons of false positives. Instead, we replaced it with public_names(), a method which returns all the names that are publicly available in a module, that is that don't start with an underscore, even though this means that there is a possibility for other names to be leaked out even though they are not present in the __all__ variable. Close #322 --- ChangeLog | 14 +++++++ astroid/interpreter/lookup.py | 2 +- astroid/tests/unittest_scoped_nodes.py | 15 +++---- astroid/tree/scoped_nodes.py | 54 ++------------------------ 4 files changed, 26 insertions(+), 59 deletions(-) diff --git a/ChangeLog b/ChangeLog index f405b4e4db..7024c1b799 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,20 @@ Change log for the astroid package (used to be astng) ===================================================== -- + + * wildcard_imported_names() got replaced by public_names() + + Our understanding of wildcard imports through __all__ was + half baked to say at least, since we couldn't account for + modifications of the list, which results in tons of false positives. + Instead, we replaced it with public_names(), a method which returns + all the names that are publicly available in a module, that is that + don't start with an underscore, even though this means that there + is a possibility for other names to be leaked out even though + they are not present in the __all__ variable. + + Closes issue #322 + * Changed the way how parameters are being built The old way consisted in having the parameter names, their diff --git a/astroid/interpreter/lookup.py b/astroid/interpreter/lookup.py index 20fda77a81..441d62f7a1 100644 --- a/astroid/interpreter/lookup.py +++ b/astroid/interpreter/lookup.py @@ -300,7 +300,7 @@ def sort_locals(my_list): imported = util.do_import_module(node, node.modname) except exceptions.AstroidBuildingError: continue - for name in imported.wildcard_import_names(): + for name in imported.public_names(): locals_[name].append(node) sort_locals(locals_[name]) else: diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index c362f274a8..a90db7a7a5 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -109,7 +109,7 @@ def test_getattr(self): self.assertEqual(len(self.nonregr.getattr('enumerate')), 2) self.assertRaises(InferenceError, self.nonregr.igetattr, 'YOAA') - def test_wildcard_import_names(self): + def test_public_names(self): m = builder.parse(''' name = 'a' _bla = 2 @@ -118,7 +118,8 @@ class Aaa: pass def func(): print('yo') __all__ = 'Aaa', '_bla', 'name' ''') - self.assertEqual(m.wildcard_import_names(), ['Aaa', '_bla', 'name']) + values = sorted(['Aaa', 'name', 'other', 'func']) + self.assertEqual(sorted(m.public_names()), values) m = builder.parse(''' name = 'a' _bla = 2 @@ -127,22 +128,22 @@ class Aaa: pass def func(): return 'yo' ''') - res = sorted(m.wildcard_import_names()) - self.assertEqual(res, ['Aaa', 'func', 'name', 'other']) + res = sorted(m.public_names()) + self.assertEqual(res, values) m = builder.parse(''' from missing import tzop trop = "test" __all__ = (trop, "test1", tzop, 42) ''') - res = sorted(m.wildcard_import_names()) - self.assertEqual(res, ["test", "test1"]) + res = sorted(m.public_names()) + self.assertEqual(res, ["trop", "tzop"]) m = builder.parse(''' test = tzop = 42 __all__ = ('test', ) + ('tzop', ) ''') - res = sorted(m.wildcard_import_names()) + res = sorted(m.public_names()) self.assertEqual(res, ['test', 'tzop']) def test_module_getattr(self): diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 6744ec8279..36483ae764 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -380,57 +380,9 @@ def relative_to_absolute_name(self, modname, level): return '%s.%s' % (package_name, modname) return modname - def wildcard_import_names(self): - """return the list of imported names when this module is 'wildcard - imported' - - It doesn't include the '__builtins__' name which is added by the - current CPython implementation of wildcard imports. - """ - # We separate the different steps of lookup in try/excepts - # to avoid catching too many Exceptions - default = [name for name in self.keys() if not name.startswith('_')] - if '__all__' in self: - # This is a workaround for the case where a module defines - # __all__ and then dynamically alters it, requiring a - # custom hack to define a static __all__ for the module. - # The static __all__ ends up at the end of the locals list - # for __all__, not the beginning. - all = self.locals['__all__'][-1] - else: - return default - - try: - explicit = next(all.assigned_stmts()) - except exceptions.InferenceError: - return default - except AttributeError: - # not an assignment node - # XXX infer? - return default - - # Try our best to detect the exported name. - inferred = [] - try: - explicit = next(explicit.infer()) - except exceptions.InferenceError: - return default - if not isinstance(explicit, (node_classes.Tuple, node_classes.List)): - return default - - str_const = lambda node: (isinstance(node, node_classes.Const) and - isinstance(node.value, six.string_types)) - for node in explicit.elts: - if str_const(node): - inferred.append(node.value) - else: - try: - inferred_node = next(node.infer()) - except exceptions.InferenceError: - continue - if str_const(inferred_node): - inferred.append(inferred_node.value) - return inferred + def public_names(self): + """Get the list of the names which are publicly available in this module.""" + return [name for name in self.keys() if not name.startswith('_')] def bool_value(self): return True From 3c843fd67d5576c1ad3b1b8ba70adb00cce5a4a5 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Thu, 18 Feb 2016 22:15:02 +0000 Subject: [PATCH 210/312] Try to run tox as a module for Jython compatibility. --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index e3a7da100b..9075168a8b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -35,7 +35,7 @@ install: - $PYTHON_EXE -m pip --version - $PYTHON_EXE -m tox --version script: - - tox -e $TOXENV + - python -m tox -e $TOXENV after_failure: - more .tox/log/* | cat - more .tox/*/log/* | cat From 5b3745d6ca55d6cc9aca6f4974177438b9442841 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Fri, 19 Feb 2016 00:48:43 +0000 Subject: [PATCH 211/312] Get the first built object when building default and annotation for arguments This didn't get triggered until now on CPython, but it does trigger on PyPy, resulting in a tuple being the default value of a Parameter. --- astroid/raw_building.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/astroid/raw_building.py b/astroid/raw_building.py index bccd83dafc..9da407214c 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -330,11 +330,11 @@ def _extract_args(parameters, parent): annotation = node_classes.Empty if parameter.default is not _Parameter.empty: default = _ast_from_object(parameter.default, built_objects, - module, parent=parent) + module, parent=parent)[0] if parameter.annotation is not _Parameter.empty: annotation = _ast_from_object(parameter.annotation, built_objects, - module, parent=parent) + module, parent=parent)[0] param.postinit(default=default, annotation=annotation) yield param From 290913832bc639f7dbeebb721d81880898be05b9 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Fri, 19 Feb 2016 11:33:16 +0000 Subject: [PATCH 212/312] Run test from Python 3.5 onwards, since inspect.signature can't obtain the signature of issubclass post 3.5. --- astroid/tests/unittest_python3.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astroid/tests/unittest_python3.py b/astroid/tests/unittest_python3.py index 3cd7b1f5de..c5e3d35340 100644 --- a/astroid/tests/unittest_python3.py +++ b/astroid/tests/unittest_python3.py @@ -250,7 +250,7 @@ def test_unpacking_in_dict_getitem(self): self.assertIsInstance(value, nodes.Const) self.assertEqual(value.value, expected) - @require_version('3.4') + @require_version(minver='3.5') def test_positional_only_parameters(self): ast = raw_building.ast_from_object(issubclass) self.assertEqual(len(ast.args.positional_only), 2) From daec35d2e283f2582b6a195e7330b1cc4094f028 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Fri, 19 Feb 2016 15:59:51 +0000 Subject: [PATCH 213/312] Set the call context for binary methods only for instances The instance's binary special methods are the only places where the other argument should be set, since for the other nodes, we're handling the binary operations specifically, while for Instances we are delegating to infer_call_result. Close #271 --- astroid/inference.py | 32 +++++++------------------------- astroid/protocols.py | 8 ++++++++ 2 files changed, 15 insertions(+), 25 deletions(-) diff --git a/astroid/inference.py b/astroid/inference.py index 7bf2361025..4eafe19120 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -477,29 +477,13 @@ def _bin_op(instance, opnode, op, other, context, nodes, reverse=False): nodes=nodes) -def _get_binop_contexts(context, left, right): - """Get contexts for binary operations. - - This will return two inferrence contexts, the first one - for x.__op__(y), the other one for y.__rop__(x), where - only the arguments are inversed. - """ - # The order is important, since the first one should be - # left.__op__(right). - for arg in (right, left): - new_context = context.clone() - new_context.callcontext = contextmod.CallContext(args=[arg]) - new_context.boundnode = None - yield new_context - - def _same_type(type1, type2): """Check if type1 is the same as type2.""" return type1.qname() == type2.qname() def _get_binop_flow(left, left_type, binary_opnode, right, right_type, - context, reverse_context, nodes): + context, nodes): """Get the flow for binary operations. The rules are a bit messy: @@ -520,16 +504,16 @@ def _get_binop_flow(left, left_type, binary_opnode, right, right_type, elif inferenceutil.is_subtype(left_type, right_type): methods = [_bin_op(left, binary_opnode, op, right, context, nodes)] elif inferenceutil.is_supertype(left_type, right_type): - methods = [_bin_op(right, binary_opnode, op, left, reverse_context, nodes, reverse=True), + methods = [_bin_op(right, binary_opnode, op, left, context, nodes, reverse=True), _bin_op(left, binary_opnode, op, right, context, nodes)] else: methods = [_bin_op(left, binary_opnode, op, right, context, nodes), - _bin_op(right, binary_opnode, op, left, reverse_context, nodes, reverse=True)] + _bin_op(right, binary_opnode, op, left, context, nodes, reverse=True)] return methods def _get_aug_flow(left, left_type, aug_opnode, right, right_type, - context, reverse_context, nodes): + context, nodes): """Get the flow for augmented binary operations. The rules are a bit messy: @@ -555,12 +539,12 @@ def _get_aug_flow(left, left_type, aug_opnode, right, right_type, _bin_op(left, aug_opnode, bin_op, right, context, nodes)] elif inferenceutil.is_supertype(left_type, right_type): methods = [_aug_op(left, aug_opnode, aug_op, right, context, nodes), - _bin_op(right, aug_opnode, bin_op, left, reverse_context, nodes, reverse=True), + _bin_op(right, aug_opnode, bin_op, left, context, nodes, reverse=True), _bin_op(left, aug_opnode, bin_op, right, context, nodes)] else: methods = [_aug_op(left, aug_opnode, aug_op, right, context, nodes), _bin_op(left, aug_opnode, bin_op, right, context, nodes), - _bin_op(right, aug_opnode, bin_op, left, reverse_context, nodes, reverse=True)] + _bin_op(right, aug_opnode, bin_op, left, context, nodes, reverse=True)] return methods @@ -570,12 +554,10 @@ def _infer_binary_operation(left, right, binary_opnode, context, flow_factory, n This is used by both normal binary operations and augmented binary operations, the only difference is the flow factory used. """ - - context, reverse_context = _get_binop_contexts(context, left, right) left_type = inferenceutil.object_type(left) right_type = inferenceutil.object_type(right) methods = flow_factory(left, left_type, binary_opnode, right, right_type, - context, reverse_context, nodes) + context, nodes) for method in methods: try: results = list(method()) diff --git a/astroid/protocols.py b/astroid/protocols.py index 8ca9ee1ccd..a417259578 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -217,9 +217,17 @@ def tl_infer_binary_op(self, opnode, operator, other, context, method, nodes): yield not_implemented + +def _get_binop_context(context, other): + context.callcontext = contextmod.CallContext(args=[other]) + context.boundnode = None + return context + + @infer_binary_op.register(runtimeabc.Instance) @decorators.yes_if_nothing_inferred def instance_infer_binary_op(self, opnode, operator, other, context, method, nodes): + context = _get_binop_context(context, other) return method.infer_call_result(self, context) From d45c397d5957545605283d291a315926993886a8 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Fri, 19 Feb 2016 17:07:53 +0000 Subject: [PATCH 214/312] Add a __repr__ for CallSite. Close #209 --- astroid/tree/scoped_nodes.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 36483ae764..e16ab41335 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -535,6 +535,14 @@ def __init__(self, funcnode, args, keywords): if value is not util.Uninferable } + def __repr__(self): + string = '{name}(funcnode={funcnode}, args={args}, keywords={keywords})' + return string.format(name=type(self).__name__, + funcnode=self._funcnode, + args=self.positional_arguments, + keywords=self.keyword_arguments) + + def has_invalid_arguments(self): """Check if in the current CallSite were passed *invalid* arguments From 25575e47e144e1591dca27af32e386b12b6c2b1d Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sat, 13 Feb 2016 23:53:24 +0000 Subject: [PATCH 215/312] Introduce a special attributes model Through this model, astroid starts knowing special attributes of certain Python objects, such as functions, classes, super objects and so on. This was previously possible before, but now the lookup and the attributes themselves are separated into a new module, objectmodel.py, which describes, in a more comprehensive way, the data model of each object. --- ChangeLog | 8 + astroid/interpreter/objectmodel.py | 514 +++++++++++++++++++++++++ astroid/interpreter/objects.py | 57 +-- astroid/tests/unittest_object_model.py | 432 +++++++++++++++++++++ astroid/tests/unittest_scoped_nodes.py | 12 +- astroid/tree/scoped_nodes.py | 79 +--- astroid/util.py | 7 + 7 files changed, 1017 insertions(+), 92 deletions(-) create mode 100644 astroid/interpreter/objectmodel.py create mode 100644 astroid/tests/unittest_object_model.py diff --git a/ChangeLog b/ChangeLog index 7024c1b799..876bd2648b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -16,6 +16,14 @@ Change log for the astroid package (used to be astng) Closes issue #322 + * Introduce a special attributes model + + Through this model, astroid starts knowing special attributes of certain Python objects, + such as functions, classes, super objects and so on. This was previously possible before, + but now the lookup and the attributes themselves are separated into a new module, + objectmodel.py, which describes, in a more comprehensive way, the data model of each + object. + * Changed the way how parameters are being built The old way consisted in having the parameter names, their diff --git a/astroid/interpreter/objectmodel.py b/astroid/interpreter/objectmodel.py new file mode 100644 index 0000000000..ac38275e4d --- /dev/null +++ b/astroid/interpreter/objectmodel.py @@ -0,0 +1,514 @@ +# copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved. +# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr +# +# This file is part of astroid. +# +# astroid is free software: you can redistribute it and/or modify it +# under the terms of the GNU Lesser General Public License as published by the +# Free Software Foundation, either version 2.1 of the License, or (at your +# option) any later version. +# +# astroid is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License +# for more details. +# +# You should have received a copy of the GNU Lesser General Public License along +# with astroid. If not, see . +# +# The code in this file was originally part of logilab-common, licensed under +# the same license. + +""" +Data object model, as per https://docs.python.org/3/reference/datamodel.html. + +This module describes, at least partially, a data object model for some +of astroid's nodes. The model contains special attributes that nodes such +as functions, classes, modules etc have, such as __doc__, __class__, +__module__ etc, being used when doing attribute lookups over nodes. + +For instance, inferring `obj.__class__` will first trigger an inference +of the `obj` variable. If it was succesfully inferred, then an attribute +`__class__ will be looked for in the inferred object. This is the part +where the data model occurs. The model is attached to those nodes +and the lookup mechanism will try to see if attributes such as +`__class__` are defined by the model or not. If they are defined, +the model will be requested to return the corresponding value of that +attribute. Thus the model can be viewed as a special part of the lookup +mechanism. +""" + +import itertools +import pprint +import os + +import six + +import astroid +from astroid import context as contextmod +from astroid import exceptions +from astroid.interpreter import util +from astroid.tree import node_classes +from astroid.tree import treeabc +from astroid.util import lazy_import + +objects = lazy_import('interpreter.objects') + + +def _dunder_dict(instance, attributes): + obj = node_classes.Dict(parent=instance) + + # Convert the keys to node strings + keys = [node_classes.Const(value=value, parent=obj) + for value in list(attributes.keys())] + + # The original attribute has a list of elements for each key, + # but that is not useful for retrieving the special attribute's value. + # In this case, we're picking the last value from each list. + values = [elem[-1] for elem in attributes.values()] + + obj.postinit(keys=keys, values=values) + return obj + + +class ObjectModel(object): + + def __repr__(self): + result = [] + cname = type(self).__name__ + string = '%(cname)s(%(fields)s)' + alignment = len(cname) + 1 + for field in sorted(self.attributes()): + width = 80 - len(field) - alignment + lines = pprint.pformat(field, indent=2, + width=width).splitlines(True) + + inner = [lines[0]] + for line in lines[1:]: + inner.append(' ' * alignment + line) + result.append(field) + + return string % {'cname': cname, + 'fields': (',\n' + ' ' * alignment).join(result)} + + def __call__(self, instance): + self._instance = instance + return self + + def __get__(self, instance, cls=None): + # ObjectModel needs to be a descriptor so that just doing + # `special_attributes = SomeObjectModel` should be enough in the body of a node. + # But at the same time, node.special_attributes should return an object + # which can be used for manipulating the special attributes. That's the reason + # we pass the instance through which it got accessed to ObjectModel.__call__, + # returning itself afterwards, so we can still have access to the + # underlying data model and to the instance for which it got accessed. + return self(instance) + + def __contains__(self, name): + return name in self.attributes() + + def attributes(self): + """Get the attributes which are exported by this object model.""" + return [obj[2:] for obj in dir(self) if obj.startswith('py')] + + def lookup(self, name): + """Look up the given *name* in the current model + + It should return an AST or an interpreter object, + but if the name is not found, then an AttributeInferenceError will be raised. + """ + + if name in self.attributes(): + return getattr(self, "py" + name) + raise exceptions.AttributeInferenceError(target=self._instance, attribute=name) + + +class ModuleModel(ObjectModel): + + def _builtins(self): + builtins = astroid.MANAGER.builtins() + return builtins.special_attributes.lookup('__dict__') + + if six.PY3: + @property + def pybuiltins(self): + return self._builtins() + + else: + @property + def py__builtin__(self): + return self._builtins() + + # __path__ is a standard attribute on *packages* not + # non-package modules. The only mention of it in the + # official 2.7 documentation I can find is in the + # tutorial. + + @property + def py__path__(self): + if not self._instance.package: + raise exceptions.AttributeInferenceError(target=self._instance, + attribute='__path__') + + path = os.path.dirname(self._instance.source_file) + path_obj = node_classes.Const(value=path, parent=self._instance) + + container = node_classes.List(parent=self._instance) + container.postinit([path_obj]) + + return container + + @property + def py__name__(self): + return node_classes.Const(value=self._instance.name, + parent=self._instance) + + @property + def py__doc__(self): + return node_classes.Const(value=self._instance.doc, + parent=self._instance) + + @property + def py__file__(self): + return node_classes.Const(value=self._instance.source_file, + parent=self._instance) + + @property + def py__dict__(self): + return _dunder_dict(self._instance, self._instance.globals) + + # __package__ isn't mentioned anywhere outside a PEP: + # https://www.python.org/dev/peps/pep-0366/ + @property + def py__package__(self): + if not self._instance.package: + value = '' + else: + value = self._instance.name + + return node_classes.Const(value=value, parent=self._instance) + + # These are related to the Python 3 implementation of the + # import system, + # https://docs.python.org/3/reference/import.html#import-related-module-attributes + + @property + def py__spec__(self): + # No handling for now. + return node_classes.Unknown() + + @property + def py__loader__(self): + # No handling for now. + return node_classes.Unknown() + + @property + def py__cached__(self): + # No handling for now. + return node_classes.Unknown() + + +class FunctionModel(ObjectModel): + + @property + def py__name__(self): + return node_classes.Const(value=self._instance.name, + parent=self._instance) + + @property + def py__doc__(self): + return node_classes.Const(value=self._instance.doc, + parent=self._instance) + + @property + def py__qualname__(self): + return node_classes.Const(value=self._instance.qname(), + parent=self._instance) + + @property + def py__defaults__(self): + func = self._instance + defaults = [arg.default for arg in func.args.args if arg.default] + + if not defaults: + return node_classes.Const(value=None, parent=func) + + defaults_obj = node_classes.Tuple(parent=func) + defaults_obj.postinit(defaults) + return defaults_obj + + @property + def py__annotations__(self): + obj = node_classes.Dict(parent=self._instance) + + if not self._instance.returns: + returns = node_classes.Empty + else: + returns = self._instance.returns + + args = itertools.chain(self._instance.args.positional_and_keyword, + (self._instance.args.vararg, ), + (self._instance.args.kwarg, ), + self._instance.args.keyword_only) + annotations = {arg.name: arg.annotation for arg in args + if arg and arg.annotation} + annotations['return'] = returns + + keys = [node_classes.Const(key, parent=obj) + for key in annotations.keys()] + + obj.postinit(keys=keys, values=list(annotations.values())) + return obj + + @property + def py__dict__(self): + return node_classes.Dict(parent=self._instance) + + py__globals__ = py__dict__ + + @property + def py__kwdefaults__(self): + defaults = {arg.name: arg.default for arg in self._instance.args.keyword_only + if arg.default} + + obj = node_classes.Dict(parent=self._instance) + keys = [node_classes.Const(key, parent=obj) for key in defaults.keys()] + obj.postinit(keys=keys, values=list(defaults.values())) + return obj + + @property + def py__module__(self): + return node_classes.Const(self._instance.root().qname()) + + @property + def py__get__(self): + func = self._instance + + class DescriptorBoundMethod(objects.BoundMethod): + """Bound method which knows how to understand calling descriptor binding.""" + def infer_call_result(self, caller, context=None): + if len(caller.args) != 2: + raise exceptions.InferenceError( + "Invalid arguments for descriptor binding", + target=self, context=context) + + context = contextmod.copy_context(context) + cls = next(caller.args[0].infer(context=context)) + + # Rebuild the original value, but with the parent set as the + # class where it will be bound. + new_func = func.__class__(name=func.name, doc=func.doc, + lineno=func.lineno, col_offset=func.col_offset, + parent=cls) + new_func.postinit(func.args, func.body, + func.decorators, func.returns) + + # Build a proper bound method that points to our newly built function. + proxy = objects.UnboundMethod(new_func) + yield objects.BoundMethod(proxy=proxy, bound=cls) + + return DescriptorBoundMethod(proxy=self._instance, bound=self._instance) + + # These are here just for completion. + @property + def py__ne__(self): + return node_classes.Unknown() + + py__subclasshook__ = py__ne__ + py__str__ = py__ne__ + py__sizeof__ = py__ne__ + py__setattr__ = py__ne__ + py__repr__ = py__ne__ + py__reduce__ = py__ne__ + py__reduce_ex__ = py__ne__ + py__new__ = py__ne__ + py__lt__ = py__ne__ + py__eq__ = py__ne__ + py__gt__ = py__ne__ + py__format__ = py__ne__ + py__delattr__ = py__ne__ + py__getattribute__ = py__ne__ + py__hash__ = py__ne__ + py__init__ = py__ne__ + py__dir__ = py__ne__ + py__call__ = py__ne__ + py__class__ = py__ne__ + py__closure__ = py__ne__ + py__code__ = py__ne__ + + +class ClassModel(ObjectModel): + + @property + def py__module__(self): + return node_classes.Const(self._instance.root().qname()) + + @property + def py__name__(self): + return node_classes.Const(self._instance.name) + + @property + def py__qualname__(self): + return node_classes.Const(self._instance.qname()) + + @property + def py__doc__(self): + return node_classes.Const(self._instance.doc) + + @property + def py__mro__(self): + if not self._instance.newstyle: + raise exceptions.AttributeInferenceError(target=self._instance, + attribute='__mro__') + + mro = self._instance.mro() + obj = node_classes.Tuple(parent=self._instance) + obj.postinit(mro) + return obj + + @property + def pymro(self): + other_self = self + + # Cls.mro is a method and we need to return one in order to have a proper inference. + # The method we're returning is capable of inferring the underlying MRO though. + class MroBoundMethod(objects.BoundMethod): + def infer_call_result(self, caller, context=None): + yield other_self.py__mro__ + return MroBoundMethod(proxy=self._instance, bound=self._instance) + + @property + def py__bases__(self): + obj = node_classes.Tuple() + context = contextmod.InferenceContext() + elts = list(self._instance._inferred_bases(context)) + obj.postinit(elts=elts) + return obj + + @property + def py__class__(self): + return util.object_type(self._instance) + + @property + def py__subclasses__(self): + """Get the subclasses of the underlying class + + This looks only in the current module for retrieving the subclasses, + thus it might miss a couple of them. + """ + + if not self._instance.newstyle: + raise exceptions.AttributeInferenceError(target=self._instance, + attribute='__subclasses__') + + qname = self._instance.qname() + root = self._instance.root() + classes = [cls for cls in root.nodes_of_class(treeabc.ClassDef) + if cls != self._instance and cls.is_subtype_of(qname)] + + obj = node_classes.List(parent=self._instance) + obj.postinit(classes) + + class SubclassesBoundMethod(objects.BoundMethod): + def infer_call_result(self, caller, context=None): + yield obj + + return SubclassesBoundMethod(proxy=self._instance, bound=self._instance) + + @property + def py__dict__(self): + return node_classes.Dict(parent=self._instance) + + +class SuperModel(ObjectModel): + + @property + def py__thisclass__(self): + return self._instance.mro_pointer + + @property + def py__self_class__(self): + return self._instance._self_class + + @property + def py__self__(self): + return self._instance.type + + @property + def py__class__(self): + return self._instance._proxied + + +class UnboundMethodModel(ObjectModel): + + @property + def py__class__(self): + return util.object_type(self._instance) + + @property + def py__func__(self): + return self._instance._proxied + + @property + def py__self__(self): + return node_classes.Const(value=None, parent=self._instance) + + pyim_func = py__func__ + pyim_class = py__class__ + pyim_self = py__self__ + + +class BoundMethodModel(FunctionModel): + + @property + def py__func__(self): + return self._instance._proxied._proxied + + @property + def py__self__(self): + return self._instance.bound + + +class GeneratorModel(FunctionModel): + + def __new__(self, *args, **kwargs): + # Append the values from the GeneratorType unto this object. + cls = super(GeneratorModel, self).__new__(self, *args, **kwargs) + generator = astroid.MANAGER.builtins()['generator'] + for name, values in generator.locals.items(): + method = values[0] + patched = lambda self, meth=method: meth + + setattr(type(cls), 'py' + name, property(patched)) + + return cls + + @property + def py__name__(self): + return node_classes.Const(value=self._instance.parent.name, + parent=self._instance) + + @property + def py__doc__(self): + return node_classes.Const(value=self._instance.parent.doc, + parent=self._instance) + + +class InstanceModel(ObjectModel): + + @property + def py__class__(self): + return self._instance._proxied + + @property + def py__module__(self): + return node_classes.Const(self._instance.root().qname()) + + @property + def py__doc__(self): + return node_classes.Const(self._instance.doc) + + @property + def py__dict__(self): + return _dunder_dict(self._instance, self._instance.instance_attrs) diff --git a/astroid/interpreter/objects.py b/astroid/interpreter/objects.py index 2c570a1e3a..00ee945b9b 100644 --- a/astroid/interpreter/objects.py +++ b/astroid/interpreter/objects.py @@ -39,6 +39,7 @@ from astroid import util manager = util.lazy_import('manager') +objectmodel = util.lazy_import('interpreter.objectmodel') MANAGER = manager.AstroidManager() BUILTINS = six.moves.builtins.__name__ @@ -108,10 +109,11 @@ def _infer_method_result_truth(instance, method_name, context): return util.Uninferable - class BaseInstance(Proxy): """An instance base class, which provides lookup methods for potential instances.""" + special_attributes = None + def display_type(self): return 'Instance of' @@ -119,15 +121,15 @@ def getattr(self, name, context=None, lookupclass=True): try: values = self._proxied.instance_attr(name, context) except exceptions.AttributeInferenceError: - if name == '__class__': - return [self._proxied] + if self.special_attributes and name in self.special_attributes: + return [self.special_attributes.lookup(name)] + if lookupclass: # Class attributes not available through the instance # unless they are explicitly defined. - if name in ('__name__', '__bases__', '__mro__', '__subclasses__'): - return self._proxied.local_attr(name) return self._proxied.getattr(name, context, class_context=False) + util.reraise(exceptions.AttributeInferenceError(target=self, attribute=name, context=context)) @@ -151,16 +153,16 @@ def igetattr(self, name, context=None): return # XXX frame should be self._proxied, or not ? - get_attr = self.getattr(name, context, lookupclass=False) - for stmt in infer_stmts(self._wrap_attr(get_attr, context), + attrs = self.getattr(name, context, lookupclass=False) + for stmt in infer_stmts(self._wrap_attr(attrs, context), context, frame=self): yield stmt except exceptions.AttributeInferenceError: try: # fallback to class'igetattr since it has some logic to handle # descriptors - for stmt in self._wrap_attr(self._proxied.igetattr(name, context), - context): + attrs = self._proxied.igetattr(name, context, class_context=False) + for stmt in self._wrap_attr(attrs, context): yield stmt except exceptions.AttributeInferenceError as error: util.reraise(exceptions.InferenceError(**vars(error))) @@ -205,7 +207,8 @@ def infer_call_result(self, caller, context=None): @util.register_implementation(runtimeabc.Instance) class Instance(BaseInstance): """A special node representing a class instance.""" - special_attributes = frozenset(('__dict__', '__class__')) + + special_attributes = util.lazy_descriptor(lambda: objectmodel.InstanceModel()) def __repr__(self): return '' % (self._proxied.root().name, @@ -278,6 +281,9 @@ def getitem(self, index, context=None): @util.register_implementation(runtimeabc.UnboundMethod) class UnboundMethod(Proxy): """a special node representing a method not bound to an instance""" + + special_attributes = util.lazy_descriptor(lambda: objectmodel.UnboundMethodModel()) + def __repr__(self): frame = self._proxied.parent.frame() return '<%s %s of %s at 0x%s' % (self.__class__.__name__, @@ -288,13 +294,13 @@ def is_bound(self): return False def getattr(self, name, context=None): - if name == 'im_func': - return [self._proxied] + if name in self.special_attributes: + return [self.special_attributes.lookup(name)] return self._proxied.getattr(name, context) def igetattr(self, name, context=None): - if name == 'im_func': - return iter((self._proxied,)) + if name in self.special_attributes: + return iter((self.special_attributes.lookup(name), )) return self._proxied.igetattr(name, context) def infer_call_result(self, caller, context): @@ -315,10 +321,8 @@ class BoundMethod(UnboundMethod): """a special node representing a method bound to an instance""" # __func__ and __self__ are method-only special attributes, the # rest are general function special attributes. - special_attributes = frozenset( - ('__doc__', '__name__', '__qualname__', '__module__', '__defaults__', - '__code__', '__globals__', '__dict__', '__closure__', - '__annotations__', '__kwdefaults__', '__func__', '__self__')) + + special_attributes = util.lazy_descriptor(lambda: objectmodel.BoundMethodModel()) def __init__(self, proxy, bound): UnboundMethod.__init__(self, proxy) @@ -344,6 +348,9 @@ class Generator(BaseInstance): Proxied class is set once for all in raw_building. """ + + special_attributes = util.lazy_descriptor(lambda: objectmodel.GeneratorModel()) + def __init__(self, parent): self.parent = parent @@ -398,18 +405,15 @@ class Super(base.NodeNG): *scope* is the function where the super call is. """ + # Need to make this lazy, due to circular dependencies. + special_attributes = util.lazy_descriptor(lambda: objectmodel.SuperModel()) + def __init__(self, mro_pointer, mro_type, self_class, scope): self.type = mro_type self.mro_pointer = mro_pointer self._class_based = False self._self_class = self_class self._scope = scope - self._model = { - '__thisclass__': self.mro_pointer, - '__self_class__': self._self_class, - '__self__': self.type, - '__class__': self._proxied, - } def super_mro(self): """Get the MRO which will be used to lookup attributes in this super.""" @@ -462,9 +466,8 @@ def name(self): def igetattr(self, name, context=None): """Retrieve the inferred values of the given attribute name.""" - local_name = self._model.get(name) - if local_name: - yield local_name + if name in self.special_attributes: + yield self.special_attributes.lookup(name) return try: diff --git a/astroid/tests/unittest_object_model.py b/astroid/tests/unittest_object_model.py new file mode 100644 index 0000000000..af4bd1a699 --- /dev/null +++ b/astroid/tests/unittest_object_model.py @@ -0,0 +1,432 @@ +# copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved. +# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr +# +# This file is part of astroid. +# +# astroid is free software: you can redistribute it and/or modify it +# under the terms of the GNU Lesser General Public License as published by the +# Free Software Foundation, either version 2.1 of the License, or (at your +# option) any later version. +# +# astroid is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License +# for more details. +# +# You should have received a copy of the GNU Lesser General Public License along +# with astroid. If not, see . +# +# The code in this file was originally part of logilab-common, licensed under +# the same license. + +import unittest +import types +import xml + +import six + +import astroid +from astroid import exceptions +from astroid import MANAGER +from astroid import test_utils + + +BUILTINS = MANAGER.builtins() + + +class InstanceModelTest(unittest.TestCase): + + def test_instance_special_model(self): + ast_nodes = test_utils.extract_node(''' + class A: + "test" + def __init__(self): + self.a = 42 + a = A() + a.__class__ #@ + a.__module__ #@ + a.__doc__ #@ + a.__dict__ #@ + ''', module_name='collections') + + cls = next(ast_nodes[0].infer()) + self.assertIsInstance(cls, astroid.ClassDef) + self.assertEqual(cls.name, 'A') + + module = next(ast_nodes[1].infer()) + self.assertIsInstance(module, astroid.Const) + self.assertEqual(module.value, 'collections') + + doc = next(ast_nodes[2].infer()) + self.assertIsInstance(doc, astroid.Const) + self.assertEqual(doc.value, 'test') + + dunder_dict = next(ast_nodes[3].infer()) + self.assertIsInstance(dunder_dict, astroid.Dict) + attr = next(dunder_dict.getitem('a').infer()) + self.assertIsInstance(attr, astroid.Const) + self.assertEqual(attr.value, 42) + + @unittest.expectedFailure + def test_instance_local_attributes_overrides_object_model(self): + # The instance lookup needs to be changed in order for this to work. + ast_node = test_utils.extract_node(''' + class A: + @property + def __dict__(self): + return [] + A().__dict__ + ''') + inferred = next(ast_node.infer()) + self.assertIsInstance(inferred, astroid.List) + self.assertEqual(inferred.elts, []) + + +class BoundMethodModelTest(unittest.TestCase): + + def test_bound_method_model(self): + ast_nodes = test_utils.extract_node(''' + class A: + def test(self): pass + a = A() + a.test.__func__ #@ + a.test.__self__ #@ + ''') + + func = next(ast_nodes[0].infer()) + self.assertIsInstance(func, astroid.FunctionDef) + self.assertEqual(func.name, 'test') + + self_ = next(ast_nodes[1].infer()) + self.assertIsInstance(self_, astroid.Instance) + self.assertEqual(self_.name, 'A') + + +class UnboundMethodModelTest(unittest.TestCase): + + def test_unbound_method_model(self): + ast_nodes = test_utils.extract_node(''' + class A: + def test(self): pass + t = A.test + t.__class__ #@ + t.__func__ #@ + t.__self__ #@ + t.im_class #@ + t.im_func #@ + t.im_self #@ + ''') + + cls = next(ast_nodes[0].infer()) + self.assertIsInstance(cls, astroid.ClassDef) + if six.PY2: + unbound = BUILTINS.locals[types.MethodType.__name__][0] + else: + unbound = BUILTINS.locals[types.FunctionType.__name__][0] + self.assertIs(cls, unbound) + + func = next(ast_nodes[1].infer()) + self.assertIsInstance(func, astroid.FunctionDef) + self.assertEqual(func.name, 'test') + + self_ = next(ast_nodes[2].infer()) + self.assertIsInstance(self_, astroid.Const) + self.assertIsNone(self_.value) + + self.assertEqual(cls, next(ast_nodes[3].infer())) + self.assertEqual(func, next(ast_nodes[4].infer())) + self.assertIsNone(next(ast_nodes[5].infer()).value) + + +class ClassModelTest(unittest.TestCase): + + @test_utils.require_version(maxver='3.0') + def test__mro__old_style(self): + ast_node = test_utils.extract_node(''' + class A: + pass + A.__mro__ + ''') + with self.assertRaises(exceptions.InferenceError): + next(ast_node.infer()) + + @test_utils.require_version(maxver='3.0') + def test__subclasses__old_style(self): + ast_node = test_utils.extract_node(''' + class A: + pass + A.__subclasses__ + ''') + with self.assertRaises(exceptions.InferenceError): + next(ast_node.infer()) + + def test_class_model(self): + ast_nodes = test_utils.extract_node(''' + class A(object): + "test" + + class B(A): pass + class C(A): pass + + A.__module__ #@ + A.__name__ #@ + A.__qualname__ #@ + A.__doc__ #@ + A.__mro__ #@ + A.mro() #@ + A.__bases__ #@ + A.__class__ #@ + A.__dict__ #@ + A.__subclasses__() #@ + ''', module_name='collections') + + module = next(ast_nodes[0].infer()) + self.assertIsInstance(module, astroid.Const) + self.assertEqual(module.value, 'collections') + + name = next(ast_nodes[1].infer()) + self.assertIsInstance(name, astroid.Const) + self.assertEqual(name.value, 'A') + + qualname = next(ast_nodes[2].infer()) + self.assertIsInstance(qualname, astroid.Const) + self.assertEqual(qualname.value, 'collections.A') + + doc = next(ast_nodes[3].infer()) + self.assertIsInstance(doc, astroid.Const) + self.assertEqual(doc.value, 'test') + + mro = next(ast_nodes[4].infer()) + self.assertIsInstance(mro, astroid.Tuple) + self.assertEqual([cls.name for cls in mro.elts], + ['A', 'object']) + + called_mro = next(ast_nodes[5].infer()) + self.assertEqual(called_mro.elts, mro.elts) + + bases = next(ast_nodes[6].infer()) + self.assertIsInstance(bases, astroid.Tuple) + self.assertEqual([cls.name for cls in bases.elts], + ['object']) + + cls = next(ast_nodes[7].infer()) + self.assertIsInstance(cls, astroid.ClassDef) + self.assertEqual(cls.name, 'type') + + cls_dict = next(ast_nodes[8].infer()) + self.assertIsInstance(cls_dict, astroid.Dict) + + subclasses = next(ast_nodes[9].infer()) + self.assertIsInstance(subclasses, astroid.List) + self.assertEqual([cls.name for cls in subclasses.elts], ['B', 'C']) + + +class ModuleModelTest(unittest.TestCase): + + def test__path__not_a_package(self): + ast_node = test_utils.extract_node(''' + import sys + sys.__path__ #@ + ''') + with self.assertRaises(exceptions.InferenceError): + next(ast_node.infer()) + + def test_module_model(self): + ast_nodes = test_utils.extract_node(''' + import xml + xml.__path__ #@ + xml.__name__ #@ + xml.__doc__ #@ + xml.__file__ #@ + xml.__spec__ #@ + xml.__loader__ #@ + xml.__cached__ #@ + xml.__package__ #@ + xml.__dict__ #@ + ''') + + path = next(ast_nodes[0].infer()) + self.assertIsInstance(path, astroid.List) + self.assertIsInstance(path.elts[0], astroid.Const) + self.assertEqual(path.elts[0].value, xml.__path__[0]) + + name = next(ast_nodes[1].infer()) + self.assertIsInstance(name, astroid.Const) + self.assertEqual(name.value, 'xml') + + doc = next(ast_nodes[2].infer()) + self.assertIsInstance(doc, astroid.Const) + self.assertEqual(doc.value, xml.__doc__) + + file_ = next(ast_nodes[3].infer()) + self.assertIsInstance(file_, astroid.Const) + self.assertEqual(file_.value, xml.__file__.replace(".pyc", ".py")) + + for ast_node in ast_nodes[4:7]: + inferred = next(ast_node.infer()) + self.assertIs(inferred, astroid.Uninferable) + + package = next(ast_nodes[7].infer()) + self.assertIsInstance(package, astroid.Const) + self.assertEqual(package.value, 'xml') + + dict_ = next(ast_nodes[8].infer()) + self.assertIsInstance(dict_, astroid.Dict) + + +class FunctionModelTest(unittest.TestCase): + + def test_partial_descriptor_support(self): + bound, result = test_utils.extract_node(''' + class A(object): pass + def test(self): return 42 + f = test.__get__(A(), A) + f #@ + f() #@ + ''') + bound = next(bound.infer()) + self.assertIsInstance(bound, astroid.BoundMethod) + self.assertEqual(bound._proxied._proxied.name, 'test') + result = next(result.infer()) + self.assertIsInstance(result, astroid.Const) + self.assertEqual(result.value, 42) + + @unittest.expectedFailure + def test_descriptor_not_inferrring_self(self): + # We can't infer __get__(X, Y)() when the bounded function + # uses self, because of the tree's parent not being propagating good enough. + result = test_utils.extract_node(''' + class A(object): + x = 42 + def test(self): return self.x + f = test.__get__(A(), A) + f() #@ + ''') + result = next(result.infer()) + self.assertIsInstance(result, astroid.Const) + self.assertEqual(result.value, 42) + + def test_descriptors_binding_invalid(self): + ast_nodes = test_utils.extract_node(''' + class A: pass + def test(self): return 42 + test.__get__()() #@ + test.__get__(1)() #@ + test.__get__(2, 3, 4) #@ + ''') + for node in ast_nodes: + with self.assertRaises(exceptions.InferenceError): + next(node.infer()) + + def test_function_model(self): + ast_nodes = test_utils.extract_node(''' + def func(a=1, b=2): + """test""" + func.__name__ #@ + func.__doc__ #@ + func.__qualname__ #@ + func.__module__ #@ + func.__defaults__ #@ + func.__dict__ #@ + func.__globals__ #@ + func.__code__ #@ + func.__closure__ #@ + ''', module_name='collections') + + name = next(ast_nodes[0].infer()) + self.assertIsInstance(name, astroid.Const) + self.assertEqual(name.value, 'func') + + doc = next(ast_nodes[1].infer()) + self.assertIsInstance(doc, astroid.Const) + self.assertEqual(doc.value, 'test') + + qualname = next(ast_nodes[2].infer()) + self.assertIsInstance(qualname, astroid.Const) + self.assertEqual(qualname.value, 'collections.func') + + module = next(ast_nodes[3].infer()) + self.assertIsInstance(module, astroid.Const) + self.assertEqual(module.value, 'collections') + + defaults = next(ast_nodes[4].infer()) + self.assertIsInstance(defaults, astroid.Tuple) + self.assertEqual([default.value for default in defaults.elts], [1, 2]) + + dict_ = next(ast_nodes[5].infer()) + self.assertIsInstance(dict_, astroid.Dict) + + globals_ = next(ast_nodes[6].infer()) + self.assertIsInstance(globals_, astroid.Dict) + + for ast_node in ast_nodes[7:9]: + self.assertIs(next(ast_node.infer()), astroid.Uninferable) + + @test_utils.require_version(minver='3.0') + def test_empty_return_annotation(self): + ast_node = test_utils.extract_node(''' + def test(): pass + test.__annotations__ + ''') + annotations = next(ast_node.infer()) + self.assertIsInstance(annotations, astroid.Dict) + self.assertIs(annotations.getitem('return'), astroid.Empty) + + @test_utils.require_version(minver='3.0') + def test_annotations_kwdefaults(self): + ast_node = test_utils.extract_node(''' + def test(a: 1, *args: 2, f:4='lala', **kwarg:3)->2: pass + test.__annotations__ #@ + test.__kwdefaults__ #@ + ''') + annotations = next(ast_node[0].infer()) + self.assertIsInstance(annotations, astroid.Dict) + self.assertIsInstance(annotations.getitem('return'), astroid.Const) + self.assertEqual(annotations.getitem('return').value, 2) + self.assertIsInstance(annotations.getitem('a'), astroid.Const) + self.assertEqual(annotations.getitem('a').value, 1) + self.assertEqual(annotations.getitem('args').value, 2) + self.assertEqual(annotations.getitem('kwarg').value, 3) + self.assertEqual(annotations.getitem('f').value, 4) + + kwdefaults = next(ast_node[1].infer()) + self.assertIsInstance(kwdefaults, astroid.Dict) + self.assertEqual(kwdefaults.getitem('f').value, 'lala') + + +class GeneratorModelTest(unittest.TestCase): + + def test_model(self): + ast_nodes = test_utils.extract_node(''' + def test(): + "a" + yield + + gen = test() + gen.__name__ #@ + gen.__doc__ #@ + gen.gi_code #@ + gen.gi_frame #@ + gen.send #@ + ''') + + name = next(ast_nodes[0].infer()) + self.assertEqual(name.value, 'test') + + doc = next(ast_nodes[1].infer()) + self.assertEqual(doc.value, 'a') + + gi_code = next(ast_nodes[2].infer()) + self.assertIsInstance(gi_code, astroid.Instance) + self.assertEqual(gi_code.name, 'member_descriptor') + + gi_frame = next(ast_nodes[3].infer()) + self.assertIsInstance(gi_frame, astroid.Instance) + self.assertEqual(gi_frame.name, 'member_descriptor') + + send = next(ast_nodes[4].infer()) + self.assertIsInstance(send, astroid.FunctionDef) + + +if __name__ == '__main__': + unittest.main() diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index a90db7a7a5..ef66ad1ae0 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -636,12 +636,18 @@ class B: pass self.assertIsInstance(astroid['A'].getattr('__bases__')[1], nodes.AssignAttr) def test_instance_special_attributes(self): - for inst in (Instance(self.module['YO']), nodes.List(), nodes.Const(1)): + instance = self.module['YO'].instantiate_class() + self.assertEqual(len(instance.getattr('__dict__')), 1) + self.assertEqual(len(instance.getattr('__doc__')), 1) + + # Don't have these special attributes, because they belong to the Instance, + # not BaseInstance. + for inst in (nodes.List(), nodes.Const(1)): self.assertRaises(AttributeInferenceError, inst.getattr, '__mro__') self.assertRaises(AttributeInferenceError, inst.getattr, '__bases__') self.assertRaises(AttributeInferenceError, inst.getattr, '__name__') - self.assertEqual(len(inst.getattr('__dict__')), 1) - self.assertEqual(len(inst.getattr('__doc__')), 1) + self.assertRaises(AttributeInferenceError, inst.getattr, '__doc__') + self.assertRaises(AttributeInferenceError, inst.getattr, '__dict__') def test_navigation(self): klass = self.module['YO'] diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index e16ab41335..92df6a7120 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -40,6 +40,7 @@ from astroid import decorators as decorators_mod from astroid.interpreter import lookup from astroid.interpreter import objects +from astroid.interpreter import objectmodel from astroid.interpreter import runtimeabc from astroid.interpreter.util import infer_stmts from astroid import manager @@ -114,21 +115,6 @@ def function_to_method(n, klass): return n -def std_special_attributes(self, name, add_locals=True): - if add_locals: - locals = self.locals - else: - locals = {} - if name == '__name__': - return [node_classes.Const(self.name)] + locals.get(name, []) - if name == '__doc__': - return [node_classes.Const(self.doc)] + locals.get(name, []) - if name == '__dict__': - return [node_classes.Dict()] + locals.get(name, []) - # TODO: missing context - raise exceptions.AttributeInferenceError(target=self, attribute=name) - - class QualifiedNameMixin(object): def qname(node): @@ -162,22 +148,7 @@ class Module(QualifiedNameMixin, lookup.LocalsDictNode): # boolean for package module package = None - special_attributes = frozenset( - # These attributes are listed in the data model documentation. - # https://docs.python.org/3/reference/datamodel.html#the-standard-type-hierarchy - ('__name__', '__doc__', '__file__', '__dict__', - # __path__ is a standard attribute on *packages* not - # non-package modules. The only mention of it in the - # official 2.7 documentation I can find is in the - # tutorial. __package__ isn't mentioned anywhere outside a PEP: - # https://www.python.org/dev/peps/pep-0366/ - '__path__', '__package__', - # The builtins package is a member of every module's namespace. - six.moves.builtins.__name__, - # These are related to the Python 3 implementation of the - # import system, - # https://docs.python.org/3/reference/import.html#import-related-module-attributes - '__loader__', '__spec__', '__cached__')) + special_attributes = objectmodel.ModuleModel() # names of module attributes available through the global scope scope_attrs = frozenset(('__name__', '__doc__', '__file__', '__path__')) @@ -270,12 +241,7 @@ def display_type(self): def getattr(self, name, context=None, ignore_locals=False): result = [] if name in self.special_attributes: - if name == '__file__': - result = ([node_classes.Const(self.source_file)] + self.locals.get(name, [])) - elif name == '__path__' and self.package: - result = [node_classes.List()] + self.locals.get(name, []) - else: - result = std_special_attributes(self, name) + result = [self.special_attributes.lookup(name)] elif not ignore_locals and name in self.locals: result = self.locals[name] # TODO: should ignore_locals also affect external_attrs? @@ -857,10 +823,7 @@ class FunctionDef(LambdaFunctionMixin, lookup.LocalsDictNode, _astroid_fields = ('decorators', 'args', 'body') decorators = None - special_attributes = frozenset( - ('__doc__', '__name__', '__qualname__', '__module__', '__defaults__', - '__code__', '__globals__', '__dict__', '__closure__', '__annotations__', - '__kwdefaults__')) + special_attributes = objectmodel.FunctionModel() is_function = True # attributes below are set by the builder module or by raw factories _other_fields = ('name', 'doc') @@ -1016,11 +979,11 @@ def getattr(self, name, context=None): """this method doesn't look in the instance_attrs dictionary since it's done by an Instance proxy at inference time. """ - if name == '__module__': - return [node_classes.Const(self.root().qname())] if name in self.instance_attrs: return self.instance_attrs[name] - return std_special_attributes(self, name, False) + if name in self.special_attributes: + return [self.special_attributes.lookup(name)] + raise exceptions.AttributeInferenceError(target=self, attribute=name) def igetattr(self, name, context=None): """Inferred getattr, which returns an iterator of inferred statements.""" @@ -1240,9 +1203,7 @@ class ClassDef(QualifiedNameMixin, base.FilterStmtsMixin, _astroid_fields = ('decorators', 'bases', 'body') decorators = None - special_attributes = frozenset( - ('__name__', '__module__', '__dict__', '__bases__', '__doc__', - '__qualname__', '__mro__', '__subclasses__', '__class__')) + special_attributes = objectmodel.ClassModel() _type = None _metaclass_hack = False @@ -1551,20 +1512,14 @@ def getattr(self, name, context=None, class_context=True): """ values = self.locals.get(name, []) + self.external_attrs.get(name, []) - if name in self.special_attributes: - if name == '__module__': - return [node_classes.Const(self.root().qname())] + values + if name in self.special_attributes and class_context: + result = [self.special_attributes.lookup(name)] if name == '__bases__': - node = node_classes.Tuple() - elts = list(self._inferred_bases(context)) - node.postinit(elts=elts) - return [node] + values - if name == '__mro__' and self.newstyle: - mro = self.mro() - node = node_classes.Tuple() - node.postinit(elts=mro) - return [node] - return std_special_attributes(self, name) + # Need special treatment, since they are mutable + # and we need to return all the values. + result += values + return result + # don't modify the list in self.locals! values = list(values) for classnode in self.ancestors(recurs=True, context=context): @@ -1619,7 +1574,7 @@ def _get_attribute_from_metaclass(self, cls, name, context): else: yield objects.BoundMethod(attr, self) - def igetattr(self, name, context=None): + def igetattr(self, name, context=None, class_context=True): """inferred getattr, need special treatment in class to handle descriptors """ @@ -1628,7 +1583,7 @@ def igetattr(self, name, context=None): context = contextmod.copy_context(context) context.lookupname = name try: - for inferred in infer_stmts(self.getattr(name, context), + for inferred in infer_stmts(self.getattr(name, context, class_context=class_context), context, frame=self): # yield Uninferable object instead of descriptors when necessary if (not isinstance(inferred, node_classes.Const) diff --git a/astroid/util.py b/astroid/util.py index 6fd59cfb2c..21c99469ea 100644 --- a/astroid/util.py +++ b/astroid/util.py @@ -49,6 +49,13 @@ def wrapper(func, instance, args, kws): return new_generic_func +def lazy_descriptor(obj): + class DescriptorProxy(lazy_object_proxy.Proxy): + def __get__(self, instance, owner=None): + return self.__class__.__get__(self, instance) + return DescriptorProxy(obj) + + def lazy_import(module_name): return lazy_object_proxy.Proxy( lambda: importlib.import_module('.' + module_name, 'astroid')) From 53aca0ec839637e63d0a0c537a62597d066bce47 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Thu, 25 Feb 2016 00:16:45 +0000 Subject: [PATCH 216/312] Unbound methods don't occur anymore on Python 3 Instead, we're inferring FunctionDefs where an unbound access to a method was found. Close #257 --- ChangeLog | 6 +++ astroid/interpreter/objectmodel.py | 14 +++++-- astroid/interpreter/objects.py | 30 +++++++------ astroid/tests/unittest_inference.py | 11 ++--- astroid/tests/unittest_object_model.py | 8 ++-- astroid/tests/unittest_objects.py | 58 ++++++++++++++++++++++++++ astroid/tests/unittest_scoped_nodes.py | 4 +- astroid/tree/scoped_nodes.py | 11 +++-- 8 files changed, 107 insertions(+), 35 deletions(-) diff --git a/ChangeLog b/ChangeLog index 876bd2648b..4b9d69c3ce 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,12 @@ Change log for the astroid package (used to be astng) -- + * Unbound methods don't occur anymore on Python 3 + + Instead, we're inferring FunctionDefs where an unbound access + to a method was found. + Closes issue #257 + * wildcard_imported_names() got replaced by public_names() Our understanding of wildcard imports through __all__ was diff --git a/astroid/interpreter/objectmodel.py b/astroid/interpreter/objectmodel.py index ac38275e4d..7c32e6fce4 100644 --- a/astroid/interpreter/objectmodel.py +++ b/astroid/interpreter/objectmodel.py @@ -211,6 +211,10 @@ def py__cached__(self): class FunctionModel(ObjectModel): + @property + def py__class__(self): + return util.object_type(self._instance) + @property def py__name__(self): return node_classes.Const(value=self._instance.name, @@ -305,8 +309,7 @@ def infer_call_result(self, caller, context=None): func.decorators, func.returns) # Build a proper bound method that points to our newly built function. - proxy = objects.UnboundMethod(new_func) - yield objects.BoundMethod(proxy=proxy, bound=cls) + yield objects.BoundMethod(proxy=new_func, bound=cls) return DescriptorBoundMethod(proxy=self._instance, bound=self._instance) @@ -333,7 +336,6 @@ def py__ne__(self): py__init__ = py__ne__ py__dir__ = py__ne__ py__call__ = py__ne__ - py__class__ = py__ne__ py__closure__ = py__ne__ py__code__ = py__ne__ @@ -461,9 +463,13 @@ def py__self__(self): class BoundMethodModel(FunctionModel): + @property + def py__class__(self): + return util.object_type(self._instance) + @property def py__func__(self): - return self._instance._proxied._proxied + return self._instance._proxied @property def py__self__(self): diff --git a/astroid/interpreter/objects.py b/astroid/interpreter/objects.py index 00ee945b9b..383ea8ac00 100644 --- a/astroid/interpreter/objects.py +++ b/astroid/interpreter/objects.py @@ -175,7 +175,7 @@ def _wrap_attr(self, attrs, context=None): for inferred in attr.infer_call_result(self, context): yield inferred else: - yield BoundMethod(attr, self) + yield BoundMethod(attr._proxied, self) elif hasattr(attr, 'name') and attr.name == '': # This is a lambda function defined at class level, # since its scope is the underlying _proxied class. @@ -278,11 +278,7 @@ def getitem(self, index, context=None): node=self, index=index, context=context)) -@util.register_implementation(runtimeabc.UnboundMethod) -class UnboundMethod(Proxy): - """a special node representing a method not bound to an instance""" - - special_attributes = util.lazy_descriptor(lambda: objectmodel.UnboundMethodModel()) +class Method(Proxy): def __repr__(self): frame = self._proxied.parent.frame() @@ -290,9 +286,6 @@ def __repr__(self): self._proxied.name, frame.qname(), id(self)) - def is_bound(self): - return False - def getattr(self, name, context=None): if name in self.special_attributes: return [self.special_attributes.lookup(name)] @@ -303,6 +296,16 @@ def igetattr(self, name, context=None): return iter((self.special_attributes.lookup(name), )) return self._proxied.igetattr(name, context) + def bool_value(self): + return True + + +@util.register_implementation(runtimeabc.UnboundMethod) +class UnboundMethod(Method): + """a special node representing a method not bound to an instance""" + + special_attributes = util.lazy_descriptor(lambda: objectmodel.UnboundMethodModel()) + def infer_call_result(self, caller, context): # If we're unbound method __new__ of builtin object, the result is an # instance of the class given as first argument. @@ -312,15 +315,13 @@ def infer_call_result(self, caller, context): return ((x is util.Uninferable and x or Instance(x)) for x in infer) return self._proxied.infer_call_result(caller, context) - def bool_value(self): - return True + def is_bound(self): + return False @util.register_implementation(runtimeabc.BoundMethod) class BoundMethod(UnboundMethod): """a special node representing a method bound to an instance""" - # __func__ and __self__ are method-only special attributes, the - # rest are general function special attributes. special_attributes = util.lazy_descriptor(lambda: objectmodel.BoundMethodModel()) @@ -338,9 +339,6 @@ def infer_call_result(self, caller, context=None): context.boundnode = self.bound return super(BoundMethod, self).infer_call_result(caller, context) - def bool_value(self): - return True - @util.register_implementation(runtimeabc.Generator) class Generator(BaseInstance): diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index 328b813b19..fbfaddc809 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -293,7 +293,7 @@ def test_callfunc_inference(self): def test_unbound_method_inference(self): inferred = self.ast['m_unbound'].infer() meth1 = next(inferred) - self.assertIsInstance(meth1, UnboundMethod) + self.assertIsInstance(meth1, UnboundMethod if six.PY2 else nodes.FunctionDef) self.assertEqual(meth1.name, 'meth1') self.assertEqual(meth1.parent.frame().name, 'C') self.assertRaises(StopIteration, partial(next, inferred)) @@ -1223,6 +1223,7 @@ def me(self): self.assertEqual(propinferred.name, 'SendMailController') self.assertEqual(propinferred.root().name, __name__) + @unittest.skipUnless(six.PY2, "Tests UnboundMethod's im_func, which don't exist in Python 3.") def test_im_func_unwrap(self): code = ''' class EnvBasedTC: @@ -1449,7 +1450,7 @@ def __init__(self): callfunc = next(ast.nodes_of_class(nodes.Call)) func = callfunc.func inferred = func.inferred()[0] - self.assertIsInstance(inferred, UnboundMethod) + self.assertIsInstance(inferred, UnboundMethod if six.PY2 else nodes.FunctionDef) def test_instance_binary_operations(self): code = """ @@ -3285,7 +3286,7 @@ def test(self): self.assertEqual(first.bound.name, 'A') second = next(ast_nodes[1].infer()) - self.assertIsInstance(second, UnboundMethod) + self.assertIsInstance(second, UnboundMethod if six.PY2 else nodes.FunctionDef) self.assertIsInstance(second.parent, nodes.ClassDef) self.assertEqual(second.parent.name, 'A') @@ -3293,12 +3294,12 @@ def test(self): self.assertIsInstance(third, BoundMethod) # Bound to E, but the provider is B. self.assertEqual(third.bound.name, 'E') - self.assertEqual(third._proxied._proxied.parent.name, 'B') + self.assertEqual(third.parent.name, 'B') fourth = next(ast_nodes[3].infer()) self.assertIsInstance(fourth, BoundMethod) self.assertEqual(fourth.bound.name, 'E') - self.assertEqual(third._proxied._proxied.parent.name, 'B') + self.assertEqual(third.parent.name, 'B') fifth = next(ast_nodes[4].infer()) self.assertIsInstance(fifth, BoundMethod) diff --git a/astroid/tests/unittest_object_model.py b/astroid/tests/unittest_object_model.py index af4bd1a699..a058c99825 100644 --- a/astroid/tests/unittest_object_model.py +++ b/astroid/tests/unittest_object_model.py @@ -104,6 +104,7 @@ def test(self): pass class UnboundMethodModelTest(unittest.TestCase): + @unittest.skipUnless(six.PY2, "Unbound methods are available in Python 2 only.") def test_unbound_method_model(self): ast_nodes = test_utils.extract_node(''' class A: @@ -119,10 +120,7 @@ def test(self): pass cls = next(ast_nodes[0].infer()) self.assertIsInstance(cls, astroid.ClassDef) - if six.PY2: - unbound = BUILTINS.locals[types.MethodType.__name__][0] - else: - unbound = BUILTINS.locals[types.FunctionType.__name__][0] + unbound = BUILTINS.locals[types.MethodType.__name__][0] self.assertIs(cls, unbound) func = next(ast_nodes[1].infer()) @@ -286,7 +284,7 @@ def test(self): return 42 ''') bound = next(bound.infer()) self.assertIsInstance(bound, astroid.BoundMethod) - self.assertEqual(bound._proxied._proxied.name, 'test') + self.assertEqual(bound.name, 'test') result = next(result.infer()) self.assertIsInstance(result, astroid.Const) self.assertEqual(result.value, 42) diff --git a/astroid/tests/unittest_objects.py b/astroid/tests/unittest_objects.py index 1f5d078067..afcf76d305 100644 --- a/astroid/tests/unittest_objects.py +++ b/astroid/tests/unittest_objects.py @@ -518,5 +518,63 @@ def dict(self): self.assertEqual(inferred.value, 42) +class MethodTest(unittest.TestCase): + + def test_unbound_function_method_difference(self): + node = test_utils.extract_node(''' + class A: + def test(self): pass + A.test + ''') + inferred = next(node.infer()) + if six.PY2: + self.assertIsInstance(inferred, objects.UnboundMethod) + else: + self.assertIsInstance(inferred, nodes.FunctionDef) + + def test_unbound_function_from_classmethods(self): + node = test_utils.extract_node(''' + class A: + @classmethod + def test(cls): return cls.b + def b(self): return self + A.test() + ''') + inferred = next(node.infer()) + if six.PY2: + self.assertIsInstance(inferred, objects.UnboundMethod) + else: + self.assertIsInstance(inferred, nodes.FunctionDef) + + def test_static_method(self): + node = test_utils.extract_node(''' + class A: + @staticmethod + def test(self): pass + A.test + ''') + inferred = next(node.infer()) + self.assertIsInstance(inferred, nodes.FunctionDef) + + @unittest.skipUnless(six.PY2, "Unbound methods are available only on Python 2") + def test_underlying_proxied_unbound_method(self): + node = test_utils.extract_node(''' + class A: + def test(self): pass + A.test + ''') + inferred = next(node.infer()) + self.assertIsInstance(inferred._proxied, nodes.FunctionDef) + + def test_underlying_proxied_bound_method(self): + node = test_utils.extract_node(''' + class A: + def test(self): pass + A().test + ''') + inferred = next(node.infer()) + self.assertIsInstance(inferred._proxied, nodes.FunctionDef) + + if __name__ == '__main__': unittest.main() diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index ef66ad1ae0..f9c73a8c61 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -876,7 +876,7 @@ def func(arg1, arg2): for method in ('m1', 'm2', 'm3'): inferred = list(cls.igetattr(method)) self.assertEqual(len(inferred), 1) - self.assertIsInstance(inferred[0], UnboundMethod) + self.assertIsInstance(inferred[0], UnboundMethod if six.PY2 else nodes.FunctionDef) inferred = list(Instance(cls).igetattr(method)) self.assertEqual(len(inferred), 1) self.assertIsInstance(inferred[0], BoundMethod) @@ -1489,7 +1489,7 @@ class A(object): # from the class that uses the metaclass, the value # of the property property_meta = next(module['Metaclass'].igetattr('meta_property')) - self.assertIsInstance(property_meta, UnboundMethod) + self.assertIsInstance(property_meta, UnboundMethod if six.PY2 else nodes.FunctionDef) wrapping = scoped_nodes.get_wrapping_class(property_meta) self.assertEqual(wrapping, module['Metaclass']) diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 92df6a7120..87db07bba3 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -106,12 +106,17 @@ def _verify_duplicates_mro(sequences, cls, context): mros=sequences, cls=cls, context=context) -def function_to_method(n, klass): +def function_to_method(n, klass, klass_context): if isinstance(n, FunctionDef): if n.type == 'classmethod': return objects.BoundMethod(n, klass) if n.type != 'staticmethod': - return objects.UnboundMethod(n) + if six.PY2: + return objects.UnboundMethod(n) + else: + if klass_context: + return n + return objects.BoundMethod(n, klass) return n @@ -1595,7 +1600,7 @@ def igetattr(self, name, context=None, class_context=True): else: yield util.Uninferable else: - yield function_to_method(inferred, self) + yield function_to_method(inferred, self, class_context) except exceptions.AttributeInferenceError as error: if not name.startswith('__') and self.has_dynamic_getattr(context): # class handle some dynamic attributes, return a Uninferable object From 8f5ad6282949962b75b6a34edb0fca0957c01050 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sat, 27 Feb 2016 17:45:00 +0000 Subject: [PATCH 217/312] Separate the UnboundMethod from BoundMethod They were previously tied because UnboundMethod had logic for type.__new__. Now this logic was moved into a brain tip, simplifying the implementations of the methods. --- astroid/brain/brain_builtin_inference.py | 35 ++++++++++++++---------- astroid/builder.py | 2 +- astroid/interpreter/objects.py | 24 ++++++---------- astroid/interpreter/runtimeabc.py | 8 ++++-- astroid/interpreter/util.py | 2 +- 5 files changed, 38 insertions(+), 33 deletions(-) diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index a3c95370bc..524669740d 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -514,45 +514,55 @@ def infer_slice(node, context=None): def infer_type_dunder_new(caller, context=None): - """Try to infer what type.__new__(mcs, name, bases, attrs) returns. + """Try to infer what __new__ returns when called + It also looks for type.__new__(mcs, name, bases, attrs). In order for such call to be valid, the metaclass needs to be a subtype of ``type``, the name needs to be a string, the bases needs to be a tuple of classes and the attributes a dictionary of strings to values. """ - if len(caller.args) != 4: + if not caller.args: raise UseInferenceDefault + inferred = next(caller.args[0].infer()) + if inferred is util.Uninferable: + raise UseInferenceDefault + + default = iter((objects.Instance(inferred), )) + + if len(caller.args) != 4: + return default + # Verify the metaclass mcs = next(caller.args[0].infer(context=context)) if not isinstance(mcs, nodes.ClassDef): # Not a valid first argument. - raise UseInferenceDefault + return default if not mcs.is_subtype_of("%s.type" % BUILTINS): # Not a valid metaclass. - raise UseInferenceDefault + return default # Verify the name name = next(caller.args[1].infer(context=context)) if not isinstance(name, nodes.Const): # Not a valid name, needs to be a const. - raise UseInferenceDefault + return default if not isinstance(name.value, str): # Needs to be a string. - raise UseInferenceDefault + return default # Verify the bases bases = next(caller.args[2].infer(context=context)) if not isinstance(bases, nodes.Tuple): # Needs to be a tuple. - raise UseInferenceDefault + return default inferred_bases = [next(elt.infer(context=context)) for elt in bases.elts] if not all(isinstance(base, nodes.ClassDef) for base in inferred_bases): # All the bases needs to be Classes - raise UseInferenceDefault + return default cls = nodes.ClassDef(name=name.value, lineno=caller.lineno, col_offset=caller.col_offset, parent=caller) @@ -561,17 +571,17 @@ def infer_type_dunder_new(caller, context=None): attrs = next(caller.args[3].infer(context=context)) if not isinstance(attrs, nodes.Dict): # Needs to be a dictionary. - raise UseInferenceDefault + return default body = [] for key, value in attrs.items: key = next(key.infer(context=context)) value = next(value.infer(context=context)) if not isinstance(key, nodes.Const): # Something invalid as an attribute. - raise UseInferenceDefault + return default if not isinstance(key.value, str): # Not a proper attribute. - raise UseInferenceDefault + return default assign = nodes.Assign(parent=cls) assign.postinit(targets=nodes.AssignName(key.value, parent=assign), value=value) @@ -584,11 +594,8 @@ def infer_type_dunder_new(caller, context=None): def _looks_like_type_dunder_new(node): return (isinstance(node.func, nodes.Attribute) - and isinstance(node.func.expr, nodes.Name) - and node.func.expr.name == 'type' and node.func.attrname == '__new__') - # Builtins inference register_builtin_transform(infer_bool, 'bool') register_builtin_transform(infer_super, 'super') diff --git a/astroid/builder.py b/astroid/builder.py index b3a5d0f00f..b549402422 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -154,11 +154,11 @@ def _post_build(self, module, encoding): """Handles encoding and delayed nodes after a module has been built""" module.file_encoding = encoding self._manager.cache_module(module) - delayed_assignments(module) # Visit the transforms if self._apply_transforms: module = self._manager.visit_transforms(module) + delayed_assignments(module) return module def _data_build(self, data, modname, path): diff --git a/astroid/interpreter/objects.py b/astroid/interpreter/objects.py index 383ea8ac00..ad585445f7 100644 --- a/astroid/interpreter/objects.py +++ b/astroid/interpreter/objects.py @@ -170,7 +170,7 @@ def igetattr(self, name, context=None): def _wrap_attr(self, attrs, context=None): """wrap bound methods of attrs in a InstanceMethod proxies""" for attr in attrs: - if isinstance(attr, UnboundMethod): + if isinstance(attr, Method): if is_property(attr): for inferred in attr.infer_call_result(self, context): yield inferred @@ -296,9 +296,15 @@ def igetattr(self, name, context=None): return iter((self.special_attributes.lookup(name), )) return self._proxied.igetattr(name, context) + def infer_call_result(self, caller, context): + return self._proxied.infer_call_result(caller, context) + def bool_value(self): return True + def is_bound(self): + return False + @util.register_implementation(runtimeabc.UnboundMethod) class UnboundMethod(Method): @@ -306,27 +312,15 @@ class UnboundMethod(Method): special_attributes = util.lazy_descriptor(lambda: objectmodel.UnboundMethodModel()) - def infer_call_result(self, caller, context): - # If we're unbound method __new__ of builtin object, the result is an - # instance of the class given as first argument. - if (self._proxied.name == '__new__' and - self._proxied.parent.frame().qname() == '%s.object' % BUILTINS): - infer = caller.args[0].infer() if caller.args else [] - return ((x is util.Uninferable and x or Instance(x)) for x in infer) - return self._proxied.infer_call_result(caller, context) - - def is_bound(self): - return False - @util.register_implementation(runtimeabc.BoundMethod) -class BoundMethod(UnboundMethod): +class BoundMethod(Method): """a special node representing a method bound to an instance""" special_attributes = util.lazy_descriptor(lambda: objectmodel.BoundMethodModel()) def __init__(self, proxy, bound): - UnboundMethod.__init__(self, proxy) + super(BoundMethod, self).__init__(proxy) self.bound = bound def is_bound(self): diff --git a/astroid/interpreter/runtimeabc.py b/astroid/interpreter/runtimeabc.py index 4fc1a15db8..9f140cde81 100644 --- a/astroid/interpreter/runtimeabc.py +++ b/astroid/interpreter/runtimeabc.py @@ -40,11 +40,15 @@ class BuiltinInstance(RuntimeObject): """Represents an instance of a builtin.""" -class UnboundMethod(RuntimeObject): +class Method(RuntimeObject): + """Base class for methods.""" + + +class UnboundMethod(Method): """Class representing an unbound method.""" -class BoundMethod(UnboundMethod): +class BoundMethod(Method): """Class representing a bound method.""" diff --git a/astroid/interpreter/util.py b/astroid/interpreter/util.py index 18e199b88e..207cd1439e 100644 --- a/astroid/interpreter/util.py +++ b/astroid/interpreter/util.py @@ -241,7 +241,7 @@ def _object_type(node, context=None): yield metaclass continue yield builtins_ast.getattr('type')[0] - elif isinstance(inferred, (treeabc.Lambda, runtimeabc.UnboundMethod)): + elif isinstance(inferred, (treeabc.Lambda, runtimeabc.Method)): if isinstance(inferred, treeabc.Lambda): if inferred.root() is builtins_ast: yield builtins_ast[types.BuiltinFunctionType.__name__] From 9613be5921f777fc0a41269f869e20a4e7124834 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 1 Mar 2016 23:27:28 +0000 Subject: [PATCH 218/312] Try to compute descriptors only for objects with the source code available. --- astroid/inference.py | 2 +- astroid/interpreter/objects.py | 1 - astroid/tree/scoped_nodes.py | 17 ++++++++++++----- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/astroid/inference.py b/astroid/inference.py index 4eafe19120..3d749afd66 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -703,7 +703,7 @@ def infer_interpreter_object(self, context=None): yield util.Uninferable else: try: - for inferred in self.object.infer(): + for inferred in self.object.infer(context=context): yield inferred except exceptions.AstroidError: yield util.Uninferable diff --git a/astroid/interpreter/objects.py b/astroid/interpreter/objects.py index ad585445f7..3c61aef492 100644 --- a/astroid/interpreter/objects.py +++ b/astroid/interpreter/objects.py @@ -151,7 +151,6 @@ def igetattr(self, name, context=None): # avoid recursively inferring the same attr on the same class if context.push((self._proxied, name)): return - # XXX frame should be self._proxied, or not ? attrs = self.getattr(name, context, lookupclass=False) for stmt in infer_stmts(self._wrap_attr(attrs, context), diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 87db07bba3..5fe030b86e 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -1593,12 +1593,19 @@ def igetattr(self, name, context=None, class_context=True): # yield Uninferable object instead of descriptors when necessary if (not isinstance(inferred, node_classes.Const) and isinstance(inferred, objects.Instance)): - try: - inferred._proxied.getattr('__get__', context) - except exceptions.AttributeInferenceError: - yield inferred + if inferred.root().pure_python: + # We need to process only those descriptors which are custom + # classes, with their own implementation of __get__. + # Other objects, coming from builtins, shouldn't be of interest. + # TODO: do the __get__ computation. + try: + inferred._proxied.local_attr('__get__', context) + except exceptions.AttributeInferenceError: + yield inferred + else: + yield util.Uninferable else: - yield util.Uninferable + yield inferred else: yield function_to_method(inferred, self, class_context) except exceptions.AttributeInferenceError as error: From a315ef29ff51ff861cb5111d706bc17d6375f0de Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Thu, 3 Mar 2016 21:19:55 +0000 Subject: [PATCH 219/312] Exceptions have their own object model Some of exceptions's attributes, such as .args and .message, can't be inferred correctly since they are descriptors that get transformed into the proper objects at runtime. This can cause issues with the static analysis, since they are inferred as different than what's expected. Now when we're creating instances of exceptions, we're inferring a special object that knows how to transform those runtime attributes into the proper objects via a custom object model. Closes #81 --- ChangeLog | 11 +++++++ astroid/interpreter/objectmodel.py | 27 +++++++++++++++++ astroid/interpreter/objects.py | 12 ++++++++ astroid/interpreter/runtimeabc.py | 4 +++ astroid/protocols.py | 4 ++- astroid/raw_building.py | 2 +- astroid/tests/unittest_object_model.py | 42 ++++++++++++++++++++++++++ 7 files changed, 100 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 4b9d69c3ce..fe7ddd6a18 100644 --- a/ChangeLog +++ b/ChangeLog @@ -9,6 +9,17 @@ Change log for the astroid package (used to be astng) to a method was found. Closes issue #257 + * Exceptions have their own object model + + Some of exceptions's attributes, such as .args and .message, + can't be inferred correctly since they are descriptors that get + transformed into the proper objects at runtime. This can cause issues + with the static analysis, since they are inferred as different than + what's expected. Now when we're creating instances of exceptions, + we're inferring a special object that knows how to transform those + runtime attributes into the proper objects via a custom object model. + Closes issue #81 + * wildcard_imported_names() got replaced by public_names() Our understanding of wildcard imports through __all__ was diff --git a/astroid/interpreter/objectmodel.py b/astroid/interpreter/objectmodel.py index 7c32e6fce4..af1205cd42 100644 --- a/astroid/interpreter/objectmodel.py +++ b/astroid/interpreter/objectmodel.py @@ -41,6 +41,7 @@ import itertools import pprint import os +import types import six @@ -518,3 +519,29 @@ def py__doc__(self): @property def py__dict__(self): return _dunder_dict(self._instance, self._instance.instance_attrs) + + +class ExceptionInstanceModel(InstanceModel): + + @property + def pyargs(self): + message = node_classes.Const('') + args = node_classes.Tuple(parent=self._instance) + args.postinit((message, )) + return args + + if six.PY3: + # It's available only on Python 3. + + @property + def py__traceback__(self): + builtins = astroid.MANAGER.builtins() + traceback_type = builtins[types.TracebackType.__name__] + return traceback_type.instantiate_class() + + if six.PY2: + # It's available only on Python 2. + + @property + def pymessage(self): + return node_classes.Const('') diff --git a/astroid/interpreter/objects.py b/astroid/interpreter/objects.py index 3c61aef492..237caad68b 100644 --- a/astroid/interpreter/objects.py +++ b/astroid/interpreter/objects.py @@ -277,6 +277,18 @@ def getitem(self, index, context=None): node=self, index=index, context=context)) +@util.register_implementation(runtimeabc.ExceptionInstance) +class ExceptionInstance(Instance): + """Class for instances of exceptions + + It has special treatment for some of the exceptions's attributes, + which are transformed at runtime into certain concrete objects, such as + the case of .args. + """ + + special_attributes = util.lazy_descriptor(lambda: objectmodel.ExceptionInstanceModel()) + + class Method(Proxy): def __repr__(self): diff --git a/astroid/interpreter/runtimeabc.py b/astroid/interpreter/runtimeabc.py index 9f140cde81..8dec108985 100644 --- a/astroid/interpreter/runtimeabc.py +++ b/astroid/interpreter/runtimeabc.py @@ -36,6 +36,10 @@ class Instance(RuntimeObject): """Class representing an instance.""" +class ExceptionInstance(Instance): + pass + + class BuiltinInstance(RuntimeObject): """Represents an instance of a builtin.""" diff --git a/astroid/protocols.py b/astroid/protocols.py index a417259578..47741e33eb 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -34,6 +34,7 @@ from astroid import util raw_building = util.lazy_import('raw_building') +objects = util.lazy_import('interpreter.objects') def _reflected_name(name): @@ -425,7 +426,8 @@ def _resolve_asspart(parts, assign_path, context): def excepthandler_assigned_stmts(self, nodes, node=None, context=None, assign_path=None): for assigned in inferenceutil.unpack_infer(self.type): if isinstance(assigned, treeabc.ClassDef): - assigned = assigned.instantiate_class() + assigned = objects.ExceptionInstance(assigned) + yield assigned # Explicit StopIteration to return error information, see comment diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 9da407214c..265124e18b 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -630,7 +630,7 @@ def _make_assignment(new_name, old_name, parent): BUILTIN_TYPES = (types.GetSetDescriptorType, types.MemberDescriptorType, type(None), type(NotImplemented), types.GeneratorType, types.FunctionType, types.MethodType, - types.BuiltinFunctionType, types.ModuleType) + types.BuiltinFunctionType, types.ModuleType, types.TracebackType) def ast_from_builtins(): diff --git a/astroid/tests/unittest_object_model.py b/astroid/tests/unittest_object_model.py index a058c99825..7ed433fbda 100644 --- a/astroid/tests/unittest_object_model.py +++ b/astroid/tests/unittest_object_model.py @@ -426,5 +426,47 @@ def test(): self.assertIsInstance(send, astroid.FunctionDef) +class ExceptionModelTest(unittest.TestCase): + + @unittest.skipIf(six.PY2, "needs Python 3") + def test_model_py3(self): + ast_nodes = test_utils.extract_node(''' + try: + x[42] + except ValueError as err: + err.args #@ + err.__traceback__ #@ + + err.message #@ + ''') + args = next(ast_nodes[0].infer()) + self.assertIsInstance(args, astroid.Tuple) + tb = next(ast_nodes[1].infer()) + self.assertIsInstance(tb, astroid.Instance) + self.assertEqual(tb.name, 'traceback') + + with self.assertRaises(exceptions.InferenceError): + next(ast_nodes[2].infer()) + + @unittest.skipUnless(six.PY2, "needs Python 2") + def test_model_py3(self): + ast_nodes = test_utils.extract_node(''' + try: + x[42] + except ValueError as err: + err.args #@ + err.message #@ + + err.__traceback__ #@ + ''') + args = next(ast_nodes[0].infer()) + self.assertIsInstance(args, astroid.Tuple) + message = next(ast_nodes[1].infer()) + self.assertIsInstance(message, astroid.Const) + + with self.assertRaises(exceptions.InferenceError): + next(ast_nodes[2].infer()) + + if __name__ == '__main__': unittest.main() From 46961fc4fc546f65323f35bb62d991031669ad15 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 6 Mar 2016 17:10:51 +0000 Subject: [PATCH 220/312] Add missing Python 2 function model attributes. --- astroid/interpreter/objectmodel.py | 9 ++++++++ astroid/tests/unittest_object_model.py | 29 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/astroid/interpreter/objectmodel.py b/astroid/interpreter/objectmodel.py index af1205cd42..f663ee5672 100644 --- a/astroid/interpreter/objectmodel.py +++ b/astroid/interpreter/objectmodel.py @@ -340,6 +340,15 @@ def py__ne__(self): py__closure__ = py__ne__ py__code__ = py__ne__ + if six.PY2: + pyfunc_name = py__name__ + pyfunc_doc = py__doc__ + pyfunc_globals = py__globals__ + pyfunc_dict = py__dict__ + pyfunc_defaults = py__defaults__ + pyfunc_code = py__code__ + pyfunc_closure = py__closure__ + class ClassModel(ObjectModel): diff --git a/astroid/tests/unittest_object_model.py b/astroid/tests/unittest_object_model.py index 7ed433fbda..8c493bf3f1 100644 --- a/astroid/tests/unittest_object_model.py +++ b/astroid/tests/unittest_object_model.py @@ -391,6 +391,35 @@ def test(a: 1, *args: 2, f:4='lala', **kwarg:3)->2: pass self.assertIsInstance(kwdefaults, astroid.Dict) self.assertEqual(kwdefaults.getitem('f').value, 'lala') + @test_utils.require_version(maxver='3.0') + def test_function_model_for_python2(self): + ast_nodes = test_utils.extract_node(''' + def test(a=1): + "a" + + test.func_name #@ + test.func_doc #@ + test.func_dict #@ + test.func_globals #@ + test.func_defaults #@ + test.func_code #@ + test.func_closure #@ + ''') + name = next(ast_nodes[0].infer()) + self.assertIsInstance(name, astroid.Const) + self.assertEqual(name.value, 'test') + doc = next(ast_nodes[1].infer()) + self.assertIsInstance(doc, astroid.Const) + self.assertEqual(doc.value, 'a') + pydict = next(ast_nodes[2].infer()) + self.assertIsInstance(pydict, astroid.Dict) + pyglobals = next(ast_nodes[3].infer()) + self.assertIsInstance(pyglobals, astroid.Dict) + defaults = next(ast_nodes[4].infer()) + self.assertIsInstance(defaults, astroid.Tuple) + for node in ast_nodes[5:]: + self.assertIs(next(node.infer()), astroid.Uninferable) + class GeneratorModelTest(unittest.TestCase): From 3f6af118d19342fc92167c78865f58461e091ccc Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 6 Mar 2016 21:44:45 +0000 Subject: [PATCH 221/312] dict.values, dict.keys and dict.items are properly inferred --- ChangeLog | 3 ++ astroid/brain/brain_builtin_inference.py | 14 ++++-- astroid/interpreter/objectmodel.py | 51 +++++++++++++++++++++ astroid/interpreter/objects.py | 33 +++++++++++++- astroid/tests/unittest_inference.py | 14 ++++++ astroid/tests/unittest_object_model.py | 57 ++++++++++++++++++++++++ astroid/tree/node_classes.py | 2 +- 7 files changed, 168 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index fe7ddd6a18..9a5146a192 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,9 @@ Change log for the astroid package (used to be astng) ===================================================== -- + * dict.values, dict.keys and dict.items are properly + inferred to their corresponding type, which also + includes the proper containers for Python 3. * Unbound methods don't occur anymore on Python 3 diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index 524669740d..86c3f79a81 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -206,25 +206,31 @@ def _infer_builtin(node, context, infer_tuple = functools.partial( _infer_builtin, klass=nodes.Tuple, - iterables=(nodes.List, nodes.Set, objects.FrozenSet), + iterables=(nodes.List, nodes.Set, objects.FrozenSet, + objects.DictItems, objects.DictKeys, + objects.DictValues), build_elts=tuple) infer_list = functools.partial( _infer_builtin, klass=nodes.List, - iterables=(nodes.Tuple, nodes.Set, objects.FrozenSet), + iterables=(nodes.Tuple, nodes.Set, objects.FrozenSet, + objects.DictItems, objects.DictKeys, + objects.DictValues), build_elts=list) infer_set = functools.partial( _infer_builtin, klass=nodes.Set, - iterables=(nodes.List, nodes.Tuple, objects.FrozenSet), + iterables=(nodes.List, nodes.Tuple, objects.FrozenSet, + objects.DictKeys), build_elts=set) infer_frozenset = functools.partial( _infer_builtin, klass=objects.FrozenSet, - iterables=(nodes.List, nodes.Tuple, nodes.Set, objects.FrozenSet), + iterables=(nodes.List, nodes.Tuple, nodes.Set, objects.FrozenSet, + objects.DictKeys), build_elts=frozenset) diff --git a/astroid/interpreter/objectmodel.py b/astroid/interpreter/objectmodel.py index f663ee5672..61ae70081e 100644 --- a/astroid/interpreter/objectmodel.py +++ b/astroid/interpreter/objectmodel.py @@ -554,3 +554,54 @@ def py__traceback__(self): @property def pymessage(self): return node_classes.Const('') + + +class DictModel(ObjectModel): + + @property + def py__class__(self): + return self._instance._proxied + + def _generic_dict_attribute(self, obj, name): + """Generate a bound method that can infer the given *obj*.""" + class DictMethodBoundMethod(objects.BoundMethod): + def infer_call_result(self, caller, context=None): + yield obj + + meth = next(self._instance._proxied.igetattr(name)) + return DictMethodBoundMethod(proxy=meth, bound=self._instance) + + @property + def pyitems(self): + elems = [] + obj = node_classes.List(parent=self._instance) + for key, value in self._instance.items: + elem = node_classes.Tuple(parent=obj) + elem.postinit((key, value)) + elems.append(elem) + obj.postinit(elts=elems) + + if six.PY3: + obj = objects.DictItems(obj) + + return self._generic_dict_attribute(obj, 'items') + + @property + def pykeys(self): + obj = node_classes.List(parent=self._instance) + obj.postinit(elts=self._instance.keys) + + if six.PY3: + obj = objects.DictKeys(obj) + + return self._generic_dict_attribute(obj, 'keys') + + @property + def pyvalues(self): + obj = node_classes.List(parent=self._instance) + obj.postinit(elts=self._instance.values) + + if six.PY3: + obj = objects.DictValues(obj) + + return self._generic_dict_attribute(obj, 'values') diff --git a/astroid/interpreter/objects.py b/astroid/interpreter/objects.py index 237caad68b..a919649182 100644 --- a/astroid/interpreter/objects.py +++ b/astroid/interpreter/objects.py @@ -173,8 +173,12 @@ def _wrap_attr(self, attrs, context=None): if is_property(attr): for inferred in attr.infer_call_result(self, context): yield inferred - else: + elif type(attr) in (BoundMethod, UnboundMethod): + # This is a strict type check because we can have subclasses + # of these two objects which we don't want to be unproxied. yield BoundMethod(attr._proxied, self) + else: + yield attr elif hasattr(attr, 'name') and attr.name == '': # This is a lambda function defined at class level, # since its scope is the underlying _proxied class. @@ -526,3 +530,30 @@ def igetattr(self, name, context=None): def getattr(self, name, context=None): return list(self.igetattr(name, context=context)) + + +class DictInstance(BaseInstance): + """Special kind of instances for dictionaries + + This instance knows the underlying object model of the dictionaries, which means + that methods such as .values or .items can be properly inferred. + """ + + special_attributes = util.lazy_descriptor(lambda: objectmodel.DictModel()) + + +# Custom objects tailored for dictionaries, which are used to +# disambiguate between the types of Python 2 dict's method returns +# and Python 3 (where they return set like objects). +class DictItems(Proxy): + __str__ = base.NodeNG.__str__ + __repr__ = base.NodeNG.__repr__ + + +class DictKeys(Proxy): + __str__ = base.NodeNG.__str__ + __repr__ = base.NodeNG.__repr__ + +class DictValues(Proxy): + __str__ = base.NodeNG.__str__ + __repr__ = base.NodeNG.__repr__ diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index fbfaddc809..e71dfbd7db 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -1694,6 +1694,20 @@ def test_list_builtin_inference(self): self.assertIsInstance(inferred, Instance) self.assertEqual(inferred.qname(), "{}.list".format(BUILTINS)) + def test_conversion_of_dict_methods(self): + ast_nodes = test_utils.extract_node(''' + list({1:2, 2:3}.values()) #@ + list({1:2, 2:3}.keys()) #@ + tuple({1:2, 2:3}.values()) #@ + tuple({1:2, 3:4}.keys()) #@ + set({1:2, 2:4}.keys()) #@ + ''') + self.assertInferList(ast_nodes[0], [2, 3]) + self.assertInferList(ast_nodes[1], [1, 2]) + self.assertInferTuple(ast_nodes[2], [2, 3]) + self.assertInferTuple(ast_nodes[3], [1, 3]) + self.assertInferSet(ast_nodes[4], [1, 2]) + @test_utils.require_version('3.0') def test_builtin_inference_py3k(self): code = """ diff --git a/astroid/tests/unittest_object_model.py b/astroid/tests/unittest_object_model.py index 8c493bf3f1..57529259cb 100644 --- a/astroid/tests/unittest_object_model.py +++ b/astroid/tests/unittest_object_model.py @@ -29,6 +29,7 @@ from astroid import exceptions from astroid import MANAGER from astroid import test_utils +from astroid.interpreter import objects BUILTINS = MANAGER.builtins() @@ -497,5 +498,61 @@ def test_model_py3(self): next(ast_nodes[2].infer()) +class DictObjectModelTest(unittest.TestCase): + + def test__class__(self): + ast_node = test_utils.extract_node('{}.__class__') + inferred = next(ast_node.infer()) + self.assertIsInstance(inferred, astroid.ClassDef) + self.assertEqual(inferred.name, 'dict') + + def test_attributes_inferred_as_methods(self): + ast_nodes = test_utils.extract_node(''' + {}.values #@ + {}.items #@ + {}.keys #@ + ''') + for node in ast_nodes: + inferred = next(node.infer()) + self.assertIsInstance(inferred, astroid.BoundMethod) + + @unittest.skipUnless(six.PY2, "needs Python 2") + def test_concrete_objects_for_dict_methods(self): + ast_nodes = test_utils.extract_node(''' + {1:1, 2:3}.values() #@ + {1:1, 2:3}.keys() #@ + {1:1, 2:3}.items() #@ + ''') + values = next(ast_nodes[0].infer()) + self.assertIsInstance(values, astroid.List) + self.assertEqual([value.value for value in values.elts], [1, 3]) + + keys = next(ast_nodes[1].infer()) + self.assertIsInstance(keys, astroid.List) + self.assertEqual([key.value for key in keys.elts], [1, 2]) + + items = next(ast_nodes[2].infer()) + self.assertIsInstance(items, astroid.List) + for expected, elem in zip([(1, 1), (2, 3)], items.elts): + self.assertIsInstance(elem, astroid.Tuple) + self.assertEqual(list(expected), [elt.value for elt in elem.elts]) + + @unittest.skipIf(six.PY2, "needs Python 3") + def test_wrapper_objects_for_dict_methods_python3(self): + ast_nodes = test_utils.extract_node(''' + {1:1, 2:3}.values() #@ + {1:1, 2:3}.keys() #@ + {1:1, 2:3}.items() #@ + ''') + values = next(ast_nodes[0].infer()) + self.assertIsInstance(values, objects.DictValues) + self.assertEqual([elt.value for elt in values.elts], [1, 3]) + keys = next(ast_nodes[1].infer()) + self.assertIsInstance(keys, objects.DictKeys) + self.assertEqual([elt.value for elt in keys.elts], [1, 2]) + items = next(ast_nodes[2].infer()) + self.assertIsInstance(items, objects.DictItems) + + if __name__ == '__main__': unittest.main() diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index 7189b068be..a5ba8c8829 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -613,7 +613,7 @@ def postinit(self, targets=None): @util.register_implementation(treeabc.Dict) @util.register_implementation(runtimeabc.BuiltinInstance) -class Dict(base.NodeNG, objects.BaseInstance): +class Dict(base.NodeNG, objects.DictInstance): """class representing a Dict node""" _astroid_fields = ('keys', 'values') From de0c9baaf10fc455c8ee23a51ceda80b0825f090 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 6 Mar 2016 21:51:26 +0000 Subject: [PATCH 222/312] Add test for slots and dict.keys. --- astroid/tests/unittest_scoped_nodes.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index f9c73a8c61..c0db13968a 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -1226,6 +1226,18 @@ class OldStyle: module['OldStyle'].slots() self.assertEqual(str(cm.exception), msg) + def test_slots_for_dict_keys(self): + module = builder.parse(''' + class Issue(object): + SlotDefaults = {'id': 0, 'id1':1} + __slots__ = SlotDefaults.keys() + ''') + cls = module['Issue'] + slots = cls.slots() + self.assertEqual(len(slots), 2) + self.assertEqual(slots[0].value, 'id') + self.assertEqual(slots[1].value, 'id1') + def test_slots_empty_list_of_slots(self): module = builder.parse(""" class Klass(object): From 7c9fef13cffe23c7e0c90b4bb10dede023766ae5 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Fri, 11 Mar 2016 01:04:54 +0000 Subject: [PATCH 223/312] decoratornames() does not leak InferenceError anymore. --- ChangeLog | 2 ++ astroid/tests/unittest_regrtest.py | 15 +++++++++++++++ astroid/tree/scoped_nodes.py | 7 +++++-- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9a5146a192..270a742fb1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -6,6 +6,8 @@ Change log for the astroid package (used to be astng) inferred to their corresponding type, which also includes the proper containers for Python 3. + * decoratornames() does not leak InferenceError anymore. + * Unbound methods don't occur anymore on Python 3 Instead, we're inferring FunctionDefs where an unbound access diff --git a/astroid/tests/unittest_regrtest.py b/astroid/tests/unittest_regrtest.py index 52a45cf939..48abcc50c1 100644 --- a/astroid/tests/unittest_regrtest.py +++ b/astroid/tests/unittest_regrtest.py @@ -331,6 +331,21 @@ def inner(*more_args): self.assertIsNotNone(inferred.parent) self.assertIsInstance(inferred.parent, nodes.BinOp) + def test_decorator_names_inference_error_leaking(self): + node = extract_node(''' + class Parent(object): + @property + def foo(self): + pass + + class Child(Parent): + @Parent.foo.getter + def foo(self): #@ + return super(Child, self).foo + ['oink'] + ''') + inferred = next(node.infer()) + self.assertEqual(inferred.decoratornames(), set()) + class Whatever(object): a = property(lambda x: x, lambda x: x) diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 5fe030b86e..9fe5e9c0a7 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -1017,8 +1017,11 @@ def decoratornames(self): decoratornodes += self.decorators.nodes decoratornodes += self.extra_decorators for decnode in decoratornodes: - for infnode in decnode.infer(): - result.add(infnode.qname()) + try: + for infnode in decnode.infer(): + result.add(infnode.qname()) + except exceptions.InferenceError: + continue return result def is_bound(self): From 4681c9502b88361ed2ba3b016383df8a85e63c4f Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sat, 19 Mar 2016 12:08:48 +0000 Subject: [PATCH 224/312] Try to see if the object from the determined module is actually the object we're building They can be different when in the original module (let's say an __init__ file) the object is imported under a name and then used as a base class for a class with the same name as the object in question, paired with the fact that the initial object is faking its name inside is original module. This happens for instance with the bitarray module, which has a bitarray._bitarray C extensions, which defines a bitarray._bitarray._bitarray type, the latter faking its name as bitarray._bitarray. In the __init__ file, the original type gets used as a base class in the actual bitarray class that the module exports. What happens is that this behaviour will introduce a cycle in the form of an ImportFrom as in 'from bitarray._bitarray import _bitarray as _bitarray` when analyzing the bitarray._bitarray module, which means that the resulting ast will reimport itself, leading to not inferring anything from the module. The solution we're using is to check that the module where we expected to have our object actually has the object we want. Unfortunately I couldn't find a Python test through which we can reflect this behaviour --- astroid/raw_building.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 265124e18b..f16bd6df23 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -250,13 +250,17 @@ def ast_from_class(cls, built_objects, module, name=None, parent=None): # appropriate ClassDef node is returned. Arguably, it would be # possible to check if a class is in a module under another name, # but does this ever happen? - if (inspected_module is not None and - inspected_module is not module and - hasattr(inspected_module, cls.__name__)): - return (node_classes.ImportFrom(fromname= - getattr(inspected_module, '__name__', None), - names=[[cls.__name__, name]], - parent=parent),) + if inspected_module is not None and inspected_module is not module: + sentinel = object() + declared_obj = getattr(inspected_module, cls.__name__, sentinel) + if declared_obj is not sentinel and declared_obj == cls: + # They are indeed the same objects, but one of them declares to be in + # another module. + return (node_classes.ImportFrom(fromname= + getattr(inspected_module, '__name__', None), + names=[[cls.__name__, name]], + parent=parent),) + class_node = scoped_nodes.ClassDef(name=cls.__name__, doc=inspect.getdoc(cls), parent=parent) result = [class_node] if name is not None and name != cls.__name__: From a097ec330853382140729c4a3fa8c497173b567b Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Thu, 24 Mar 2016 17:31:47 +0000 Subject: [PATCH 225/312] Add brain tips for the ssl library. --- ChangeLog | 2 + astroid/brain/brain_ssl.py | 65 ++++++++++++++++++++++++++++++ astroid/tests/unittest_regrtest.py | 8 ++++ 3 files changed, 75 insertions(+) create mode 100644 astroid/brain/brain_ssl.py diff --git a/ChangeLog b/ChangeLog index 270a742fb1..182e892379 100644 --- a/ChangeLog +++ b/ChangeLog @@ -6,6 +6,8 @@ Change log for the astroid package (used to be astng) inferred to their corresponding type, which also includes the proper containers for Python 3. + * Brain tips for the ssl library. + * decoratornames() does not leak InferenceError anymore. * Unbound methods don't occur anymore on Python 3 diff --git a/astroid/brain/brain_ssl.py b/astroid/brain/brain_ssl.py new file mode 100644 index 0000000000..1cf8d1b8f1 --- /dev/null +++ b/astroid/brain/brain_ssl.py @@ -0,0 +1,65 @@ +"""Astroid hooks for the ssl library.""" + +from astroid import MANAGER, register_module_extender +from astroid.builder import AstroidBuilder +from astroid import nodes +from astroid import parse + + +def ssl_transform(): + return parse(''' + from _ssl import OPENSSL_VERSION_NUMBER, OPENSSL_VERSION_INFO, OPENSSL_VERSION + from _ssl import _SSLContext, MemoryBIO + from _ssl import ( + SSLError, SSLZeroReturnError, SSLWantReadError, SSLWantWriteError, + SSLSyscallError, SSLEOFError, + ) + from _ssl import CERT_NONE, CERT_OPTIONAL, CERT_REQUIRED + from _ssl import txt2obj as _txt2obj, nid2obj as _nid2obj + from _ssl import RAND_status, RAND_add, RAND_bytes, RAND_pseudo_bytes + try: + from _ssl import RAND_egd + except ImportError: + # LibreSSL does not provide RAND_egd + pass + from _ssl import (OP_ALL, OP_CIPHER_SERVER_PREFERENCE, + OP_NO_COMPRESSION, OP_NO_SSLv2, OP_NO_SSLv3, + OP_NO_TLSv1, OP_NO_TLSv1_1, OP_NO_TLSv1_2, + OP_SINGLE_DH_USE, OP_SINGLE_ECDH_USE) + + from _ssl import (ALERT_DESCRIPTION_ACCESS_DENIED, ALERT_DESCRIPTION_BAD_CERTIFICATE, + ALERT_DESCRIPTION_BAD_CERTIFICATE_HASH_VALUE, + ALERT_DESCRIPTION_BAD_CERTIFICATE_STATUS_RESPONSE, + ALERT_DESCRIPTION_BAD_RECORD_MAC, + ALERT_DESCRIPTION_CERTIFICATE_EXPIRED, + ALERT_DESCRIPTION_CERTIFICATE_REVOKED, + ALERT_DESCRIPTION_CERTIFICATE_UNKNOWN, + ALERT_DESCRIPTION_CERTIFICATE_UNOBTAINABLE, + ALERT_DESCRIPTION_CLOSE_NOTIFY, ALERT_DESCRIPTION_DECODE_ERROR, + ALERT_DESCRIPTION_DECOMPRESSION_FAILURE, + ALERT_DESCRIPTION_DECRYPT_ERROR, + ALERT_DESCRIPTION_HANDSHAKE_FAILURE, + ALERT_DESCRIPTION_ILLEGAL_PARAMETER, + ALERT_DESCRIPTION_INSUFFICIENT_SECURITY, + ALERT_DESCRIPTION_INTERNAL_ERROR, + ALERT_DESCRIPTION_NO_RENEGOTIATION, + ALERT_DESCRIPTION_PROTOCOL_VERSION, + ALERT_DESCRIPTION_RECORD_OVERFLOW, + ALERT_DESCRIPTION_UNEXPECTED_MESSAGE, + ALERT_DESCRIPTION_UNKNOWN_CA, + ALERT_DESCRIPTION_UNKNOWN_PSK_IDENTITY, + ALERT_DESCRIPTION_UNRECOGNIZED_NAME, + ALERT_DESCRIPTION_UNSUPPORTED_CERTIFICATE, + ALERT_DESCRIPTION_UNSUPPORTED_EXTENSION, + ALERT_DESCRIPTION_USER_CANCELLED) + from _ssl import (SSL_ERROR_EOF, SSL_ERROR_INVALID_ERROR_CODE, SSL_ERROR_SSL, + SSL_ERROR_SYSCALL, SSL_ERROR_WANT_CONNECT, SSL_ERROR_WANT_READ, + SSL_ERROR_WANT_WRITE, SSL_ERROR_WANT_X509_LOOKUP, SSL_ERROR_ZERO_RETURN) + from _ssl import VERIFY_CRL_CHECK_CHAIN, VERIFY_CRL_CHECK_LEAF, VERIFY_DEFAULT, VERIFY_X509_STRICT + from _ssl import HAS_SNI, HAS_ECDH, HAS_NPN, HAS_ALPN + from _ssl import _OPENSSL_API_VERSION + from _ssl import PROTOCOL_SSLv23, PROTOCOL_TLSv1, PROTOCOL_TLSv1_1, PROTOCOL_TLSv1_2 + ''') + + +register_module_extender(MANAGER, 'ssl', ssl_transform) diff --git a/astroid/tests/unittest_regrtest.py b/astroid/tests/unittest_regrtest.py index 48abcc50c1..deba8b4183 100644 --- a/astroid/tests/unittest_regrtest.py +++ b/astroid/tests/unittest_regrtest.py @@ -346,6 +346,14 @@ def foo(self): #@ inferred = next(node.infer()) self.assertEqual(inferred.decoratornames(), set()) + def test_ssl_protocol(self): + node = extract_node(''' + import ssl + ssl.PROTOCOL_TLSv1 + ''') + inferred = next(node.infer()) + self.assertIsInstance(inferred, nodes.Const) + class Whatever(object): a = property(lambda x: x, lambda x: x) From 5ea9e83b76f75754ac4660513e8b42c3a4de2af7 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 28 Mar 2016 14:56:18 +0100 Subject: [PATCH 226/312] Add brain tips for the functools.lru_cache decorator. --- ChangeLog | 2 + astroid/brain/brain_functools.py | 76 ++++++++++++++++++++++++++ astroid/tests/unittest_object_model.py | 24 ++++++++ 3 files changed, 102 insertions(+) create mode 100644 astroid/brain/brain_functools.py diff --git a/ChangeLog b/ChangeLog index 182e892379..356580af45 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,8 @@ Change log for the astroid package (used to be astng) ===================================================== -- + * Add brain tips for functools.lru_cache. + * dict.values, dict.keys and dict.items are properly inferred to their corresponding type, which also includes the proper containers for Python 3. diff --git a/astroid/brain/brain_functools.py b/astroid/brain/brain_functools.py new file mode 100644 index 0000000000..7fff071bbe --- /dev/null +++ b/astroid/brain/brain_functools.py @@ -0,0 +1,76 @@ +"""Astroid hooks for understanding functools library module.""" + +import astroid +from astroid.interpreter import util as interpreter_util +from astroid.interpreter import objects +from astroid.interpreter import objectmodel +from astroid.test_utils import extract_node +from astroid import MANAGER + + +LRU_CACHE = 'functools.lru_cache' + + +class LruWrappedModel(objectmodel.FunctionModel): + """Special attribute model for functions decorated with functools.lru_cache. + + The said decorators patches at decoration time some functions onto + the decorated function. + """ + + @property + def py__wrapped__(self): + return self._instance + + @property + def pycache_info(self): + cache_info = extract_node(''' + from functools import _CacheInfo + _CacheInfo(0, 0, 0, 0) + ''') + class CacheInfoBoundMethod(objects.BoundMethod): + def infer_call_result(self, caller, context=None): + yield interpreter_util.safe_infer(cache_info) + + return CacheInfoBoundMethod(proxy=self._instance, bound=self._instance) + + @property + def pycache_clear(self): + node = extract_node('''def cache_clear(): pass''') + return objects.BoundMethod(proxy=node, bound=self._instance.parent.scope()) + + +class LruWrappedFunctionDef(astroid.FunctionDef): + special_attributes = LruWrappedModel() + + +def _transform_lru_cache(node, context=None): + # TODO: this needs the zipper, because the new node's attributes + # will still point to the old node. + new_func = LruWrappedFunctionDef(name=node.name, doc=node.name, + lineno=node.lineno, col_offset=node.col_offset, + parent=node.parent) + new_func.postinit(node.args, node.body, node.decorators, node.returns) + return new_func + + +def _looks_like_lru_cache(node): + """Check if the given function node is decorated with lru_cache.""" + if not node.decorators: + return False + + for decorator in node.decorators.nodes: + if not isinstance(decorator, astroid.Call): + continue + + func = interpreter_util.safe_infer(decorator.func) + if func in (None, astroid.Uninferable): + continue + + if isinstance(func, astroid.FunctionDef) and func.qname() == LRU_CACHE: + return True + return False + + +MANAGER.register_transform(astroid.FunctionDef, _transform_lru_cache, + _looks_like_lru_cache) diff --git a/astroid/tests/unittest_object_model.py b/astroid/tests/unittest_object_model.py index 57529259cb..29a867811d 100644 --- a/astroid/tests/unittest_object_model.py +++ b/astroid/tests/unittest_object_model.py @@ -554,5 +554,29 @@ def test_wrapper_objects_for_dict_methods_python3(self): self.assertIsInstance(items, objects.DictItems) +class LruCacheModelTest(unittest.TestCase): + + @unittest.skipIf(six.PY2, "needs Python 3") + def test_lru_cache(self): + ast_nodes = test_utils.extract_node(''' + import functools + class Foo(object): + @functools.lru_cache() + def foo(): + pass + f = Foo() + f.foo.cache_clear #@ + f.foo.__wrapped__ #@ + f.foo.cache_info() #@ + ''') + cache_clear = next(ast_nodes[0].infer()) + self.assertIsInstance(cache_clear, objects.BoundMethod) + wrapped = next(ast_nodes[1].infer()) + self.assertIsInstance(wrapped, astroid.FunctionDef) + self.assertEqual(wrapped.name, 'foo') + cache_info = next(ast_nodes[2].infer()) + self.assertIsInstance(cache_info, objects.Instance) + + if __name__ == '__main__': unittest.main() From 5a3e967b2326170632a27160075d22f36d37c677 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Wed, 30 Mar 2016 18:40:31 +0100 Subject: [PATCH 227/312] Apply the strings / bytes patching only when the builtins module gets built This patch fixes a problem with the transforms on Python 2, which were never called due to a bad interaction between a transform and the underlying inference mechanism. The string / byte class was patched before the brain modules were loaded, since extend_str was called at the module level. Inside extend_str we were using .locals, which triggers .delayed_assignments, which can trigger as well inference. In the case of Python 2, this led to importing of the six module before actually having the transforms ready for it, which means that no transforming was ever done for some of these modules. By delaying the patching of the builtins module until builtins is ready, we alleviate this problem. --- astroid/__init__.py | 7 ++++--- astroid/brain/brain_builtin_inference.py | 22 +++++++++++++--------- astroid/brain/brain_six.py | 1 - 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/astroid/__init__.py b/astroid/__init__.py index efae8eca8b..4eb0b4cbf5 100644 --- a/astroid/__init__.py +++ b/astroid/__init__.py @@ -127,9 +127,10 @@ def transform(node, infer_function=infer_function): def register_module_extender(manager, module_name, get_extension_mod): def transform(module): extension_module = get_extension_mod() - for statement in extension_module.body: - statement.parent = module - module.body.append(statement) + if extension_module: + for statement in extension_module.body: + statement.parent = module + module.body.append(statement) manager.register_transform(Module, transform, lambda n: n.name == module_name) diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index 86c3f79a81..c99c9d90b6 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -7,7 +7,8 @@ import six from astroid import (MANAGER, UseInferenceDefault, AttributeInferenceError, - inference_tip, InferenceError, NameInferenceError) + inference_tip, InferenceError, NameInferenceError, + register_module_extender) from astroid.builder import AstroidBuilder from astroid.interpreter import objects from astroid.interpreter import util as interpreterutil @@ -86,14 +87,6 @@ def extend_builtins(class_transforms): for class_name, transform in class_transforms.items(): transform(builtin_ast[class_name]) -if sys.version_info > (3, 0): - extend_builtins({'bytes': functools.partial(_extend_str, rvalue="b''"), - 'str': functools.partial(_extend_str, rvalue="''")}) -else: - # TODO: what about unicode_literals? This is hopelessly broken. - extend_builtins({'str': functools.partial(_extend_str, rvalue="''"), - 'unicode': functools.partial(_extend_str, rvalue="u''")}) - def register_builtin_transform(transform, builtin_name): """Register a new transform function for the given *builtin_name*. @@ -602,6 +595,16 @@ def _looks_like_type_dunder_new(node): return (isinstance(node.func, nodes.Attribute) and node.func.attrname == '__new__') + +def _patch_strings(): + if sys.version_info > (3, 0): + extend_builtins({'bytes': functools.partial(_extend_str, rvalue="b''"), + 'str': functools.partial(_extend_str, rvalue="''")}) + else: + # TODO: what about unicode_literals? This is hopelessly broken. + extend_builtins({'str': functools.partial(_extend_str, rvalue="''"), + 'unicode': functools.partial(_extend_str, rvalue="u''")}) + # Builtins inference register_builtin_transform(infer_bool, 'bool') register_builtin_transform(infer_super, 'super') @@ -619,3 +622,4 @@ def _looks_like_type_dunder_new(node): # infer type.__new__ calls MANAGER.register_transform(nodes.Call, inference_tip(infer_type_dunder_new), _looks_like_type_dunder_new) +register_module_extender(MANAGER, six.moves.builtins.__name__, _patch_strings) diff --git a/astroid/brain/brain_six.py b/astroid/brain/brain_six.py index 1c0ddf6396..d2b0510f31 100644 --- a/astroid/brain/brain_six.py +++ b/astroid/brain/brain_six.py @@ -280,7 +280,6 @@ def transform_six_add_metaclass(node): node._metaclass = metaclass return node - register_module_extender(MANAGER, 'six', six_moves_transform) register_module_extender(MANAGER, 'requests.packages.urllib3.packages.six', six_moves_transform) From 0a5f44ffe754cabe6f99369d90a2c20cea758f22 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 5 Apr 2016 11:07:28 +0300 Subject: [PATCH 228/312] Understand the `dir` builtin. This can be used for understanding highly dynamic code, which involves modifications of attributes at runtime (updating globals for example, where the elements come from a dir(something)) --- ChangeLog | 6 ++ astroid/brain/brain_builtin_inference.py | 116 ++++++++++++++++++++++ astroid/tests/unittest_inference.py | 120 +++++++++++++++++++++++ 3 files changed, 242 insertions(+) diff --git a/ChangeLog b/ChangeLog index 356580af45..51bd2efa82 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,12 @@ Change log for the astroid package (used to be astng) ===================================================== -- + * Understand the `dir` builtin. + + This can be used for understanding highly dynamic code, which involves + modifications of attributes at runtime (updating globals for example, + where the elements come from a dir(something)) + * Add brain tips for functools.lru_cache. * dict.values, dict.keys and dict.items are properly diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index c99c9d90b6..ef0866c552 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -2,6 +2,7 @@ import collections import functools +import itertools import sys import textwrap @@ -10,7 +11,9 @@ inference_tip, InferenceError, NameInferenceError, register_module_extender) from astroid.builder import AstroidBuilder +from astroid import context as contextmod from astroid.interpreter import objects +from astroid.interpreter import runtimeabc from astroid.interpreter import util as interpreterutil from astroid import nodes from astroid import raw_building @@ -512,6 +515,118 @@ def infer_slice(node, context=None): return slice_node +def _custom_dir_result(node): + context = contextmod.InferenceContext() + context.callcontext = contextmod.CallContext(args=[node]) + + try: + for inferred in node.igetattr('__dir__', context=context): + if isinstance(inferred, nodes.FunctionDef): + # Need to rewrap it as a bound method. + inferred = objects.BoundMethod(inferred, node) + if not isinstance(inferred, runtimeabc.Method): + continue + + method_root = inferred.root() + if method_root.name != BUILTINS and getattr(method_root, 'pure_python', None): + for result in inferred.infer_call_result(node, context=context): + return result + except InferenceError: + pass + + +@util.singledispatch +def _object_dir(obj): + """Generic dispatch method for determining the `dir` results of objects.""" + obj_type = interpreterutil.object_type(obj) + if obj_type is util.Uninferable: + raise UseInferenceDefault + + attributes = obj_type.special_attributes.attributes() + list(obj_type.locals) + return _attrs_to_node(attributes, obj_type) + + +def _attrs_to_node(attrs, parent): + """Convert the given list of attributes into a List node.""" + new_node = nodes.List(parent=parent) + attrs = sorted([nodes.Const(attr, parent=new_node) for attr in set(attrs)], + key=lambda const: const.value) + new_node.postinit(attrs) + return new_node + + +def _instance_attrs(instance): + """Get the attributes of the given instance.""" + attrs = instance.special_attributes.attributes() + list(instance.locals.keys()) + attrs += [item.value for item in instance.special_attributes.py__dict__.keys] + return attrs + + +def _complete_instance_attrs(instance): + """Get the attributes of the given instance recursively.""" + for item in _instance_attrs(instance): + yield item + for parent in instance.ancestors(): + if parent.name == 'object': + # Just ignore the builtin object, since it doesn't provide any real value. + continue + for item in _instance_attrs(parent.instantiate_class()): + yield item + + +@_object_dir.register(objects.Instance) +def _object_dir_instance(instance): + result = _custom_dir_result(instance) + if result and result is not util.Uninferable: + return result + + attrs = sorted(set(_complete_instance_attrs(instance))) + return _attrs_to_node(attrs, instance) + + +@_object_dir.register(nodes.ClassDef) +def _object_dir_class(cls): + metaclass = cls.metaclass() + if metaclass is not None: + result = _custom_dir_result(metaclass) + if result is not util.Uninferable: + return result + + attrs = [] + for ancestor in itertools.chain(cls.ancestors(), (cls, )): + if isinstance(ancestor, nodes.ClassDef) and ancestor.name == 'object': + continue + + attrs += list(ancestor.locals.keys()) + ancestor.special_attributes.attributes() + + return _attrs_to_node(sorted(set(attrs)), cls) + + +@_object_dir.register(nodes.Module) +def _object_dir_module(module): + attrs = sorted(module.locals.keys()) + return _attrs_to_node(attrs, module) + + +def infer_dir(node, context=None): + """Understand `dir` calls.""" + if len(node.args) > 1: + raise UseInferenceDefault + + if not node.args: + root = node.root() + return _object_dir(root) + + inferred = interpreterutil.safe_infer(node.args[0]) + if not inferred or inferred is util.Uninferable: + raise UseInferenceDefault + + attrs = _object_dir(inferred) + if attrs is None: + raise UseInferenceDefault + return attrs + + def infer_type_dunder_new(caller, context=None): """Try to infer what __new__ returns when called @@ -618,6 +733,7 @@ def _patch_strings(): register_builtin_transform(infer_frozenset, 'frozenset') register_builtin_transform(infer_type, 'type') register_builtin_transform(infer_slice, 'slice') +register_builtin_transform(infer_dir, 'dir') # infer type.__new__ calls MANAGER.register_transform(nodes.Call, inference_tip(infer_type_dunder_new), diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index e71dfbd7db..7059529829 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -3888,5 +3888,125 @@ def test_slice_type(self): self.assertEqual(inferred.name, 'slice') +class DirTest(unittest.TestCase): + + def test_instance_custom_ok_dir(self): + ast_nodes = test_utils.extract_node(''' + class A: + def __dir__(self): + return ['a', 'b', 'c'] + dir(A()) #@ + + class B: + x = ['a', 'b'] + def __dir__(self): + return self.x + dir(B()) #@ + class C: + y = 24 + def __init__(self): + self.x = 24 + + dir(C()) #@ + + class D(C): + z = 24 + def __init__(self): + self.t = 24 + dir(D()) #@ + ''') + expected = [ + ['a', 'b', 'c'], + ['a', 'b'], + ['__class__', '__module__', '__init__', '__dict__', '__doc__', 'y', 'x'], + ['__class__', '__module__', '__init__', '__dict__', '__doc__', 't', 'x', 'y', 'z'], + ] + for node, expected_values in zip(ast_nodes, expected): + inferred = next(node.infer()) + self.assertIsInstance(inferred, nodes.List) + self.assertEqual(sorted([elt.value for elt in inferred.elts]), + sorted(expected_values)) + + def test_modules_dir(self): + ast_nodes = test_utils.extract_node(''' + import collections + class A: + def __dir__(self): + return dir(collections) + dir(collections) #@ + dir(A()) #@ + ''') + for ast_node in ast_nodes: + inferred = next(ast_node.infer()) + self.assertIsInstance(inferred, nodes.List) + elts = {elt.value for elt in inferred.elts} + self.assertTrue({'OrderedDict', 'deque', 'defaultdict', 'namedtuple'}.issubset(elts)) + + def test_list_dir(self): + ast_node = test_utils.extract_node('dir([])') + inferred = next(ast_node.infer()) + elts = {elt.value for elt in inferred.elts} + self.assertTrue({'append', 'count', 'extend', 'index', 'insert', + 'mro', 'pop', 'remove', 'reverse', 'sort'}.issubset(elts)) + + def test_metaclass_dir(self): + ast_nodes = test_utils.extract_node(''' + import six + class Meta(type): + def __dir__(self): + return ['a', 'b'] + class Meta1(type): + @property + def teta(cls): + return ['a', 'b'] + def __dir__(cls): + return cls.teta + @six.add_metaclass(Meta) + class A(object): + pass + @six.add_metaclass(Meta1) + class B(object): + pass + dir(A) #@ + dir(B) #@ + ''') + for node in ast_nodes: + inferred = next(node.infer()) + self.assertIsInstance(inferred, nodes.List) + elts = [elt.value for elt in inferred.elts] + self.assertEqual(elts, ['a', 'b']) + + def test_class_dir(self): + ast_nodes = test_utils.extract_node(''' + class A: + x = 42 + class B(A): + y = 24 + dir(A) #@ + dir(B) #@ + ''') + first = next(ast_nodes[0].infer()) + self.assertIsInstance(first, nodes.List) + elts = {elt.value for elt in first.elts} + self.assertTrue({'__bases__', 'x', '__dict__', '__doc__'}.issubset(elts)) + + second = next(ast_nodes[1].infer()) + elts = {elt.value for elt in second.elts} + self.assertTrue({'__bases__', 'x', 'y', '__dict__', '__doc__'}.issubset(elts)) + + def test_no_args_dir(self): + ast_node = test_utils.extract_node(''' + class A: + pass + class B: + pass + dir() #@ + ''') + inferred = next(ast_node.infer()) + self.assertIsInstance(inferred, nodes.List) + elts = [elt.value for elt in inferred.elts] + self.assertEqual(elts, ['A', 'B']) + + if __name__ == '__main__': unittest.main() From ae49ff7b502c22ecc4cc6b0d69e98400c9c83f43 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Wed, 13 Apr 2016 14:52:58 +0300 Subject: [PATCH 229/312] Functional form of enums support accessing values through __call__. --- ChangeLog | 2 ++ astroid/brain/brain_stdlib.py | 12 ++++++++++-- astroid/tests/unittest_brain.py | 15 +++++++++------ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index 51bd2efa82..7e13f3111d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,8 @@ Change log for the astroid package (used to be astng) ===================================================== -- + * Functional form of enums support accessing values through __call__ + * Understand the `dir` builtin. This can be used for understanding highly dynamic code, which involves diff --git a/astroid/brain/brain_stdlib.py b/astroid/brain/brain_stdlib.py index 0791d93fbc..4cb6dd0c09 100644 --- a/astroid/brain/brain_stdlib.py +++ b/astroid/brain/brain_stdlib.py @@ -316,18 +316,26 @@ def infer_enum(enum_call, context=None): """Mock module to hold enum classes""" class EnumMeta(object): """Mock Enum metaclass""" + class EnumAttribute(object): + def __init__(self, name=''): + self.name = name + self.value = 0 class {name}(EnumMeta): """Mock Enum class""" + def __call__(self, node): + return EnumAttribute() + def __init__(self, {attributes}): """Fake __init__ for enums""" ''') code = template.format(name=type_name, attributes=', '.join(attributes)) - assignment_lines = [(' '*8 + 'self.{a} = {a}'.format(a=a)) + assignment_lines = [(' '*8 + 'self.{a} = EnumAttribute("{a}")'.format(a=a)) for a in attributes] code += '\n'.join(assignment_lines) module = AstroidBuilder(MANAGER).string_build(code) - return iter([module.body[1]]) + built_class = module.locals[type_name][0] + return iter([built_class.instantiate_class()]) def infer_enum_class(enum_node): diff --git a/astroid/tests/unittest_brain.py b/astroid/tests/unittest_brain.py index a12b242e8d..dd3e413e7d 100644 --- a/astroid/tests/unittest_brain.py +++ b/astroid/tests/unittest_brain.py @@ -469,17 +469,20 @@ class MyEnum(enum.IntEnum): self.assertTrue(cls.is_subtype_of(int_type), 'IntEnum based enums should be a subtype of int') - def test_enum_func_form_is_class_not_instance(self): - cls, instance = test_utils.extract_node(''' + def test_enum_func_form(self): + instance_1, instance_2 = test_utils.extract_node(''' from enum import Enum f = Enum('Audience', ['a', 'b', 'c']) f #@ - f() #@ + f(1) #@ ''') - inferred_cls = next(cls.infer()) - self.assertIsInstance(inferred_cls, nodes.ClassDef) - inferred_instance = next(instance.infer()) + inferred = next(instance_1.infer()) + self.assertIsInstance(inferred, objects.Instance) + + inferred_instance = next(instance_2.infer()) self.assertIsInstance(inferred_instance, objects.Instance) + self.assertIsInstance(next(inferred_instance.igetattr('name')), nodes.Const) + self.assertIsInstance(next(inferred_instance.igetattr('value')), nodes.Const) @unittest.skipUnless(HAS_DATEUTIL, "This test requires the dateutil library.") From 41af80f7e8d480c685615203ce3ed7f427362193 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 25 Apr 2016 17:23:11 +0300 Subject: [PATCH 230/312] Don't take in consideration invalid assignments, especially when __slots__ declaration forbids them. Close issue #332 --- ChangeLog | 5 ++ astroid/builder.py | 4 ++ astroid/interpreter/assign.py | 71 +++++++++++++++++++++++++++++ astroid/interpreter/objectmodel.py | 2 +- astroid/tests/unittest_inference.py | 61 +++++++++++++++++++++++++ 5 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 astroid/interpreter/assign.py diff --git a/ChangeLog b/ChangeLog index 7e13f3111d..38052cd4c9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,11 @@ Change log for the astroid package (used to be astng) ===================================================== -- + * Don't take in consideration invalid assignments, especially when __slots__ + declaration forbids them. + + Close issue #332 + * Functional form of enums support accessing values through __call__ * Understand the `dir` builtin. diff --git a/astroid/builder.py b/astroid/builder.py index b549402422..ca2b92ba43 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -27,6 +27,7 @@ import textwrap from astroid import exceptions +from astroid.interpreter import assign from astroid.interpreter import runtimeabc from astroid import manager from astroid import modutils @@ -80,6 +81,7 @@ def open_source_file(filename): class AstroidBuilder(object): + """Class for building an astroid tree from source code or from a live module. The param *manager* specifies the manager class which should be used. @@ -220,6 +222,8 @@ def delayed_assignments(root): continue if node in values: continue + elif not assign.can_assign(inferred, node.attrname): + continue else: # I have no idea why there's a special case # for __init__ that changes the order of the diff --git a/astroid/interpreter/assign.py b/astroid/interpreter/assign.py new file mode 100644 index 0000000000..4b9fb1e82a --- /dev/null +++ b/astroid/interpreter/assign.py @@ -0,0 +1,71 @@ +# copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved. +# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr +# +# This file is part of astroid. +# +# astroid is free software: you can redistribute it and/or modify it +# under the terms of the GNU Lesser General Public License as published by the +# Free Software Foundation, either version 2.1 of the License, or (at your +# option) any later version. +# +# astroid is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License +# for more details. +# +# You should have received a copy of the GNU Lesser General Public License along +# with astroid. If not, see . + +from astroid import exceptions +from astroid.interpreter import runtimeabc +from astroid.interpreter import objects +from astroid.tree import treeabc +from astroid import util + + +@util.singledispatch +def can_assign(node, attrname): + """Check if the given attribute can be assigned onto the given node. + + By default, everything is assignable. + """ + return True + + +def _settable_property(func_node, attrname): + expected_setter = "%s.setter" % (attrname, ) + if not func_node.decorators: + return False + + for decorator in func_node.decorators.nodes: + if decorator.as_string() == expected_setter: + return True + + +@can_assign.register(runtimeabc.Instance) +def _instance_can_assign(node, attrname): + try: + slots = node.slots() + except TypeError: + pass + else: + if slots and attrname not in set(slot.value for slot in slots): + return False + + # Check non settable property. + try: + names = node.getattr(attrname) + except exceptions.AttributeInferenceError: + # In what circumstances can this fail? + return True + + if not all(isinstance(name, treeabc.FunctionDef) for name in names): + return True + + if len(names) == 1 and objects.is_property(names[0]): + # This should even emit a type checking error. + return False + elif all(objects.is_property(name) or _settable_property(name, attrname) + for name in names): + return True + return False diff --git a/astroid/interpreter/objectmodel.py b/astroid/interpreter/objectmodel.py index 61ae70081e..186be464f3 100644 --- a/astroid/interpreter/objectmodel.py +++ b/astroid/interpreter/objectmodel.py @@ -66,7 +66,7 @@ def _dunder_dict(instance, attributes): # The original attribute has a list of elements for each key, # but that is not useful for retrieving the special attribute's value. # In this case, we're picking the last value from each list. - values = [elem[-1] for elem in attributes.values()] + values = [elem[-1] for elem in attributes.values() if elem] obj.postinit(keys=keys, values=values) return obj diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index 7059529829..27fdd29727 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -27,6 +27,7 @@ from astroid import InferenceError, builder, nodes from astroid.builder import parse +from astroid import exceptions from astroid.inference import infer_end as inference_infer_end from astroid.interpreter.objects import ( Instance, BoundMethod, UnboundMethod, FrozenSet @@ -3215,6 +3216,66 @@ def test(cls): return cls self.assertIsInstance(inferred, ClassDef) self.assertEqual(inferred.name, 'A') + def test_delayed_attributes_without_slots(self): + ast_node = test_utils.extract_node(''' + class A(object): + __slots__ = ('a', ) + a = A() + a.teta = 24 + a.a = 24 + a #@ + ''') + inferred = next(ast_node.infer()) + with self.assertRaises(exceptions.AttributeInferenceError): + inferred.getattr('teta') + inferred.getattr('a') + + @test_utils.require_version(maxver='3.0') + def test_delayed_attributes_with_old_style_classes(self): + ast_node = test_utils.extract_node(''' + class A: + __slots__ = ('a', ) + a = A() + a.teta = 42 + a #@ + ''') + next(ast_node.infer()).getattr('teta') + + def test_delayed_attributes_writable_property(self): + ast_node = test_utils.extract_node(''' + class A(object): + @property + def test(self): + return 24 + @test.setter + def test(self, value): + pass + a = A() + a.test = "a" + a #@ + ''') + inferred = next(ast_node.infer()) + value = next(inferred.igetattr('test')) + self.assertIsInstance(value, nodes.Const) + self.assertEqual(value.value, "a") + + def test_delayed_attributes_non_writable_property(self): + ast_node = test_utils.extract_node(''' + class A(object): + @property + def test(self): + return 24 + a = A() + a.test = "a" + a #@ + ''') + inferred = next(ast_node.infer()) + values = list(inferred.igetattr('test')) + self.assertEqual(len(values), 1) + value = values[0] + self.assertIsInstance(value, nodes.Const) + self.assertEqual(value.value, 24) + class GetattrTest(unittest.TestCase): From 2b18bad058b3eef1e2bff1ceb544270d88572e51 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 25 Apr 2016 19:23:40 +0300 Subject: [PATCH 231/312] Apply automatically the transforms for the builtin module, since they might not be applied. --- astroid/__init__.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/astroid/__init__.py b/astroid/__init__.py index 4eb0b4cbf5..6afecf2553 100644 --- a/astroid/__init__.py +++ b/astroid/__init__.py @@ -76,7 +76,7 @@ from astroid import raw_building # Cache the builtins AST -raw_building.ast_from_builtins() +_builtins = raw_building.ast_from_builtins() from astroid.interpreter.util import are_exclusive, unpack_infer from astroid.interpreter.lookup import builtin_lookup from astroid.builder import parse @@ -146,3 +146,7 @@ def transform(module): for module in listdir(BRAIN_MODULES_DIR): if module.endswith('.py'): importlib.import_module(module[:-3]) + +# Apply transforms to be builtin module at this point. +MANAGER.visit_transforms(_builtins) +del _builtins From 279d45c4afea69a3be6d06dd738abc9eb04444cb Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 26 Apr 2016 13:31:03 +0300 Subject: [PATCH 232/312] Move pkg_resources tip into their own file --- astroid/brain/brain_pkg_resources.py | 79 ++++++++++++++++++++++++++++ astroid/brain/brain_stdlib.py | 55 ------------------- 2 files changed, 79 insertions(+), 55 deletions(-) create mode 100644 astroid/brain/brain_pkg_resources.py diff --git a/astroid/brain/brain_pkg_resources.py b/astroid/brain/brain_pkg_resources.py new file mode 100644 index 0000000000..a84024580c --- /dev/null +++ b/astroid/brain/brain_pkg_resources.py @@ -0,0 +1,79 @@ +# copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved. +# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr +# +# This file is part of astroid. +# +# astroid is free software: you can redistribute it and/or modify it under +# the terms of the GNU Lesser General Public License as published by the Free +# Software Foundation, either version 2.1 of the License, or (at your option) any +# later version. +# +# astroid is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more +# details. +# +# You should have received a copy of the GNU Lesser General Public License along +# with astroid. If not, see . + +import astroid +from astroid import parse +from astroid import inference_tip +from astroid import register_module_extender +from astroid import MANAGER + + +def pkg_resources_transform(): + return parse(''' +def require(*requirements): + return pkg_resources.working_set.require(*requirements) + +def run_script(requires, script_name): + return pkg_resources.working_set.run_script(requires, script_name) + +def iter_entry_points(group, name=None): + return pkg_resources.working_set.iter_entry_points(group, name) + +def resource_exists(package_or_requirement, resource_name): + return get_provider(package_or_requirement).has_resource(resource_name) + +def resource_isdir(package_or_requirement, resource_name): + return get_provider(package_or_requirement).resource_isdir( + resource_name) + +def resource_filename(package_or_requirement, resource_name): + return get_provider(package_or_requirement).get_resource_filename( + self, resource_name) + +def resource_stream(package_or_requirement, resource_name): + return get_provider(package_or_requirement).get_resource_stream( + self, resource_name) + +def resource_string(package_or_requirement, resource_name): + return get_provider(package_or_requirement).get_resource_string( + self, resource_name) + +def resource_listdir(package_or_requirement, resource_name): + return get_provider(package_or_requirement).resource_listdir( + resource_name) + +def extraction_error(): + pass + +def get_cache_path(archive_name, names=()): + extract_path = self.extraction_path or get_default_cache() + target_path = os.path.join(extract_path, archive_name+'-tmp', *names) + return target_path + +def postprocess(tempname, filename): + pass + +def set_extraction_path(path): + pass + +def cleanup_resources(force=False): + pass + +''') + +register_module_extender(MANAGER, 'pkg_resources', pkg_resources_transform) diff --git a/astroid/brain/brain_stdlib.py b/astroid/brain/brain_stdlib.py index 4cb6dd0c09..f3a6a71622 100644 --- a/astroid/brain/brain_stdlib.py +++ b/astroid/brain/brain_stdlib.py @@ -94,60 +94,6 @@ def __delitem__(self, index): pass ''') -def pkg_resources_transform(): - return AstroidBuilder(MANAGER).string_build(''' -def require(*requirements): - return pkg_resources.working_set.require(*requirements) - -def run_script(requires, script_name): - return pkg_resources.working_set.run_script(requires, script_name) - -def iter_entry_points(group, name=None): - return pkg_resources.working_set.iter_entry_points(group, name) - -def resource_exists(package_or_requirement, resource_name): - return get_provider(package_or_requirement).has_resource(resource_name) - -def resource_isdir(package_or_requirement, resource_name): - return get_provider(package_or_requirement).resource_isdir( - resource_name) - -def resource_filename(package_or_requirement, resource_name): - return get_provider(package_or_requirement).get_resource_filename( - self, resource_name) - -def resource_stream(package_or_requirement, resource_name): - return get_provider(package_or_requirement).get_resource_stream( - self, resource_name) - -def resource_string(package_or_requirement, resource_name): - return get_provider(package_or_requirement).get_resource_string( - self, resource_name) - -def resource_listdir(package_or_requirement, resource_name): - return get_provider(package_or_requirement).resource_listdir( - resource_name) - -def extraction_error(): - pass - -def get_cache_path(archive_name, names=()): - extract_path = self.extraction_path or get_default_cache() - target_path = os.path.join(extract_path, archive_name+'-tmp', *names) - return target_path - -def postprocess(tempname, filename): - pass - -def set_extraction_path(path): - pass - -def cleanup_resources(force=False): - pass - -''') - - def subprocess_transform(): if PY3K: communicate = (bytes('string', 'ascii'), bytes('string', 'ascii')) @@ -488,7 +434,6 @@ def Lock(): MANAGER.register_transform(nodes.ClassDef, infer_enum_class) register_module_extender(MANAGER, 'hashlib', hashlib_transform) register_module_extender(MANAGER, 'collections', collections_transform) -register_module_extender(MANAGER, 'pkg_resources', pkg_resources_transform) register_module_extender(MANAGER, 'subprocess', subprocess_transform) register_module_extender(MANAGER, 'multiprocessing.managers', multiprocessing_managers_transform) From 11e53a2608a5b34d3922a54963c0eab0e87ecb53 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 26 Apr 2016 13:31:20 +0300 Subject: [PATCH 233/312] Add brain tip for get_distribution --- astroid/brain/brain_pkg_resources.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/astroid/brain/brain_pkg_resources.py b/astroid/brain/brain_pkg_resources.py index a84024580c..7777fb71e3 100644 --- a/astroid/brain/brain_pkg_resources.py +++ b/astroid/brain/brain_pkg_resources.py @@ -74,6 +74,9 @@ def set_extraction_path(path): def cleanup_resources(force=False): pass +def get_distribution(dist): + return Distribution(dist) + ''') register_module_extender(MANAGER, 'pkg_resources', pkg_resources_transform) From f6fdedef1f9cafbfb48fb3c6b31ef51156b3802a Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 26 Apr 2016 16:13:11 +0300 Subject: [PATCH 234/312] Calling lambda methods (defined at class level) can be understood. --- ChangeLog | 2 ++ astroid/tests/unittest_inference.py | 12 ++++++++++++ astroid/tree/scoped_nodes.py | 7 ++++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 38052cd4c9..746542d9e6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,8 @@ Change log for the astroid package (used to be astng) ===================================================== -- + * Calling lambda methods (defined at class level) can be understood. + * Don't take in consideration invalid assignments, especially when __slots__ declaration forbids them. diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index 27fdd29727..af82cc90cc 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -3276,6 +3276,18 @@ def test(self): self.assertIsInstance(value, nodes.Const) self.assertEqual(value.value, 24) + def test_lambda_as_methods(self): + ast_node = test_utils.extract_node(''' + class X: + m = lambda self, arg: self.z + arg + z = 24 + + X().m(4) #@ + ''') + inferred = next(ast_node.infer()) + self.assertIsInstance(inferred, nodes.Const) + self.assertEqual(inferred.value, 28) + class GetattrTest(unittest.TestCase): diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 9fe5e9c0a7..674de3d8ba 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -784,7 +784,12 @@ class Lambda(LambdaFunctionMixin, lookup.LocalsDictNode): name = '' # function's type, 'function' | 'method' | 'staticmethod' | 'classmethod' - type = 'function' + @property + def type(self): + if self.args.args and self.args.args[0].name == 'self': + if isinstance(self.parent.scope(), ClassDef): + return 'method' + return 'function' def __init__(self, lineno=None, col_offset=None, parent=None): self.args = [] From 97cc0fdf7a5806724339fde08b731b68849d8b05 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 26 Apr 2016 16:36:20 +0300 Subject: [PATCH 235/312] Improve inference with regard redefinition of values in subclasses The inference can handle the case where the attribute is accessed through a subclass of a base class and the attribute is defined at the base class's level, by taking in consideration a redefinition in the subclass. This should fix https://github.com/PyCQA/pylint/issues/432 --- ChangeLog | 8 +++++- astroid/inference.py | 11 ++++++++ astroid/tests/unittest_inference.py | 42 +++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 746542d9e6..d300e03286 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,7 +1,13 @@ Change log for the astroid package (used to be astng) ===================================================== --- +-- + * The inference can handle the case where the attribute is accessed through a subclass + of a base class and the attribute is defined at the base class's level, + by taking in consideration a redefinition in the subclass. + + This should fix https://github.com/PyCQA/pylint/issues/432 + * Calling lambda methods (defined at class level) can be understood. * Don't take in consideration invalid assignments, especially when __slots__ diff --git a/astroid/inference.py b/astroid/inference.py index 3d749afd66..48de4341c2 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -175,6 +175,17 @@ def infer_attribute(self, context=None): if owner is util.Uninferable: yield owner continue + + if context and context.boundnode: + # This handles the situation where the attribute is accessed through a subclass + # of a base class and the attribute is defined at the base class's level, + # by taking in consideration a redefinition in the subclass. + if (isinstance(owner, runtimeabc.Instance) + and isinstance(context.boundnode, runtimeabc.Instance)): + if inferenceutil.is_subtype(inferenceutil.object_type(context.boundnode), + inferenceutil.object_type(owner)): + owner = context.boundnode + try: context.boundnode = owner for obj in owner.igetattr(self.attrname, context): diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index af82cc90cc..7c8a58ea64 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -3288,6 +3288,48 @@ class X: self.assertIsInstance(inferred, nodes.Const) self.assertEqual(inferred.value, 28) + def test_inner_value_redefined_by_subclass(self): + ast_node = test_utils.extract_node(''' + class X(object): + M = lambda self, arg: "a" + x = 24 + def __init__(self): + x = 24 + self.m = self.M(x) + + class Y(X): + M = lambda self, arg: arg + 1 + def blurb(self): + self.m #@ + ''') + inferred = next(ast_node.infer()) + self.assertIsInstance(inferred, nodes.Const) + self.assertEqual(inferred.value, 25) + + @unittest.expectedFailure + def test_inner_value_redefined_by_subclass_with_mro(self): + # This might work, but it currently doesn't due to not being able + # to reuse inference contexts. + ast_node = test_utils.extract_node(''' + class X(object): + M = lambda self, arg: arg + 1 + x = 24 + def __init__(self): + y = self + self.m = y.M(1) + y.z + + class C(object): + z = 24 + + class Y(X, C): + M = lambda self, arg: arg + 1 + def blurb(self): + self.m #@ + ''') + inferred = next(ast_node.infer()) + self.assertIsInstance(inferred, nodes.Const) + self.assertEqual(inferred.value, 25) + class GetattrTest(unittest.TestCase): From 62bd3729ad95c7cd7b78b1e9c7bd1ac0f7ae9906 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sat, 30 Apr 2016 15:09:07 +0300 Subject: [PATCH 236/312] Don't crash when a decorated method has the same name as the decorator, which is a builtin object (a property) --- ChangeLog | 3 +++ astroid/tests/unittest_regrtest.py | 10 ++++++++++ astroid/tree/scoped_nodes.py | 10 +++++++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index d300e03286..5be60aa52c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,9 @@ Change log for the astroid package (used to be astng) ===================================================== -- + * Fix a crash which occurred when a method had a same name as a builtin object, + decorated at the same time by that builtin object ( a property for instance) + * The inference can handle the case where the attribute is accessed through a subclass of a base class and the attribute is defined at the base class's level, by taking in consideration a redefinition in the subclass. diff --git a/astroid/tests/unittest_regrtest.py b/astroid/tests/unittest_regrtest.py index deba8b4183..d99a74d061 100644 --- a/astroid/tests/unittest_regrtest.py +++ b/astroid/tests/unittest_regrtest.py @@ -354,6 +354,16 @@ def test_ssl_protocol(self): inferred = next(node.infer()) self.assertIsInstance(inferred, nodes.Const) + def test_recursive_property_method(self): + node = extract_node(''' + class APropert(): + @property + def property(self): + return self + APropert().property + ''') + next(node.infer()) + class Whatever(object): a = property(lambda x: x, lambda x: x) diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 674de3d8ba..46420dfbaa 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -1361,8 +1361,16 @@ def infer_call_result(self, caller, context=None): yield objects.Instance(self) def scope_lookup(self, node, name, offset=0): + # If the name looks like a builtin name, just try to look + # into the upper scope of this class. We might have a + # decorator that it's poorly named after a builtin object + # inside this class. + lookup_upper_frame = ( + isinstance(node.parent, node_classes.Decorators) and + name in MANAGER.builtins() + ) if any(node == base or base.parent_of(node) - for base in self.bases): + for base in self.bases) or lookup_upper_frame: # Handle the case where we have either a name # in the bases of a class, which exists before # the actual definition or the case where we have From 197fca8656e0a970066aea54072bc5dfc553adf4 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sat, 7 May 2016 16:13:33 +0300 Subject: [PATCH 237/312] Add test for old namespace packages protocol. --- .../path_pkg_resources_1/package/__init__.py | 1 + .../data/path_pkg_resources_1/package/foo.py | 0 .../path_pkg_resources_2/package/__init__.py | 1 + .../data/path_pkg_resources_2/package/bar.py | 0 .../path_pkg_resources_3/package/__init__.py | 1 + .../data/path_pkg_resources_3/package/baz.py | 0 .../data/path_pkgutil_1/package/__init__.py | 2 ++ .../data/path_pkgutil_1/package/foo.py | 0 .../data/path_pkgutil_2/package/__init__.py | 2 ++ .../data/path_pkgutil_2/package/bar.py | 0 .../data/path_pkgutil_3/package/__init__.py | 2 ++ .../data/path_pkgutil_3/package/baz.py | 0 .../path_pkg_resources_1/package/__init__.py | 1 + .../data/path_pkg_resources_1/package/foo.py | 0 .../path_pkg_resources_2/package/__init__.py | 1 + .../data/path_pkg_resources_2/package/bar.py | 0 .../path_pkg_resources_3/package/__init__.py | 1 + .../data/path_pkg_resources_3/package/baz.py | 0 .../data/path_pkgutil_1/package/__init__.py | 2 ++ .../data/path_pkgutil_1/package/foo.py | 0 .../data/path_pkgutil_2/package/__init__.py | 2 ++ .../data/path_pkgutil_2/package/bar.py | 0 .../data/path_pkgutil_3/package/__init__.py | 2 ++ .../data/path_pkgutil_3/package/baz.py | 0 astroid/tests/unittest_manager.py | 19 +++++++++++++++++++ 25 files changed, 37 insertions(+) create mode 100644 astroid/tests/testdata/python2/data/path_pkg_resources_1/package/__init__.py create mode 100644 astroid/tests/testdata/python2/data/path_pkg_resources_1/package/foo.py create mode 100644 astroid/tests/testdata/python2/data/path_pkg_resources_2/package/__init__.py create mode 100644 astroid/tests/testdata/python2/data/path_pkg_resources_2/package/bar.py create mode 100644 astroid/tests/testdata/python2/data/path_pkg_resources_3/package/__init__.py create mode 100644 astroid/tests/testdata/python2/data/path_pkg_resources_3/package/baz.py create mode 100644 astroid/tests/testdata/python2/data/path_pkgutil_1/package/__init__.py create mode 100644 astroid/tests/testdata/python2/data/path_pkgutil_1/package/foo.py create mode 100644 astroid/tests/testdata/python2/data/path_pkgutil_2/package/__init__.py create mode 100644 astroid/tests/testdata/python2/data/path_pkgutil_2/package/bar.py create mode 100644 astroid/tests/testdata/python2/data/path_pkgutil_3/package/__init__.py create mode 100644 astroid/tests/testdata/python2/data/path_pkgutil_3/package/baz.py create mode 100644 astroid/tests/testdata/python3/data/path_pkg_resources_1/package/__init__.py create mode 100644 astroid/tests/testdata/python3/data/path_pkg_resources_1/package/foo.py create mode 100644 astroid/tests/testdata/python3/data/path_pkg_resources_2/package/__init__.py create mode 100644 astroid/tests/testdata/python3/data/path_pkg_resources_2/package/bar.py create mode 100644 astroid/tests/testdata/python3/data/path_pkg_resources_3/package/__init__.py create mode 100644 astroid/tests/testdata/python3/data/path_pkg_resources_3/package/baz.py create mode 100644 astroid/tests/testdata/python3/data/path_pkgutil_1/package/__init__.py create mode 100644 astroid/tests/testdata/python3/data/path_pkgutil_1/package/foo.py create mode 100644 astroid/tests/testdata/python3/data/path_pkgutil_2/package/__init__.py create mode 100644 astroid/tests/testdata/python3/data/path_pkgutil_2/package/bar.py create mode 100644 astroid/tests/testdata/python3/data/path_pkgutil_3/package/__init__.py create mode 100644 astroid/tests/testdata/python3/data/path_pkgutil_3/package/baz.py diff --git a/astroid/tests/testdata/python2/data/path_pkg_resources_1/package/__init__.py b/astroid/tests/testdata/python2/data/path_pkg_resources_1/package/__init__.py new file mode 100644 index 0000000000..b0d6433717 --- /dev/null +++ b/astroid/tests/testdata/python2/data/path_pkg_resources_1/package/__init__.py @@ -0,0 +1 @@ +__import__('pkg_resources').declare_namespace(__name__) \ No newline at end of file diff --git a/astroid/tests/testdata/python2/data/path_pkg_resources_1/package/foo.py b/astroid/tests/testdata/python2/data/path_pkg_resources_1/package/foo.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/astroid/tests/testdata/python2/data/path_pkg_resources_2/package/__init__.py b/astroid/tests/testdata/python2/data/path_pkg_resources_2/package/__init__.py new file mode 100644 index 0000000000..b0d6433717 --- /dev/null +++ b/astroid/tests/testdata/python2/data/path_pkg_resources_2/package/__init__.py @@ -0,0 +1 @@ +__import__('pkg_resources').declare_namespace(__name__) \ No newline at end of file diff --git a/astroid/tests/testdata/python2/data/path_pkg_resources_2/package/bar.py b/astroid/tests/testdata/python2/data/path_pkg_resources_2/package/bar.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/astroid/tests/testdata/python2/data/path_pkg_resources_3/package/__init__.py b/astroid/tests/testdata/python2/data/path_pkg_resources_3/package/__init__.py new file mode 100644 index 0000000000..b0d6433717 --- /dev/null +++ b/astroid/tests/testdata/python2/data/path_pkg_resources_3/package/__init__.py @@ -0,0 +1 @@ +__import__('pkg_resources').declare_namespace(__name__) \ No newline at end of file diff --git a/astroid/tests/testdata/python2/data/path_pkg_resources_3/package/baz.py b/astroid/tests/testdata/python2/data/path_pkg_resources_3/package/baz.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/astroid/tests/testdata/python2/data/path_pkgutil_1/package/__init__.py b/astroid/tests/testdata/python2/data/path_pkgutil_1/package/__init__.py new file mode 100644 index 0000000000..0bfb5a62b4 --- /dev/null +++ b/astroid/tests/testdata/python2/data/path_pkgutil_1/package/__init__.py @@ -0,0 +1,2 @@ +from pkgutil import extend_path +__path__ = extend_path(__path__, __name__) \ No newline at end of file diff --git a/astroid/tests/testdata/python2/data/path_pkgutil_1/package/foo.py b/astroid/tests/testdata/python2/data/path_pkgutil_1/package/foo.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/astroid/tests/testdata/python2/data/path_pkgutil_2/package/__init__.py b/astroid/tests/testdata/python2/data/path_pkgutil_2/package/__init__.py new file mode 100644 index 0000000000..0bfb5a62b4 --- /dev/null +++ b/astroid/tests/testdata/python2/data/path_pkgutil_2/package/__init__.py @@ -0,0 +1,2 @@ +from pkgutil import extend_path +__path__ = extend_path(__path__, __name__) \ No newline at end of file diff --git a/astroid/tests/testdata/python2/data/path_pkgutil_2/package/bar.py b/astroid/tests/testdata/python2/data/path_pkgutil_2/package/bar.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/astroid/tests/testdata/python2/data/path_pkgutil_3/package/__init__.py b/astroid/tests/testdata/python2/data/path_pkgutil_3/package/__init__.py new file mode 100644 index 0000000000..0bfb5a62b4 --- /dev/null +++ b/astroid/tests/testdata/python2/data/path_pkgutil_3/package/__init__.py @@ -0,0 +1,2 @@ +from pkgutil import extend_path +__path__ = extend_path(__path__, __name__) \ No newline at end of file diff --git a/astroid/tests/testdata/python2/data/path_pkgutil_3/package/baz.py b/astroid/tests/testdata/python2/data/path_pkgutil_3/package/baz.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/astroid/tests/testdata/python3/data/path_pkg_resources_1/package/__init__.py b/astroid/tests/testdata/python3/data/path_pkg_resources_1/package/__init__.py new file mode 100644 index 0000000000..b0d6433717 --- /dev/null +++ b/astroid/tests/testdata/python3/data/path_pkg_resources_1/package/__init__.py @@ -0,0 +1 @@ +__import__('pkg_resources').declare_namespace(__name__) \ No newline at end of file diff --git a/astroid/tests/testdata/python3/data/path_pkg_resources_1/package/foo.py b/astroid/tests/testdata/python3/data/path_pkg_resources_1/package/foo.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/astroid/tests/testdata/python3/data/path_pkg_resources_2/package/__init__.py b/astroid/tests/testdata/python3/data/path_pkg_resources_2/package/__init__.py new file mode 100644 index 0000000000..b0d6433717 --- /dev/null +++ b/astroid/tests/testdata/python3/data/path_pkg_resources_2/package/__init__.py @@ -0,0 +1 @@ +__import__('pkg_resources').declare_namespace(__name__) \ No newline at end of file diff --git a/astroid/tests/testdata/python3/data/path_pkg_resources_2/package/bar.py b/astroid/tests/testdata/python3/data/path_pkg_resources_2/package/bar.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/astroid/tests/testdata/python3/data/path_pkg_resources_3/package/__init__.py b/astroid/tests/testdata/python3/data/path_pkg_resources_3/package/__init__.py new file mode 100644 index 0000000000..b0d6433717 --- /dev/null +++ b/astroid/tests/testdata/python3/data/path_pkg_resources_3/package/__init__.py @@ -0,0 +1 @@ +__import__('pkg_resources').declare_namespace(__name__) \ No newline at end of file diff --git a/astroid/tests/testdata/python3/data/path_pkg_resources_3/package/baz.py b/astroid/tests/testdata/python3/data/path_pkg_resources_3/package/baz.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/astroid/tests/testdata/python3/data/path_pkgutil_1/package/__init__.py b/astroid/tests/testdata/python3/data/path_pkgutil_1/package/__init__.py new file mode 100644 index 0000000000..0bfb5a62b4 --- /dev/null +++ b/astroid/tests/testdata/python3/data/path_pkgutil_1/package/__init__.py @@ -0,0 +1,2 @@ +from pkgutil import extend_path +__path__ = extend_path(__path__, __name__) \ No newline at end of file diff --git a/astroid/tests/testdata/python3/data/path_pkgutil_1/package/foo.py b/astroid/tests/testdata/python3/data/path_pkgutil_1/package/foo.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/astroid/tests/testdata/python3/data/path_pkgutil_2/package/__init__.py b/astroid/tests/testdata/python3/data/path_pkgutil_2/package/__init__.py new file mode 100644 index 0000000000..0bfb5a62b4 --- /dev/null +++ b/astroid/tests/testdata/python3/data/path_pkgutil_2/package/__init__.py @@ -0,0 +1,2 @@ +from pkgutil import extend_path +__path__ = extend_path(__path__, __name__) \ No newline at end of file diff --git a/astroid/tests/testdata/python3/data/path_pkgutil_2/package/bar.py b/astroid/tests/testdata/python3/data/path_pkgutil_2/package/bar.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/astroid/tests/testdata/python3/data/path_pkgutil_3/package/__init__.py b/astroid/tests/testdata/python3/data/path_pkgutil_3/package/__init__.py new file mode 100644 index 0000000000..0bfb5a62b4 --- /dev/null +++ b/astroid/tests/testdata/python3/data/path_pkgutil_3/package/__init__.py @@ -0,0 +1,2 @@ +from pkgutil import extend_path +__path__ = extend_path(__path__, __name__) \ No newline at end of file diff --git a/astroid/tests/testdata/python3/data/path_pkgutil_3/package/baz.py b/astroid/tests/testdata/python3/data/path_pkgutil_3/package/baz.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/astroid/tests/unittest_manager.py b/astroid/tests/unittest_manager.py index 12674516b2..aa1fd4007c 100644 --- a/astroid/tests/unittest_manager.py +++ b/astroid/tests/unittest_manager.py @@ -22,6 +22,7 @@ import six +import astroid from astroid import exceptions from astroid import manager from astroid.tests import resources @@ -94,6 +95,24 @@ def test_ast_from_module_name_astro_builder_exception(self): self.manager.ast_from_module_name, 'unhandledModule') + def _test_ast_from_old_namespace_package_protocol(self, root): + origpath = sys.path[:] + paths = [resources.find('data/path_{}_{}'.format(root, index)) + for index in range(1, 4)] + sys.path.extend(paths) + try: + for name in ('foo', 'bar', 'baz'): + module = self.manager.ast_from_module_name('package.' + name) + self.assertIsInstance(module, astroid.Module) + finally: + sys.path = origpath + + def test_ast_from_namespace_pkgutil(self): + self._test_ast_from_old_namespace_package_protocol('pkgutil') + + def test_ast_from_namespace_pkg_resources(self): + self._test_ast_from_old_namespace_package_protocol('pkg_resources') + def _test_ast_from_zip(self, archive): origpath = sys.path[:] sys.modules.pop('mypypa', None) From fdd911c08fd1b6f4a19ec270d8a87f72a8b2f7d1 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 8 May 2016 15:25:19 +0300 Subject: [PATCH 238/312] Switch to a spec finder protocol for finding files or packages --- astroid/manager.py | 20 +- astroid/modutils.py | 317 +++++++++++++++++++---------- astroid/tests/unittest_manager.py | 2 +- astroid/tests/unittest_modutils.py | 12 +- 4 files changed, 226 insertions(+), 125 deletions(-) diff --git a/astroid/manager.py b/astroid/manager.py index 533d102adb..f6c9a2614f 100644 --- a/astroid/manager.py +++ b/astroid/manager.py @@ -114,30 +114,30 @@ def ast_from_module_name(self, modname, context_file=None): if context_file: os.chdir(os.path.dirname(context_file)) try: - filepath, mp_type = self.file_from_module_name(modname, context_file) - if mp_type == modutils.PY_ZIPMODULE: - module = self.zip_import_data(filepath) + spec = self.file_from_module_name(modname, context_file) + if spec.type == modutils.ModuleType.PY_ZIPMODULE: + module = self.zip_import_data(spec.location) if module is not None: return module - elif mp_type in (imp.C_BUILTIN, imp.C_EXTENSION): - if mp_type == imp.C_EXTENSION and not self._can_load_extension(modname): + elif spec.type in (modutils.ModuleType.C_BUILTIN, modutils.ModuleType.C_EXTENSION): + if spec.type == modutils.ModuleType.C_EXTENSION and not self._can_load_extension(modname): return self._build_stub_module(modname) try: module = modutils.load_module_from_name(modname) except Exception as ex: # pylint: disable=broad-except util.reraise(exceptions.AstroidImportError( 'Loading {modname} failed with:\n{error}', - modname=modname, path=filepath, error=ex)) + modname=modname, path=spec.location, error=ex)) return self.ast_from_module(module, modname) - elif mp_type == imp.PY_COMPILED: + elif spec.type == modutils.ModuleType.PY_COMPILED: raise exceptions.AstroidImportError( "Unable to load compiled module {modname}.", - modname=modname, path=filepath) - if filepath is None: + modname=modname, path=spec.location) + if spec.location is None: raise exceptions.AstroidImportError( "Can't find a file for module {modname}.", modname=modname) - return self.ast_from_file(filepath, modname, fallback=False) + return self.ast_from_file(spec.location, modname, fallback=False) except exceptions.AstroidBuildingError as e: for hook in self._failed_import_hooks: try: diff --git a/astroid/modutils.py b/astroid/modutils.py index 3e1d556479..abc8981402 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -27,6 +27,9 @@ :var BUILTIN_MODULES: dictionary with builtin module names has key """ +import abc +import collections +import enum import imp import os import platform @@ -34,15 +37,34 @@ from distutils.sysconfig import get_python_lib from distutils.errors import DistutilsPlatformError import zipimport +try: + import importlib.machinery + _HAS_MACHINERY = True +except ImportError: + _HAS_MACHINERY = False try: import pkg_resources except ImportError: pkg_resources = None -from astroid import util - -PY_ZIPMODULE = object() +ModuleType = enum.Enum('ModuleType', 'C_BUILTIN C_EXTENSION PKG_DIRECTORY ' + 'PY_CODERESOURCE PY_COMPILED PY_FROZEN PY_RESOURCE ' + 'PY_SOURCE PY_ZIPMODULE PY_NAMESPACE') +_ImpTypes = {imp.C_BUILTIN: ModuleType.C_BUILTIN, + imp.C_EXTENSION: ModuleType.C_EXTENSION, + imp.PKG_DIRECTORY: ModuleType.PKG_DIRECTORY, + imp.PY_COMPILED: ModuleType.PY_COMPILED, + imp.PY_FROZEN: ModuleType.PY_FROZEN, + imp.PY_SOURCE: ModuleType.PY_SOURCE, + } +if hasattr(imp, 'PY_RESOURCE'): + _ImpTypes[imp.PY_RESOURCE] = ModuleType.PY_RESOURCE +if hasattr(imp, 'PY_CODERESOURCE'): + _ImpTypes[imp.PY_CODERESOURCE] = ModuleType.PY_CODERESOURCE + +def _imp_type_to_module_type(imp_type): + return _ImpTypes[imp_type] if sys.platform.startswith('win'): PY_SOURCE_EXTS = ('py', 'pyw') @@ -321,7 +343,7 @@ def modpath_from_file(filename, extrapath=None): def file_from_modpath(modpath, path=None, context_file=None): - return file_info_from_modpath(modpath, path, context_file)[0] + return file_info_from_modpath(modpath, path, context_file).location def file_info_from_modpath(modpath, path=None, context_file=None): """given a mod path (i.e. splitted module / package name), return the @@ -360,13 +382,13 @@ def file_info_from_modpath(modpath, path=None, context_file=None): if modpath[0] == 'xml': # handle _xmlplus try: - return _file_from_modpath(['_xmlplus'] + modpath[1:], path, context) + return _spec_from_modpath(['_xmlplus'] + modpath[1:], path, context) except ImportError: - return _file_from_modpath(modpath, path, context) + return _spec_from_modpath(modpath, path, context) elif modpath == ['os', 'path']: # FIXME: currently ignoring search_path... - return os.path.__file__, imp.PY_SOURCE - return _file_from_modpath(modpath, path, context) + return ModuleSpec(name='os.path', location=os.path.__file__, type=imp.PY_SOURCE) + return _spec_from_modpath(modpath, path, context) def get_module_part(dotted_name, context_file=None): @@ -569,42 +591,49 @@ def is_relative(modname, from_file): # internal only functions ##################################################### -def _file_from_modpath(modpath, path=None, context=None): +def _spec_from_modpath(modpath, path=None, context=None): """given a mod path (i.e. splitted module / package name), return the - corresponding file + corresponding spec this function is used internally, see `file_from_modpath`'s documentation for more information """ assert len(modpath) > 0 + location = None if context is not None: try: - mtype, mp_filename = _module_file(modpath, [context]) + spec = _find_spec(modpath, [context]) + location = spec.location except ImportError: - mtype, mp_filename = _module_file(modpath, path) + spec = _find_spec(modpath, path) + location = spec.location else: - mtype, mp_filename = _module_file(modpath, path) - if mtype == imp.PY_COMPILED: + spec = _find_spec(modpath, path) + if spec.type == ModuleType.PY_COMPILED: try: - return get_source_file(mp_filename), imp.PY_SOURCE + location = get_source_file(spec.location) + return spec._replace(location=location, type=ModuleSpec.PY_SOURCE) except NoSourceFile: - return mp_filename, imp.PY_COMPILED - elif mtype == imp.C_BUILTIN: + return spec.replace(location=location) + elif spec.type == ModuleType.C_BUILTIN: # integrated builtin module - return None, imp.C_BUILTIN - elif mtype == imp.PKG_DIRECTORY: - mp_filename = _has_init(mp_filename) - mtype = imp.PY_SOURCE - return mp_filename, mtype + return spec._replace(location=None) + elif spec.type == ModuleType.PKG_DIRECTORY: + location = _has_init(spec.location) + return spec._replace(location=location, type=ModuleType.PY_SOURCE) + return spec + def _search_zip(modpath, pic): for filepath, importer in list(pic.items()): if importer is not None: - if importer.find_module(modpath[0]): + found = importer.find_module(modpath[0]) + if found: if not importer.find_module(os.path.sep.join(modpath)): raise ImportError('No module named %s in %s/%s' % ( '.'.join(modpath[1:]), filepath, modpath)) - return (PY_ZIPMODULE, + #import code; code.interact(local=locals()) + return (ModuleType.PY_ZIPMODULE, os.path.abspath(filepath) + os.path.sep + os.path.sep.join(modpath), filepath) raise ImportError('No module named %s' % '.'.join(modpath)) @@ -615,12 +644,146 @@ def _search_zip(modpath, pic): pkg_resources = None +def _precache_zipimporters(path=None): + pic = sys.path_importer_cache + path = path or sys.path + for entry_path in path: + if entry_path not in pic: + try: + pic[entry_path] = zipimport.zipimporter(entry_path) + except zipimport.ZipImportError: + pic[entry_path] = None + return pic + + def _is_namespace(modname): return (pkg_resources is not None and modname in pkg_resources._namespace_packages) -def _module_file(modpath, path=None): +def _is_setuptools_namespace(location): + try: + with open(os.path.join(location, '__init__.py'), 'rb') as stream: + data = stream.read(4096) + except IOError: + pass + else: + extend_path = b'pkgutil' in data and b'extend_path' in data + declare_namespace = ( + b"pkg_resources" in data + and b"declare_namespace(__name__)" in data) + return extend_path or declare_namespace + + +# Spec finders. + +_ModuleSpec = collections.namedtuple('_ModuleSpec', 'name type location ' + 'origin submodule_search_locations') + +class ModuleSpec(_ModuleSpec): + + def __new__(cls, name, type, location=None, origin=None, + submodule_search_locations=None): + return _ModuleSpec.__new__(cls, name=name, type=type, + location=location, origin=origin, + submodule_search_locations=submodule_search_locations) + + +class Finder(object): + + def __init__(self, path=None): + self._path = path or sys.path + + @abc.abstractmethod + def find_module(self, modname, module_parts, processed, submodule_path): + pass + + def contribute_to_path(self, filename, processed): + return None + + +class ImpFinder(Finder): + + def find_module(self, modname, _, processed, submodule_path): + try: + stream, mp_filename, mp_desc = imp.find_module(modname, submodule_path) + except ImportError: + return None + + # Close resources. + if stream: + stream.close() + + return ModuleSpec(name=modname, location=mp_filename, + type=_imp_type_to_module_type(mp_desc[2])) + + def contribute_to_path(self, spec, processed): + if spec.location is None: + # Builtin. + return None + + if _is_setuptools_namespace(spec.location): + # extend_path is called, search sys.path for module/packages + # of this name see pkgutil.extend_path documentation + path = [os.path.join(p, *processed) for p in sys.path + if os.path.isdir(os.path.join(p, *processed))] + else: + path = [spec.location] + return path + + +class ZipFinder(Finder): + + def __init__(self, path): + super(ZipFinder, self).__init__(path) + self._zipimporters = _precache_zipimporters(path) + + def find_module(self, modname, module_parts, processed, submodule_path): + try: + file_type, filename, path = _search_zip(module_parts, self._zipimporters) + except ImportError: + return None + + return ModuleSpec(name=modname, location=filename, + origin='egg', type=file_type, + submodule_search_locations=path) + + +class PEP420SpecFinder(Finder): + + def find_module(self, modname, module_parts, processed, submodule_path): + spec = importlib.machinery.PathFinder.find_spec(modname, path=submodule_path) + if spec: + location = spec.origin if spec.origin != 'namespace' else None + type = ModuleType.PY_NAMESPACE if spec.origin == 'namespace' else None + spec = ModuleSpec(name=spec.name, location=location, + origin=spec.origin, type=type, + submodule_search_locations=list(spec.submodule_search_locations or [])) + return spec + + def contribute_to_path(self, spec, processed): + if spec.type == ModuleType.PY_NAMESPACE: + return spec.submodule_search_locations + return None + + +def _find_spec_with_path(search_path, modname, module_parts, processed, submodule_path): + finders = [finder(search_path) for finder in (ImpFinder, ZipFinder)] + if _HAS_MACHINERY: + finders.append(PEP420SpecFinder(search_path)) + + for finder in finders: + spec = finder.find_module(modname, module_parts, processed, submodule_path) + if spec is None: + continue + return finder, spec + + raise ImportError('No module %s in %s' % ('.'.join(module_parts), + '.'.join(processed))) + + + +def _find_spec(modpath, path=None): """get a module type / file path :type modpath: list or tuple @@ -637,91 +800,29 @@ def _module_file(modpath, path=None): :rtype: tuple(int, str) :return: the module type flag and the file path for a module """ - # egg support compat - try: - pic = sys.path_importer_cache - _path = (path is None and sys.path or path) - for __path in _path: - if not __path in pic: - try: - pic[__path] = zipimport.zipimporter(__path) - except zipimport.ZipImportError: - pic[__path] = None - checkeggs = True - except AttributeError: - checkeggs = False - # pkg_resources support (aka setuptools namespace packages) - if _is_namespace(modpath[0]) and modpath[0] in sys.modules: - # setuptools has added into sys.modules a module object with proper - # __path__, get back information from there - module = sys.modules[modpath.pop(0)] - path = module.__path__ - if not modpath: - return imp.C_BUILTIN, None - imported = [] + _path = path or sys.path + + # Need a copy for not mutating the argument. + modpath = modpath[:] + + submodule_path = [] + module_parts = modpath[:] + processed = [] + while modpath: - modname = modpath[0] - # take care to changes in find_module implementation wrt builtin modules - # - # Python 2.6.6 (r266:84292, Sep 11 2012, 08:34:23) - # >>> imp.find_module('posix') - # (None, 'posix', ('', '', 6)) - # - # Python 3.3.1 (default, Apr 26 2013, 12:08:46) - # >>> imp.find_module('posix') - # (None, None, ('', '', 6)) - try: - stream, mp_filename, mp_desc = imp.find_module(modname, path) - except ImportError: - if checkeggs: - return _search_zip(modpath, pic)[:2] - raise - else: - # Don't forget to close the stream to avoid - # spurious ResourceWarnings. - if stream: - stream.close() - - if checkeggs and mp_filename: - fullabspath = [_cache_normalize_path(x) for x in _path] - try: - pathindex = fullabspath.index(os.path.dirname(_normalize_path(mp_filename))) - emtype, emp_filename, zippath = _search_zip(modpath, pic) - if pathindex > _path.index(zippath): - # an egg takes priority - return emtype, emp_filename - except ValueError: - # XXX not in _path - pass - except ImportError: - pass - checkeggs = False - imported.append(modpath.pop(0)) - mtype = mp_desc[2] + modname = modpath.pop(0) + finder, spec = _find_spec_with_path(_path, modname, + module_parts, processed, + submodule_path or path) + processed.append(modname) if modpath: - if mtype != imp.PKG_DIRECTORY: - raise ImportError('No module %s in %s' % ('.'.join(modpath), - '.'.join(imported))) - # XXX guess if package is using pkgutil.extend_path by looking for - # those keywords in the first four Kbytes - try: - with open(os.path.join(mp_filename, '__init__.py'), 'rb') as stream: - data = stream.read(4096) - except IOError: - path = [mp_filename] - else: - extend_path = b'pkgutil' in data and b'extend_path' in data - declare_namespace = ( - b"pkg_resources" in data - and b"declare_namespace(__name__)" in data) - if extend_path or declare_namespace: - # extend_path is called, search sys.path for module/packages - # of this name see pkgutil.extend_path documentation - path = [os.path.join(p, *imported) for p in sys.path - if os.path.isdir(os.path.join(p, *imported))] - else: - path = [mp_filename] - return mtype, mp_filename + submodule_path = finder.contribute_to_path(spec, processed) + + if spec.type == ModuleType.PKG_DIRECTORY: + spec = spec._replace(submodule_search_locations=submodule_path) + + return spec + def _is_python_file(filename): """return true if the given filename should be considered as a python file diff --git a/astroid/tests/unittest_manager.py b/astroid/tests/unittest_manager.py index aa1fd4007c..9c74b78f4a 100644 --- a/astroid/tests/unittest_manager.py +++ b/astroid/tests/unittest_manager.py @@ -157,7 +157,7 @@ def test_file_from_module(self): """check if the unittest filepath is equals to the result of the method""" self.assertEqual( _get_file_from_object(unittest), - self.manager.file_from_module_name('unittest', None)[0]) + self.manager.file_from_module_name('unittest', None).location) def test_file_from_module_name_astro_building_exception(self): """check if the method launch a exception with a wrong module name""" diff --git a/astroid/tests/unittest_modutils.py b/astroid/tests/unittest_modutils.py index 6dfd3bd889..47c418bbab 100644 --- a/astroid/tests/unittest_modutils.py +++ b/astroid/tests/unittest_modutils.py @@ -39,16 +39,16 @@ def tearDown(self): del sys.path_importer_cache[k] def test_find_zipped_module(self): - mtype, mfile = modutils._module_file( + spec = modutils._find_spec( [self.package], [resources.find('data/MyPyPa-0.1.0-py2.5.zip')]) - self.assertEqual(mtype, modutils.PY_ZIPMODULE) - self.assertEqual(mfile.split(os.sep)[-3:], ["data", "MyPyPa-0.1.0-py2.5.zip", self.package]) + self.assertEqual(spec.type, modutils.ModuleType.PY_ZIPMODULE) + self.assertEqual(spec.location.split(os.sep)[-3:], ["data", "MyPyPa-0.1.0-py2.5.zip", self.package]) def test_find_egg_module(self): - mtype, mfile = modutils._module_file( + spec = modutils._find_spec( [self.package], [resources.find('data/MyPyPa-0.1.0-py2.5.egg')]) - self.assertEqual(mtype, modutils.PY_ZIPMODULE) - self.assertEqual(mfile.split(os.sep)[-3:], ["data", "MyPyPa-0.1.0-py2.5.egg", self.package]) + self.assertEqual(spec.type, modutils.ModuleType.PY_ZIPMODULE) + self.assertEqual(spec.location.split(os.sep)[-3:], ["data", "MyPyPa-0.1.0-py2.5.egg", self.package]) class LoadModuleFromNameTest(unittest.TestCase): From 5c1c00bfa28eb71677d2561689a919d9ebce6fd1 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 8 May 2016 16:30:02 +0300 Subject: [PATCH 239/312] Don't introduce empty values for non-zip importers in path_importer_cache and add a test for implicit namespace packages. --- astroid/modutils.py | 4 ++-- .../testdata/python2/data/namespace_pep_420/module.py | 0 .../testdata/python3/data/namespace_pep_420/module.py | 0 astroid/tests/unittest_manager.py | 11 +++++++++++ 4 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 astroid/tests/testdata/python2/data/namespace_pep_420/module.py create mode 100644 astroid/tests/testdata/python3/data/namespace_pep_420/module.py diff --git a/astroid/modutils.py b/astroid/modutils.py index abc8981402..8b45d4f5e7 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -652,7 +652,7 @@ def _precache_zipimporters(path=None): try: pic[entry_path] = zipimport.zipimporter(entry_path) except zipimport.ZipImportError: - pic[entry_path] = None + continue return pic @@ -805,7 +805,7 @@ def _find_spec(modpath, path=None): # Need a copy for not mutating the argument. modpath = modpath[:] - submodule_path = [] + submodule_path = None module_parts = modpath[:] processed = [] diff --git a/astroid/tests/testdata/python2/data/namespace_pep_420/module.py b/astroid/tests/testdata/python2/data/namespace_pep_420/module.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/astroid/tests/testdata/python3/data/namespace_pep_420/module.py b/astroid/tests/testdata/python3/data/namespace_pep_420/module.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/astroid/tests/unittest_manager.py b/astroid/tests/unittest_manager.py index 9c74b78f4a..abf1d32c5b 100644 --- a/astroid/tests/unittest_manager.py +++ b/astroid/tests/unittest_manager.py @@ -113,6 +113,17 @@ def test_ast_from_namespace_pkgutil(self): def test_ast_from_namespace_pkg_resources(self): self._test_ast_from_old_namespace_package_protocol('pkg_resources') + @unittest.skipUnless(sys.version_info[:2] > (3, 3), "Needs PEP 420 namespace protocol") + def test_implicit_namespace_package(self): + data_dir = os.path.abspath(os.path.join(resources.DATA_DIR, 'data')) + sys.path.insert(0, data_dir) + try: + module = self.manager.ast_from_module_name('namespace_pep_420.module') + self.assertIsInstance(module, astroid.Module) + self.assertEqual(module.name, 'namespace_pep_420.module') + finally: + sys.path.pop(0) + def _test_ast_from_zip(self, archive): origpath = sys.path[:] sys.modules.pop('mypypa', None) From 98a91c84e39dd5e4f18657d4fa7295ea3e1ecd5a Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 8 May 2016 16:43:53 +0300 Subject: [PATCH 240/312] Support namespace packages only for versions with the module spec feature implemented. --- astroid/modutils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astroid/modutils.py b/astroid/modutils.py index 8b45d4f5e7..194b71ba6a 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -769,7 +769,7 @@ def contribute_to_path(self, spec, processed): def _find_spec_with_path(search_path, modname, module_parts, processed, submodule_path): finders = [finder(search_path) for finder in (ImpFinder, ZipFinder)] - if _HAS_MACHINERY: + if _HAS_MACHINERY and sys.version_info[:2] > (3, 3): finders.append(PEP420SpecFinder(search_path)) for finder in finders: From 9206ffb17b74b1e65bae72d461a9e05d6154e17f Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 8 May 2016 17:24:03 +0300 Subject: [PATCH 241/312] Compute the namespace package path with pkg_resources --- astroid/tests/unittest_manager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astroid/tests/unittest_manager.py b/astroid/tests/unittest_manager.py index abf1d32c5b..40c21770f7 100644 --- a/astroid/tests/unittest_manager.py +++ b/astroid/tests/unittest_manager.py @@ -115,7 +115,7 @@ def test_ast_from_namespace_pkg_resources(self): @unittest.skipUnless(sys.version_info[:2] > (3, 3), "Needs PEP 420 namespace protocol") def test_implicit_namespace_package(self): - data_dir = os.path.abspath(os.path.join(resources.DATA_DIR, 'data')) + data_dir = os.path.dirname(resources.find('data/namespace_pep_420')) sys.path.insert(0, data_dir) try: module = self.manager.ast_from_module_name('namespace_pep_420.module') From e31b3e1fe4113ec3867df93272d9d71db1e2b9b6 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 8 May 2016 17:42:49 +0300 Subject: [PATCH 242/312] Improve the message. --- astroid/modutils.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/astroid/modutils.py b/astroid/modutils.py index 194b71ba6a..28be81f8c2 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -778,9 +778,7 @@ def _find_spec_with_path(search_path, modname, module_parts, processed, submodul continue return finder, spec - raise ImportError('No module %s in %s' % ('.'.join(module_parts), - '.'.join(processed))) - + raise ImportError('No module named %s' % '.'.join(module_parts)) def _find_spec(modpath, path=None): From cfab7072ce63a207774685782529ba2d6157328d Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 9 May 2016 13:12:16 +0300 Subject: [PATCH 243/312] Implicit namespace package support for modpath_from_file for Python 3.3+. --- astroid/modutils.py | 6 ++++-- astroid/tests/unittest_modutils.py | 13 +++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/astroid/modutils.py b/astroid/modutils.py index 28be81f8c2..5f66eacca2 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -296,8 +296,10 @@ def _check_init(path, mod_path): for part in mod_path: modpath.append(part) path = os.path.join(path, part) - if not _is_namespace('.'.join(modpath)) and not _has_init(path): - return False + if not _has_init(path): + old_namespace = _is_namespace('.'.join(modpath)) + if not (old_namespace or sys.version_info[:2] > (3, 3)): + return False return True diff --git a/astroid/tests/unittest_modutils.py b/astroid/tests/unittest_modutils.py index 47c418bbab..b8d5213c62 100644 --- a/astroid/tests/unittest_modutils.py +++ b/astroid/tests/unittest_modutils.py @@ -110,6 +110,19 @@ def test_knownValues_modpath_from_file_2(self): def test_raise_modpath_from_file_Exception(self): self.assertRaises(Exception, modutils.modpath_from_file, '/turlututu') + def test_modpath_from_file_namespace(self): + datadir = os.path.abspath(resources.find('data')) + path = os.path.abspath( + os.path.join(resources.find('data/namespace_pep_420'), 'module.py') + ) + + sys.path.insert(0, datadir) + try: + modpath = modutils.modpath_from_file(path) + finally: + sys.path.pop(0) + self.assertEqual(modpath, ['namespace_pep_420', 'module']) + class LoadModuleFromPathTest(resources.SysPathSetup, unittest.TestCase): From 9f8998945feb8ab986ebc7ba66af775edeba3b59 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 9 May 2016 14:19:00 +0300 Subject: [PATCH 244/312] Build a dummy module object for namespace directories and add a test for multiple directories contributing to the same namespace. --- astroid/builder.py | 5 +++++ astroid/manager.py | 6 ++++++ .../namespace_pep_420/submodule.py | 1 + .../testdata/python2/data/namespace_pep_420/module.py | 1 + .../namespace_pep_420/submodule.py | 1 + .../testdata/python3/data/namespace_pep_420/module.py | 1 + astroid/tests/unittest_manager.py | 11 +++++++++-- astroid/tests/unittest_modutils.py | 1 + astroid/tree/scoped_nodes.py | 2 ++ 9 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 astroid/tests/testdata/python2/data/contribute_to_namespace/namespace_pep_420/submodule.py create mode 100644 astroid/tests/testdata/python3/data/contribute_to_namespace/namespace_pep_420/submodule.py diff --git a/astroid/builder.py b/astroid/builder.py index ca2b92ba43..0c05f8cfd3 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -36,6 +36,7 @@ from astroid import util raw_building = util.lazy_import('raw_building') +nodes = util.lazy_import('nodes') def _parse(string): @@ -237,6 +238,10 @@ def delayed_assignments(root): pass +def build_namespace_package_module(name, path): + return nodes.Module(name, doc='', path=path, package=True) + + def parse(code, module_name='', path=None, apply_transforms=True): """Parses a source string in order to obtain an astroid AST from it diff --git a/astroid/manager.py b/astroid/manager.py index f6c9a2614f..73dd961a36 100644 --- a/astroid/manager.py +++ b/astroid/manager.py @@ -94,6 +94,10 @@ def ast_from_file(self, filepath, modname=None, fallback=True, source=False): def _build_stub_module(self, modname): return builder.AstroidBuilder(self).string_build('', modname) + def _build_namespace_module(self, modname, path): + from astroid.builder import build_namespace_package_module + return build_namespace_package_module(modname, path) + def _can_load_extension(self, modname): if self.always_load_extensions: return True @@ -133,6 +137,8 @@ def ast_from_module_name(self, modname, context_file=None): raise exceptions.AstroidImportError( "Unable to load compiled module {modname}.", modname=modname, path=spec.location) + elif spec.type == modutils.ModuleType.PY_NAMESPACE: + return self._build_namespace_module(modname, spec.submodule_search_locations) if spec.location is None: raise exceptions.AstroidImportError( "Can't find a file for module {modname}.", diff --git a/astroid/tests/testdata/python2/data/contribute_to_namespace/namespace_pep_420/submodule.py b/astroid/tests/testdata/python2/data/contribute_to_namespace/namespace_pep_420/submodule.py new file mode 100644 index 0000000000..6fbcff414a --- /dev/null +++ b/astroid/tests/testdata/python2/data/contribute_to_namespace/namespace_pep_420/submodule.py @@ -0,0 +1 @@ +var = 42 \ No newline at end of file diff --git a/astroid/tests/testdata/python2/data/namespace_pep_420/module.py b/astroid/tests/testdata/python2/data/namespace_pep_420/module.py index e69de29bb2..a4d111e6ea 100644 --- a/astroid/tests/testdata/python2/data/namespace_pep_420/module.py +++ b/astroid/tests/testdata/python2/data/namespace_pep_420/module.py @@ -0,0 +1 @@ +from namespace_pep_420.submodule import var \ No newline at end of file diff --git a/astroid/tests/testdata/python3/data/contribute_to_namespace/namespace_pep_420/submodule.py b/astroid/tests/testdata/python3/data/contribute_to_namespace/namespace_pep_420/submodule.py new file mode 100644 index 0000000000..6fbcff414a --- /dev/null +++ b/astroid/tests/testdata/python3/data/contribute_to_namespace/namespace_pep_420/submodule.py @@ -0,0 +1 @@ +var = 42 \ No newline at end of file diff --git a/astroid/tests/testdata/python3/data/namespace_pep_420/module.py b/astroid/tests/testdata/python3/data/namespace_pep_420/module.py index e69de29bb2..a4d111e6ea 100644 --- a/astroid/tests/testdata/python3/data/namespace_pep_420/module.py +++ b/astroid/tests/testdata/python3/data/namespace_pep_420/module.py @@ -0,0 +1 @@ +from namespace_pep_420.submodule import var \ No newline at end of file diff --git a/astroid/tests/unittest_manager.py b/astroid/tests/unittest_manager.py index 40c21770f7..1b83862c2b 100644 --- a/astroid/tests/unittest_manager.py +++ b/astroid/tests/unittest_manager.py @@ -116,13 +116,20 @@ def test_ast_from_namespace_pkg_resources(self): @unittest.skipUnless(sys.version_info[:2] > (3, 3), "Needs PEP 420 namespace protocol") def test_implicit_namespace_package(self): data_dir = os.path.dirname(resources.find('data/namespace_pep_420')) - sys.path.insert(0, data_dir) + contribute = os.path.join(data_dir, 'contribute_to_namespace') + for value in (data_dir, contribute): + sys.path.insert(0, value) + try: module = self.manager.ast_from_module_name('namespace_pep_420.module') self.assertIsInstance(module, astroid.Module) self.assertEqual(module.name, 'namespace_pep_420.module') + var = next(module.igetattr('var')) + self.assertIsInstance(var, astroid.Const) + self.assertEqual(var.value, 42) finally: - sys.path.pop(0) + for _ in range(2): + sys.path.pop(0) def _test_ast_from_zip(self, archive): origpath = sys.path[:] diff --git a/astroid/tests/unittest_modutils.py b/astroid/tests/unittest_modutils.py index b8d5213c62..05a7e9d553 100644 --- a/astroid/tests/unittest_modutils.py +++ b/astroid/tests/unittest_modutils.py @@ -110,6 +110,7 @@ def test_knownValues_modpath_from_file_2(self): def test_raise_modpath_from_file_Exception(self): self.assertRaises(Exception, modutils.modpath_from_file, '/turlututu') + @unittest.skipUnless(sys.version_info[:2] > (3, 3), "Needs Python 3.3+ namespace package.") def test_modpath_from_file_namespace(self): datadir = os.path.abspath(resources.find('data')) path = os.path.abspath( diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 46420dfbaa..7bdc8aab90 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -314,6 +314,7 @@ def import_module(self, modname, relative_only=False, level=None): if relative_only and level is None: level = 0 absmodname = self.relative_to_absolute_name(modname, level) + try: return MANAGER.ast_from_module_name(absmodname) except exceptions.AstroidBuildingError: @@ -345,6 +346,7 @@ def relative_to_absolute_name(self, modname, level): package_name = self.name else: package_name = self.name.rsplit('.', 1)[0] + if package_name: if not modname: return package_name From bdbe0ad26ed32a7a3bf1f7a6876501b98a6ae0e9 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 9 May 2016 17:20:53 +0300 Subject: [PATCH 245/312] Add Changelog entry for namespace packages. --- ChangeLog | 13 ++++++++++++- astroid/modutils.py | 2 ++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 5be60aa52c..1604a4b0f8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,7 +1,18 @@ Change log for the astroid package (used to be astng) ===================================================== --- +-- + * Add support for implicit namespace packages (PEP 420) + + This change involves a couple of modifications. First, we're relying on a + spec finder protocol, inspired by importlib's ModuleSpec, for finding where + a file or package is, using importlib's PathFinder as well, which enable + us to discover namespace packages as well. + This discovery is the center piece of the namespace package support, + the other part being the construction of a dummy Module node whenever + a namespace package root directory is requested during astroid's import + references. + * Fix a crash which occurred when a method had a same name as a builtin object, decorated at the same time by that builtin object ( a property for instance) diff --git a/astroid/modutils.py b/astroid/modutils.py index 5f66eacca2..aa62f564bf 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -48,6 +48,8 @@ except ImportError: pkg_resources = None +from astroid import util + ModuleType = enum.Enum('ModuleType', 'C_BUILTIN C_EXTENSION PKG_DIRECTORY ' 'PY_CODERESOURCE PY_COMPILED PY_FROZEN PY_RESOURCE ' 'PY_SOURCE PY_ZIPMODULE PY_NAMESPACE') From f438bb8f400a17bfd61276c9c50232ca4f7056e5 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 9 May 2016 17:47:19 +0300 Subject: [PATCH 246/312] Move the test data namespace packages where they belong. --- .../data/contribute_to_namespace/namespace_pep_420/submodule.py | 0 .../testdata/{python2 => }/data/namespace_pep_420/module.py | 0 .../{python2 => }/data/path_pkg_resources_1/package/__init__.py | 0 .../{python2 => }/data/path_pkg_resources_1/package/foo.py | 0 .../{python2 => }/data/path_pkg_resources_2/package/__init__.py | 0 .../{python2 => }/data/path_pkg_resources_2/package/bar.py | 0 .../{python2 => }/data/path_pkg_resources_3/package/__init__.py | 0 .../{python2 => }/data/path_pkg_resources_3/package/baz.py | 0 .../{python2 => }/data/path_pkgutil_1/package/__init__.py | 0 .../testdata/{python2 => }/data/path_pkgutil_1/package/foo.py | 0 .../{python2 => }/data/path_pkgutil_2/package/__init__.py | 0 .../testdata/{python2 => }/data/path_pkgutil_2/package/bar.py | 0 .../{python2 => }/data/path_pkgutil_3/package/__init__.py | 0 .../testdata/{python2 => }/data/path_pkgutil_3/package/baz.py | 0 .../data/contribute_to_namespace/namespace_pep_420/submodule.py | 1 - astroid/tests/testdata/python3/data/namespace_pep_420/module.py | 1 - .../python3/data/path_pkg_resources_1/package/__init__.py | 1 - .../testdata/python3/data/path_pkg_resources_1/package/foo.py | 0 .../python3/data/path_pkg_resources_2/package/__init__.py | 1 - .../testdata/python3/data/path_pkg_resources_2/package/bar.py | 0 .../python3/data/path_pkg_resources_3/package/__init__.py | 1 - .../testdata/python3/data/path_pkg_resources_3/package/baz.py | 0 .../testdata/python3/data/path_pkgutil_1/package/__init__.py | 2 -- .../tests/testdata/python3/data/path_pkgutil_1/package/foo.py | 0 .../testdata/python3/data/path_pkgutil_2/package/__init__.py | 2 -- .../tests/testdata/python3/data/path_pkgutil_2/package/bar.py | 0 .../testdata/python3/data/path_pkgutil_3/package/__init__.py | 2 -- .../tests/testdata/python3/data/path_pkgutil_3/package/baz.py | 0 28 files changed, 11 deletions(-) rename astroid/tests/testdata/{python2 => }/data/contribute_to_namespace/namespace_pep_420/submodule.py (100%) rename astroid/tests/testdata/{python2 => }/data/namespace_pep_420/module.py (100%) rename astroid/tests/testdata/{python2 => }/data/path_pkg_resources_1/package/__init__.py (100%) rename astroid/tests/testdata/{python2 => }/data/path_pkg_resources_1/package/foo.py (100%) rename astroid/tests/testdata/{python2 => }/data/path_pkg_resources_2/package/__init__.py (100%) rename astroid/tests/testdata/{python2 => }/data/path_pkg_resources_2/package/bar.py (100%) rename astroid/tests/testdata/{python2 => }/data/path_pkg_resources_3/package/__init__.py (100%) rename astroid/tests/testdata/{python2 => }/data/path_pkg_resources_3/package/baz.py (100%) rename astroid/tests/testdata/{python2 => }/data/path_pkgutil_1/package/__init__.py (100%) rename astroid/tests/testdata/{python2 => }/data/path_pkgutil_1/package/foo.py (100%) rename astroid/tests/testdata/{python2 => }/data/path_pkgutil_2/package/__init__.py (100%) rename astroid/tests/testdata/{python2 => }/data/path_pkgutil_2/package/bar.py (100%) rename astroid/tests/testdata/{python2 => }/data/path_pkgutil_3/package/__init__.py (100%) rename astroid/tests/testdata/{python2 => }/data/path_pkgutil_3/package/baz.py (100%) delete mode 100644 astroid/tests/testdata/python3/data/contribute_to_namespace/namespace_pep_420/submodule.py delete mode 100644 astroid/tests/testdata/python3/data/namespace_pep_420/module.py delete mode 100644 astroid/tests/testdata/python3/data/path_pkg_resources_1/package/__init__.py delete mode 100644 astroid/tests/testdata/python3/data/path_pkg_resources_1/package/foo.py delete mode 100644 astroid/tests/testdata/python3/data/path_pkg_resources_2/package/__init__.py delete mode 100644 astroid/tests/testdata/python3/data/path_pkg_resources_2/package/bar.py delete mode 100644 astroid/tests/testdata/python3/data/path_pkg_resources_3/package/__init__.py delete mode 100644 astroid/tests/testdata/python3/data/path_pkg_resources_3/package/baz.py delete mode 100644 astroid/tests/testdata/python3/data/path_pkgutil_1/package/__init__.py delete mode 100644 astroid/tests/testdata/python3/data/path_pkgutil_1/package/foo.py delete mode 100644 astroid/tests/testdata/python3/data/path_pkgutil_2/package/__init__.py delete mode 100644 astroid/tests/testdata/python3/data/path_pkgutil_2/package/bar.py delete mode 100644 astroid/tests/testdata/python3/data/path_pkgutil_3/package/__init__.py delete mode 100644 astroid/tests/testdata/python3/data/path_pkgutil_3/package/baz.py diff --git a/astroid/tests/testdata/python2/data/contribute_to_namespace/namespace_pep_420/submodule.py b/astroid/tests/testdata/data/contribute_to_namespace/namespace_pep_420/submodule.py similarity index 100% rename from astroid/tests/testdata/python2/data/contribute_to_namespace/namespace_pep_420/submodule.py rename to astroid/tests/testdata/data/contribute_to_namespace/namespace_pep_420/submodule.py diff --git a/astroid/tests/testdata/python2/data/namespace_pep_420/module.py b/astroid/tests/testdata/data/namespace_pep_420/module.py similarity index 100% rename from astroid/tests/testdata/python2/data/namespace_pep_420/module.py rename to astroid/tests/testdata/data/namespace_pep_420/module.py diff --git a/astroid/tests/testdata/python2/data/path_pkg_resources_1/package/__init__.py b/astroid/tests/testdata/data/path_pkg_resources_1/package/__init__.py similarity index 100% rename from astroid/tests/testdata/python2/data/path_pkg_resources_1/package/__init__.py rename to astroid/tests/testdata/data/path_pkg_resources_1/package/__init__.py diff --git a/astroid/tests/testdata/python2/data/path_pkg_resources_1/package/foo.py b/astroid/tests/testdata/data/path_pkg_resources_1/package/foo.py similarity index 100% rename from astroid/tests/testdata/python2/data/path_pkg_resources_1/package/foo.py rename to astroid/tests/testdata/data/path_pkg_resources_1/package/foo.py diff --git a/astroid/tests/testdata/python2/data/path_pkg_resources_2/package/__init__.py b/astroid/tests/testdata/data/path_pkg_resources_2/package/__init__.py similarity index 100% rename from astroid/tests/testdata/python2/data/path_pkg_resources_2/package/__init__.py rename to astroid/tests/testdata/data/path_pkg_resources_2/package/__init__.py diff --git a/astroid/tests/testdata/python2/data/path_pkg_resources_2/package/bar.py b/astroid/tests/testdata/data/path_pkg_resources_2/package/bar.py similarity index 100% rename from astroid/tests/testdata/python2/data/path_pkg_resources_2/package/bar.py rename to astroid/tests/testdata/data/path_pkg_resources_2/package/bar.py diff --git a/astroid/tests/testdata/python2/data/path_pkg_resources_3/package/__init__.py b/astroid/tests/testdata/data/path_pkg_resources_3/package/__init__.py similarity index 100% rename from astroid/tests/testdata/python2/data/path_pkg_resources_3/package/__init__.py rename to astroid/tests/testdata/data/path_pkg_resources_3/package/__init__.py diff --git a/astroid/tests/testdata/python2/data/path_pkg_resources_3/package/baz.py b/astroid/tests/testdata/data/path_pkg_resources_3/package/baz.py similarity index 100% rename from astroid/tests/testdata/python2/data/path_pkg_resources_3/package/baz.py rename to astroid/tests/testdata/data/path_pkg_resources_3/package/baz.py diff --git a/astroid/tests/testdata/python2/data/path_pkgutil_1/package/__init__.py b/astroid/tests/testdata/data/path_pkgutil_1/package/__init__.py similarity index 100% rename from astroid/tests/testdata/python2/data/path_pkgutil_1/package/__init__.py rename to astroid/tests/testdata/data/path_pkgutil_1/package/__init__.py diff --git a/astroid/tests/testdata/python2/data/path_pkgutil_1/package/foo.py b/astroid/tests/testdata/data/path_pkgutil_1/package/foo.py similarity index 100% rename from astroid/tests/testdata/python2/data/path_pkgutil_1/package/foo.py rename to astroid/tests/testdata/data/path_pkgutil_1/package/foo.py diff --git a/astroid/tests/testdata/python2/data/path_pkgutil_2/package/__init__.py b/astroid/tests/testdata/data/path_pkgutil_2/package/__init__.py similarity index 100% rename from astroid/tests/testdata/python2/data/path_pkgutil_2/package/__init__.py rename to astroid/tests/testdata/data/path_pkgutil_2/package/__init__.py diff --git a/astroid/tests/testdata/python2/data/path_pkgutil_2/package/bar.py b/astroid/tests/testdata/data/path_pkgutil_2/package/bar.py similarity index 100% rename from astroid/tests/testdata/python2/data/path_pkgutil_2/package/bar.py rename to astroid/tests/testdata/data/path_pkgutil_2/package/bar.py diff --git a/astroid/tests/testdata/python2/data/path_pkgutil_3/package/__init__.py b/astroid/tests/testdata/data/path_pkgutil_3/package/__init__.py similarity index 100% rename from astroid/tests/testdata/python2/data/path_pkgutil_3/package/__init__.py rename to astroid/tests/testdata/data/path_pkgutil_3/package/__init__.py diff --git a/astroid/tests/testdata/python2/data/path_pkgutil_3/package/baz.py b/astroid/tests/testdata/data/path_pkgutil_3/package/baz.py similarity index 100% rename from astroid/tests/testdata/python2/data/path_pkgutil_3/package/baz.py rename to astroid/tests/testdata/data/path_pkgutil_3/package/baz.py diff --git a/astroid/tests/testdata/python3/data/contribute_to_namespace/namespace_pep_420/submodule.py b/astroid/tests/testdata/python3/data/contribute_to_namespace/namespace_pep_420/submodule.py deleted file mode 100644 index 6fbcff414a..0000000000 --- a/astroid/tests/testdata/python3/data/contribute_to_namespace/namespace_pep_420/submodule.py +++ /dev/null @@ -1 +0,0 @@ -var = 42 \ No newline at end of file diff --git a/astroid/tests/testdata/python3/data/namespace_pep_420/module.py b/astroid/tests/testdata/python3/data/namespace_pep_420/module.py deleted file mode 100644 index a4d111e6ea..0000000000 --- a/astroid/tests/testdata/python3/data/namespace_pep_420/module.py +++ /dev/null @@ -1 +0,0 @@ -from namespace_pep_420.submodule import var \ No newline at end of file diff --git a/astroid/tests/testdata/python3/data/path_pkg_resources_1/package/__init__.py b/astroid/tests/testdata/python3/data/path_pkg_resources_1/package/__init__.py deleted file mode 100644 index b0d6433717..0000000000 --- a/astroid/tests/testdata/python3/data/path_pkg_resources_1/package/__init__.py +++ /dev/null @@ -1 +0,0 @@ -__import__('pkg_resources').declare_namespace(__name__) \ No newline at end of file diff --git a/astroid/tests/testdata/python3/data/path_pkg_resources_1/package/foo.py b/astroid/tests/testdata/python3/data/path_pkg_resources_1/package/foo.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/astroid/tests/testdata/python3/data/path_pkg_resources_2/package/__init__.py b/astroid/tests/testdata/python3/data/path_pkg_resources_2/package/__init__.py deleted file mode 100644 index b0d6433717..0000000000 --- a/astroid/tests/testdata/python3/data/path_pkg_resources_2/package/__init__.py +++ /dev/null @@ -1 +0,0 @@ -__import__('pkg_resources').declare_namespace(__name__) \ No newline at end of file diff --git a/astroid/tests/testdata/python3/data/path_pkg_resources_2/package/bar.py b/astroid/tests/testdata/python3/data/path_pkg_resources_2/package/bar.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/astroid/tests/testdata/python3/data/path_pkg_resources_3/package/__init__.py b/astroid/tests/testdata/python3/data/path_pkg_resources_3/package/__init__.py deleted file mode 100644 index b0d6433717..0000000000 --- a/astroid/tests/testdata/python3/data/path_pkg_resources_3/package/__init__.py +++ /dev/null @@ -1 +0,0 @@ -__import__('pkg_resources').declare_namespace(__name__) \ No newline at end of file diff --git a/astroid/tests/testdata/python3/data/path_pkg_resources_3/package/baz.py b/astroid/tests/testdata/python3/data/path_pkg_resources_3/package/baz.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/astroid/tests/testdata/python3/data/path_pkgutil_1/package/__init__.py b/astroid/tests/testdata/python3/data/path_pkgutil_1/package/__init__.py deleted file mode 100644 index 0bfb5a62b4..0000000000 --- a/astroid/tests/testdata/python3/data/path_pkgutil_1/package/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -from pkgutil import extend_path -__path__ = extend_path(__path__, __name__) \ No newline at end of file diff --git a/astroid/tests/testdata/python3/data/path_pkgutil_1/package/foo.py b/astroid/tests/testdata/python3/data/path_pkgutil_1/package/foo.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/astroid/tests/testdata/python3/data/path_pkgutil_2/package/__init__.py b/astroid/tests/testdata/python3/data/path_pkgutil_2/package/__init__.py deleted file mode 100644 index 0bfb5a62b4..0000000000 --- a/astroid/tests/testdata/python3/data/path_pkgutil_2/package/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -from pkgutil import extend_path -__path__ = extend_path(__path__, __name__) \ No newline at end of file diff --git a/astroid/tests/testdata/python3/data/path_pkgutil_2/package/bar.py b/astroid/tests/testdata/python3/data/path_pkgutil_2/package/bar.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/astroid/tests/testdata/python3/data/path_pkgutil_3/package/__init__.py b/astroid/tests/testdata/python3/data/path_pkgutil_3/package/__init__.py deleted file mode 100644 index 0bfb5a62b4..0000000000 --- a/astroid/tests/testdata/python3/data/path_pkgutil_3/package/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -from pkgutil import extend_path -__path__ = extend_path(__path__, __name__) \ No newline at end of file diff --git a/astroid/tests/testdata/python3/data/path_pkgutil_3/package/baz.py b/astroid/tests/testdata/python3/data/path_pkgutil_3/package/baz.py deleted file mode 100644 index e69de29bb2..0000000000 From c6563e8664fa0c94496907ad64f6f6c07ee0b63a Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 10 May 2016 11:56:46 +0300 Subject: [PATCH 247/312] Don't look for namespace packages implicitly into functions doing concrete imports. --- astroid/modutils.py | 47 +++++++++++++++++------------- astroid/tests/unittest_modutils.py | 14 --------- 2 files changed, 26 insertions(+), 35 deletions(-) diff --git a/astroid/modutils.py b/astroid/modutils.py index aa62f564bf..c6ff938556 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -292,7 +292,7 @@ def load_module_from_file(filepath, path=None, use_sys=True, extrapath=None): return load_module_from_modpath(modpath, path, use_sys) -def _check_init(path, mod_path): +def check_modpath_has_init(path, mod_path): """check there are some __init__.py all along the way""" modpath = [] for part in mod_path: @@ -300,11 +300,34 @@ def _check_init(path, mod_path): path = os.path.join(path, part) if not _has_init(path): old_namespace = _is_namespace('.'.join(modpath)) - if not (old_namespace or sys.version_info[:2] > (3, 3)): + if not old_namespace: return False return True +def modpath_from_file_with_callback(filename, extrapath=None, is_package_cb=None): + filename = _path_from_filename(filename) + filename = os.path.abspath(filename) + base = os.path.splitext(filename)[0] + if extrapath is not None: + for path_ in extrapath: + path = os.path.abspath(path_) + if path and os.path.normcase(base[:len(path)]) == os.path.normcase(path): + submodpath = [pkg for pkg in base[len(path):].split(os.sep) + if pkg] + if is_package_cb(path, submodpath[:-1]): + return extrapath[path_].split('.') + submodpath + for path in sys.path: + path = _cache_normalize_path(path) + if path and os.path.normcase(base).startswith(path): + modpath = [pkg for pkg in base[len(path):].split(os.sep) if pkg] + if is_package_cb(path, modpath[:-1]): + return modpath + raise ImportError('Unable to find module for %s in %s' % ( + filename, ', \n'.join(sys.path))) + + + def modpath_from_file(filename, extrapath=None): """given a file path return the corresponding splitted module's name (i.e name of a module or package splitted on '.') @@ -325,25 +348,7 @@ def modpath_from_file(filename, extrapath=None): :rtype: list(str) :return: the corresponding splitted module's name """ - filename = _path_from_filename(filename) - filename = os.path.abspath(filename) - base = os.path.splitext(filename)[0] - if extrapath is not None: - for path_ in extrapath: - path = os.path.abspath(path_) - if path and os.path.normcase(base[:len(path)]) == os.path.normcase(path): - submodpath = [pkg for pkg in base[len(path):].split(os.sep) - if pkg] - if _check_init(path, submodpath[:-1]): - return extrapath[path_].split('.') + submodpath - for path in sys.path: - path = _cache_normalize_path(path) - if path and os.path.normcase(base).startswith(path): - modpath = [pkg for pkg in base[len(path):].split(os.sep) if pkg] - if _check_init(path, modpath[:-1]): - return modpath - raise ImportError('Unable to find module for %s in %s' % ( - filename, ', \n'.join(sys.path))) + return modpath_from_file_with_callback(filename, extrapath, check_modpath_has_init) def file_from_modpath(modpath, path=None, context_file=None): diff --git a/astroid/tests/unittest_modutils.py b/astroid/tests/unittest_modutils.py index 05a7e9d553..47c418bbab 100644 --- a/astroid/tests/unittest_modutils.py +++ b/astroid/tests/unittest_modutils.py @@ -110,20 +110,6 @@ def test_knownValues_modpath_from_file_2(self): def test_raise_modpath_from_file_Exception(self): self.assertRaises(Exception, modutils.modpath_from_file, '/turlututu') - @unittest.skipUnless(sys.version_info[:2] > (3, 3), "Needs Python 3.3+ namespace package.") - def test_modpath_from_file_namespace(self): - datadir = os.path.abspath(resources.find('data')) - path = os.path.abspath( - os.path.join(resources.find('data/namespace_pep_420'), 'module.py') - ) - - sys.path.insert(0, datadir) - try: - modpath = modutils.modpath_from_file(path) - finally: - sys.path.pop(0) - self.assertEqual(modpath, ['namespace_pep_420', 'module']) - class LoadModuleFromPathTest(resources.SysPathSetup, unittest.TestCase): From e16db7fc2d007c2310c5db8e61057c94624a6d8f Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Mon, 16 May 2016 18:56:21 -0400 Subject: [PATCH 248/312] Convert all files to new license header --- astroid/__init__.py | 20 +- astroid/__pkginfo__.py | 20 +- astroid/as_string.py | 20 +- astroid/brain/brain_builtin_inference.py | 3 + astroid/brain/brain_dateutil.py | 33 +- astroid/brain/brain_functools.py | 3 + astroid/brain/brain_gi.py | 3 + astroid/brain/brain_mechanize.py | 3 + astroid/brain/brain_nose.py | 20 +- astroid/brain/brain_numpy.py | 19 +- astroid/brain/brain_pkg_resources.py | 19 +- astroid/brain/brain_pytest.py | 65 +- astroid/brain/brain_qt.py | 3 + astroid/brain/brain_six.py | 19 +- astroid/brain/brain_ssl.py | 3 + astroid/brain/brain_stdlib.py | 3 + astroid/builder.py | 20 +- astroid/context.py | 20 +- astroid/decorators.py | 20 +- astroid/exceptions.py | 20 +- astroid/inference.py | 20 +- astroid/interpreter/__init__.py | 3 + astroid/interpreter/assign.py | 20 +- astroid/interpreter/lookup.py | 20 +- astroid/interpreter/objectmodel.py | 20 +- astroid/interpreter/objects.py | 20 +- astroid/interpreter/runtimeabc.py | 20 +- astroid/interpreter/scope.py | 223 ++--- astroid/interpreter/util.py | 20 +- astroid/manager.py | 20 +- astroid/modutils.py | 20 +- astroid/nodes.py | 20 +- astroid/protocols.py | 20 +- astroid/raw_building.py | 20 +- astroid/test_utils.py | 3 + astroid/tests/__init__.py | 3 + astroid/tests/resources.py | 20 +- astroid/tests/unittest_brain.py | 19 +- astroid/tests/unittest_builder.py | 20 +- astroid/tests/unittest_helpers.py | 20 +- astroid/tests/unittest_inference.py | 20 +- astroid/tests/unittest_lookup.py | 20 +- astroid/tests/unittest_manager.py | 20 +- astroid/tests/unittest_modutils.py | 20 +- astroid/tests/unittest_nodes.py | 20 +- astroid/tests/unittest_object_model.py | 20 +- astroid/tests/unittest_objects.py | 1146 +++++++++++----------- astroid/tests/unittest_protocols.py | 20 +- astroid/tests/unittest_python3.py | 20 +- astroid/tests/unittest_raw_building.py | 3 + astroid/tests/unittest_regrtest.py | 20 +- astroid/tests/unittest_scoped_nodes.py | 20 +- astroid/tests/unittest_transforms.py | 476 +++++---- astroid/tests/unittest_utils.py | 20 +- astroid/transforms.py | 20 +- astroid/tree/__init__.py | 3 + astroid/tree/base.py | 20 +- astroid/tree/node_classes.py | 20 +- astroid/tree/rebuilder.py | 20 +- astroid/tree/scoped_nodes.py | 20 +- astroid/tree/treeabc.py | 20 +- astroid/util.py | 20 +- 62 files changed, 1121 insertions(+), 1754 deletions(-) diff --git a/astroid/__init__.py b/astroid/__init__.py index 6afecf2553..72f2c78a79 100644 --- a/astroid/__init__.py +++ b/astroid/__init__.py @@ -1,20 +1,6 @@ -# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + """Python Abstract Syntax Tree New Generation The aim of this module is to provide a common base representation of diff --git a/astroid/__pkginfo__.py b/astroid/__pkginfo__.py index ee4712d713..df41ce7d34 100644 --- a/astroid/__pkginfo__.py +++ b/astroid/__pkginfo__.py @@ -1,20 +1,6 @@ -# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + """astroid packaging information""" import sys diff --git a/astroid/as_string.py b/astroid/as_string.py index 800617e79c..652720a7bd 100644 --- a/astroid/as_string.py +++ b/astroid/as_string.py @@ -1,20 +1,6 @@ -# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + """This module renders Astroid nodes as string: * :func:`to_code` function return equivalent (hopefuly valid) python string diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index ef0866c552..96acbd39be 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -1,3 +1,6 @@ +# 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 + """Astroid hooks for various builtins.""" import collections diff --git a/astroid/brain/brain_dateutil.py b/astroid/brain/brain_dateutil.py index 0b45412c48..2c385ecc5e 100644 --- a/astroid/brain/brain_dateutil.py +++ b/astroid/brain/brain_dateutil.py @@ -1,15 +1,18 @@ -"""Astroid hooks for dateutil""" - -import textwrap - -from astroid import MANAGER, register_module_extender -from astroid.builder import AstroidBuilder - -def dateutil_transform(): - return AstroidBuilder(MANAGER).string_build(textwrap.dedent(''' - import datetime - def parse(timestr, parserinfo=None, **kwargs): - return datetime.datetime() - ''')) - -register_module_extender(MANAGER, 'dateutil.parser', dateutil_transform) +# 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 + +"""Astroid hooks for dateutil""" + +import textwrap + +from astroid import MANAGER, register_module_extender +from astroid.builder import AstroidBuilder + +def dateutil_transform(): + return AstroidBuilder(MANAGER).string_build(textwrap.dedent(''' + import datetime + def parse(timestr, parserinfo=None, **kwargs): + return datetime.datetime() + ''')) + +register_module_extender(MANAGER, 'dateutil.parser', dateutil_transform) diff --git a/astroid/brain/brain_functools.py b/astroid/brain/brain_functools.py index 7fff071bbe..b80ec1cae4 100644 --- a/astroid/brain/brain_functools.py +++ b/astroid/brain/brain_functools.py @@ -1,3 +1,6 @@ +# 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 + """Astroid hooks for understanding functools library module.""" import astroid diff --git a/astroid/brain/brain_gi.py b/astroid/brain/brain_gi.py index 62b013c3ae..0e42330797 100644 --- a/astroid/brain/brain_gi.py +++ b/astroid/brain/brain_gi.py @@ -1,3 +1,6 @@ +# 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 + """Astroid hooks for the Python 2 GObject introspection bindings. Helps with understanding everything imported from 'gi.repository' diff --git a/astroid/brain/brain_mechanize.py b/astroid/brain/brain_mechanize.py index 20a253a458..2026d3cdb5 100644 --- a/astroid/brain/brain_mechanize.py +++ b/astroid/brain/brain_mechanize.py @@ -1,3 +1,6 @@ +# 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 + from astroid import MANAGER, register_module_extender from astroid.builder import AstroidBuilder diff --git a/astroid/brain/brain_nose.py b/astroid/brain/brain_nose.py index 80d6563da2..b9adb2201f 100644 --- a/astroid/brain/brain_nose.py +++ b/astroid/brain/brain_nose.py @@ -1,20 +1,6 @@ -# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + """Hooks for nose library.""" diff --git a/astroid/brain/brain_numpy.py b/astroid/brain/brain_numpy.py index 25647ee9f3..493a0dcfbd 100644 --- a/astroid/brain/brain_numpy.py +++ b/astroid/brain/brain_numpy.py @@ -1,20 +1,5 @@ -# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it under -# the terms of the GNU Lesser General Public License as published by the Free -# Software Foundation, either version 2.1 of the License, or (at your option) any -# later version. -# -# astroid is distributed in the hope that it will be useful, but WITHOUT -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more -# details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 """Astroid hooks for numpy.""" diff --git a/astroid/brain/brain_pkg_resources.py b/astroid/brain/brain_pkg_resources.py index 7777fb71e3..dbbc88850d 100644 --- a/astroid/brain/brain_pkg_resources.py +++ b/astroid/brain/brain_pkg_resources.py @@ -1,20 +1,5 @@ -# copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it under -# the terms of the GNU Lesser General Public License as published by the Free -# Software Foundation, either version 2.1 of the License, or (at your option) any -# later version. -# -# astroid is distributed in the hope that it will be useful, but WITHOUT -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more -# details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 import astroid from astroid import parse diff --git a/astroid/brain/brain_pytest.py b/astroid/brain/brain_pytest.py index 4f615c14d1..f20605f008 100644 --- a/astroid/brain/brain_pytest.py +++ b/astroid/brain/brain_pytest.py @@ -1,31 +1,34 @@ -"""Astroid hooks for pytest.""" -from __future__ import absolute_import -from astroid import MANAGER, register_module_extender -from astroid.builder import AstroidBuilder - - -def pytest_transform(): - return AstroidBuilder(MANAGER).string_build(''' - -try: - import _pytest.mark - import _pytest.recwarn - import _pytest.runner - import _pytest.python -except ImportError: - pass -else: - deprecated_call = _pytest.recwarn.deprecated_call - exit = _pytest.runner.exit - fail = _pytest.runner.fail - fixture = _pytest.python.fixture - importorskip = _pytest.runner.importorskip - mark = _pytest.mark.MarkGenerator() - raises = _pytest.python.raises - skip = _pytest.runner.skip - yield_fixture = _pytest.python.yield_fixture - -''') - -register_module_extender(MANAGER, 'pytest', pytest_transform) -register_module_extender(MANAGER, 'py.test', pytest_transform) +# 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 + +"""Astroid hooks for pytest.""" +from __future__ import absolute_import +from astroid import MANAGER, register_module_extender +from astroid.builder import AstroidBuilder + + +def pytest_transform(): + return AstroidBuilder(MANAGER).string_build(''' + +try: + import _pytest.mark + import _pytest.recwarn + import _pytest.runner + import _pytest.python +except ImportError: + pass +else: + deprecated_call = _pytest.recwarn.deprecated_call + exit = _pytest.runner.exit + fail = _pytest.runner.fail + fixture = _pytest.python.fixture + importorskip = _pytest.runner.importorskip + mark = _pytest.mark.MarkGenerator() + raises = _pytest.python.raises + skip = _pytest.runner.skip + yield_fixture = _pytest.python.yield_fixture + +''') + +register_module_extender(MANAGER, 'pytest', pytest_transform) +register_module_extender(MANAGER, 'py.test', pytest_transform) diff --git a/astroid/brain/brain_qt.py b/astroid/brain/brain_qt.py index f568b65f80..30d8de752c 100644 --- a/astroid/brain/brain_qt.py +++ b/astroid/brain/brain_qt.py @@ -1,3 +1,6 @@ +# 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 + """Astroid hooks for the PyQT library.""" from astroid import MANAGER, register_module_extender diff --git a/astroid/brain/brain_six.py b/astroid/brain/brain_six.py index d2b0510f31..2130807891 100644 --- a/astroid/brain/brain_six.py +++ b/astroid/brain/brain_six.py @@ -1,20 +1,5 @@ -# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it under -# the terms of the GNU Lesser General Public License as published by the Free -# Software Foundation, either version 2.1 of the License, or (at your option) any -# later version. -# -# astroid is distributed in the hope that it will be useful, but WITHOUT -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more -# details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 """Astroid hooks for six module.""" diff --git a/astroid/brain/brain_ssl.py b/astroid/brain/brain_ssl.py index 1cf8d1b8f1..5f33d014b2 100644 --- a/astroid/brain/brain_ssl.py +++ b/astroid/brain/brain_ssl.py @@ -1,3 +1,6 @@ +# 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 + """Astroid hooks for the ssl library.""" from astroid import MANAGER, register_module_extender diff --git a/astroid/brain/brain_stdlib.py b/astroid/brain/brain_stdlib.py index f3a6a71622..33dfcd83d6 100644 --- a/astroid/brain/brain_stdlib.py +++ b/astroid/brain/brain_stdlib.py @@ -1,3 +1,6 @@ +# 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 + """Astroid hooks for the Python standard library.""" # Namedtuple template strings diff --git a/astroid/builder.py b/astroid/builder.py index 0c05f8cfd3..bd941c53f6 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -1,20 +1,6 @@ -# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + """The AstroidBuilder makes astroid from living object and / or from _ast The builder is not thread safe and can't be used to parse different sources diff --git a/astroid/context.py b/astroid/context.py index 6497e7d8c4..95f1d9f5a6 100644 --- a/astroid/context.py +++ b/astroid/context.py @@ -1,20 +1,6 @@ -# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + """Various context related utilities, including inference and call contexts.""" import contextlib diff --git a/astroid/decorators.py b/astroid/decorators.py index 826b7b2570..20bb5b7d43 100644 --- a/astroid/decorators.py +++ b/astroid/decorators.py @@ -1,20 +1,6 @@ -# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + # # The code in this file was originally part of logilab-common, licensed under # the same license. diff --git a/astroid/exceptions.py b/astroid/exceptions.py index 21e9b61f6c..3e57100603 100644 --- a/astroid/exceptions.py +++ b/astroid/exceptions.py @@ -1,20 +1,6 @@ -# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + """this module contains exceptions used in the astroid library """ from astroid import util diff --git a/astroid/inference.py b/astroid/inference.py index 48de4341c2..c32c3be1c7 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -1,20 +1,6 @@ -# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + """this module contains a set of functions to handle inference on astroid trees """ diff --git a/astroid/interpreter/__init__.py b/astroid/interpreter/__init__.py index e69de29bb2..7a8e4fceb2 100644 --- a/astroid/interpreter/__init__.py +++ b/astroid/interpreter/__init__.py @@ -0,0 +1,3 @@ +# 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 + diff --git a/astroid/interpreter/assign.py b/astroid/interpreter/assign.py index 4b9fb1e82a..5b00afb859 100644 --- a/astroid/interpreter/assign.py +++ b/astroid/interpreter/assign.py @@ -1,20 +1,6 @@ -# copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + from astroid import exceptions from astroid.interpreter import runtimeabc diff --git a/astroid/interpreter/lookup.py b/astroid/interpreter/lookup.py index 441d62f7a1..53f59b7d10 100644 --- a/astroid/interpreter/lookup.py +++ b/astroid/interpreter/lookup.py @@ -1,20 +1,6 @@ -# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + import collections diff --git a/astroid/interpreter/objectmodel.py b/astroid/interpreter/objectmodel.py index 186be464f3..9ab06e4a2a 100644 --- a/astroid/interpreter/objectmodel.py +++ b/astroid/interpreter/objectmodel.py @@ -1,20 +1,6 @@ -# copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + # # The code in this file was originally part of logilab-common, licensed under # the same license. diff --git a/astroid/interpreter/objects.py b/astroid/interpreter/objects.py index a919649182..6b8b02e2a0 100644 --- a/astroid/interpreter/objects.py +++ b/astroid/interpreter/objects.py @@ -1,20 +1,6 @@ -# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + """ Inference objects are a way to represent objects which are available diff --git a/astroid/interpreter/runtimeabc.py b/astroid/interpreter/runtimeabc.py index 8dec108985..04ea153f2b 100644 --- a/astroid/interpreter/runtimeabc.py +++ b/astroid/interpreter/runtimeabc.py @@ -1,20 +1,6 @@ -# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + import abc diff --git a/astroid/interpreter/scope.py b/astroid/interpreter/scope.py index 47fb3a0cd5..6314dcc78d 100644 --- a/astroid/interpreter/scope.py +++ b/astroid/interpreter/scope.py @@ -1,119 +1,104 @@ -# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with astroid. If not, see . - -"""Implements logic for determing the scope of a node.""" - -import itertools - -import six - -from astroid.tree import treeabc -from astroid import util - - -# import pdb; pdb.set_trace() - -@util.singledispatch -def _scope_by_parent(parent, node): - """Detect the scope of the *node* by parent's rules. - - The scope for certain kind of nodes depends on the - parent, as it is the case for default values of arguments - and function annotation, where the scope is not the scope of - the parent, but the parent scope of the parent. - """ - # This is separated in multiple dispatch methods on parents, - # in order to decouple the implementation for the normal cases. - - -def _node_arguments(node): - for arg in itertools.chain(node.positional_and_keyword, node.keyword_only, - (node.vararg, ), (node.kwarg, )): - if arg and arg.annotation: - yield arg - - -@_scope_by_parent.register(treeabc.Arguments) -def _scope_by_argument_parent(parent, node): - args = parent - for param in itertools.chain(args.positional_and_keyword, args.keyword_only): - if param.default == node: - return args.parent.parent.scope() - - if six.PY3 and node in _node_arguments(args): - return args.parent.parent.scope() - - -@_scope_by_parent.register(treeabc.FunctionDef) -def _scope_by_function_parent(parent, node): - # Verify if the node is the return annotation of a function, - # in which case the scope is the parent scope of the function. - if six.PY3 and node is parent.returns: - return parent.parent.scope() - - -@_scope_by_parent.register(treeabc.Comprehension) -def _scope_by_comprehension_parent(parent, node): - # Get the scope of a node which has a comprehension - # as a parent. The rules are a bit hairy, but in essence - # it is simple enough: list comprehensions leaks variables - # on Python 2, so they have the parent scope of the list comprehension - # itself. The iter part of the comprehension has almost always - # another scope than the comprehension itself, but only for the - # first generator (the outer one). Other comprehensions don't leak - # variables on Python 2 and 3. - - comprehension = parent_scope = parent.parent - generators = comprehension.generators - - # The first outer generator always has a different scope - first_iter = generators[0].iter - if node is first_iter: - return parent_scope.parent.scope() - - # This might not be correct for all the cases, but it - # should be enough for most of them. - if six.PY2 and isinstance(parent_scope, treeabc.ListComp): - return parent_scope.parent.scope() - return parent.scope() - - -@util.singledispatch -def node_scope(node): - """Get the scope of the given node.""" - scope = _scope_by_parent(node.parent, node) - return scope or node.parent.scope() - - -@node_scope.register(treeabc.Decorators) -def _decorators_scope(node): - return node.parent.parent.scope() - - -@node_scope.register(treeabc.Module) -@node_scope.register(treeabc.GeneratorExp) -@node_scope.register(treeabc.DictComp) -@node_scope.register(treeabc.SetComp) -@node_scope.register(treeabc.Lambda) -@node_scope.register(treeabc.FunctionDef) -@node_scope.register(treeabc.ClassDef) -def _scoped_nodes(node): - return node - -if six.PY3: - node_scope.register(treeabc.ListComp, _scoped_nodes) +# 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 + +"""Implements logic for determing the scope of a node.""" + +import itertools + +import six + +from astroid.tree import treeabc +from astroid import util + + +# import pdb; pdb.set_trace() + +@util.singledispatch +def _scope_by_parent(parent, node): + """Detect the scope of the *node* by parent's rules. + + The scope for certain kind of nodes depends on the + parent, as it is the case for default values of arguments + and function annotation, where the scope is not the scope of + the parent, but the parent scope of the parent. + """ + # This is separated in multiple dispatch methods on parents, + # in order to decouple the implementation for the normal cases. + + +def _node_arguments(node): + for arg in itertools.chain(node.positional_and_keyword, node.keyword_only, + (node.vararg, ), (node.kwarg, )): + if arg and arg.annotation: + yield arg + + +@_scope_by_parent.register(treeabc.Arguments) +def _scope_by_argument_parent(parent, node): + args = parent + for param in itertools.chain(args.positional_and_keyword, args.keyword_only): + if param.default == node: + return args.parent.parent.scope() + + if six.PY3 and node in _node_arguments(args): + return args.parent.parent.scope() + + +@_scope_by_parent.register(treeabc.FunctionDef) +def _scope_by_function_parent(parent, node): + # Verify if the node is the return annotation of a function, + # in which case the scope is the parent scope of the function. + if six.PY3 and node is parent.returns: + return parent.parent.scope() + + +@_scope_by_parent.register(treeabc.Comprehension) +def _scope_by_comprehension_parent(parent, node): + # Get the scope of a node which has a comprehension + # as a parent. The rules are a bit hairy, but in essence + # it is simple enough: list comprehensions leaks variables + # on Python 2, so they have the parent scope of the list comprehension + # itself. The iter part of the comprehension has almost always + # another scope than the comprehension itself, but only for the + # first generator (the outer one). Other comprehensions don't leak + # variables on Python 2 and 3. + + comprehension = parent_scope = parent.parent + generators = comprehension.generators + + # The first outer generator always has a different scope + first_iter = generators[0].iter + if node is first_iter: + return parent_scope.parent.scope() + + # This might not be correct for all the cases, but it + # should be enough for most of them. + if six.PY2 and isinstance(parent_scope, treeabc.ListComp): + return parent_scope.parent.scope() + return parent.scope() + + +@util.singledispatch +def node_scope(node): + """Get the scope of the given node.""" + scope = _scope_by_parent(node.parent, node) + return scope or node.parent.scope() + + +@node_scope.register(treeabc.Decorators) +def _decorators_scope(node): + return node.parent.parent.scope() + + +@node_scope.register(treeabc.Module) +@node_scope.register(treeabc.GeneratorExp) +@node_scope.register(treeabc.DictComp) +@node_scope.register(treeabc.SetComp) +@node_scope.register(treeabc.Lambda) +@node_scope.register(treeabc.FunctionDef) +@node_scope.register(treeabc.ClassDef) +def _scoped_nodes(node): + return node + +if six.PY3: + node_scope.register(treeabc.ListComp, _scoped_nodes) diff --git a/astroid/interpreter/util.py b/astroid/interpreter/util.py index 207cd1439e..d5ba51de3f 100644 --- a/astroid/interpreter/util.py +++ b/astroid/interpreter/util.py @@ -1,20 +1,6 @@ -# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + """Utilities for inference.""" import types diff --git a/astroid/manager.py b/astroid/manager.py index 73dd961a36..24f353c833 100644 --- a/astroid/manager.py +++ b/astroid/manager.py @@ -1,20 +1,6 @@ -# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + """astroid manager: avoid multiple astroid build of a same module when possible by providing a class responsible to get astroid representation from various source and using a cache of built modules) diff --git a/astroid/modutils.py b/astroid/modutils.py index c6ff938556..da47d4c60d 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -1,20 +1,6 @@ -# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it under -# the terms of the GNU Lesser General Public License as published by the Free -# Software Foundation, either version 2.1 of the License, or (at your option) any -# later version. -# -# astroid is distributed in the hope that it will be useful, but WITHOUT -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more -# details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + """Python modules manipulation utility functions. :type PY_SOURCE_EXTS: tuple(str) diff --git a/astroid/nodes.py b/astroid/nodes.py index 6fb3765282..4fd5f4e705 100644 --- a/astroid/nodes.py +++ b/astroid/nodes.py @@ -1,20 +1,6 @@ -# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + """ on all nodes : .is_statement, returning true if the node should be considered as a diff --git a/astroid/protocols.py b/astroid/protocols.py index 47741e33eb..da10b496c6 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -1,20 +1,6 @@ -# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + """this module contains a set of functions to handle python protocols for nodes where it makes sense. """ diff --git a/astroid/raw_building.py b/astroid/raw_building.py index f16bd6df23..eb81cfb896 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -1,20 +1,6 @@ -# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + """this module contains a set of functions to create astroid trees from scratch (build_* functions) or from living object (object_build_* functions) """ diff --git a/astroid/test_utils.py b/astroid/test_utils.py index ff81cd59f8..8dd5bf4e17 100644 --- a/astroid/test_utils.py +++ b/astroid/test_utils.py @@ -1,3 +1,6 @@ +# 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 + """Utility functions for test code that uses astroid ASTs as input.""" import contextlib import functools diff --git a/astroid/tests/__init__.py b/astroid/tests/__init__.py index e69de29bb2..7a8e4fceb2 100644 --- a/astroid/tests/__init__.py +++ b/astroid/tests/__init__.py @@ -0,0 +1,3 @@ +# 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 + diff --git a/astroid/tests/resources.py b/astroid/tests/resources.py index 1390088cd3..461c8df18b 100644 --- a/astroid/tests/resources.py +++ b/astroid/tests/resources.py @@ -1,20 +1,6 @@ -# Copyright 2014 Google, Inc. All rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + import binascii import contextlib import os diff --git a/astroid/tests/unittest_brain.py b/astroid/tests/unittest_brain.py index dd3e413e7d..766dc56e03 100644 --- a/astroid/tests/unittest_brain.py +++ b/astroid/tests/unittest_brain.py @@ -1,19 +1,6 @@ -# Copyright 2013 Google Inc. All Rights Reserved. -# -# This file is part of astroid. -# -# logilab-astng is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# logilab-astng is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with logilab-astng. If not, see . +# 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 + """Tests for basic functionality in astroid.brain.""" import sys import unittest diff --git a/astroid/tests/unittest_builder.py b/astroid/tests/unittest_builder.py index 813c2e3dbd..dcb4e57c94 100644 --- a/astroid/tests/unittest_builder.py +++ b/astroid/tests/unittest_builder.py @@ -1,20 +1,6 @@ -# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + """tests for the astroid builder and rebuilder module""" import os diff --git a/astroid/tests/unittest_helpers.py b/astroid/tests/unittest_helpers.py index 6be6512a5a..efb25f0ddb 100644 --- a/astroid/tests/unittest_helpers.py +++ b/astroid/tests/unittest_helpers.py @@ -1,20 +1,6 @@ -# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + import types import unittest diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index 7c8a58ea64..5b05a3f66f 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -1,20 +1,6 @@ -# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + """tests for the astroid inference capabilities """ import os diff --git a/astroid/tests/unittest_lookup.py b/astroid/tests/unittest_lookup.py index 2bdf569118..a376e3be20 100644 --- a/astroid/tests/unittest_lookup.py +++ b/astroid/tests/unittest_lookup.py @@ -1,20 +1,6 @@ -# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + """tests for the astroid variable lookup capabilities """ import functools diff --git a/astroid/tests/unittest_manager.py b/astroid/tests/unittest_manager.py index 1b83862c2b..2cb64b9dc7 100644 --- a/astroid/tests/unittest_manager.py +++ b/astroid/tests/unittest_manager.py @@ -1,20 +1,6 @@ -# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + import os import platform import sys diff --git a/astroid/tests/unittest_modutils.py b/astroid/tests/unittest_modutils.py index 47c418bbab..2b32422911 100644 --- a/astroid/tests/unittest_modutils.py +++ b/astroid/tests/unittest_modutils.py @@ -1,20 +1,6 @@ -# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it under -# the terms of the GNU Lesser General Public License as published by the Free -# Software Foundation, either version 2.1 of the License, or (at your option) any -# later version. -# -# astroid is distributed in the hope that it will be useful, but WITHOUT -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more -# details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + """ unit tests for module modutils (module manipulation utilities) """ diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index 419df96842..4a3a314600 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -1,20 +1,6 @@ -# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + """tests for specific behaviour of astroid nodes """ import os diff --git a/astroid/tests/unittest_object_model.py b/astroid/tests/unittest_object_model.py index 29a867811d..03590fe564 100644 --- a/astroid/tests/unittest_object_model.py +++ b/astroid/tests/unittest_object_model.py @@ -1,20 +1,6 @@ -# copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + # # The code in this file was originally part of logilab-common, licensed under # the same license. diff --git a/astroid/tests/unittest_objects.py b/astroid/tests/unittest_objects.py index afcf76d305..f5c70c0fd2 100644 --- a/astroid/tests/unittest_objects.py +++ b/astroid/tests/unittest_objects.py @@ -1,580 +1,566 @@ -# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . - -import unittest - -import six - -from astroid import exceptions -from astroid import nodes -from astroid.interpreter import objects -from astroid import test_utils - - -BUILTINS = six.moves.builtins.__name__ - - -class ObjectsTest(unittest.TestCase): - - def test_frozenset(self): - node = test_utils.extract_node(""" - frozenset({1: 2, 2: 3}) #@ - """) - inferred = next(node.infer()) - self.assertIsInstance(inferred, objects.FrozenSet) - - self.assertEqual(inferred.pytype(), "%s.frozenset" % BUILTINS) - - itered = inferred.itered() - self.assertEqual(len(itered), 2) - self.assertIsInstance(itered[0], nodes.Const) - self.assertEqual([const.value for const in itered], [1, 2]) - - proxied = inferred._proxied - self.assertEqual(inferred.qname(), "%s.frozenset" % BUILTINS) - self.assertIsInstance(proxied, nodes.ClassDef) - - -class SuperTests(unittest.TestCase): - - def test_inferring_super_outside_methods(self): - ast_nodes = test_utils.extract_node(''' - class Module(object): - pass - class StaticMethod(object): - @staticmethod - def static(): - # valid, but we don't bother with it. - return super(StaticMethod, StaticMethod) #@ - # super outside methods aren't inferred - super(Module, Module) #@ - # no argument super is not recognised outside methods as well. - super() #@ - ''') - in_static = next(ast_nodes[0].value.infer()) - self.assertIsInstance(in_static, objects.Instance) - self.assertEqual(in_static.qname(), "%s.super" % BUILTINS) - - module_level = next(ast_nodes[1].infer()) - self.assertIsInstance(module_level, objects.Instance) - self.assertEqual(in_static.qname(), "%s.super" % BUILTINS) - - no_arguments = next(ast_nodes[2].infer()) - self.assertIsInstance(no_arguments, objects.Instance) - self.assertEqual(no_arguments.qname(), "%s.super" % BUILTINS) - - def test_inferring_unbound_super_doesnt_work(self): - node = test_utils.extract_node(''' - class Test(object): - def __init__(self): - super(Test) #@ - ''') - unbounded = next(node.infer()) - self.assertIsInstance(unbounded, objects.Instance) - self.assertEqual(unbounded.qname(), "%s.super" % BUILTINS) - - def test_use_default_inference_on_not_inferring_args(self): - ast_nodes = test_utils.extract_node(''' - class Test(object): - def __init__(self): - super(Lala, self) #@ - super(Test, lala) #@ - ''') - first = next(ast_nodes[0].infer()) - self.assertIsInstance(first, objects.Instance) - self.assertEqual(first.qname(), "%s.super" % BUILTINS) - - second = next(ast_nodes[1].infer()) - self.assertIsInstance(second, objects.Instance) - self.assertEqual(second.qname(), "%s.super" % BUILTINS) - - @test_utils.require_version(maxver='3.0') - def test_super_on_old_style_class(self): - # super doesn't work on old style class, but leave - # that as an error for pylint. We'll infer Super objects, - # but every call will result in a failure at some point. - node = test_utils.extract_node(''' - class OldStyle: - def __init__(self): - super(OldStyle, self) #@ - ''') - old = next(node.infer()) - self.assertIsInstance(old, objects.Super) - self.assertIsInstance(old.mro_pointer, nodes.ClassDef) - self.assertEqual(old.mro_pointer.name, 'OldStyle') - with self.assertRaises(exceptions.SuperError) as cm: - old.super_mro() - self.assertEqual(str(cm.exception), - "Unable to call super on old-style classes.") - - @test_utils.require_version(minver='3.0') - def test_no_arguments_super(self): - ast_nodes = test_utils.extract_node(''' - class First(object): pass - class Second(First): - def test(self): - super() #@ - @classmethod - def test_classmethod(cls): - super() #@ - ''') - first = next(ast_nodes[0].infer()) - self.assertIsInstance(first, objects.Super) - self.assertIsInstance(first.type, objects.Instance) - self.assertEqual(first.type.name, 'Second') - self.assertIsInstance(first.mro_pointer, nodes.ClassDef) - self.assertEqual(first.mro_pointer.name, 'Second') - - second = next(ast_nodes[1].infer()) - self.assertIsInstance(second, objects.Super) - self.assertIsInstance(second.type, nodes.ClassDef) - self.assertEqual(second.type.name, 'Second') - self.assertIsInstance(second.mro_pointer, nodes.ClassDef) - self.assertEqual(second.mro_pointer.name, 'Second') - - def test_super_simple_cases(self): - ast_nodes = test_utils.extract_node(''' - class First(object): pass - class Second(First): pass - class Third(First): - def test(self): - super(Third, self) #@ - super(Second, self) #@ - - # mro position and the type - super(Third, Third) #@ - super(Third, Second) #@ - super(Fourth, Fourth) #@ - - class Fourth(Third): - pass - ''') - - # .type is the object which provides the mro. - # .mro_pointer is the position in the mro from where - # the lookup should be done. - - # super(Third, self) - first = next(ast_nodes[0].infer()) - self.assertIsInstance(first, objects.Super) - self.assertIsInstance(first.type, objects.Instance) - self.assertEqual(first.type.name, 'Third') - self.assertIsInstance(first.mro_pointer, nodes.ClassDef) - self.assertEqual(first.mro_pointer.name, 'Third') - - # super(Second, self) - second = next(ast_nodes[1].infer()) - self.assertIsInstance(second, objects.Super) - self.assertIsInstance(second.type, objects.Instance) - self.assertEqual(second.type.name, 'Third') - self.assertIsInstance(first.mro_pointer, nodes.ClassDef) - self.assertEqual(second.mro_pointer.name, 'Second') - - # super(Third, Third) - third = next(ast_nodes[2].infer()) - self.assertIsInstance(third, objects.Super) - self.assertIsInstance(third.type, nodes.ClassDef) - self.assertEqual(third.type.name, 'Third') - self.assertIsInstance(third.mro_pointer, nodes.ClassDef) - self.assertEqual(third.mro_pointer.name, 'Third') - - # super(Third, second) - fourth = next(ast_nodes[3].infer()) - self.assertIsInstance(fourth, objects.Super) - self.assertIsInstance(fourth.type, nodes.ClassDef) - self.assertEqual(fourth.type.name, 'Second') - self.assertIsInstance(fourth.mro_pointer, nodes.ClassDef) - self.assertEqual(fourth.mro_pointer.name, 'Third') - - # Super(Fourth, Fourth) - fifth = next(ast_nodes[4].infer()) - self.assertIsInstance(fifth, objects.Super) - self.assertIsInstance(fifth.type, nodes.ClassDef) - self.assertEqual(fifth.type.name, 'Fourth') - self.assertIsInstance(fifth.mro_pointer, nodes.ClassDef) - self.assertEqual(fifth.mro_pointer.name, 'Fourth') - - def test_super_infer(self): - node = test_utils.extract_node(''' - class Super(object): - def __init__(self): - super(Super, self) #@ - ''') - inferred = next(node.infer()) - self.assertIsInstance(inferred, objects.Super) - reinferred = next(inferred.infer()) - self.assertIsInstance(reinferred, objects.Super) - self.assertIs(inferred, reinferred) - - def test_inferring_invalid_supers(self): - ast_nodes = test_utils.extract_node(''' - class Super(object): - def __init__(self): - # MRO pointer is not a type - super(1, self) #@ - # MRO type is not a subtype - super(Super, 1) #@ - # self is not a subtype of Bupper - super(Bupper, self) #@ - class Bupper(Super): - pass - ''') - first = next(ast_nodes[0].infer()) - self.assertIsInstance(first, objects.Super) - with self.assertRaises(exceptions.SuperError) as cm: - first.super_mro() - self.assertIsInstance(cm.exception.super_.mro_pointer, nodes.Const) - self.assertEqual(cm.exception.super_.mro_pointer.value, 1) - for node, invalid_type in zip(ast_nodes[1:], - (nodes.Const, objects.Instance)): - inferred = next(node.infer()) - self.assertIsInstance(inferred, objects.Super, node) - with self.assertRaises(exceptions.SuperError) as cm: - inferred.super_mro() - self.assertIsInstance(cm.exception.super_.type, invalid_type) - - def test_proxied(self): - node = test_utils.extract_node(''' - class Super(object): - def __init__(self): - super(Super, self) #@ - ''') - inferred = next(node.infer()) - proxied = inferred._proxied - self.assertEqual(proxied.qname(), "%s.super" % BUILTINS) - self.assertIsInstance(proxied, nodes.ClassDef) - - def test_super_bound_model(self): - ast_nodes = test_utils.extract_node(''' - class First(object): - def method(self): - pass - @classmethod - def class_method(cls): - pass - class Super_Type_Type(First): - def method(self): - super(Super_Type_Type, Super_Type_Type).method #@ - super(Super_Type_Type, Super_Type_Type).class_method #@ - @classmethod - def class_method(cls): - super(Super_Type_Type, Super_Type_Type).method #@ - super(Super_Type_Type, Super_Type_Type).class_method #@ - - class Super_Type_Object(First): - def method(self): - super(Super_Type_Object, self).method #@ - super(Super_Type_Object, self).class_method #@ - ''') - # Super(type, type) is the same for both functions and classmethods. - first = next(ast_nodes[0].infer()) - self.assertIsInstance(first, nodes.FunctionDef) - self.assertEqual(first.name, 'method') - - second = next(ast_nodes[1].infer()) - self.assertIsInstance(second, objects.BoundMethod) - self.assertEqual(second.bound.name, 'First') - self.assertEqual(second.type, 'classmethod') - - third = next(ast_nodes[2].infer()) - self.assertIsInstance(third, nodes.FunctionDef) - self.assertEqual(third.name, 'method') - - fourth = next(ast_nodes[3].infer()) - self.assertIsInstance(fourth, objects.BoundMethod) - self.assertEqual(fourth.bound.name, 'First') - self.assertEqual(fourth.type, 'classmethod') - - # Super(type, obj) can lead to different attribute bindings - # depending on the type of the place where super was called. - fifth = next(ast_nodes[4].infer()) - self.assertIsInstance(fifth, objects.BoundMethod) - self.assertEqual(fifth.bound.name, 'First') - self.assertEqual(fifth.type, 'method') - - sixth = next(ast_nodes[5].infer()) - self.assertIsInstance(sixth, objects.BoundMethod) - self.assertEqual(sixth.bound.name, 'First') - self.assertEqual(sixth.type, 'classmethod') - - def test_super_getattr_single_inheritance(self): - ast_nodes = test_utils.extract_node(''' - class First(object): - def test(self): pass - class Second(First): - def test2(self): pass - class Third(Second): - test3 = 42 - def __init__(self): - super(Third, self).test2 #@ - super(Third, self).test #@ - # test3 is local, no MRO lookup is done. - super(Third, self).test3 #@ - super(Third, self) #@ - - # Unbounds. - super(Third, Third).test2 #@ - super(Third, Third).test #@ - - ''') - first = next(ast_nodes[0].infer()) - self.assertIsInstance(first, objects.BoundMethod) - self.assertEqual(first.bound.name, 'Second') - - second = next(ast_nodes[1].infer()) - self.assertIsInstance(second, objects.BoundMethod) - self.assertEqual(second.bound.name, 'First') - - with self.assertRaises(exceptions.InferenceError): - next(ast_nodes[2].infer()) - fourth = next(ast_nodes[3].infer()) - with self.assertRaises(exceptions.AttributeInferenceError): - fourth.getattr('test3') - with self.assertRaises(exceptions.AttributeInferenceError): - next(fourth.igetattr('test3')) - - first_unbound = next(ast_nodes[4].infer()) - self.assertIsInstance(first_unbound, nodes.FunctionDef) - self.assertEqual(first_unbound.name, 'test2') - self.assertEqual(first_unbound.parent.name, 'Second') - - second_unbound = next(ast_nodes[5].infer()) - self.assertIsInstance(second_unbound, nodes.FunctionDef) - self.assertEqual(second_unbound.name, 'test') - self.assertEqual(second_unbound.parent.name, 'First') - - def test_super_invalid_mro(self): - node = test_utils.extract_node(''' - class A(object): - test = 42 - class Super(A, A): - def __init__(self): - super(Super, self) #@ - ''') - inferred = next(node.infer()) - with self.assertRaises(exceptions.AttributeInferenceError): - next(inferred.getattr('test')) - - def test_super_complex_mro(self): - ast_nodes = test_utils.extract_node(''' - class A(object): - def spam(self): return "A" - def foo(self): return "A" - @staticmethod - def static(self): pass - class B(A): - def boo(self): return "B" - def spam(self): return "B" - class C(A): - def boo(self): return "C" - class E(C, B): - def __init__(self): - super(E, self).boo #@ - super(C, self).boo #@ - super(E, self).spam #@ - super(E, self).foo #@ - super(E, self).static #@ - ''') - first = next(ast_nodes[0].infer()) - self.assertIsInstance(first, objects.BoundMethod) - self.assertEqual(first.bound.name, 'C') - second = next(ast_nodes[1].infer()) - self.assertIsInstance(second, objects.BoundMethod) - self.assertEqual(second.bound.name, 'B') - third = next(ast_nodes[2].infer()) - self.assertIsInstance(third, objects.BoundMethod) - self.assertEqual(third.bound.name, 'B') - fourth = next(ast_nodes[3].infer()) - self.assertEqual(fourth.bound.name, 'A') - static = next(ast_nodes[4].infer()) - self.assertIsInstance(static, nodes.FunctionDef) - self.assertEqual(static.parent.scope().name, 'A') - - def test_super_data_model(self): - ast_nodes = test_utils.extract_node(''' - class X(object): pass - class A(X): - def __init__(self): - super(A, self) #@ - super(A, A) #@ - super(X, A) #@ - ''') - first = next(ast_nodes[0].infer()) - thisclass = first.getattr('__thisclass__')[0] - self.assertIsInstance(thisclass, nodes.ClassDef) - self.assertEqual(thisclass.name, 'A') - selfclass = first.getattr('__self_class__')[0] - self.assertIsInstance(selfclass, nodes.ClassDef) - self.assertEqual(selfclass.name, 'A') - self_ = first.getattr('__self__')[0] - self.assertIsInstance(self_, objects.Instance) - self.assertEqual(self_.name, 'A') - cls = first.getattr('__class__')[0] - self.assertEqual(cls, first._proxied) - - second = next(ast_nodes[1].infer()) - thisclass = second.getattr('__thisclass__')[0] - self.assertEqual(thisclass.name, 'A') - self_ = second.getattr('__self__')[0] - self.assertIsInstance(self_, nodes.ClassDef) - self.assertEqual(self_.name, 'A') - - third = next(ast_nodes[2].infer()) - thisclass = third.getattr('__thisclass__')[0] - self.assertEqual(thisclass.name, 'X') - selfclass = third.getattr('__self_class__')[0] - self.assertEqual(selfclass.name, 'A') - - def assertEqualMro(self, klass, expected_mro): - self.assertEqual( - [member.name for member in klass.super_mro()], - expected_mro) - - def test_super_mro(self): - ast_nodes = test_utils.extract_node(''' - class A(object): pass - class B(A): pass - class C(A): pass - class E(C, B): - def __init__(self): - super(E, self) #@ - super(C, self) #@ - super(B, self) #@ - - super(B, 1) #@ - super(1, B) #@ - ''') - first = next(ast_nodes[0].infer()) - self.assertEqualMro(first, ['C', 'B', 'A', 'object']) - second = next(ast_nodes[1].infer()) - self.assertEqualMro(second, ['B', 'A', 'object']) - third = next(ast_nodes[2].infer()) - self.assertEqualMro(third, ['A', 'object']) - - fourth = next(ast_nodes[3].infer()) - with self.assertRaises(exceptions.SuperError): - fourth.super_mro() - fifth = next(ast_nodes[4].infer()) - with self.assertRaises(exceptions.SuperError): - fifth.super_mro() - - def test_super_yes_objects(self): - ast_nodes = test_utils.extract_node(''' - from collections import Missing - class A(object): - def __init__(self): - super(Missing, self) #@ - super(A, Missing) #@ - ''') - first = next(ast_nodes[0].infer()) - self.assertIsInstance(first, objects.Instance) - second = next(ast_nodes[1].infer()) - self.assertIsInstance(second, objects.Instance) - - def test_super_invalid_types(self): - node = test_utils.extract_node(''' - import collections - class A(object): - def __init__(self): - super(A, collections) #@ - ''') - inferred = next(node.infer()) - with self.assertRaises(exceptions.SuperError): - inferred.super_mro() - with self.assertRaises(exceptions.SuperError): - inferred.super_mro() - - def test_super_properties(self): - node = test_utils.extract_node(''' - class Foo(object): - @property - def dict(self): - return 42 - - class Bar(Foo): - @property - def dict(self): - return super(Bar, self).dict - - Bar().dict - ''') - inferred = next(node.infer()) - self.assertIsInstance(inferred, nodes.Const) - self.assertEqual(inferred.value, 42) - - -class MethodTest(unittest.TestCase): - - def test_unbound_function_method_difference(self): - node = test_utils.extract_node(''' - class A: - def test(self): pass - A.test - ''') - inferred = next(node.infer()) - if six.PY2: - self.assertIsInstance(inferred, objects.UnboundMethod) - else: - self.assertIsInstance(inferred, nodes.FunctionDef) - - def test_unbound_function_from_classmethods(self): - node = test_utils.extract_node(''' - class A: - @classmethod - def test(cls): return cls.b - def b(self): return self - A.test() - ''') - inferred = next(node.infer()) - if six.PY2: - self.assertIsInstance(inferred, objects.UnboundMethod) - else: - self.assertIsInstance(inferred, nodes.FunctionDef) - - def test_static_method(self): - node = test_utils.extract_node(''' - class A: - @staticmethod - def test(self): pass - A.test - ''') - inferred = next(node.infer()) - self.assertIsInstance(inferred, nodes.FunctionDef) - - @unittest.skipUnless(six.PY2, "Unbound methods are available only on Python 2") - def test_underlying_proxied_unbound_method(self): - node = test_utils.extract_node(''' - class A: - def test(self): pass - A.test - ''') - inferred = next(node.infer()) - self.assertIsInstance(inferred._proxied, nodes.FunctionDef) - - def test_underlying_proxied_bound_method(self): - node = test_utils.extract_node(''' - class A: - def test(self): pass - A().test - ''') - inferred = next(node.infer()) - self.assertIsInstance(inferred._proxied, nodes.FunctionDef) - - -if __name__ == '__main__': - unittest.main() +# 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 + + +import unittest + +import six + +from astroid import exceptions +from astroid import nodes +from astroid.interpreter import objects +from astroid import test_utils + + +BUILTINS = six.moves.builtins.__name__ + + +class ObjectsTest(unittest.TestCase): + + def test_frozenset(self): + node = test_utils.extract_node(""" + frozenset({1: 2, 2: 3}) #@ + """) + inferred = next(node.infer()) + self.assertIsInstance(inferred, objects.FrozenSet) + + self.assertEqual(inferred.pytype(), "%s.frozenset" % BUILTINS) + + itered = inferred.itered() + self.assertEqual(len(itered), 2) + self.assertIsInstance(itered[0], nodes.Const) + self.assertEqual([const.value for const in itered], [1, 2]) + + proxied = inferred._proxied + self.assertEqual(inferred.qname(), "%s.frozenset" % BUILTINS) + self.assertIsInstance(proxied, nodes.ClassDef) + + +class SuperTests(unittest.TestCase): + + def test_inferring_super_outside_methods(self): + ast_nodes = test_utils.extract_node(''' + class Module(object): + pass + class StaticMethod(object): + @staticmethod + def static(): + # valid, but we don't bother with it. + return super(StaticMethod, StaticMethod) #@ + # super outside methods aren't inferred + super(Module, Module) #@ + # no argument super is not recognised outside methods as well. + super() #@ + ''') + in_static = next(ast_nodes[0].value.infer()) + self.assertIsInstance(in_static, objects.Instance) + self.assertEqual(in_static.qname(), "%s.super" % BUILTINS) + + module_level = next(ast_nodes[1].infer()) + self.assertIsInstance(module_level, objects.Instance) + self.assertEqual(in_static.qname(), "%s.super" % BUILTINS) + + no_arguments = next(ast_nodes[2].infer()) + self.assertIsInstance(no_arguments, objects.Instance) + self.assertEqual(no_arguments.qname(), "%s.super" % BUILTINS) + + def test_inferring_unbound_super_doesnt_work(self): + node = test_utils.extract_node(''' + class Test(object): + def __init__(self): + super(Test) #@ + ''') + unbounded = next(node.infer()) + self.assertIsInstance(unbounded, objects.Instance) + self.assertEqual(unbounded.qname(), "%s.super" % BUILTINS) + + def test_use_default_inference_on_not_inferring_args(self): + ast_nodes = test_utils.extract_node(''' + class Test(object): + def __init__(self): + super(Lala, self) #@ + super(Test, lala) #@ + ''') + first = next(ast_nodes[0].infer()) + self.assertIsInstance(first, objects.Instance) + self.assertEqual(first.qname(), "%s.super" % BUILTINS) + + second = next(ast_nodes[1].infer()) + self.assertIsInstance(second, objects.Instance) + self.assertEqual(second.qname(), "%s.super" % BUILTINS) + + @test_utils.require_version(maxver='3.0') + def test_super_on_old_style_class(self): + # super doesn't work on old style class, but leave + # that as an error for pylint. We'll infer Super objects, + # but every call will result in a failure at some point. + node = test_utils.extract_node(''' + class OldStyle: + def __init__(self): + super(OldStyle, self) #@ + ''') + old = next(node.infer()) + self.assertIsInstance(old, objects.Super) + self.assertIsInstance(old.mro_pointer, nodes.ClassDef) + self.assertEqual(old.mro_pointer.name, 'OldStyle') + with self.assertRaises(exceptions.SuperError) as cm: + old.super_mro() + self.assertEqual(str(cm.exception), + "Unable to call super on old-style classes.") + + @test_utils.require_version(minver='3.0') + def test_no_arguments_super(self): + ast_nodes = test_utils.extract_node(''' + class First(object): pass + class Second(First): + def test(self): + super() #@ + @classmethod + def test_classmethod(cls): + super() #@ + ''') + first = next(ast_nodes[0].infer()) + self.assertIsInstance(first, objects.Super) + self.assertIsInstance(first.type, objects.Instance) + self.assertEqual(first.type.name, 'Second') + self.assertIsInstance(first.mro_pointer, nodes.ClassDef) + self.assertEqual(first.mro_pointer.name, 'Second') + + second = next(ast_nodes[1].infer()) + self.assertIsInstance(second, objects.Super) + self.assertIsInstance(second.type, nodes.ClassDef) + self.assertEqual(second.type.name, 'Second') + self.assertIsInstance(second.mro_pointer, nodes.ClassDef) + self.assertEqual(second.mro_pointer.name, 'Second') + + def test_super_simple_cases(self): + ast_nodes = test_utils.extract_node(''' + class First(object): pass + class Second(First): pass + class Third(First): + def test(self): + super(Third, self) #@ + super(Second, self) #@ + + # mro position and the type + super(Third, Third) #@ + super(Third, Second) #@ + super(Fourth, Fourth) #@ + + class Fourth(Third): + pass + ''') + + # .type is the object which provides the mro. + # .mro_pointer is the position in the mro from where + # the lookup should be done. + + # super(Third, self) + first = next(ast_nodes[0].infer()) + self.assertIsInstance(first, objects.Super) + self.assertIsInstance(first.type, objects.Instance) + self.assertEqual(first.type.name, 'Third') + self.assertIsInstance(first.mro_pointer, nodes.ClassDef) + self.assertEqual(first.mro_pointer.name, 'Third') + + # super(Second, self) + second = next(ast_nodes[1].infer()) + self.assertIsInstance(second, objects.Super) + self.assertIsInstance(second.type, objects.Instance) + self.assertEqual(second.type.name, 'Third') + self.assertIsInstance(first.mro_pointer, nodes.ClassDef) + self.assertEqual(second.mro_pointer.name, 'Second') + + # super(Third, Third) + third = next(ast_nodes[2].infer()) + self.assertIsInstance(third, objects.Super) + self.assertIsInstance(third.type, nodes.ClassDef) + self.assertEqual(third.type.name, 'Third') + self.assertIsInstance(third.mro_pointer, nodes.ClassDef) + self.assertEqual(third.mro_pointer.name, 'Third') + + # super(Third, second) + fourth = next(ast_nodes[3].infer()) + self.assertIsInstance(fourth, objects.Super) + self.assertIsInstance(fourth.type, nodes.ClassDef) + self.assertEqual(fourth.type.name, 'Second') + self.assertIsInstance(fourth.mro_pointer, nodes.ClassDef) + self.assertEqual(fourth.mro_pointer.name, 'Third') + + # Super(Fourth, Fourth) + fifth = next(ast_nodes[4].infer()) + self.assertIsInstance(fifth, objects.Super) + self.assertIsInstance(fifth.type, nodes.ClassDef) + self.assertEqual(fifth.type.name, 'Fourth') + self.assertIsInstance(fifth.mro_pointer, nodes.ClassDef) + self.assertEqual(fifth.mro_pointer.name, 'Fourth') + + def test_super_infer(self): + node = test_utils.extract_node(''' + class Super(object): + def __init__(self): + super(Super, self) #@ + ''') + inferred = next(node.infer()) + self.assertIsInstance(inferred, objects.Super) + reinferred = next(inferred.infer()) + self.assertIsInstance(reinferred, objects.Super) + self.assertIs(inferred, reinferred) + + def test_inferring_invalid_supers(self): + ast_nodes = test_utils.extract_node(''' + class Super(object): + def __init__(self): + # MRO pointer is not a type + super(1, self) #@ + # MRO type is not a subtype + super(Super, 1) #@ + # self is not a subtype of Bupper + super(Bupper, self) #@ + class Bupper(Super): + pass + ''') + first = next(ast_nodes[0].infer()) + self.assertIsInstance(first, objects.Super) + with self.assertRaises(exceptions.SuperError) as cm: + first.super_mro() + self.assertIsInstance(cm.exception.super_.mro_pointer, nodes.Const) + self.assertEqual(cm.exception.super_.mro_pointer.value, 1) + for node, invalid_type in zip(ast_nodes[1:], + (nodes.Const, objects.Instance)): + inferred = next(node.infer()) + self.assertIsInstance(inferred, objects.Super, node) + with self.assertRaises(exceptions.SuperError) as cm: + inferred.super_mro() + self.assertIsInstance(cm.exception.super_.type, invalid_type) + + def test_proxied(self): + node = test_utils.extract_node(''' + class Super(object): + def __init__(self): + super(Super, self) #@ + ''') + inferred = next(node.infer()) + proxied = inferred._proxied + self.assertEqual(proxied.qname(), "%s.super" % BUILTINS) + self.assertIsInstance(proxied, nodes.ClassDef) + + def test_super_bound_model(self): + ast_nodes = test_utils.extract_node(''' + class First(object): + def method(self): + pass + @classmethod + def class_method(cls): + pass + class Super_Type_Type(First): + def method(self): + super(Super_Type_Type, Super_Type_Type).method #@ + super(Super_Type_Type, Super_Type_Type).class_method #@ + @classmethod + def class_method(cls): + super(Super_Type_Type, Super_Type_Type).method #@ + super(Super_Type_Type, Super_Type_Type).class_method #@ + + class Super_Type_Object(First): + def method(self): + super(Super_Type_Object, self).method #@ + super(Super_Type_Object, self).class_method #@ + ''') + # Super(type, type) is the same for both functions and classmethods. + first = next(ast_nodes[0].infer()) + self.assertIsInstance(first, nodes.FunctionDef) + self.assertEqual(first.name, 'method') + + second = next(ast_nodes[1].infer()) + self.assertIsInstance(second, objects.BoundMethod) + self.assertEqual(second.bound.name, 'First') + self.assertEqual(second.type, 'classmethod') + + third = next(ast_nodes[2].infer()) + self.assertIsInstance(third, nodes.FunctionDef) + self.assertEqual(third.name, 'method') + + fourth = next(ast_nodes[3].infer()) + self.assertIsInstance(fourth, objects.BoundMethod) + self.assertEqual(fourth.bound.name, 'First') + self.assertEqual(fourth.type, 'classmethod') + + # Super(type, obj) can lead to different attribute bindings + # depending on the type of the place where super was called. + fifth = next(ast_nodes[4].infer()) + self.assertIsInstance(fifth, objects.BoundMethod) + self.assertEqual(fifth.bound.name, 'First') + self.assertEqual(fifth.type, 'method') + + sixth = next(ast_nodes[5].infer()) + self.assertIsInstance(sixth, objects.BoundMethod) + self.assertEqual(sixth.bound.name, 'First') + self.assertEqual(sixth.type, 'classmethod') + + def test_super_getattr_single_inheritance(self): + ast_nodes = test_utils.extract_node(''' + class First(object): + def test(self): pass + class Second(First): + def test2(self): pass + class Third(Second): + test3 = 42 + def __init__(self): + super(Third, self).test2 #@ + super(Third, self).test #@ + # test3 is local, no MRO lookup is done. + super(Third, self).test3 #@ + super(Third, self) #@ + + # Unbounds. + super(Third, Third).test2 #@ + super(Third, Third).test #@ + + ''') + first = next(ast_nodes[0].infer()) + self.assertIsInstance(first, objects.BoundMethod) + self.assertEqual(first.bound.name, 'Second') + + second = next(ast_nodes[1].infer()) + self.assertIsInstance(second, objects.BoundMethod) + self.assertEqual(second.bound.name, 'First') + + with self.assertRaises(exceptions.InferenceError): + next(ast_nodes[2].infer()) + fourth = next(ast_nodes[3].infer()) + with self.assertRaises(exceptions.AttributeInferenceError): + fourth.getattr('test3') + with self.assertRaises(exceptions.AttributeInferenceError): + next(fourth.igetattr('test3')) + + first_unbound = next(ast_nodes[4].infer()) + self.assertIsInstance(first_unbound, nodes.FunctionDef) + self.assertEqual(first_unbound.name, 'test2') + self.assertEqual(first_unbound.parent.name, 'Second') + + second_unbound = next(ast_nodes[5].infer()) + self.assertIsInstance(second_unbound, nodes.FunctionDef) + self.assertEqual(second_unbound.name, 'test') + self.assertEqual(second_unbound.parent.name, 'First') + + def test_super_invalid_mro(self): + node = test_utils.extract_node(''' + class A(object): + test = 42 + class Super(A, A): + def __init__(self): + super(Super, self) #@ + ''') + inferred = next(node.infer()) + with self.assertRaises(exceptions.AttributeInferenceError): + next(inferred.getattr('test')) + + def test_super_complex_mro(self): + ast_nodes = test_utils.extract_node(''' + class A(object): + def spam(self): return "A" + def foo(self): return "A" + @staticmethod + def static(self): pass + class B(A): + def boo(self): return "B" + def spam(self): return "B" + class C(A): + def boo(self): return "C" + class E(C, B): + def __init__(self): + super(E, self).boo #@ + super(C, self).boo #@ + super(E, self).spam #@ + super(E, self).foo #@ + super(E, self).static #@ + ''') + first = next(ast_nodes[0].infer()) + self.assertIsInstance(first, objects.BoundMethod) + self.assertEqual(first.bound.name, 'C') + second = next(ast_nodes[1].infer()) + self.assertIsInstance(second, objects.BoundMethod) + self.assertEqual(second.bound.name, 'B') + third = next(ast_nodes[2].infer()) + self.assertIsInstance(third, objects.BoundMethod) + self.assertEqual(third.bound.name, 'B') + fourth = next(ast_nodes[3].infer()) + self.assertEqual(fourth.bound.name, 'A') + static = next(ast_nodes[4].infer()) + self.assertIsInstance(static, nodes.FunctionDef) + self.assertEqual(static.parent.scope().name, 'A') + + def test_super_data_model(self): + ast_nodes = test_utils.extract_node(''' + class X(object): pass + class A(X): + def __init__(self): + super(A, self) #@ + super(A, A) #@ + super(X, A) #@ + ''') + first = next(ast_nodes[0].infer()) + thisclass = first.getattr('__thisclass__')[0] + self.assertIsInstance(thisclass, nodes.ClassDef) + self.assertEqual(thisclass.name, 'A') + selfclass = first.getattr('__self_class__')[0] + self.assertIsInstance(selfclass, nodes.ClassDef) + self.assertEqual(selfclass.name, 'A') + self_ = first.getattr('__self__')[0] + self.assertIsInstance(self_, objects.Instance) + self.assertEqual(self_.name, 'A') + cls = first.getattr('__class__')[0] + self.assertEqual(cls, first._proxied) + + second = next(ast_nodes[1].infer()) + thisclass = second.getattr('__thisclass__')[0] + self.assertEqual(thisclass.name, 'A') + self_ = second.getattr('__self__')[0] + self.assertIsInstance(self_, nodes.ClassDef) + self.assertEqual(self_.name, 'A') + + third = next(ast_nodes[2].infer()) + thisclass = third.getattr('__thisclass__')[0] + self.assertEqual(thisclass.name, 'X') + selfclass = third.getattr('__self_class__')[0] + self.assertEqual(selfclass.name, 'A') + + def assertEqualMro(self, klass, expected_mro): + self.assertEqual( + [member.name for member in klass.super_mro()], + expected_mro) + + def test_super_mro(self): + ast_nodes = test_utils.extract_node(''' + class A(object): pass + class B(A): pass + class C(A): pass + class E(C, B): + def __init__(self): + super(E, self) #@ + super(C, self) #@ + super(B, self) #@ + + super(B, 1) #@ + super(1, B) #@ + ''') + first = next(ast_nodes[0].infer()) + self.assertEqualMro(first, ['C', 'B', 'A', 'object']) + second = next(ast_nodes[1].infer()) + self.assertEqualMro(second, ['B', 'A', 'object']) + third = next(ast_nodes[2].infer()) + self.assertEqualMro(third, ['A', 'object']) + + fourth = next(ast_nodes[3].infer()) + with self.assertRaises(exceptions.SuperError): + fourth.super_mro() + fifth = next(ast_nodes[4].infer()) + with self.assertRaises(exceptions.SuperError): + fifth.super_mro() + + def test_super_yes_objects(self): + ast_nodes = test_utils.extract_node(''' + from collections import Missing + class A(object): + def __init__(self): + super(Missing, self) #@ + super(A, Missing) #@ + ''') + first = next(ast_nodes[0].infer()) + self.assertIsInstance(first, objects.Instance) + second = next(ast_nodes[1].infer()) + self.assertIsInstance(second, objects.Instance) + + def test_super_invalid_types(self): + node = test_utils.extract_node(''' + import collections + class A(object): + def __init__(self): + super(A, collections) #@ + ''') + inferred = next(node.infer()) + with self.assertRaises(exceptions.SuperError): + inferred.super_mro() + with self.assertRaises(exceptions.SuperError): + inferred.super_mro() + + def test_super_properties(self): + node = test_utils.extract_node(''' + class Foo(object): + @property + def dict(self): + return 42 + + class Bar(Foo): + @property + def dict(self): + return super(Bar, self).dict + + Bar().dict + ''') + inferred = next(node.infer()) + self.assertIsInstance(inferred, nodes.Const) + self.assertEqual(inferred.value, 42) + + +class MethodTest(unittest.TestCase): + + def test_unbound_function_method_difference(self): + node = test_utils.extract_node(''' + class A: + def test(self): pass + A.test + ''') + inferred = next(node.infer()) + if six.PY2: + self.assertIsInstance(inferred, objects.UnboundMethod) + else: + self.assertIsInstance(inferred, nodes.FunctionDef) + + def test_unbound_function_from_classmethods(self): + node = test_utils.extract_node(''' + class A: + @classmethod + def test(cls): return cls.b + def b(self): return self + A.test() + ''') + inferred = next(node.infer()) + if six.PY2: + self.assertIsInstance(inferred, objects.UnboundMethod) + else: + self.assertIsInstance(inferred, nodes.FunctionDef) + + def test_static_method(self): + node = test_utils.extract_node(''' + class A: + @staticmethod + def test(self): pass + A.test + ''') + inferred = next(node.infer()) + self.assertIsInstance(inferred, nodes.FunctionDef) + + @unittest.skipUnless(six.PY2, "Unbound methods are available only on Python 2") + def test_underlying_proxied_unbound_method(self): + node = test_utils.extract_node(''' + class A: + def test(self): pass + A.test + ''') + inferred = next(node.infer()) + self.assertIsInstance(inferred._proxied, nodes.FunctionDef) + + def test_underlying_proxied_bound_method(self): + node = test_utils.extract_node(''' + class A: + def test(self): pass + A().test + ''') + inferred = next(node.infer()) + self.assertIsInstance(inferred._proxied, nodes.FunctionDef) + + +if __name__ == '__main__': + unittest.main() diff --git a/astroid/tests/unittest_protocols.py b/astroid/tests/unittest_protocols.py index cea3945214..75b8d65b84 100644 --- a/astroid/tests/unittest_protocols.py +++ b/astroid/tests/unittest_protocols.py @@ -1,20 +1,6 @@ -# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + import contextlib import unittest diff --git a/astroid/tests/unittest_python3.py b/astroid/tests/unittest_python3.py index c5e3d35340..d37f73dd34 100644 --- a/astroid/tests/unittest_python3.py +++ b/astroid/tests/unittest_python3.py @@ -1,20 +1,6 @@ -# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + from textwrap import dedent import unittest diff --git a/astroid/tests/unittest_raw_building.py b/astroid/tests/unittest_raw_building.py index a284647392..bf4b762a5d 100644 --- a/astroid/tests/unittest_raw_building.py +++ b/astroid/tests/unittest_raw_building.py @@ -1,3 +1,6 @@ +# 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 + import inspect import os import unittest diff --git a/astroid/tests/unittest_regrtest.py b/astroid/tests/unittest_regrtest.py index d99a74d061..66613d7535 100644 --- a/astroid/tests/unittest_regrtest.py +++ b/astroid/tests/unittest_regrtest.py @@ -1,20 +1,6 @@ -# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + import sys import unittest import textwrap diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index c0db13968a..4294a844a6 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -1,20 +1,6 @@ -# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + """tests for specific behaviour of astroid scoped nodes (i.e. module, class and function) """ diff --git a/astroid/tests/unittest_transforms.py b/astroid/tests/unittest_transforms.py index 6bcb1db0ae..285aafdcb6 100644 --- a/astroid/tests/unittest_transforms.py +++ b/astroid/tests/unittest_transforms.py @@ -1,245 +1,231 @@ -# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . - -from __future__ import print_function - -import contextlib -import time -import unittest - -from astroid import builder -from astroid import nodes -from astroid import parse -from astroid import transforms - - -@contextlib.contextmanager -def add_transform(manager, node, transform, predicate=None): - manager.register_transform(node, transform, predicate) - try: - yield - finally: - manager.unregister_transform(node, transform, predicate) - - -class TestTransforms(unittest.TestCase): - - def setUp(self): - self.transformer = transforms.TransformVisitor() - - def parse_transform(self, code): - module = parse(code, apply_transforms=False) - return self.transformer.visit(module) - - def test_function_inlining_transform(self): - def transform_call(node): - # Let's do some function inlining - inferred = next(node.infer()) - return inferred - - self.transformer.register_transform(nodes.Call, - transform_call) - - module = self.parse_transform(''' - def test(): return 42 - test() #@ - ''') - - self.assertIsInstance(module.body[1], nodes.Expr) - self.assertIsInstance(module.body[1].value, nodes.Const) - self.assertEqual(module.body[1].value.value, 42) - - def test_recursive_transforms_into_astroid_fields(self): - # Test that the transformer walks properly the tree - # by going recursively into the _astroid_fields per each node. - def transform_compare(node): - # Let's check the values of the ops - right = node.comparators[0] - # Assume they are Consts and they were transformed before - # us. - return nodes.Const(node.left.value < right.value) - - def transform_name(node): - # Should be Consts - return next(node.infer()) - - self.transformer.register_transform(nodes.Compare, transform_compare) - self.transformer.register_transform(nodes.Name, transform_name) - - module = self.parse_transform(''' - a = 42 - b = 24 - a < b - ''') - - self.assertIsInstance(module.body[2], nodes.Expr) - self.assertIsInstance(module.body[2].value, nodes.Const) - self.assertFalse(module.body[2].value.value) - - def test_transform_patches_locals(self): - def transform_function(node): - assign = nodes.Assign() - name = nodes.AssignName() - name.name = 'value' - assign.targets = [name] - assign.value = nodes.Const(42) - node.body.append(assign) - - self.transformer.register_transform(nodes.FunctionDef, - transform_function) - - module = self.parse_transform(''' - def test(): - pass - ''') - - func = module.body[0] - self.assertEqual(len(func.body), 2) - self.assertIsInstance(func.body[1], nodes.Assign) - self.assertEqual(func.body[1].as_string(), 'value = 42') - - def test_predicates(self): - def transform_call(node): - inferred = next(node.infer()) - return inferred - - def should_inline(node): - return node.func.name.startswith('inlineme') - - self.transformer.register_transform(nodes.Call, - transform_call, - should_inline) - - module = self.parse_transform(''' - def inlineme_1(): - return 24 - def dont_inline_me(): - return 42 - def inlineme_2(): - return 2 - inlineme_1() - dont_inline_me() - inlineme_2() - ''') - values = module.body[-3:] - self.assertIsInstance(values[0], nodes.Expr) - self.assertIsInstance(values[0].value, nodes.Const) - self.assertEqual(values[0].value.value, 24) - self.assertIsInstance(values[1], nodes.Expr) - self.assertIsInstance(values[1].value, nodes.Call) - self.assertIsInstance(values[2], nodes.Expr) - self.assertIsInstance(values[2].value, nodes.Const) - self.assertEqual(values[2].value.value, 2) - - def test_transforms_are_separated(self): - # Test that the transforming is done at a separate - # step, which means that we are not doing inference - # on a partially constructred tree anymore, which was the - # source of crashes in the past when certain inference rules - # were used in a transform. - def transform_function(node): - if node.decorators: - for decorator in node.decorators.nodes: - inferred = next(decorator.infer()) - if inferred.qname() == 'abc.abstractmethod': - return next(node.infer_call_result(node)) - - manager = builder.MANAGER - with add_transform(manager, nodes.FunctionDef, transform_function): - module = builder.parse(''' - import abc - from abc import abstractmethod - - class A(object): - @abc.abstractmethod - def ala(self): - return 24 - - @abstractmethod - def bala(self): - return 42 - ''') - - cls = module['A'] - ala = cls.body[0] - bala = cls.body[1] - self.assertIsInstance(ala, nodes.Const) - self.assertEqual(ala.value, 24) - self.assertIsInstance(bala, nodes.Const) - self.assertEqual(bala.value, 42) - - def test_transforms_are_called_for_builtin_modules(self): - # Test that transforms are called for builtin modules. - def transform_function(node): - name = nodes.AssignName() - name.name = 'value' - node.args.args = [name] - return node - - manager = builder.MANAGER - predicate = lambda node: node.root().name == 'time' - with add_transform(manager, nodes.FunctionDef, - transform_function, predicate): - builder_instance = builder.AstroidBuilder() - module = builder_instance.module_build(time) - - asctime = module['asctime'] - self.assertEqual(len(asctime.args.args), 1) - self.assertIsInstance(asctime.args.args[0], nodes.AssignName) - self.assertEqual(asctime.args.args[0].name, 'value') - - def test_builder_apply_transforms(self): - def transform_function(node): - return nodes.Const(42) - - manager = builder.MANAGER - with add_transform(manager, nodes.FunctionDef, transform_function): - astroid_builder = builder.AstroidBuilder(apply_transforms=False) - module = astroid_builder.string_build('''def test(): pass''') - - # The transform wasn't applied. - self.assertIsInstance(module.body[0], nodes.FunctionDef) - - def test_transform_crashes_on_is_subtype_of(self): - # Test that we don't crash when having is_subtype_of - # in a transform, as per issue #188. This happened - # before, when the transforms weren't in their own step. - def transform_class(cls): - if cls.is_subtype_of('django.db.models.base.Model'): - return cls - return cls - - self.transformer.register_transform(nodes.ClassDef, - transform_class) - - self.parse_transform(''' - # Change environ to automatically call putenv() if it exists - import os - putenv = os.putenv - try: - # This will fail if there's no putenv - putenv - except NameError: - pass - else: - import UserDict - ''') - - -if __name__ == '__main__': - unittest.main() +# 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 + + +from __future__ import print_function + +import contextlib +import time +import unittest + +from astroid import builder +from astroid import nodes +from astroid import parse +from astroid import transforms + + +@contextlib.contextmanager +def add_transform(manager, node, transform, predicate=None): + manager.register_transform(node, transform, predicate) + try: + yield + finally: + manager.unregister_transform(node, transform, predicate) + + +class TestTransforms(unittest.TestCase): + + def setUp(self): + self.transformer = transforms.TransformVisitor() + + def parse_transform(self, code): + module = parse(code, apply_transforms=False) + return self.transformer.visit(module) + + def test_function_inlining_transform(self): + def transform_call(node): + # Let's do some function inlining + inferred = next(node.infer()) + return inferred + + self.transformer.register_transform(nodes.Call, + transform_call) + + module = self.parse_transform(''' + def test(): return 42 + test() #@ + ''') + + self.assertIsInstance(module.body[1], nodes.Expr) + self.assertIsInstance(module.body[1].value, nodes.Const) + self.assertEqual(module.body[1].value.value, 42) + + def test_recursive_transforms_into_astroid_fields(self): + # Test that the transformer walks properly the tree + # by going recursively into the _astroid_fields per each node. + def transform_compare(node): + # Let's check the values of the ops + right = node.comparators[0] + # Assume they are Consts and they were transformed before + # us. + return nodes.Const(node.left.value < right.value) + + def transform_name(node): + # Should be Consts + return next(node.infer()) + + self.transformer.register_transform(nodes.Compare, transform_compare) + self.transformer.register_transform(nodes.Name, transform_name) + + module = self.parse_transform(''' + a = 42 + b = 24 + a < b + ''') + + self.assertIsInstance(module.body[2], nodes.Expr) + self.assertIsInstance(module.body[2].value, nodes.Const) + self.assertFalse(module.body[2].value.value) + + def test_transform_patches_locals(self): + def transform_function(node): + assign = nodes.Assign() + name = nodes.AssignName() + name.name = 'value' + assign.targets = [name] + assign.value = nodes.Const(42) + node.body.append(assign) + + self.transformer.register_transform(nodes.FunctionDef, + transform_function) + + module = self.parse_transform(''' + def test(): + pass + ''') + + func = module.body[0] + self.assertEqual(len(func.body), 2) + self.assertIsInstance(func.body[1], nodes.Assign) + self.assertEqual(func.body[1].as_string(), 'value = 42') + + def test_predicates(self): + def transform_call(node): + inferred = next(node.infer()) + return inferred + + def should_inline(node): + return node.func.name.startswith('inlineme') + + self.transformer.register_transform(nodes.Call, + transform_call, + should_inline) + + module = self.parse_transform(''' + def inlineme_1(): + return 24 + def dont_inline_me(): + return 42 + def inlineme_2(): + return 2 + inlineme_1() + dont_inline_me() + inlineme_2() + ''') + values = module.body[-3:] + self.assertIsInstance(values[0], nodes.Expr) + self.assertIsInstance(values[0].value, nodes.Const) + self.assertEqual(values[0].value.value, 24) + self.assertIsInstance(values[1], nodes.Expr) + self.assertIsInstance(values[1].value, nodes.Call) + self.assertIsInstance(values[2], nodes.Expr) + self.assertIsInstance(values[2].value, nodes.Const) + self.assertEqual(values[2].value.value, 2) + + def test_transforms_are_separated(self): + # Test that the transforming is done at a separate + # step, which means that we are not doing inference + # on a partially constructred tree anymore, which was the + # source of crashes in the past when certain inference rules + # were used in a transform. + def transform_function(node): + if node.decorators: + for decorator in node.decorators.nodes: + inferred = next(decorator.infer()) + if inferred.qname() == 'abc.abstractmethod': + return next(node.infer_call_result(node)) + + manager = builder.MANAGER + with add_transform(manager, nodes.FunctionDef, transform_function): + module = builder.parse(''' + import abc + from abc import abstractmethod + + class A(object): + @abc.abstractmethod + def ala(self): + return 24 + + @abstractmethod + def bala(self): + return 42 + ''') + + cls = module['A'] + ala = cls.body[0] + bala = cls.body[1] + self.assertIsInstance(ala, nodes.Const) + self.assertEqual(ala.value, 24) + self.assertIsInstance(bala, nodes.Const) + self.assertEqual(bala.value, 42) + + def test_transforms_are_called_for_builtin_modules(self): + # Test that transforms are called for builtin modules. + def transform_function(node): + name = nodes.AssignName() + name.name = 'value' + node.args.args = [name] + return node + + manager = builder.MANAGER + predicate = lambda node: node.root().name == 'time' + with add_transform(manager, nodes.FunctionDef, + transform_function, predicate): + builder_instance = builder.AstroidBuilder() + module = builder_instance.module_build(time) + + asctime = module['asctime'] + self.assertEqual(len(asctime.args.args), 1) + self.assertIsInstance(asctime.args.args[0], nodes.AssignName) + self.assertEqual(asctime.args.args[0].name, 'value') + + def test_builder_apply_transforms(self): + def transform_function(node): + return nodes.Const(42) + + manager = builder.MANAGER + with add_transform(manager, nodes.FunctionDef, transform_function): + astroid_builder = builder.AstroidBuilder(apply_transforms=False) + module = astroid_builder.string_build('''def test(): pass''') + + # The transform wasn't applied. + self.assertIsInstance(module.body[0], nodes.FunctionDef) + + def test_transform_crashes_on_is_subtype_of(self): + # Test that we don't crash when having is_subtype_of + # in a transform, as per issue #188. This happened + # before, when the transforms weren't in their own step. + def transform_class(cls): + if cls.is_subtype_of('django.db.models.base.Model'): + return cls + return cls + + self.transformer.register_transform(nodes.ClassDef, + transform_class) + + self.parse_transform(''' + # Change environ to automatically call putenv() if it exists + import os + putenv = os.putenv + try: + # This will fail if there's no putenv + putenv + except NameError: + pass + else: + import UserDict + ''') + + +if __name__ == '__main__': + unittest.main() diff --git a/astroid/tests/unittest_utils.py b/astroid/tests/unittest_utils.py index 81777e376c..8cef049c3f 100644 --- a/astroid/tests/unittest_utils.py +++ b/astroid/tests/unittest_utils.py @@ -1,20 +1,6 @@ -# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + import unittest from astroid import builder diff --git a/astroid/transforms.py b/astroid/transforms.py index 5d8fc91bbc..83243ad0ec 100644 --- a/astroid/transforms.py +++ b/astroid/transforms.py @@ -1,20 +1,6 @@ -# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + import collections import warnings diff --git a/astroid/tree/__init__.py b/astroid/tree/__init__.py index e69de29bb2..7a8e4fceb2 100644 --- a/astroid/tree/__init__.py +++ b/astroid/tree/__init__.py @@ -0,0 +1,3 @@ +# 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 + diff --git a/astroid/tree/base.py b/astroid/tree/base.py index 58298b8d5c..3fe989971e 100644 --- a/astroid/tree/base.py +++ b/astroid/tree/base.py @@ -1,20 +1,6 @@ -# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + import abc import pprint diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index a5ba8c8829..47d9d04707 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -1,20 +1,6 @@ -# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 """ diff --git a/astroid/tree/rebuilder.py b/astroid/tree/rebuilder.py index 074aea1e8c..d5324e7df3 100644 --- a/astroid/tree/rebuilder.py +++ b/astroid/tree/rebuilder.py @@ -1,20 +1,6 @@ -# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + """this module contains utilities for rebuilding a ast tree in order to get a single Astroid representation """ diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 7bdc8aab90..627903c899 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -1,20 +1,6 @@ -# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + """ This module contains the classes for "scoped" node, i.e. which are opening a diff --git a/astroid/tree/treeabc.py b/astroid/tree/treeabc.py index 5c00825584..22e6aeb84b 100644 --- a/astroid/tree/treeabc.py +++ b/astroid/tree/treeabc.py @@ -1,20 +1,6 @@ -# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + """Abstract classes for nodes and other runtime objects. diff --git a/astroid/util.py b/astroid/util.py index 21c99469ea..c3b3fc1adc 100644 --- a/astroid/util.py +++ b/astroid/util.py @@ -1,20 +1,6 @@ -# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by the -# Free Software Foundation, either version 2.1 of the License, or (at your -# option) any later version. -# -# astroid is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License -# for more details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . +# 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 + # # The code in this file was originally part of logilab-common, licensed under # the same license. From 319c91af870af332236dad44a7a6b89a1e1ed228 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Fri, 20 May 2016 18:09:18 +0100 Subject: [PATCH 249/312] Fix a crash which occurred when the class of a namedtuple could not be inferred. --- ChangeLog | 2 ++ astroid/brain/brain_stdlib.py | 14 +++++++++++--- astroid/tests/unittest_regrtest.py | 7 +++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1604a4b0f8..e3a2817a67 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,8 @@ Change log for the astroid package (used to be astng) ===================================================== -- + * Fix a crash which occurred when the class of a namedtuple could not be inferred. + * Add support for implicit namespace packages (PEP 420) This change involves a couple of modifications. First, we're relying on a diff --git a/astroid/brain/brain_stdlib.py b/astroid/brain/brain_stdlib.py index 33dfcd83d6..6144f1bffb 100644 --- a/astroid/brain/brain_stdlib.py +++ b/astroid/brain/brain_stdlib.py @@ -185,6 +185,10 @@ def infer_namedtuple_enum_fields(call_node, context): # whitespace-separate string try: type_name = infer_first(call_node.args[0], context).value + # The type might not be inferred properly in the case it is a string + # formatting operation, such as '{...}'.format(...) + type_name = type_name or "Uninferable" + fields = infer_first(call_node.args[1], context) return type_name, fields except (AttributeError, exceptions.InferenceError): @@ -203,6 +207,9 @@ def infer_namedtuple(namedtuple_call, context=None): else: raise UseInferenceDefault() + if not field_names: + raise UseInferenceDefault() + class_definition = _class_template.format( typename = type_name, field_names = field_names, @@ -213,13 +220,14 @@ def infer_namedtuple(namedtuple_call, context=None): field_defs = '\n'.join(_field_template.format(index=index, name=name) for index, name in enumerate(field_names)) ) - namedtuple_node = parse(class_definition).getattr(type_name)[0] init_template = '''def __init__(self, {args}): -{assignments}''' +{assignments} + ''' init_definition = init_template.format( args=', '.join(field_names), - assignments='\n'.join((' '*4 + 'self.{f} = {f}').format(f=f) for f in field_names)) + assignments='\n'.join((' ' * 4 + 'self.{f} = {f}').format(f=f) for f in field_names)) + init_node = parse(init_definition).getattr('__init__')[0] init_node.parent = namedtuple_node namedtuple_node.body.append(init_node) diff --git a/astroid/tests/unittest_regrtest.py b/astroid/tests/unittest_regrtest.py index 66613d7535..a1a4642021 100644 --- a/astroid/tests/unittest_regrtest.py +++ b/astroid/tests/unittest_regrtest.py @@ -350,6 +350,13 @@ def property(self): ''') next(node.infer()) + def test_uninferable_string_argument_of_namedtuple(self): + node = extract_node(''' + import collections + collections.namedtuple('{}'.format("a"), '')() + ''') + next(node.infer()) + class Whatever(object): a = property(lambda x: x, lambda x: x) From d1e6dafc4634f8612ccf2cfb57b9b1a583071d81 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Wed, 25 May 2016 14:45:38 +0100 Subject: [PATCH 250/312] Add test for not recording assignment locals in ExceptHandlers --- ChangeLog | 1 + astroid/tests/unittest_regrtest.py | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/ChangeLog b/ChangeLog index e3a2817a67..c873290464 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,7 @@ Change log for the astroid package (used to be astng) ===================================================== -- + * Fix a crash which occurred when the class of a namedtuple could not be inferred. * Add support for implicit namespace packages (PEP 420) diff --git a/astroid/tests/unittest_regrtest.py b/astroid/tests/unittest_regrtest.py index a1a4642021..deaad3c3af 100644 --- a/astroid/tests/unittest_regrtest.py +++ b/astroid/tests/unittest_regrtest.py @@ -357,6 +357,19 @@ def test_uninferable_string_argument_of_namedtuple(self): ''') next(node.infer()) + @require_version(maxver='3.0') + def test_reassignment_in_except_handler(self): + node = extract_node(''' + import exceptions + try: + {}["a"] + except KeyError, exceptions.IndexError: + pass + + IndexError #@ + ''') + self.assertEqual(len(node.inferred()), 1) + class Whatever(object): a = property(lambda x: x, lambda x: x) From 0442a3c822e902f40c4bc89d53375780354c7411 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 31 May 2016 17:29:34 +0100 Subject: [PATCH 251/312] Canonicalize file paths in modpath_from_file, this should help when receiving symlinks and whatnot. Close PyCQA/pylint#791 --- astroid/modutils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/astroid/modutils.py b/astroid/modutils.py index da47d4c60d..7cf33fa6b3 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -293,8 +293,9 @@ def check_modpath_has_init(path, mod_path): def modpath_from_file_with_callback(filename, extrapath=None, is_package_cb=None): filename = _path_from_filename(filename) - filename = os.path.abspath(filename) + filename = os.path.realpath(os.path.expanduser(filename)) base = os.path.splitext(filename)[0] + if extrapath is not None: for path_ in extrapath: path = os.path.abspath(path_) From 752da0693be421de04a30cdab2f55380875122a7 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 31 May 2016 18:44:55 +0100 Subject: [PATCH 252/312] Split brain_stdlib into multiple modules and finally kill it. --- astroid/brain/brain_collections.py | 35 +++ astroid/brain/brain_hashlib.py | 38 +++ astroid/brain/brain_multiprocessing.py | 104 ++++++++ ...ain_stdlib.py => brain_namedtuple_enum.py} | 232 +----------------- astroid/brain/brain_subprocess.py | 78 ++++++ astroid/brain/brain_threading.py | 20 ++ 6 files changed, 276 insertions(+), 231 deletions(-) create mode 100644 astroid/brain/brain_collections.py create mode 100644 astroid/brain/brain_hashlib.py create mode 100644 astroid/brain/brain_multiprocessing.py rename astroid/brain/{brain_stdlib.py => brain_namedtuple_enum.py} (54%) create mode 100644 astroid/brain/brain_subprocess.py create mode 100644 astroid/brain/brain_threading.py diff --git a/astroid/brain/brain_collections.py b/astroid/brain/brain_collections.py new file mode 100644 index 0000000000..ad37861850 --- /dev/null +++ b/astroid/brain/brain_collections.py @@ -0,0 +1,35 @@ +# 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 + +import astroid + + +def _collections_transform(): + return astroid.parse(''' + class defaultdict(dict): + default_factory = None + def __missing__(self, key): pass + + class deque(object): + maxlen = 0 + def __init__(self, iterable=None, maxlen=None): + self.iterable = iterable + def append(self, x): pass + def appendleft(self, x): pass + def clear(self): pass + def count(self, x): return 0 + def extend(self, iterable): pass + def extendleft(self, iterable): pass + def pop(self): pass + def popleft(self): pass + def remove(self, value): pass + def reverse(self): pass + def rotate(self, n): pass + def __iter__(self): return self + def __reversed__(self): return self.iterable[::-1] + def __getitem__(self, index): pass + def __setitem__(self, index, value): pass + def __delitem__(self, index): pass + ''') + +astroid.register_module_extender(astroid.MANAGER, 'collections', _collections_transform) diff --git a/astroid/brain/brain_hashlib.py b/astroid/brain/brain_hashlib.py new file mode 100644 index 0000000000..2ff438588a --- /dev/null +++ b/astroid/brain/brain_hashlib.py @@ -0,0 +1,38 @@ +# 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 + +import six + +import astroid + + +def _hashlib_transform(): + template = ''' + class %(name)s(object): + def __init__(self, value=''): pass + def digest(self): + return %(digest)s + def copy(self): + return self + def update(self, value): pass + def hexdigest(self): + return '' + @property + def name(self): + return %(name)r + @property + def block_size(self): + return 1 + @property + def digest_size(self): + return 1 + ''' + algorithms = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512') + classes = "".join( + template % {'name': hashfunc, 'digest': 'b""' if six.PY3 else '""'} + for hashfunc in algorithms) + return astroid.parse(classes) + + +astroid.register_module_extender(astroid.MANAGER, 'hashlib', _hashlib_transform) + diff --git a/astroid/brain/brain_multiprocessing.py b/astroid/brain/brain_multiprocessing.py new file mode 100644 index 0000000000..d0fb0724cf --- /dev/null +++ b/astroid/brain/brain_multiprocessing.py @@ -0,0 +1,104 @@ + +# 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 + +import sys + +import astroid +from astroid import exceptions +from astroid.interpreter import objects +from astroid.tree.node_classes import InterpreterObject + + +PY34 = sys.version_info[:2] >= (3, 4) + + +def _multiprocessing_transform(): + module = astroid.parse(''' + from multiprocessing.managers import SyncManager + def Manager(): + return SyncManager() + ''') + if not PY34: + return module + + # On Python 3.4, multiprocessing uses a getattr lookup inside contexts, + # in order to get the attributes they need. Since it's extremely + # dynamic, we use this approach to fake it. + node = astroid.parse(''' + from multiprocessing.context import DefaultContext, BaseContext + default = DefaultContext() + base = BaseContext() + ''') + try: + context = next(node['default'].infer()) + base = next(node['base'].infer()) + except exceptions.InferenceError: + return module + + for node in (context, base): + for key, value in node.locals.items(): + if key.startswith("_"): + continue + + value = value[0] + if isinstance(value, astroid.FunctionDef): + # We need to rebind this, since otherwise + # it will have an extra argument (self). + value = objects.BoundMethod(value, node) + module.body.append(InterpreterObject(object_=value, name=key, parent=module)) + return module + + +def _multiprocessing_managers_transform(): + return astroid.parse(''' + import array + import threading + import multiprocessing.pool as pool + + import six + + class Namespace(object): + pass + + class Value(object): + def __init__(self, typecode, value, lock=True): + self._typecode = typecode + self._value = value + def get(self): + return self._value + def set(self, value): + self._value = value + def __repr__(self): + return '%s(%r, %r)'%(type(self).__name__, self._typecode, self._value) + value = property(get, set) + + def Array(typecode, sequence, lock=True): + return array.array(typecode, sequence) + + class SyncManager(object): + Queue = JoinableQueue = six.moves.queue.Queue + Event = threading.Event + RLock = threading.RLock + BoundedSemaphore = threading.BoundedSemaphore + Condition = threading.Condition + Barrier = threading.Barrier + Pool = pool.Pool + list = list + dict = dict + Value = Value + Array = Array + Namespace = Namespace + __enter__ = lambda self: self + __exit__ = lambda *args: args + + def start(self, initializer=None, initargs=None): + pass + def shutdown(self): + pass + ''') + + +astroid.register_module_extender(astroid.MANAGER, 'multiprocessing', _multiprocessing_transform) +astroid.register_module_extender(astroid.MANAGER, 'multiprocessing.managers', + _multiprocessing_managers_transform) diff --git a/astroid/brain/brain_stdlib.py b/astroid/brain/brain_namedtuple_enum.py similarity index 54% rename from astroid/brain/brain_stdlib.py rename to astroid/brain/brain_namedtuple_enum.py index 6144f1bffb..38c6c5132a 100644 --- a/astroid/brain/brain_stdlib.py +++ b/astroid/brain/brain_namedtuple_enum.py @@ -10,10 +10,8 @@ import textwrap from astroid import ( - MANAGER, UseInferenceDefault, inference_tip, - InferenceError, register_module_extender) + MANAGER, UseInferenceDefault, inference_tip) from astroid import exceptions -from astroid.interpreter.objects import BoundMethod from astroid import nodes from astroid.builder import AstroidBuilder from astroid import parse @@ -36,131 +34,6 @@ def infer_first(node, context): return value -# module specific transformation functions ##################################### - -def hashlib_transform(): - template = ''' - -class %(name)s(object): - def __init__(self, value=''): pass - def digest(self): - return %(digest)s - def copy(self): - return self - def update(self, value): pass - def hexdigest(self): - return '' - @property - def name(self): - return %(name)r - @property - def block_size(self): - return 1 - @property - def digest_size(self): - return 1 -''' - algorithms = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512') - classes = "".join( - template % {'name': hashfunc, 'digest': 'b""' if PY3K else '""'} - for hashfunc in algorithms) - return AstroidBuilder(MANAGER).string_build(classes) - - -def collections_transform(): - return AstroidBuilder(MANAGER).string_build(''' - -class defaultdict(dict): - default_factory = None - def __missing__(self, key): pass - -class deque(object): - maxlen = 0 - def __init__(self, iterable=None, maxlen=None): - self.iterable = iterable - def append(self, x): pass - def appendleft(self, x): pass - def clear(self): pass - def count(self, x): return 0 - def extend(self, iterable): pass - def extendleft(self, iterable): pass - def pop(self): pass - def popleft(self): pass - def remove(self, value): pass - def reverse(self): pass - def rotate(self, n): pass - def __iter__(self): return self - def __reversed__(self): return self.iterable[::-1] - def __getitem__(self, index): pass - def __setitem__(self, index, value): pass - def __delitem__(self, index): pass -''') - - -def subprocess_transform(): - if PY3K: - communicate = (bytes('string', 'ascii'), bytes('string', 'ascii')) - communicate_signature = 'def communicate(self, input=None, timeout=None)' - init = """ - def __init__(self, args, bufsize=0, executable=None, - stdin=None, stdout=None, stderr=None, - preexec_fn=None, close_fds=False, shell=False, - cwd=None, env=None, universal_newlines=False, - startupinfo=None, creationflags=0, restore_signals=True, - start_new_session=False, pass_fds=()): - pass - """ - else: - communicate = ('string', 'string') - communicate_signature = 'def communicate(self, input=None)' - init = """ - def __init__(self, args, bufsize=0, executable=None, - stdin=None, stdout=None, stderr=None, - preexec_fn=None, close_fds=False, shell=False, - cwd=None, env=None, universal_newlines=False, - startupinfo=None, creationflags=0): - pass - """ - if PY33: - wait_signature = 'def wait(self, timeout=None)' - else: - wait_signature = 'def wait(self)' - if PY3K: - ctx_manager = ''' - def __enter__(self): return self - def __exit__(self, *args): pass - ''' - else: - ctx_manager = '' - code = textwrap.dedent(''' - - class Popen(object): - returncode = pid = 0 - stdin = stdout = stderr = file() - - %(init)s - - %(communicate_signature)s: - return %(communicate)r - %(wait_signature)s: - return self.returncode - def poll(self): - return self.returncode - def send_signal(self, signal): - pass - def terminate(self): - pass - def kill(self): - pass - %(ctx_manager)s - ''' % {'init': init, - 'communicate': communicate, - 'communicate_signature': communicate_signature, - 'wait_signature': wait_signature, - 'ctx_manager': ctx_manager}) - return AstroidBuilder(MANAGER).string_build(code) - - # namedtuple and Enum support def _looks_like(node, name): @@ -341,112 +214,9 @@ def name(self): break return enum_node -def multiprocessing_transform(): - module = AstroidBuilder(MANAGER).string_build(textwrap.dedent(''' - from multiprocessing.managers import SyncManager - def Manager(): - return SyncManager() - ''')) - if not PY34: - return module - - # On Python 3.4, multiprocessing uses a getattr lookup inside contexts, - # in order to get the attributes they need. Since it's extremely - # dynamic, we use this approach to fake it. - node = AstroidBuilder(MANAGER).string_build(textwrap.dedent(''' - from multiprocessing.context import DefaultContext, BaseContext - default = DefaultContext() - base = BaseContext() - ''')) - try: - context = next(node['default'].infer()) - base = next(node['base'].infer()) - except InferenceError: - return module - - for node in (context, base): - for key, value in node.locals.items(): - if key.startswith("_"): - continue - - value = value[0] - if isinstance(value, nodes.FunctionDef): - # We need to rebind this, since otherwise - # it will have an extra argument (self). - value = BoundMethod(value, node) - module.body.append(nodes.InterpreterObject(object_=value, name=key, - parent=module)) - return module - -def multiprocessing_managers_transform(): - return AstroidBuilder(MANAGER).string_build(textwrap.dedent(''' - import array - import threading - import multiprocessing.pool as pool - - import six - - class Namespace(object): - pass - - class Value(object): - def __init__(self, typecode, value, lock=True): - self._typecode = typecode - self._value = value - def get(self): - return self._value - def set(self, value): - self._value = value - def __repr__(self): - return '%s(%r, %r)'%(type(self).__name__, self._typecode, self._value) - value = property(get, set) - - def Array(typecode, sequence, lock=True): - return array.array(typecode, sequence) - - class SyncManager(object): - Queue = JoinableQueue = six.moves.queue.Queue - Event = threading.Event - RLock = threading.RLock - BoundedSemaphore = threading.BoundedSemaphore - Condition = threading.Condition - Barrier = threading.Barrier - Pool = pool.Pool - list = list - dict = dict - Value = Value - Array = Array - Namespace = Namespace - __enter__ = lambda self: self - __exit__ = lambda *args: args - - def start(self, initializer=None, initargs=None): - pass - def shutdown(self): - pass - ''')) - -def thread_transform(): - return AstroidBuilder(MANAGER).string_build(''' -class lock(object): - def acquire(self, blocking=True): - pass - def release(self): - pass - -def Lock(): - return lock() -''') MANAGER.register_transform(nodes.Call, inference_tip(infer_namedtuple), _looks_like_namedtuple) MANAGER.register_transform(nodes.Call, inference_tip(infer_enum), _looks_like_enum) MANAGER.register_transform(nodes.ClassDef, infer_enum_class) -register_module_extender(MANAGER, 'hashlib', hashlib_transform) -register_module_extender(MANAGER, 'collections', collections_transform) -register_module_extender(MANAGER, 'subprocess', subprocess_transform) -register_module_extender(MANAGER, 'multiprocessing.managers', - multiprocessing_managers_transform) -register_module_extender(MANAGER, 'multiprocessing', multiprocessing_transform) -register_module_extender(MANAGER, 'threading', thread_transform) diff --git a/astroid/brain/brain_subprocess.py b/astroid/brain/brain_subprocess.py new file mode 100644 index 0000000000..3136f450ac --- /dev/null +++ b/astroid/brain/brain_subprocess.py @@ -0,0 +1,78 @@ + +# 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 + +import sys + +import astroid + + +PY3K = sys.version_info[0] >= 3 +PY33 = sys.version_info >= (3, 3) + + +def _subprocess_transform(): + if PY3K: + communicate = (bytes('string', 'ascii'), bytes('string', 'ascii')) + communicate_signature = 'def communicate(self, input=None, timeout=None)' + init = """ + def __init__(self, args, bufsize=0, executable=None, + stdin=None, stdout=None, stderr=None, + preexec_fn=None, close_fds=False, shell=False, + cwd=None, env=None, universal_newlines=False, + startupinfo=None, creationflags=0, restore_signals=True, + start_new_session=False, pass_fds=()): + pass + """ + else: + communicate = ('string', 'string') + communicate_signature = 'def communicate(self, input=None)' + init = """ + def __init__(self, args, bufsize=0, executable=None, + stdin=None, stdout=None, stderr=None, + preexec_fn=None, close_fds=False, shell=False, + cwd=None, env=None, universal_newlines=False, + startupinfo=None, creationflags=0): + pass + """ + if PY33: + wait_signature = 'def wait(self, timeout=None)' + else: + wait_signature = 'def wait(self)' + if PY3K: + ctx_manager = ''' + def __enter__(self): return self + def __exit__(self, *args): pass + ''' + else: + ctx_manager = '' + code = ''' + + class Popen(object): + returncode = pid = 0 + stdin = stdout = stderr = file() + + %(init)s + + %(communicate_signature)s: + return %(communicate)r + %(wait_signature)s: + return self.returncode + def poll(self): + return self.returncode + def send_signal(self, signal): + pass + def terminate(self): + pass + def kill(self): + pass + %(ctx_manager)s + ''' % {'init': init, + 'communicate': communicate, + 'communicate_signature': communicate_signature, + 'wait_signature': wait_signature, + 'ctx_manager': ctx_manager} + return astroid.parse(code) + + +astroid.register_module_extender(astroid.MANAGER, 'subprocess', _subprocess_transform) diff --git a/astroid/brain/brain_threading.py b/astroid/brain/brain_threading.py new file mode 100644 index 0000000000..98f7fb0f28 --- /dev/null +++ b/astroid/brain/brain_threading.py @@ -0,0 +1,20 @@ +# 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 + +import astroid + + +def _thread_transform(): + return astroid.parse(''' + class lock(object): + def acquire(self, blocking=True): + pass + def release(self): + pass + + def Lock(): + return lock() + ''') + + +astroid.register_module_extender(astroid.MANAGER, 'threading', _thread_transform) From 7cd9c6b83d6866055ff756787966c8011a857769 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Wed, 1 Jun 2016 17:16:55 +0100 Subject: [PATCH 253/312] Try to canonicalize the lookup paths as well. --- astroid/modutils.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/astroid/modutils.py b/astroid/modutils.py index 7cf33fa6b3..7143657f2a 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -33,6 +33,7 @@ import pkg_resources except ImportError: pkg_resources = None +import six from astroid import util @@ -130,6 +131,10 @@ def _normalize_path(path): return os.path.normcase(os.path.abspath(path)) +def _canonicalize_path(path): + return os.path.realpath(os.path.expanduser(path)) + + def _path_from_filename(filename, is_jython=util.JYTHON): if not is_jython: if sys.version_info > (3, 0): @@ -297,19 +302,21 @@ def modpath_from_file_with_callback(filename, extrapath=None, is_package_cb=None base = os.path.splitext(filename)[0] if extrapath is not None: - for path_ in extrapath: + for path_ in six.moves.map(_canonicalize_path, extrapath): path = os.path.abspath(path_) if path and os.path.normcase(base[:len(path)]) == os.path.normcase(path): submodpath = [pkg for pkg in base[len(path):].split(os.sep) if pkg] if is_package_cb(path, submodpath[:-1]): return extrapath[path_].split('.') + submodpath - for path in sys.path: + + for path in six.moves.map(_canonicalize_path, sys.path): path = _cache_normalize_path(path) if path and os.path.normcase(base).startswith(path): modpath = [pkg for pkg in base[len(path):].split(os.sep) if pkg] if is_package_cb(path, modpath[:-1]): return modpath + raise ImportError('Unable to find module for %s in %s' % ( filename, ', \n'.join(sys.path))) From 67ed3537f7e0bee9de22663b479c6be444e527ce Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Fri, 3 Jun 2016 15:44:31 +0100 Subject: [PATCH 254/312] Now is_subtype / is_supertype raises an internal exception when a type hierarchy can't be determined It used to return Uninferable, but no call site was actually taking care of this potential return. It is better though to simply raise an exception and to let the call sites to handle them in which way they want to. --- astroid/exceptions.py | 4 ++++ astroid/inference.py | 35 +++++++++++++++++++++---------- astroid/interpreter/util.py | 4 ++-- astroid/tests/unittest_helpers.py | 13 +++++++----- 4 files changed, 38 insertions(+), 18 deletions(-) diff --git a/astroid/exceptions.py b/astroid/exceptions.py index 3e57100603..10e24e1b09 100644 --- a/astroid/exceptions.py +++ b/astroid/exceptions.py @@ -197,6 +197,10 @@ class BinaryOperationNotSupportedError(NotSupportedError): """ +class _NonDeducibleTypeHierarchy(Exception): + """Raised when is_subtype / is_supertype can't deduce the relation between two types.""" + + # Backwards-compatibility aliases OperationError = util.BadOperationMessage UnaryOperationError = util.BadUnaryOperationMessage diff --git a/astroid/inference.py b/astroid/inference.py index c32c3be1c7..43483b1b20 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -168,9 +168,14 @@ def infer_attribute(self, context=None): # by taking in consideration a redefinition in the subclass. if (isinstance(owner, runtimeabc.Instance) and isinstance(context.boundnode, runtimeabc.Instance)): - if inferenceutil.is_subtype(inferenceutil.object_type(context.boundnode), - inferenceutil.object_type(owner)): - owner = context.boundnode + try: + if inferenceutil.is_subtype(inferenceutil.object_type(context.boundnode), + inferenceutil.object_type(owner)): + owner = context.boundnode + except exceptions._NonDeducibleTypeHierarchy: + # Can't determine anything useful. + pass + try: context.boundnode = owner @@ -615,10 +620,14 @@ def infer_binop(self, context, nodes): yield util.Uninferable return - results = _infer_binary_operation(lhs, rhs, self, context, - _get_binop_flow, nodes) - for result in results: - yield result + try: + results = _infer_binary_operation(lhs, rhs, self, context, + _get_binop_flow, nodes) + except exceptions._NonDeducibleTypeHierarchy: + yield util.Uninferable + else: + for result in results: + yield result @decorators.yes_if_nothing_inferred @@ -652,10 +661,14 @@ def infer_augassign(self, context=None, nodes=None): yield util.Uninferable return - results = _infer_binary_operation(lhs, rhs, self, - context, _get_aug_flow, nodes) - for result in results: - yield result + try: + results = _infer_binary_operation(lhs, rhs, self, + context, _get_aug_flow, nodes) + except exceptions._NonDeducibleTypeHierarchy: + yield util.Uninferable + else: + for result in results: + yield result @decorators.path_wrapper diff --git a/astroid/interpreter/util.py b/astroid/interpreter/util.py index d5ba51de3f..148804ce75 100644 --- a/astroid/interpreter/util.py +++ b/astroid/interpreter/util.py @@ -194,7 +194,7 @@ def has_known_bases(klass, context=None): def _type_check(type1, type2): if not all(map(has_known_bases, (type1, type2))): - return util.Uninferable + raise exceptions._NonDeducibleTypeHierarchy if not all([type1.newstyle, type2.newstyle]): return False @@ -202,7 +202,7 @@ def _type_check(type1, type2): return type1 in type2.mro()[:-1] except exceptions.MroError: # The MRO is invalid. - return util.Uninferable + raise exceptions._NonDeducibleTypeHierarchy def is_subtype(type1, type2): diff --git a/astroid/tests/unittest_helpers.py b/astroid/tests/unittest_helpers.py index efb25f0ddb..1826cfd781 100644 --- a/astroid/tests/unittest_helpers.py +++ b/astroid/tests/unittest_helpers.py @@ -9,6 +9,7 @@ from six.moves import builtins from astroid import builder +from astroid import exceptions from astroid.interpreter import util as interpreterutil from astroid import manager from astroid import nodes @@ -193,8 +194,9 @@ class E(C, B): pass #@ class F(D, E): pass #@ ''') self.assertFalse(interpreterutil.is_subtype(cls_e, cls_f)) - self.assertEqual(interpreterutil.is_subtype(cls_f, cls_e), util.Uninferable) - self.assertEqual(interpreterutil.is_supertype(cls_e, cls_f), util.Uninferable) + self.assertFalse(interpreterutil.is_subtype(cls_e, cls_f)) + with self.assertRaises(exceptions._NonDeducibleTypeHierarchy): + interpreterutil.is_subtype(cls_f, cls_e) self.assertFalse(interpreterutil.is_supertype(cls_f, cls_e)) def test_is_subtype_supertype_unknown_bases(self): @@ -203,8 +205,10 @@ def test_is_subtype_supertype_unknown_bases(self): class A(Unknown): pass #@ class B(A): pass #@ ''') - self.assertTrue(interpreterutil.is_subtype(cls_b, cls_a)) - self.assertTrue(interpreterutil.is_supertype(cls_a, cls_b)) + with self.assertRaises(exceptions._NonDeducibleTypeHierarchy): + interpreterutil.is_subtype(cls_a, cls_b) + with self.assertRaises(exceptions._NonDeducibleTypeHierarchy): + interpreterutil.is_supertype(cls_a, cls_b) def test_is_subtype_supertype_unrelated_classes(self): cls_a, cls_b = test_utils.extract_node(''' @@ -236,7 +240,6 @@ class A(type): #@ @test_utils.require_version(maxver='3.0') def test_old_style_class(self): - # TODO: what is this test supposed to be testing? It will crash as-is because it calls helpers. cls = test_utils.extract_node('''class A: pass''') builtin_type = self._look_up_in_builtins('type') self.assertEqual(interpreterutil.object_type(cls), builtin_type) From 671f8902992813687c4646fee33cf4666a108056 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sat, 4 Jun 2016 20:38:31 +0100 Subject: [PATCH 255/312] Proper bound and _proxied objects for __subclasses__ and __mro__ pointing to type.mro, respectively type.__subclasses__. --- astroid/interpreter/objectmodel.py | 14 ++++++++++++-- astroid/tests/unittest_object_model.py | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/astroid/interpreter/objectmodel.py b/astroid/interpreter/objectmodel.py index 9ab06e4a2a..721127dc3c 100644 --- a/astroid/interpreter/objectmodel.py +++ b/astroid/interpreter/objectmodel.py @@ -367,6 +367,10 @@ def py__mro__(self): @property def pymro(self): + if not self._instance.newstyle: + raise exceptions.AttributeInferenceError(target=self._instance, + attribute='mro') + other_self = self # Cls.mro is a method and we need to return one in order to have a proper inference. @@ -374,7 +378,10 @@ def pymro(self): class MroBoundMethod(objects.BoundMethod): def infer_call_result(self, caller, context=None): yield other_self.py__mro__ - return MroBoundMethod(proxy=self._instance, bound=self._instance) + + implicit_metaclass = self._instance.implicit_metaclass() + mro_method = implicit_metaclass.locals['mro'][0] + return MroBoundMethod(proxy=mro_method, bound=implicit_metaclass) @property def py__bases__(self): @@ -412,7 +419,10 @@ class SubclassesBoundMethod(objects.BoundMethod): def infer_call_result(self, caller, context=None): yield obj - return SubclassesBoundMethod(proxy=self._instance, bound=self._instance) + implicit_metaclass = self._instance.implicit_metaclass() + subclasses_method = implicit_metaclass.locals['__subclasses__'][0] + return SubclassesBoundMethod(proxy=subclasses_method, + bound=implicit_metaclass) @property def py__dict__(self): diff --git a/astroid/tests/unittest_object_model.py b/astroid/tests/unittest_object_model.py index 03590fe564..f63a1b28ca 100644 --- a/astroid/tests/unittest_object_model.py +++ b/astroid/tests/unittest_object_model.py @@ -144,6 +144,30 @@ class A: ''') with self.assertRaises(exceptions.InferenceError): next(ast_node.infer()) + + def test_class_model_correct_mro_subclasses_proxied(self): + ast_nodes = test_utils.extract_node(''' + class A(object): + pass + A.mro #@ + A.__subclasses__ #@ + ''') + for node in ast_nodes: + inferred = next(node.infer()) + self.assertIsInstance(inferred, astroid.BoundMethod) + self.assertIsInstance(inferred._proxied, astroid.FunctionDef) + self.assertIsInstance(inferred.bound, astroid.ClassDef) + self.assertEqual(inferred.bound.name, 'type') + + @unittest.skipUnless(six.PY2, "Needs old style classes") + def test_old_style_classes_no_mro(self): + ast_node = test_utils.extract_node(''' + class A: + pass + A.mro #@ + ''') + with self.assertRaises(exceptions.InferenceError): + next(ast_node.infer()) def test_class_model(self): ast_nodes = test_utils.extract_node(''' From 16d24126772d0da3d7e71690b731fff79ee50c99 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sat, 4 Jun 2016 21:17:15 +0100 Subject: [PATCH 256/312] Don't restrict building of builtin type's member if they are already in special_attributes The introduction of the special attribute models led to an interesting bug, where some of the members of the builtin type were not added into it, since they were already defined at the level of the special_attributes. The latter is just a declaration of what attributes a type can have, usually leading to introspecting the type itself for retrieving the value of the special attribute. --- astroid/interpreter/objectmodel.py | 4 ++-- astroid/raw_building.py | 2 +- astroid/tests/unittest_scoped_nodes.py | 1 - 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/astroid/interpreter/objectmodel.py b/astroid/interpreter/objectmodel.py index 721127dc3c..47ca442c46 100644 --- a/astroid/interpreter/objectmodel.py +++ b/astroid/interpreter/objectmodel.py @@ -491,8 +491,8 @@ def __new__(self, *args, **kwargs): for name, values in generator.locals.items(): method = values[0] patched = lambda self, meth=method: meth - - setattr(type(cls), 'py' + name, property(patched)) + if not hasattr(type(cls), 'py' + name): + setattr(type(cls), 'py' + name, property(patched)) return cls diff --git a/astroid/raw_building.py b/astroid/raw_building.py index eb81cfb896..b0746aeb06 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -256,7 +256,7 @@ def ast_from_class(cls, built_objects, module, name=None, parent=None): bases = [node_classes.Name(name=b.__name__, parent=class_node) for b in cls.__bases__] body = [ - t for a in _classify_class_attrs(cls) if a.defining_class is cls and a.name not in scoped_nodes.ClassDef.special_attributes + t for a in _classify_class_attrs(cls) if a.defining_class is cls for t in _ast_from_object(a.object, built_objects, module, a.name, parent=class_node)] class_node.postinit(bases=bases, body=body, decorators=(), newstyle=isinstance(cls, type)) diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index 4294a844a6..d9583ddbe4 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -632,7 +632,6 @@ def test_instance_special_attributes(self): self.assertRaises(AttributeInferenceError, inst.getattr, '__mro__') self.assertRaises(AttributeInferenceError, inst.getattr, '__bases__') self.assertRaises(AttributeInferenceError, inst.getattr, '__name__') - self.assertRaises(AttributeInferenceError, inst.getattr, '__doc__') self.assertRaises(AttributeInferenceError, inst.getattr, '__dict__') def test_navigation(self): From cfd086b1a1f9f979866d82707dc6558d64b74cf8 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 5 Jun 2016 11:49:12 +0100 Subject: [PATCH 257/312] Give priority to local special attributes for classes and modules For instance, a module can decide to redefine its __file__ attribute. Previously, this was defaulting to the file name itself, without taking in account local redefinitions. --- astroid/tests/unittest_object_model.py | 18 +++++++++++++++++ astroid/tests/unittest_scoped_nodes.py | 4 ++-- astroid/tree/scoped_nodes.py | 27 ++++++++++++++++++++++---- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/astroid/tests/unittest_object_model.py b/astroid/tests/unittest_object_model.py index f63a1b28ca..de4384fa91 100644 --- a/astroid/tests/unittest_object_model.py +++ b/astroid/tests/unittest_object_model.py @@ -125,6 +125,16 @@ def test(self): pass class ClassModelTest(unittest.TestCase): + def test_priority_to_local_defined_values(self): + ast_node = test_utils.extract_node(''' + class A: + __doc__ = "first" + A.__doc__ #@ + ''') + inferred = next(ast_node.infer()) + self.assertIsInstance(inferred, astroid.Const) + self.assertEqual(inferred.value, "first") + @test_utils.require_version(maxver='3.0') def test__mro__old_style(self): ast_node = test_utils.extract_node(''' @@ -232,6 +242,14 @@ class C(A): pass class ModuleModelTest(unittest.TestCase): + def test_priority_to_local_defined_values(self): + ast_node = astroid.parse(''' + __file__ = "mine" + ''') + file_value = next(ast_node.igetattr('__file__')) + self.assertIsInstance(file_value, astroid.Const) + self.assertEqual(file_value.value, "mine") + def test__path__not_a_package(self): ast_node = test_utils.extract_node(''' import sys diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index d9583ddbe4..6a8012f8d8 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -612,8 +612,8 @@ class D(C): pass def test_cls_special_attributes_2(self): astroid = builder.parse(''' - class A: pass - class B: pass + class A(object): pass + class B(object): pass A.__bases__ += (B,) ''', __name__) diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 627903c899..0e73ec12c1 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -231,9 +231,11 @@ def display_type(self): def getattr(self, name, context=None, ignore_locals=False): result = [] - if name in self.special_attributes: + name_in_locals = name in self.locals + + if name in self.special_attributes and not ignore_locals and not name_in_locals: result = [self.special_attributes.lookup(name)] - elif not ignore_locals and name in self.locals: + elif not ignore_locals and name_in_locals: result = self.locals[name] # TODO: should ignore_locals also affect external_attrs? elif name in self.external_attrs: @@ -1520,8 +1522,23 @@ def getattr(self, name, context=None, class_context=True): metaclass will be done. """ - values = self.locals.get(name, []) + self.external_attrs.get(name, []) - if name in self.special_attributes and class_context: + local_values = self.locals.get(name, []) + external_values = self.external_attrs.get(name, []) + values = local_values + external_values + + # Determine if we should look retrieve special attributes. + # If a class has local values with the given name and that given + # name is also a special attribute, then priority should be given + # to the local defined value, irrespective of the underlying + # potential attributes defined by the special model. + # But this is not the case for builtins, for which the + # value can't be redefined locally. In the case of builtins though, + # we always look into the special method and for the rest, + # we only look if there is no local value defined with the same name. + is_builtin = self.root().name == BUILTINS + look_special_attributes = is_builtin or not local_values + + if name in self.special_attributes and class_context and look_special_attributes: result = [self.special_attributes.lookup(name)] if name == '__bases__': # Need special treatment, since they are mutable @@ -1536,9 +1553,11 @@ def getattr(self, name, context=None, class_context=True): if class_context: values += self._metaclass_lookup_attribute(name, context) + if not values: raise exceptions.AttributeInferenceError(target=self, attribute=name, context=context) + return values def _metaclass_lookup_attribute(self, name, context): From c1650a1bed12a27a8d25787e07464ab02689eaf6 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 20 Jun 2016 18:24:10 +0100 Subject: [PATCH 258/312] Add runtime virtual classes for DictKeys, DictItems, DictValues and DictInstance. --- astroid/interpreter/objects.py | 5 +++++ astroid/interpreter/runtimeabc.py | 12 ++++++++++++ astroid/tests/unittest_object_model.py | 4 ++++ 3 files changed, 21 insertions(+) diff --git a/astroid/interpreter/objects.py b/astroid/interpreter/objects.py index 6b8b02e2a0..6ad9ab48ce 100644 --- a/astroid/interpreter/objects.py +++ b/astroid/interpreter/objects.py @@ -518,6 +518,7 @@ def getattr(self, name, context=None): return list(self.igetattr(name, context=context)) +@util.register_implementation(treeabc.Dict) class DictInstance(BaseInstance): """Special kind of instances for dictionaries @@ -531,15 +532,19 @@ class DictInstance(BaseInstance): # Custom objects tailored for dictionaries, which are used to # disambiguate between the types of Python 2 dict's method returns # and Python 3 (where they return set like objects). +@util.register_implementation(runtimeabc.DictItems) class DictItems(Proxy): __str__ = base.NodeNG.__str__ __repr__ = base.NodeNG.__repr__ +@util.register_implementation(runtimeabc.DictKeys) class DictKeys(Proxy): __str__ = base.NodeNG.__str__ __repr__ = base.NodeNG.__repr__ + +@util.register_implementation(runtimeabc.DictValues) class DictValues(Proxy): __str__ = base.NodeNG.__str__ __repr__ = base.NodeNG.__repr__ diff --git a/astroid/interpreter/runtimeabc.py b/astroid/interpreter/runtimeabc.py index 04ea153f2b..8ecfc4d1a5 100644 --- a/astroid/interpreter/runtimeabc.py +++ b/astroid/interpreter/runtimeabc.py @@ -52,3 +52,15 @@ class Super(RuntimeObject): class FrozenSet(RuntimeObject): """Class representing a frozenset.""" + + +class DictKeys(RuntimeObject): + """The class of {}.keys.""" + + +class DictValues(RuntimeObject): + """The class of {}.values.""" + + +class DictItems(RuntimeObject): + """The class of {}.items().""" diff --git a/astroid/tests/unittest_object_model.py b/astroid/tests/unittest_object_model.py index de4384fa91..5d7635e616 100644 --- a/astroid/tests/unittest_object_model.py +++ b/astroid/tests/unittest_object_model.py @@ -16,6 +16,7 @@ from astroid import MANAGER from astroid import test_utils from astroid.interpreter import objects +from astroid.interpreter import runtimeabc BUILTINS = MANAGER.builtins() @@ -574,12 +575,15 @@ def test_wrapper_objects_for_dict_methods_python3(self): ''') values = next(ast_nodes[0].infer()) self.assertIsInstance(values, objects.DictValues) + self.assertIsInstance(values, runtimeabc.DictValues) self.assertEqual([elt.value for elt in values.elts], [1, 3]) keys = next(ast_nodes[1].infer()) self.assertIsInstance(keys, objects.DictKeys) + self.assertIsInstance(keys, runtimeabc.DictKeys) self.assertEqual([elt.value for elt in keys.elts], [1, 2]) items = next(ast_nodes[2].infer()) self.assertIsInstance(items, objects.DictItems) + self.assertIsInstance(items, runtimeabc.DictItems) class LruCacheModelTest(unittest.TestCase): From cc980d95798218613e8b77c7fbf720c355f8274d Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 27 Jun 2016 18:24:18 +0100 Subject: [PATCH 259/312] Add more up-to-date package information. Close #330 --- astroid/__pkginfo__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/astroid/__pkginfo__.py b/astroid/__pkginfo__.py index df41ce7d34..b61529992f 100644 --- a/astroid/__pkginfo__.py +++ b/astroid/__pkginfo__.py @@ -23,10 +23,10 @@ license = 'LGPL' -author = 'Logilab' -author_email = 'pylint-dev@lists.logilab.org' +author = 'Python Code Quality Authority' +author_email = 'code-quality@python.org' mailinglist = "mailto://%s" % author_email -web = 'http://bitbucket.org/logilab/astroid' +web = 'https://github.com/PyCQA/astroid' description = "A abstract syntax tree for Python with inference support." From 0ddafea1c4122e691fd5e21a317c5221b1a7862a Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 27 Jun 2016 18:42:12 +0100 Subject: [PATCH 260/312] Add configuration for .github issues. --- .github/ISSUE_TEMPLATE.md | 13 +++++++++++++ .github/PULL_REQUEST_TEMPLATE.md | 2 ++ 2 files changed, 15 insertions(+) create mode 100644 .github/ISSUE_TEMPLATE.md create mode 100644 .github/PULL_REQUEST_TEMPLATE.md diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md new file mode 100644 index 0000000000..cf7f86b25e --- /dev/null +++ b/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,13 @@ +### Steps to reproduce +1. +2. +3. + +### Current behavior + + +### Expected behavior + + +### ``python -c "from astroid import __pkginfo__; print(__pkginfo__.version)"`` output + diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000000..7514811c61 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,2 @@ +### Fixes / new features +- From 1e9ca50790b22b8e505edf38fd5f2e4bc31f22d5 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 27 Jun 2016 20:57:52 +0100 Subject: [PATCH 261/312] Add coverage configuration, backported from pylint. This removes the old coverage feature. --- .coveragerc | 9 +++++++++ .gitignore | 2 ++ .travis.yml | 6 ++++-- README.rst | 3 +++ tox.ini | 53 +++++++++++++++++++++++++++++++++++++++-------------- 5 files changed, 57 insertions(+), 16 deletions(-) create mode 100644 .coveragerc diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000000..d4008f466e --- /dev/null +++ b/.coveragerc @@ -0,0 +1,9 @@ +[paths] +source = + astroid + +[report] +include = + astroid/* +omit = + */test/* diff --git a/.gitignore b/.gitignore index d74774df07..28ed904729 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ build dist/ astroid.egg-info/ .tox +.coverage +.coverage.* diff --git a/.travis.yml b/.travis.yml index 9075168a8b..d46d986482 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,13 +29,15 @@ install: - $PYTHON_EXE -m pip install pip -U - python -m pip install tox - python -m pip install pip -U - - $PYTHON_EXE -m pip install tox + - $PYTHON_EXE -m pip install tox coverage coveralls - $PYTHON_EXE -m virtualenv --version - $PYTHON_EXE -m easy_install --version - $PYTHON_EXE -m pip --version - $PYTHON_EXE -m tox --version script: - - python -m tox -e $TOXENV + - python -m tox -e coverage-erase,$TOXENV +after_success: + - tox -e coveralls after_failure: - more .tox/log/* | cat - more .tox/*/log/* | cat diff --git a/README.rst b/README.rst index 86bd9d18d3..077b3e577c 100644 --- a/README.rst +++ b/README.rst @@ -8,6 +8,9 @@ Astroid :alt: AppVeyor Build Status :target: https://ci.appveyor.com/project/PCManticore/astroid +.. image:: https://coveralls.io/repos/github/PyCQA/astroid/badge.svg?branch=master + :target: https://coveralls.io/github/PyCQA/astroid?branch=master + What's this? diff --git a/tox.ini b/tox.ini index 888a955031..a778087bf9 100644 --- a/tox.ini +++ b/tox.ini @@ -1,34 +1,59 @@ [tox] -envlist = coverage-clean, py27, py33, py34, py35, pypy, jython, pylint, coverage-stats +envlist = coverage-clean, py27, py33, py34, py35, pypy, jython, pylint skip_missing_interpreters = true [testenv:pylint] commands = pylint -rn --rcfile={toxinidir}/pylintrc {envsitepackagesdir}/astroid -[testenv:coverage-clean] -commands = coverage erase - -[testenv:coverage-stats] -commands = - coverage report --ignore-errors - coverage html --ignore-errors - [testenv] deps = - coverage # Temporary hack, ignore this. dictproxyhack py27,py33,pypy,jython: enum34 lazy-object-proxy nose - py27,py33,py34,py35: numpy + #py27,py33,py34,py35: numpy pytest python-dateutil py27,py33,pypy,jython: singledispatch six wrapt pylint: git+https://github.com/pycqa/pylint@master + coverage + # Disable warnings because they overflow the Travis log on 3.5 -setenv = PYTHONWARNINGS = i -# {posargs} allows passing command line arguments to unittest -commands = coverage run --source astroid --append --branch -m unittest discover {posargs} -s {envsitepackagesdir}/astroid/tests -p "unittest*.py" +setenv = + COVERAGE_FILE = {toxinidir}/.coverage.{envname} + PYTHONWARNINGS = i + +commands = + python -Wi {envsitepackagesdir}/coverage run -m unittest discover -s {envsitepackagesdir}/astroid/tests -p "unittest*.py" + ; Transform absolute path to relative path + ; for compatibility with coveralls.io and fix 'source not available' error. + ; If you can find a cleaner way is welcome + python -c "import os;cov_strip_abspath = open(os.environ['COVERAGE_FILE'], 'r').read().replace('.tox' + os.sep + os.path.relpath('{envsitepackagesdir}', '{toxworkdir}') + os.sep, '');open(os.environ['COVERAGE_FILE'], 'w').write(cov_strip_abspath)" + +[testenv:coveralls] +setenv = + COVERAGE_FILE = {toxinidir}/.coverage +passenv = + * +deps = + coverage + coveralls +skip_install = true +commands = + python {envsitepackagesdir}/coverage combine + python {envsitepackagesdir}/coverage report --rcfile={toxinidir}/.coveragerc -m + - coveralls --rcfile={toxinidir}/.coveragerc +changedir = {toxinidir} + +[testenv:coverage-erase] +setenv = + COVERAGE_FILE = {toxinidir}/.coverage +deps = + coverage +skip_install = true +commands = + python {envsitepackagesdir}/coverage erase +changedir = {toxinidir} From 280e784e266a284e9aa6e4b1959d9554841ebcff Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 27 Jun 2016 21:27:12 +0100 Subject: [PATCH 262/312] Restore the posargs argument --- tox.ini | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index a778087bf9..656893bad2 100644 --- a/tox.ini +++ b/tox.ini @@ -26,8 +26,9 @@ setenv = COVERAGE_FILE = {toxinidir}/.coverage.{envname} PYTHONWARNINGS = i +# {posargs} allows passing command line arguments to unittest commands = - python -Wi {envsitepackagesdir}/coverage run -m unittest discover -s {envsitepackagesdir}/astroid/tests -p "unittest*.py" + python -Wi {envsitepackagesdir}/coverage run -m unittest discover {posargs} -s {envsitepackagesdir}/astroid/tests -p "unittest*.py" ; Transform absolute path to relative path ; for compatibility with coveralls.io and fix 'source not available' error. ; If you can find a cleaner way is welcome From 02f1250178c22f4f4b249e4e5377651de407c522 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 28 Jun 2016 23:47:17 +0100 Subject: [PATCH 263/312] Use the lineno and col_offset provided by variadics in Python 3.5+. Close #335 --- astroid/tests/unittest_nodes.py | 28 ++++++++++++++++++++++++++++ astroid/tree/rebuilder.py | 6 ++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index 4a3a314600..c9d0aa0fe8 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -833,5 +833,33 @@ def test_keys_values_items(self): [(1, 2), (2, 3)]) +class ParameterTest(unittest.TestCase): + + def _variadics_test_helper(self, vararg_lineno, vararg_col_offset, + kwarg_lineno, kwarg_col_offset): + node = test_utils.extract_node(''' + def test(*args, **kwargs): pass + ''') + args = node.args + self.assertIsInstance(args.vararg, astroid.Parameter) + self.assertEqual(args.vararg.lineno, vararg_lineno) + self.assertEqual(args.vararg.col_offset, vararg_col_offset) + self.assertIsInstance(args.kwarg, astroid.Parameter) + self.assertEqual(args.kwarg.lineno, kwarg_lineno) + self.assertEqual(args.kwarg.col_offset, kwarg_col_offset) + + @unittest.skipUnless(sys.version_info[:2] < (3, 5), + "variadics support lineno & col_offset in 3.5+") + def test_no_lineno_for_variadics(self): + self._variadics_test_helper(vararg_lineno=None, vararg_col_offset=None, + kwarg_lineno=None, kwarg_col_offset=None) + + @unittest.skipUnless(sys.version_info[:2] >= (3, 5), + "variadics support lineno & col_offset in 3.5+") + def test_no_lineno_for_variadics(self): + self._variadics_test_helper(vararg_lineno=2, vararg_col_offset=10, + kwarg_lineno=2, kwarg_col_offset=18) + + if __name__ == '__main__': unittest.main() diff --git a/astroid/tree/rebuilder.py b/astroid/tree/rebuilder.py index d5324e7df3..ce98afddb6 100644 --- a/astroid/tree/rebuilder.py +++ b/astroid/tree/rebuilder.py @@ -189,9 +189,11 @@ def _build_variadic(field_name): except AttributeError: param_name = variadic + lineno = getattr(variadic, 'lineno', newnode.lineno) + col_offset = getattr(variadic, 'col_offset', newnode.col_offset) param = nodes.Parameter(name=param_name, - lineno=newnode.lineno, - col_offset=newnode.col_offset, + lineno=lineno, + col_offset=col_offset, parent=newnode) # Get the annotation of the variadic node. annotation = nodes.Empty From 76cfc8e8a02a6ef7e6bc61624d3b24503ee3a235 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Wed, 29 Jun 2016 10:04:26 +0100 Subject: [PATCH 264/312] New function, astroid.extract_node, exported out from astroid.test_utils. --- ChangeLog | 2 + astroid/__init__.py | 2 +- astroid/brain/brain_functools.py | 5 +- astroid/builder.py | 176 +++++++++++++++ astroid/test_utils.py | 177 --------------- astroid/tests/unittest_brain.py | 31 ++- astroid/tests/unittest_builder.py | 6 +- astroid/tests/unittest_helpers.py | 27 +-- astroid/tests/unittest_inference.py | 296 ++++++++++++------------- astroid/tests/unittest_lookup.py | 5 +- astroid/tests/unittest_nodes.py | 56 ++--- astroid/tests/unittest_object_model.py | 54 ++--- astroid/tests/unittest_objects.py | 49 ++-- astroid/tests/unittest_protocols.py | 3 +- astroid/tests/unittest_python3.py | 4 +- astroid/tests/unittest_raw_building.py | 4 +- astroid/tests/unittest_regrtest.py | 4 +- astroid/tests/unittest_scoped_nodes.py | 62 +++--- astroid/tests/unittest_utils.py | 5 +- 19 files changed, 484 insertions(+), 484 deletions(-) diff --git a/ChangeLog b/ChangeLog index c873290464..c903b628e7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,8 @@ Change log for the astroid package (used to be astng) -- + * New function, astroid.extract_node, exported out from astroid.test_utils. + * Fix a crash which occurred when the class of a namedtuple could not be inferred. * Add support for implicit namespace packages (PEP 420) diff --git a/astroid/__init__.py b/astroid/__init__.py index 72f2c78a79..52a9f247f6 100644 --- a/astroid/__init__.py +++ b/astroid/__init__.py @@ -65,7 +65,7 @@ _builtins = raw_building.ast_from_builtins() from astroid.interpreter.util import are_exclusive, unpack_infer from astroid.interpreter.lookup import builtin_lookup -from astroid.builder import parse +from astroid.builder import parse, extract_node from astroid.util import Uninferable, YES diff --git a/astroid/brain/brain_functools.py b/astroid/brain/brain_functools.py index b80ec1cae4..a96fc0655c 100644 --- a/astroid/brain/brain_functools.py +++ b/astroid/brain/brain_functools.py @@ -7,7 +7,6 @@ from astroid.interpreter import util as interpreter_util from astroid.interpreter import objects from astroid.interpreter import objectmodel -from astroid.test_utils import extract_node from astroid import MANAGER @@ -27,7 +26,7 @@ def py__wrapped__(self): @property def pycache_info(self): - cache_info = extract_node(''' + cache_info = astroid.extract_node(''' from functools import _CacheInfo _CacheInfo(0, 0, 0, 0) ''') @@ -39,7 +38,7 @@ def infer_call_result(self, caller, context=None): @property def pycache_clear(self): - node = extract_node('''def cache_clear(): pass''') + node = astroid.extract_node('''def cache_clear(): pass''') return objects.BoundMethod(proxy=node, bound=self._instance.parent.scope()) diff --git a/astroid/builder.py b/astroid/builder.py index bd941c53f6..b3d8b524be 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -242,3 +242,179 @@ def parse(code, module_name='', path=None, apply_transforms=True): builder = AstroidBuilder(manager=MANAGER, apply_transforms=apply_transforms) return builder.string_build(code, modname=module_name, path=path) + + +# The name of the transient function that is used to +# wrap expressions to be extracted when calling +# extract_node. +_TRANSIENT_FUNCTION = '__' + +# The comment used to select a statement to be extracted +# when calling extract_node. +_STATEMENT_SELECTOR = '#@' + +def _extract_expressions(node): + """Find expressions in a call to _TRANSIENT_FUNCTION and extract them. + + The function walks the AST recursively to search for expressions that + are wrapped into a call to _TRANSIENT_FUNCTION. If it finds such an + expression, it completely removes the function call node from the tree, + replacing it by the wrapped expression inside the parent. + + :param node: An astroid node. + :type node: astroid.bases.NodeNG + :yields: The sequence of wrapped expressions on the modified tree + expression can be found. + """ + if (isinstance(node, treeabc.Call) + and isinstance(node.func, treeabc.Name) + and node.func.name == _TRANSIENT_FUNCTION): + real_expr = node.args[0] + real_expr.parent = node.parent + # Search for node in all _astng_fields (the fields checked when + # get_children is called) of its parent. Some of those fields may + # be lists or tuples, in which case the elements need to be checked. + # When we find it, replace it by real_expr, so that the AST looks + # like no call to _TRANSIENT_FUNCTION ever took place. + for name in node.parent._astroid_fields: + child = getattr(node.parent, name) + if isinstance(child, (list, tuple)): + for idx, compound_child in enumerate(child): + + # Can't find a cleaner way to do this. + if isinstance(compound_child, treeabc.Parameter): + if compound_child.default is node: + child[idx].default = real_expr + elif compound_child.annotation is node: + child[idx].annotation = real_expr + else: + child[idx] = real_expr + elif compound_child is node: + child[idx] = real_expr + + elif child is node: + setattr(node.parent, name, real_expr) + yield real_expr + else: + for child in node.get_children(): + for result in _extract_expressions(child): + yield result + + +def _find_statement_by_line(node, line): + """Extracts the statement on a specific line from an AST. + + If the line number of node matches line, it will be returned; + otherwise its children are iterated and the function is called + recursively. + + :param node: An astroid node. + :type node: astroid.bases.NodeNG + :param line: The line number of the statement to extract. + :type line: int + :returns: The statement on the line, or None if no statement for the line + can be found. + :rtype: astroid.bases.NodeNG or None + """ + if isinstance(node, (treeabc.ClassDef, treeabc.FunctionDef)): + # This is an inaccuracy in the AST: the nodes that can be + # decorated do not carry explicit information on which line + # the actual definition (class/def), but .fromline seems to + # be close enough. + node_line = node.fromlineno + else: + node_line = node.lineno + + if node_line == line: + return node + + for child in node.get_children(): + result = _find_statement_by_line(child, line) + if result: + return result + + return None + + +def extract_node(code, module_name=''): + """Parses some Python code as a module and extracts a designated AST node. + + Statements: + To extract one or more statement nodes, append #@ to the end of the line + + Examples: + >>> def x(): + >>> def y(): + >>> return 1 #@ + + The return statement will be extracted. + + >>> class X(object): + >>> def meth(self): #@ + >>> pass + + The funcion object 'meth' will be extracted. + + Expressions: + To extract arbitrary expressions, surround them with the fake + function call __(...). After parsing, the surrounded expression + will be returned and the whole AST (accessible via the returned + node's parent attribute) will look like the function call was + never there in the first place. + + Examples: + >>> a = __(1) + + The const node will be extracted. + + >>> def x(d=__(foo.bar)): pass + + The node containing the default argument will be extracted. + + >>> def foo(a, b): + >>> return 0 < __(len(a)) < b + + The node containing the function call 'len' will be extracted. + + If no statements or expressions are selected, the last toplevel + statement will be returned. + + If the selected statement is a discard statement, (i.e. an expression + turned into a statement), the wrapped expression is returned instead. + + For convenience, singleton lists are unpacked. + + :param str code: A piece of Python code that is parsed as + a module. Will be passed through textwrap.dedent first. + :param str module_name: The name of the module. + :returns: The designated node from the parse tree, or a list of nodes. + :rtype: astroid.bases.NodeNG, or a list of nodes. + """ + def _extract(node): + if isinstance(node, treeabc.Expr): + return node.value + else: + return node + + requested_lines = [] + for idx, line in enumerate(code.splitlines()): + if line.strip().endswith(_STATEMENT_SELECTOR): + requested_lines.append(idx + 1) + + tree = parse(code, module_name=module_name) + extracted = [] + if requested_lines: + for line in requested_lines: + extracted.append(_find_statement_by_line(tree, line)) + + # Modifies the tree. + extracted.extend(_extract_expressions(tree)) + + if not extracted: + extracted.append(tree.body[-1]) + + extracted = [_extract(node) for node in extracted] + if len(extracted) == 1: + return extracted[0] + else: + return extracted diff --git a/astroid/test_utils.py b/astroid/test_utils.py index 8dd5bf4e17..89ed7f3082 100644 --- a/astroid/test_utils.py +++ b/astroid/test_utils.py @@ -7,188 +7,11 @@ import sys import warnings -from astroid import builder from astroid import raw_building from astroid import nodes from astroid import util -# The name of the transient function that is used to -# wrap expressions to be extracted when calling -# extract_node. -_TRANSIENT_FUNCTION = '__' - -# The comment used to select a statement to be extracted -# when calling extract_node. -_STATEMENT_SELECTOR = '#@' - - -def _extract_expressions(node): - """Find expressions in a call to _TRANSIENT_FUNCTION and extract them. - - The function walks the AST recursively to search for expressions that - are wrapped into a call to _TRANSIENT_FUNCTION. If it finds such an - expression, it completely removes the function call node from the tree, - replacing it by the wrapped expression inside the parent. - - :param node: An astroid node. - :type node: astroid.bases.NodeNG - :yields: The sequence of wrapped expressions on the modified tree - expression can be found. - """ - if (isinstance(node, nodes.Call) - and isinstance(node.func, nodes.Name) - and node.func.name == _TRANSIENT_FUNCTION): - real_expr = node.args[0] - real_expr.parent = node.parent - # Search for node in all _astng_fields (the fields checked when - # get_children is called) of its parent. Some of those fields may - # be lists or tuples, in which case the elements need to be checked. - # When we find it, replace it by real_expr, so that the AST looks - # like no call to _TRANSIENT_FUNCTION ever took place. - for name in node.parent._astroid_fields: - child = getattr(node.parent, name) - if isinstance(child, (list, tuple)): - for idx, compound_child in enumerate(child): - - # Can't find a cleaner way to do this. - if isinstance(compound_child, nodes.Parameter): - if compound_child.default is node: - child[idx].default = real_expr - elif compound_child.annotation is node: - child[idx].annotation = real_expr - else: - child[idx] = real_expr - elif compound_child is node: - child[idx] = real_expr - - elif child is node: - setattr(node.parent, name, real_expr) - yield real_expr - else: - for child in node.get_children(): - for result in _extract_expressions(child): - yield result - - -def _find_statement_by_line(node, line): - """Extracts the statement on a specific line from an AST. - - If the line number of node matches line, it will be returned; - otherwise its children are iterated and the function is called - recursively. - - :param node: An astroid node. - :type node: astroid.bases.NodeNG - :param line: The line number of the statement to extract. - :type line: int - :returns: The statement on the line, or None if no statement for the line - can be found. - :rtype: astroid.bases.NodeNG or None - """ - if isinstance(node, (nodes.ClassDef, nodes.FunctionDef)): - # This is an inaccuracy in the AST: the nodes that can be - # decorated do not carry explicit information on which line - # the actual definition (class/def), but .fromline seems to - # be close enough. - node_line = node.fromlineno - else: - node_line = node.lineno - - if node_line == line: - return node - - for child in node.get_children(): - result = _find_statement_by_line(child, line) - if result: - return result - - return None - -def extract_node(code, module_name=''): - """Parses some Python code as a module and extracts a designated AST node. - - Statements: - To extract one or more statement nodes, append #@ to the end of the line - - Examples: - >>> def x(): - >>> def y(): - >>> return 1 #@ - - The return statement will be extracted. - - >>> class X(object): - >>> def meth(self): #@ - >>> pass - - The funcion object 'meth' will be extracted. - - Expressions: - To extract arbitrary expressions, surround them with the fake - function call __(...). After parsing, the surrounded expression - will be returned and the whole AST (accessible via the returned - node's parent attribute) will look like the function call was - never there in the first place. - - Examples: - >>> a = __(1) - - The const node will be extracted. - - >>> def x(d=__(foo.bar)): pass - - The node containing the default argument will be extracted. - - >>> def foo(a, b): - >>> return 0 < __(len(a)) < b - - The node containing the function call 'len' will be extracted. - - If no statements or expressions are selected, the last toplevel - statement will be returned. - - If the selected statement is a discard statement, (i.e. an expression - turned into a statement), the wrapped expression is returned instead. - - For convenience, singleton lists are unpacked. - - :param str code: A piece of Python code that is parsed as - a module. Will be passed through textwrap.dedent first. - :param str module_name: The name of the module. - :returns: The designated node from the parse tree, or a list of nodes. - :rtype: astroid.bases.NodeNG, or a list of nodes. - """ - def _extract(node): - if isinstance(node, nodes.Expr): - return node.value - else: - return node - - requested_lines = [] - for idx, line in enumerate(code.splitlines()): - if line.strip().endswith(_STATEMENT_SELECTOR): - requested_lines.append(idx + 1) - - tree = builder.parse(code, module_name=module_name) - extracted = [] - if requested_lines: - for line in requested_lines: - extracted.append(_find_statement_by_line(tree, line)) - - # Modifies the tree. - extracted.extend(_extract_expressions(tree)) - - if not extracted: - extracted.append(tree.body[-1]) - - extracted = [_extract(node) for node in extracted] - if len(extracted) == 1: - return extracted[0] - else: - return extracted - - def require_version(minver=None, maxver=None): """ Compare version of python interpreter to the given one. Skip the test if older. diff --git a/astroid/tests/unittest_brain.py b/astroid/tests/unittest_brain.py index 766dc56e03..b1642bdf49 100644 --- a/astroid/tests/unittest_brain.py +++ b/astroid/tests/unittest_brain.py @@ -12,7 +12,6 @@ from astroid import nodes from astroid.interpreter import objects from astroid.interpreter import util as interpreterutil -from astroid import test_utils from astroid import util import astroid @@ -82,7 +81,7 @@ def test_hashlib(self): class NamedTupleTest(unittest.TestCase): def test_namedtuple_base(self): - klass = test_utils.extract_node(""" + klass = astroid.extract_node(""" from collections import namedtuple class X(namedtuple("X", ["a", "b", "c"])): @@ -95,7 +94,7 @@ class X(namedtuple("X", ["a", "b", "c"])): self.assertFalse(anc.parent is None) def test_namedtuple_inference(self): - klass = test_utils.extract_node(""" + klass = astroid.extract_node(""" from collections import namedtuple name = "X" @@ -110,7 +109,7 @@ class X(namedtuple(name, fields)): set(base.instantiate_class().instance_attrs)) def test_namedtuple_inference_failure(self): - klass = test_utils.extract_node(""" + klass = astroid.extract_node(""" from collections import namedtuple def foo(fields): @@ -121,7 +120,7 @@ def foo(fields): def test_namedtuple_advanced_inference(self): # urlparse return an object of class ParseResult, which has a # namedtuple call and a mixin as base classes - result = test_utils.extract_node(""" + result = astroid.extract_node(""" import six result = __(six.moves.urllib.parse.urlparse('gopher://')) @@ -135,7 +134,7 @@ def test_namedtuple_advanced_inference(self): self.assertEqual(instance.name, 'ParseResult') def test_namedtuple_instance_attrs(self): - result = test_utils.extract_node(''' + result = astroid.extract_node(''' from collections import namedtuple namedtuple('a', 'a b c')(1, 2, 3) #@ ''') @@ -144,7 +143,7 @@ def test_namedtuple_instance_attrs(self): self.assertEqual(attr[0].attrname, name) def test_namedtuple_uninferable_fields(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' x = [A] * 2 from collections import namedtuple l = namedtuple('a', x) @@ -166,7 +165,7 @@ def testExtensionModules(self): class NoseBrainTest(unittest.TestCase): def test_nose_tools(self): - methods = test_utils.extract_node(""" + methods = astroid.extract_node(""" from nose.tools import assert_equal from nose.tools import assert_equals from nose.tools import assert_true @@ -192,7 +191,7 @@ def test_nose_tools(self): class SixBrainTest(unittest.TestCase): def test_attribute_access(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' import six six.moves.http_client #@ six.moves.urllib_parse #@ @@ -262,7 +261,7 @@ def test_attribute_access(self): self.assertEqual(urlretrieve.qname(), 'urllib.request.urlretrieve') def test_from_imports(self): - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' from six.moves import http_client http_client.HTTPSConnection #@ ''') @@ -285,7 +284,7 @@ def test_multiprocessing_module_attributes(self): # Test that module attributes are working, # especially on Python 3.4+, where they are obtained # from a context. - module = test_utils.extract_node(""" + module = astroid.extract_node(""" import multiprocessing """) module = interpreterutil.do_import_module(module, 'multiprocessing') @@ -296,7 +295,7 @@ def test_multiprocessing_module_attributes(self): self.assertIsInstance(cpu_count, astroid.BoundMethod) def test_module_name(self): - module = test_utils.extract_node(""" + module = astroid.extract_node(""" import multiprocessing multiprocessing.SyncManager() """) @@ -365,7 +364,7 @@ def test_multiprocessing_manager(self): class ThreadingBrainTest(unittest.TestCase): def test_threading(self): - module = test_utils.extract_node(""" + module = astroid.extract_node(""" import threading threading.Lock() """) @@ -457,7 +456,7 @@ class MyEnum(enum.IntEnum): 'IntEnum based enums should be a subtype of int') def test_enum_func_form(self): - instance_1, instance_2 = test_utils.extract_node(''' + instance_1, instance_2 = astroid.extract_node(''' from enum import Enum f = Enum('Audience', ['a', 'b', 'c']) f #@ @@ -487,7 +486,7 @@ def test_parser(self): class NumpyBrainTest(unittest.TestCase): def test_numpy(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' import numpy numpy.ones #@ ''') @@ -499,7 +498,7 @@ def test_numpy(self): class PytestBrainTest(unittest.TestCase): def test_pytest(self): - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' import pytest pytest #@ ''') diff --git a/astroid/tests/unittest_builder.py b/astroid/tests/unittest_builder.py index dcb4e57c94..140a4eeb77 100644 --- a/astroid/tests/unittest_builder.py +++ b/astroid/tests/unittest_builder.py @@ -407,7 +407,7 @@ def yiell(): #@ if noe: yield more """ - func = test_utils.extract_node(code) + func = builder.extract_node(code) self.assertIsInstance(func, nodes.FunctionDef) stmt = func.body[0] self.assertIsInstance(stmt, nodes.Expr) @@ -555,7 +555,7 @@ def func2(a={}): a.custom_attr = 0 a #@ ''' - name_nodes = test_utils.extract_node(code) + name_nodes = builder.extract_node(code) for node in name_nodes: self.assertNotIn('custom_attr', next(node.infer()).locals) self.assertNotIn('custom_attr', next(node.infer()).instance_attrs) @@ -588,7 +588,7 @@ def func(): self.assertEqual(chain.value, 'None') def test_not_implemented(self): - node = test_utils.extract_node(''' + node = builder.extract_node(''' NotImplemented #@ ''') inferred = next(node.infer()) diff --git a/astroid/tests/unittest_helpers.py b/astroid/tests/unittest_helpers.py index 1826cfd781..357a2bd94c 100644 --- a/astroid/tests/unittest_helpers.py +++ b/astroid/tests/unittest_helpers.py @@ -8,6 +8,7 @@ import six from six.moves import builtins +import astroid from astroid import builder from astroid import exceptions from astroid.interpreter import util as interpreterutil @@ -43,12 +44,12 @@ def test_object_type(self): ('import sys\nsys#@', self._look_up_in_builtins(types.ModuleType.__name__)), ] for code, expected in pairs: - node = test_utils.extract_node(code) + node = astroid.extract_node(code) objtype = interpreterutil.object_type(node) self.assertIs(objtype, expected) def test_object_type_classes_and_functions(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' def generator(): yield @@ -111,7 +112,7 @@ class Meta(metaclass=abc.ABCMeta): @test_utils.require_version(minver='3.0') def test_object_type_most_derived(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' class A(type): def __new__(*args, **kwargs): return type.__new__(*args, **kwargs) @@ -128,14 +129,14 @@ class D(B , C): #@ self.assertEqual(metaclass, obj_type) def test_inference_errors(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' from unknown import Unknown u = Unknown #@ ''') self.assertEqual(interpreterutil.object_type(node), util.Uninferable) def test_object_type_too_many_types(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' from unknown import Unknown def test(x): if x: @@ -147,7 +148,7 @@ def test(x): self.assertEqual(interpreterutil.object_type(node), util.Uninferable) def test_is_subtype(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class int_subclass(int): pass class A(object): pass #@ @@ -173,7 +174,7 @@ class C(A): pass #@ @test_utils.require_version(maxver='3.0') def test_is_subtype_supertype_old_style_classes(self): - cls_a, cls_b = test_utils.extract_node(''' + cls_a, cls_b = astroid.extract_node(''' class A: #@ pass class B(A): #@ @@ -185,7 +186,7 @@ class B(A): #@ self.assertFalse(interpreterutil.is_supertype(cls_b, cls_a)) def test_is_subtype_supertype_mro_error(self): - cls_e, cls_f = test_utils.extract_node(''' + cls_e, cls_f = astroid.extract_node(''' class A(object): pass class B(A): pass class C(A): pass @@ -200,7 +201,7 @@ class F(D, E): pass #@ self.assertFalse(interpreterutil.is_supertype(cls_f, cls_e)) def test_is_subtype_supertype_unknown_bases(self): - cls_a, cls_b = test_utils.extract_node(''' + cls_a, cls_b = astroid.extract_node(''' from unknown import Unknown class A(Unknown): pass #@ class B(A): pass #@ @@ -211,7 +212,7 @@ class B(A): pass #@ interpreterutil.is_supertype(cls_a, cls_b) def test_is_subtype_supertype_unrelated_classes(self): - cls_a, cls_b = test_utils.extract_node(''' + cls_a, cls_b = astroid.extract_node(''' class A(object): pass #@ class B(object): pass #@ ''') @@ -221,7 +222,7 @@ class B(object): pass #@ self.assertFalse(interpreterutil.is_supertype(cls_b, cls_a)) def test_is_subtype_supertype_classes_no_type_ancestor(self): - cls_a = test_utils.extract_node(''' + cls_a = astroid.extract_node(''' class A(object): #@ pass ''') @@ -230,7 +231,7 @@ class A(object): #@ self.assertFalse(interpreterutil.is_subtype(cls_a, builtin_type)) def test_is_subtype_supertype_classes_metaclasses(self): - cls_a = test_utils.extract_node(''' + cls_a = astroid.extract_node(''' class A(type): #@ pass ''') @@ -240,7 +241,7 @@ class A(type): #@ @test_utils.require_version(maxver='3.0') def test_old_style_class(self): - cls = test_utils.extract_node('''class A: pass''') + cls = astroid.extract_node('''class A: pass''') builtin_type = self._look_up_in_builtins('type') self.assertEqual(interpreterutil.object_type(cls), builtin_type) diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index 5b05a3f66f..e517730761 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -12,7 +12,7 @@ import six from astroid import InferenceError, builder, nodes -from astroid.builder import parse +from astroid.builder import parse, extract_node from astroid import exceptions from astroid.inference import infer_end as inference_infer_end from astroid.interpreter.objects import ( @@ -326,7 +326,7 @@ class A(object): #@ class A(A): #@ pass ''' - a1, a2 = test_utils.extract_node(code, __name__) + a1, a2 = extract_node(code, __name__) a2_ancestors = list(a2.ancestors()) self.assertEqual(len(a2_ancestors), 2) self.assertIs(a2_ancestors[0], a1) @@ -342,7 +342,7 @@ class B(A): #@ class A(B): #@ pass ''' - a1, b, a2 = test_utils.extract_node(code, __name__) + a1, b, a2 = extract_node(code, __name__) a2_ancestors = list(a2.ancestors()) self.assertEqual(len(a2_ancestors), 3) self.assertIs(a2_ancestors[0], b) @@ -381,7 +381,7 @@ def test_exc_ancestors(self): def f(): raise __(NotImplementedError) ''' - error = test_utils.extract_node(code, __name__) + error = extract_node(code, __name__) nie = error.inferred()[0] self.assertIsInstance(nie, nodes.ClassDef) nie_ancestors = [c.name for c in nie.ancestors()] @@ -418,7 +418,7 @@ def test_del1(self): code = ''' del undefined_attr ''' - delete = test_utils.extract_node(code, __name__) + delete = extract_node(code, __name__) self.assertRaises(InferenceError, delete.infer) def test_del2(self): @@ -587,7 +587,7 @@ def test_view(rql, vid, tags=()): tags = list(tags) __(tags).append(vid) ''' - name = test_utils.extract_node(code, __name__) + name = extract_node(code, __name__) it = name.infer() tags = next(it) self.assertIsInstance(tags, nodes.List) @@ -649,7 +649,7 @@ def no_conjugate_member(magic_flag): #@ return something return __(something).conjugate() ''' - func, retval = test_utils.extract_node(code, __name__) + func, retval = extract_node(code, __name__) self.assertEqual( [i.value for i in func.ilookup('something')], [1.0, 1.0j]) @@ -688,7 +688,7 @@ def __getitem__(self, index): A()[0] #@ A()[-1] #@ ''' - ast_nodes = test_utils.extract_node(code, __name__) + ast_nodes = extract_node(code, __name__) expected = [1, 2, 3, 6, 'value', 'f', 3, 6, 42, 41] for node, expected_value in zip(ast_nodes, expected): inferred = next(node.infer()) @@ -696,7 +696,7 @@ def __getitem__(self, index): self.assertEqual(inferred.value, expected_value) def test_invalid_subscripts(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class NoGetitem(object): pass class InvalidGetitem(object): @@ -711,7 +711,7 @@ class InvalidGetitem2(object): self.assertRaises(InferenceError, next, node.infer()) for node in ast_nodes[3:]: self.assertEqual(next(node.infer()), util.Uninferable) - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' [1, 2, 3][None] #@ 'lala'['bala'] #@ ''') @@ -719,7 +719,7 @@ class InvalidGetitem2(object): self.assertRaises(InferenceError, next, node.infer()) def test_bytes_subscript(self): - node = test_utils.extract_node('''b'a'[0]''') + node = extract_node('''b'a'[0]''') inferred = next(node.infer()) self.assertIsInstance(inferred, nodes.Const) if six.PY2: @@ -778,7 +778,7 @@ def test_builtin_help(self): ''' # XXX failing since __builtin__.help assignment has # been moved into a function... - node = test_utils.extract_node(code, __name__) + node = extract_node(code, __name__) inferred = list(node.func.infer()) self.assertEqual(len(inferred), 1, inferred) self.assertIsInstance(inferred[0], Instance) @@ -788,7 +788,7 @@ def test_builtin_open(self): code = ''' open("toto.txt") ''' - node = test_utils.extract_node(code, __name__).func + node = extract_node(code, __name__).func inferred = list(node.infer()) self.assertEqual(len(inferred), 1) if hasattr(sys, 'pypy_version_info'): @@ -886,7 +886,7 @@ def test_unary_not(self): self._test_const_inferred(ast['b'], True) def test_unary_op_numbers(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' +1 #@ -1 #@ ~1 #@ @@ -900,7 +900,7 @@ def test_unary_op_numbers(self): @test_utils.require_version(minver='3.5') def test_matmul(self): - node = test_utils.extract_node(''' + node = extract_node(''' class Array: def __matmul__(self, other): return 42 @@ -947,7 +947,7 @@ def test_binary_op_int_shiftleft(self): self._test_const_inferred(ast['a'], 23<<1) def test_binary_op_other_type(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class A: def __add__(self, other): return other + 42 @@ -962,7 +962,7 @@ def __add__(self, other): self.assertEqual(second, util.Uninferable) def test_binary_op_other_type_using_reflected_operands(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class A(object): def __radd__(self, other): return other + 42 @@ -977,7 +977,7 @@ def __radd__(self, other): self.assertEqual(second.value, 43) def test_binary_op_reflected_and_not_implemented_is_type_error(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): def __radd__(self, other): return NotImplemented @@ -1073,7 +1073,7 @@ def test_nonregr_lambda_arg(self): def f(g = lambda: None): __(g()).x ''' - callfuncnode = test_utils.extract_node(code) + callfuncnode = extract_node(code) inferred = list(callfuncnode.infer()) self.assertEqual(len(inferred), 2, inferred) inferred.remove(util.Uninferable) @@ -1332,7 +1332,7 @@ def __new__(cls, arg): self.assertEqual(len(inferred), 1, inferred) def test__new__bound_methods(self): - node = test_utils.extract_node(''' + node = extract_node(''' class cls(object): pass cls().__new__(cls) #@ ''') @@ -1533,7 +1533,7 @@ def do_a_thing(): self.assertEqual(node.type, 'function') def test_no_infinite_ancestor_loop(self): - klass = test_utils.extract_node(""" + klass = extract_node(""" import datetime def method(self): @@ -1553,7 +1553,7 @@ def __init__(self): self.config = {0: self.config[0]} self.config[0].test() #@ """ - ast = test_utils.extract_node(code, __name__) + ast = extract_node(code, __name__) expr = ast.func.expr self.assertRaises(InferenceError, next, expr.infer()) @@ -1573,7 +1573,7 @@ def test_tuple_builtin_inference(self): tuple(1) #@ tuple(1, 2) #@ """ - ast = test_utils.extract_node(code, __name__) + ast = extract_node(code, __name__) self.assertInferTuple(ast[0], []) self.assertInferTuple(ast[1], [1]) @@ -1605,7 +1605,7 @@ def test_frozenset_builtin_inference(self): frozenset(1) #@ frozenset(1, 2) #@ """ - ast = test_utils.extract_node(code, __name__) + ast = extract_node(code, __name__) self.assertInferFrozenSet(ast[0], []) self.assertInferFrozenSet(ast[1], [1, 2]) @@ -1636,7 +1636,7 @@ def test_set_builtin_inference(self): set(1) #@ set(1, 2) #@ """ - ast = test_utils.extract_node(code, __name__) + ast = extract_node(code, __name__) self.assertInferSet(ast[0], []) self.assertInferSet(ast[1], [1, 2]) @@ -1667,7 +1667,7 @@ def test_list_builtin_inference(self): list(1) #@ list(1, 2) #@ """ - ast = test_utils.extract_node(code, __name__) + ast = extract_node(code, __name__) self.assertInferList(ast[0], []) self.assertInferList(ast[1], [1, 1, 2]) self.assertInferList(ast[2], [1, 2, 3]) @@ -1682,7 +1682,7 @@ def test_list_builtin_inference(self): self.assertEqual(inferred.qname(), "{}.list".format(BUILTINS)) def test_conversion_of_dict_methods(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' list({1:2, 2:3}.values()) #@ list({1:2, 2:3}.keys()) #@ tuple({1:2, 2:3}.values()) #@ @@ -1702,7 +1702,7 @@ def test_builtin_inference_py3k(self): tuple(b"abc") #@ set(b"abc") #@ """ - ast = test_utils.extract_node(code, __name__) + ast = extract_node(code, __name__) self.assertInferList(ast[0], [97, 98, 99]) self.assertInferTuple(ast[1], [97, 98, 99]) self.assertInferSet(ast[2], [97, 98, 99]) @@ -1732,7 +1732,7 @@ def func(): dict([None, None]) #@ """ - ast = test_utils.extract_node(code, __name__) + ast = extract_node(code, __name__) self.assertInferDict(ast[0], {}) self.assertInferDict(ast[1], {'a': 1, 'b': 2, 'c': 3}) for i in range(2, 5): @@ -1750,7 +1750,7 @@ def func(): @unittest.expectedFailure def test_dict_inference_call_context_not_propagated(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' def using_unknown_kwargs(**kwargs): return dict(**kwargs) using_unknown_kwargs(a=1, b=2) #@ @@ -1758,7 +1758,7 @@ def using_unknown_kwargs(**kwargs): self.assertInferDict(ast_node, {'a': 1, 'b': 2}) def test_dict_inference_kwargs(self): - ast_node = test_utils.extract_node('''dict(a=1, b=2, **{'c': 3})''') + ast_node = extract_node('''dict(a=1, b=2, **{'c': 3})''') self.assertInferDict(ast_node, {'a': 1, 'b': 2, 'c': 3}) @test_utils.require_version('3.5') @@ -1769,7 +1769,7 @@ def test_dict_inference_for_multiple_starred(self): ('dict({"a":1}, b=2, **{"c":3})', {'a':1, 'b':2, 'c':3}), ] for code, expected_value in pairs: - node = test_utils.extract_node(code) + node = extract_node(code) self.assertInferDict(node, expected_value) def test_dict_invalid_args(self): @@ -1779,7 +1779,7 @@ def test_dict_invalid_args(self): 'dict(**[])', ] for invalid in invalid_values: - ast_node = test_utils.extract_node(invalid) + ast_node = extract_node(invalid) inferred = next(ast_node.infer()) self.assertIsInstance(inferred, Instance) self.assertEqual(inferred.qname(), "{}.dict".format(BUILTINS)) @@ -1808,7 +1808,7 @@ def test_str_methods(self): ' '.find() #@ ' '.count() #@ """ - ast = test_utils.extract_node(code, __name__) + ast = extract_node(code, __name__) self.assertInferConst(ast[0], u'') for i in range(1, 16): self.assertInferConst(ast[i], '') @@ -1839,7 +1839,7 @@ def test_unicode_methods(self): u' '.find() #@ u' '.count() #@ """ - ast = test_utils.extract_node(code, __name__) + ast = extract_node(code, __name__) self.assertInferConst(ast[0], '') for i in range(1, 16): self.assertInferConst(ast[i], u'') @@ -2026,11 +2026,11 @@ def no_yield_mgr(): self.assertRaises(InferenceError, next, module['no_yield'].infer()) def test_unary_op_leaks_stop_iteration(self): - node = test_utils.extract_node('+[] #@') + node = extract_node('+[] #@') self.assertEqual(util.Uninferable, next(node.infer())) def test_unary_operands(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' import os def func(): pass from missing import missing @@ -2082,7 +2082,7 @@ def lala(self): return 24 self.assertEqual(inferred, util.Uninferable) def test_unary_op_instance_method_not_callable(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A: __pos__ = (i for i in range(10)) +A() #@ @@ -2090,7 +2090,7 @@ class A: self.assertRaises(InferenceError, next, ast_node.infer()) def test_binary_op_type_errors(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' import collections 1 + "a" #@ 1 - [] #@ @@ -2165,7 +2165,7 @@ def __radd__(self, other): self.assertEqual(str(error), expected_value) def test_unary_type_errors(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' import collections ~[] #@ ~() #@ @@ -2208,7 +2208,7 @@ class A(object): pass def test_unary_empty_type_errors(self): # These aren't supported right now - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' ~(2 and []) #@ -(0 or {}) #@ ''') @@ -2233,7 +2233,7 @@ def test_bool_value_recursive(self): ('frozenset((1, 2))', True), ] for code, expected in pairs: - node = test_utils.extract_node(code) + node = extract_node(code) inferred = next(node.infer()) self.assertEqual(inferred.bool_value(), expected) @@ -2303,7 +2303,7 @@ def true_value(): self.assertEqual(compare.bool_value(), util.Uninferable) def test_bool_value_instances(self): - instances = test_utils.extract_node(''' + instances = extract_node(''' class FalseBoolInstance(object): def {bool}(self): return False @@ -2339,7 +2339,7 @@ class NonMethods(object): self.assertEqual(inferred.bool_value(), expected_value) def test_infer_coercion_rules_for_floats_complex(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' 1 + 1.0 #@ 1 * 1.0 #@ 2 - 1.0 #@ @@ -2355,7 +2355,7 @@ def test_infer_coercion_rules_for_floats_complex(self): self.assertEqual(inferred.value, expected) def test_binop_list_with_elts(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' x = [A] * 1 [1] + x ''') @@ -2366,7 +2366,7 @@ def test_binop_list_with_elts(self): self.assertIs(inferred.elts[1], util.Uninferable) def test_binop_same_types(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class A(object): def __add__(self, other): return 42 @@ -2382,7 +2382,7 @@ def __add__(self, other): self.assertEqual(inferred.value, expected) def test_binop_different_types_reflected_only(self): - node = test_utils.extract_node(''' + node = extract_node(''' class A(object): pass class B(object): @@ -2395,7 +2395,7 @@ def __radd__(self, other): self.assertEqual(inferred.name, 'A') def test_binop_different_types_normal_not_implemented_and_reflected(self): - node = test_utils.extract_node(''' + node = extract_node(''' class A(object): def __add__(self, other): return NotImplemented @@ -2409,7 +2409,7 @@ def __radd__(self, other): self.assertEqual(inferred.name, 'A') def test_binop_different_types_no_method_implemented(self): - node = test_utils.extract_node(''' + node = extract_node(''' class A(object): pass class B(object): pass @@ -2419,7 +2419,7 @@ class B(object): pass self.assertEqual(inferred, util.Uninferable) def test_binop_different_types_reflected_and_normal_not_implemented(self): - node = test_utils.extract_node(''' + node = extract_node(''' class A(object): def __add__(self, other): return NotImplemented class B(object): @@ -2430,7 +2430,7 @@ def __radd__(self, other): return NotImplemented self.assertEqual(inferred, util.Uninferable) def test_binop_subtype(self): - node = test_utils.extract_node(''' + node = extract_node(''' class A(object): pass class B(A): def __add__(self, other): return other @@ -2441,7 +2441,7 @@ def __add__(self, other): return other self.assertEqual(inferred.name, 'A') def test_binop_subtype_implemented_in_parent(self): - node = test_utils.extract_node(''' + node = extract_node(''' class A(object): def __add__(self, other): return other class B(A): pass @@ -2452,7 +2452,7 @@ class B(A): pass self.assertEqual(inferred.name, 'A') def test_binop_subtype_not_implemented(self): - node = test_utils.extract_node(''' + node = extract_node(''' class A(object): pass class B(A): @@ -2463,7 +2463,7 @@ def __add__(self, other): return NotImplemented self.assertEqual(inferred, util.Uninferable) def test_binop_supertype(self): - node = test_utils.extract_node(''' + node = extract_node(''' class A(object): pass class B(A): @@ -2476,7 +2476,7 @@ def __radd__(self, other): self.assertEqual(inferred.name, 'A') def test_binop_supertype_rop_not_implemented(self): - node = test_utils.extract_node(''' + node = extract_node(''' class A(object): def __add__(self, other): return other @@ -2490,7 +2490,7 @@ def __radd__(self, other): self.assertEqual(inferred.name, 'B') def test_binop_supertype_both_not_implemented(self): - node = test_utils.extract_node(''' + node = extract_node(''' class A(object): def __add__(self): return NotImplemented class B(A): @@ -2502,7 +2502,7 @@ def __radd__(self, other): self.assertEqual(inferred, util.Uninferable) def test_binop_inferrence_errors(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' from unknown import Unknown class A(object): def __add__(self, other): return NotImplemented @@ -2517,7 +2517,7 @@ def __add__(self, other): return Unknown self.assertEqual(next(node.infer()), util.Uninferable) def test_binop_ambiguity(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class A(object): def __add__(self, other): if isinstance(other, B): @@ -2540,7 +2540,7 @@ def __radd__(self, other): self.assertEqual(next(node.infer()), util.Uninferable) def test_bin_op_supertype_more_complicated_example(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): def __init__(self): self.foo = 42 @@ -2560,7 +2560,7 @@ def __radd__(self, other): self.assertEqual(int(inferred.value), 45) def test_aug_op_same_type_not_implemented(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): def __iadd__(self, other): return NotImplemented def __add__(self, other): return NotImplemented @@ -2569,7 +2569,7 @@ def __add__(self, other): return NotImplemented self.assertEqual(next(ast_node.infer()), util.Uninferable) def test_aug_op_same_type_aug_implemented(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): def __iadd__(self, other): return other f = A() @@ -2580,7 +2580,7 @@ def __iadd__(self, other): return other self.assertEqual(inferred.name, 'A') def test_aug_op_same_type_aug_not_implemented_normal_implemented(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): def __iadd__(self, other): return NotImplemented def __add__(self, other): return 42 @@ -2592,7 +2592,7 @@ def __add__(self, other): return 42 self.assertEqual(inferred.value, 42) def test_aug_op_subtype_both_not_implemented(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): def __iadd__(self, other): return NotImplemented def __add__(self, other): return NotImplemented @@ -2604,7 +2604,7 @@ class B(A): self.assertEqual(next(ast_node.infer()), util.Uninferable) def test_aug_op_subtype_aug_op_is_implemented(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): def __iadd__(self, other): return 42 class B(A): @@ -2617,7 +2617,7 @@ class B(A): self.assertEqual(inferred.value, 42) def test_aug_op_subtype_normal_op_is_implemented(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): def __add__(self, other): return 42 class B(A): @@ -2630,7 +2630,7 @@ class B(A): self.assertEqual(inferred.value, 42) def test_aug_different_types_no_method_implemented(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): pass class B(object): pass f = A() @@ -2639,7 +2639,7 @@ class B(object): pass self.assertEqual(next(ast_node.infer()), util.Uninferable) def test_aug_different_types_augop_implemented(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): def __iadd__(self, other): return other class B(object): pass @@ -2651,7 +2651,7 @@ class B(object): pass self.assertEqual(inferred.name, 'B') def test_aug_different_types_aug_not_implemented(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): def __iadd__(self, other): return NotImplemented def __add__(self, other): return other @@ -2664,7 +2664,7 @@ class B(object): pass self.assertEqual(inferred.name, 'B') def test_aug_different_types_aug_not_implemented_rop_fallback(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): def __iadd__(self, other): return NotImplemented def __add__(self, other): return NotImplemented @@ -2678,7 +2678,7 @@ def __radd__(self, other): return other self.assertEqual(inferred.name, 'A') def test_augop_supertypes_none_implemented(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): pass class B(object): pass a = A() @@ -2687,7 +2687,7 @@ class B(object): pass self.assertEqual(next(ast_node.infer()), util.Uninferable) def test_augop_supertypes_not_implemented_returned_for_all(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): def __iadd__(self, other): return NotImplemented def __add__(self, other): return NotImplemented @@ -2699,7 +2699,7 @@ def __add__(self, other): return NotImplemented self.assertEqual(next(ast_node.infer()), util.Uninferable) def test_augop_supertypes_augop_implemented(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): def __iadd__(self, other): return other class B(A): pass @@ -2711,7 +2711,7 @@ class B(A): pass self.assertEqual(inferred.name, 'B') def test_augop_supertypes_reflected_binop_implemented(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): def __iadd__(self, other): return NotImplemented class B(A): @@ -2724,7 +2724,7 @@ def __radd__(self, other): return other self.assertEqual(inferred.name, 'A') def test_augop_supertypes_normal_binop_implemented(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): def __iadd__(self, other): return NotImplemented def __add__(self, other): return other @@ -2740,7 +2740,7 @@ def __radd__(self, other): return NotImplemented @unittest.expectedFailure def test_string_interpolation(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' "a%d%d" % (1, 2) #@ "a%(x)s" % {"x": 42} #@ ''') @@ -2751,7 +2751,7 @@ def test_string_interpolation(self): self.assertEqual(inferred.value, expected_value) def test_mul_list_supports__index__(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class Index(object): def __index__(self): return 2 class NotIndex(object): pass @@ -2771,7 +2771,7 @@ def __index__(self): return None self.assertEqual(inferred, util.Uninferable) def test_subscript_supports__index__(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class Index(object): def __index__(self): return 2 class LambdaIndex(object): @@ -2794,7 +2794,7 @@ class NonIndex(object): self.assertRaises(InferenceError, next, ast_nodes[2].infer()) def test_special_method_masquerading_as_another(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class Info(object): def __add__(self, other): return "lala" @@ -2808,7 +2808,7 @@ def __add__(self, other): self.assertEqual(inferred.value, "lala") def test_unary_op_assignment(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): pass def pos(self): return 42 @@ -2822,7 +2822,7 @@ def pos(self): def _slicing_test_helper(self, pairs, cls, get_elts): for code, expected in pairs: - ast_node = test_utils.extract_node(code) + ast_node = extract_node(code) inferred = next(ast_node.infer()) self.assertIsInstance(inferred, cls) self.assertEqual(get_elts(inferred), expected, @@ -2894,11 +2894,11 @@ def test_invalid_slicing_primaries(self): "(1, 2, 3)[1:object]", ] for code in examples: - node = test_utils.extract_node(code) + node = extract_node(code) self.assertRaises(InferenceError, next, node.infer()) def test_instance_slicing(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class A(object): def __getitem__(self, index): return [1, 2, 3, 4, 5][index] @@ -2917,7 +2917,7 @@ def __getitem__(self, index): self.assertEqual([elt.value for elt in inferred.elts], expected) def test_instance_slicing_slices(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): def __getitem__(self, index): return index @@ -2929,7 +2929,7 @@ def __getitem__(self, index): self.assertIsNone(inferred.upper) def test_instance_slicing_fails(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class A(object): def __getitem__(self, index): return 1[index] @@ -2940,7 +2940,7 @@ def __getitem__(self, index): self.assertEqual(next(node.infer()), util.Uninferable) def test_type__new__with_metaclass(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class Metaclass(type): pass class Entity(object): @@ -2963,7 +2963,7 @@ class Entity(object): self.assertEqual(a_inferred.value, 1) def test_type__new__not_enough_arguments(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' type.__new__(1) #@ type.__new__(1, 2) #@ type.__new__(1, 2, 3) #@ @@ -2974,7 +2974,7 @@ def test_type__new__not_enough_arguments(self): self.assertIsInstance(inferred, Instance) def test_type__new__invalid_mcs_argument(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class Class(object): pass type.__new__(1, 2, 3, 4) #@ type.__new__(Class, 2, 3, 4) #@ @@ -2984,7 +2984,7 @@ class Class(object): pass self.assertIsInstance(inferred, Instance) def test_type__new__invalid_name(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class Class(type): pass type.__new__(Class, object, 1, 2) #@ type.__new__(Class, 1, 1, 2) #@ @@ -2995,7 +2995,7 @@ class Class(type): pass self.assertIsInstance(inferred, Instance) def test_type__new__invalid_bases(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' type.__new__(type, 'a', 1, 2) #@ type.__new__(type, 'a', [], 2) #@ type.__new__(type, 'a', {}, 2) #@ @@ -3007,7 +3007,7 @@ def test_type__new__invalid_bases(self): self.assertIsInstance(inferred, Instance) def test_type__new__invalid_attrs(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' type.__new__(type, 'a', (), ()) #@ type.__new__(type, 'a', (), object) #@ type.__new__(type, 'a', (), 1) #@ @@ -3019,7 +3019,7 @@ def test_type__new__invalid_attrs(self): self.assertIsInstance(inferred, Instance) def test_type__new__metaclass_lookup(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class Metaclass(type): def test(cls): pass @classmethod @@ -3045,7 +3045,7 @@ def test1(cls): pass self.assertEqual(attr[0].value, 42) def test_type__new__metaclass_and_ancestors_lookup(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class Book(object): title = 'Ubik' class MetaBook(type): @@ -3061,7 +3061,7 @@ class MetaBook(type): def test_function_metaclasses(self): # These are not supported right now, although # they will be in the future. - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' import six class BookMeta(type): @@ -3085,7 +3085,7 @@ class Book(object): def test_subscript_inference_error(self): # Used to raise StopIteration - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class AttributeDict(dict): def __getitem__(self, name): return self @@ -3097,7 +3097,7 @@ def __getitem__(self, name): self.assertIsNone(safe_infer(ast_node.targets[0])) def test_classmethod_inferred_by_context(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class Super(object): def instance(cls): return cls() @@ -3116,7 +3116,7 @@ def method(self): self.assertEqual(inferred.name, 'Sub') def test_infer_call_result_invalid_dunder_call_on_instance(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class A: __call__ = 42 class B: @@ -3132,7 +3132,7 @@ class C: self.assertRaises(InferenceError, next, inferred.infer_call_result(node)) def test_context_call_for_context_managers(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class A: def __enter__(self): return self @@ -3161,7 +3161,7 @@ def __enter__(self): self.assertEqual(third_c.name, 'A') def test_metaclass_subclasses_arguments_are_classes_not_instances(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(type): def test(cls): return cls @@ -3177,7 +3177,7 @@ class B(object): self.assertEqual(inferred.name, 'B') def test_infer_cls_in_class_methods(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class A(type): def __call__(cls): cls #@ @@ -3192,7 +3192,7 @@ def __call__(cls): @unittest.expectedFailure def test_metaclass_arguments_are_classes_not_instances(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(type): def test(cls): return cls A.test() #@ @@ -3203,7 +3203,7 @@ def test(cls): return cls self.assertEqual(inferred.name, 'A') def test_delayed_attributes_without_slots(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): __slots__ = ('a', ) a = A() @@ -3218,7 +3218,7 @@ class A(object): @test_utils.require_version(maxver='3.0') def test_delayed_attributes_with_old_style_classes(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A: __slots__ = ('a', ) a = A() @@ -3228,7 +3228,7 @@ class A: next(ast_node.infer()).getattr('teta') def test_delayed_attributes_writable_property(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): @property def test(self): @@ -3246,7 +3246,7 @@ def test(self, value): self.assertEqual(value.value, "a") def test_delayed_attributes_non_writable_property(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): @property def test(self): @@ -3263,7 +3263,7 @@ def test(self): self.assertEqual(value.value, 24) def test_lambda_as_methods(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class X: m = lambda self, arg: self.z + arg z = 24 @@ -3275,7 +3275,7 @@ class X: self.assertEqual(inferred.value, 28) def test_inner_value_redefined_by_subclass(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class X(object): M = lambda self, arg: "a" x = 24 @@ -3296,7 +3296,7 @@ def blurb(self): def test_inner_value_redefined_by_subclass_with_mro(self): # This might work, but it currently doesn't due to not being able # to reuse inference contexts. - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class X(object): M = lambda self, arg: arg + 1 x = 24 @@ -3320,7 +3320,7 @@ def blurb(self): class GetattrTest(unittest.TestCase): def test_yes_when_unknown(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' from missing import Missing getattr(1, Unknown) #@ getattr(Unknown, 'a') #@ @@ -3340,7 +3340,7 @@ def test_yes_when_unknown(self): self.assertEqual(inferred, util.Uninferable, node) def test_attrname_not_string(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' getattr(1, 1) #@ c = int getattr(1, c) #@ @@ -3349,7 +3349,7 @@ def test_attrname_not_string(self): self.assertRaises(InferenceError, next, node.infer()) def test_attribute_missing(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' getattr(1, 'ala') #@ getattr(int, 'ala') #@ getattr(float, 'bala') #@ @@ -3359,7 +3359,7 @@ def test_attribute_missing(self): self.assertRaises(InferenceError, next, node.infer()) def test_default(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' getattr(1, 'ala', None) #@ getattr(int, 'bala', int) #@ getattr(int, 'bala', getattr(int, 'portocala', None)) #@ @@ -3377,7 +3377,7 @@ def test_default(self): self.assertIsNone(third.value) def test_lookup(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class A(object): def test(self): pass class B(A): @@ -3421,7 +3421,7 @@ def test(self): self.assertEqual(fifth.bound.name, 'X') def test_lambda(self): - node = test_utils.extract_node(''' + node = extract_node(''' getattr(lambda x: x, 'f') #@ ''') inferred = next(node.infer()) @@ -3431,7 +3431,7 @@ def test_lambda(self): class HasattrTest(unittest.TestCase): def test_inference_errors(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' from missing import Missing hasattr(Unknown, 'ala') #@ @@ -3444,7 +3444,7 @@ def test_inference_errors(self): self.assertEqual(inferred, util.Uninferable) def test_attribute_is_missing(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class A: pass hasattr(int, 'ala') #@ hasattr({}, 'bala') #@ @@ -3456,7 +3456,7 @@ class A: pass self.assertFalse(inferred.value) def test_attribute_is_not_missing(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class A(object): def test(self): pass class B(A): @@ -3480,7 +3480,7 @@ def test(self): self.assertTrue(inferred.value) def test_lambda(self): - node = test_utils.extract_node(''' + node = extract_node(''' hasattr(lambda x: x, 'f') #@ ''') inferred = next(node.infer()) @@ -3508,12 +3508,12 @@ def test_bool_ops(self): ('not (True or False) and True', False), ] for code, expected_value in expected: - node = test_utils.extract_node(code) + node = extract_node(code) inferred = next(node.infer()) self.assertEqual(inferred.value, expected_value) def test_yes_when_unknown(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' from unknown import unknown, any, not_any 0 and unknown #@ unknown or 0 #@ @@ -3524,7 +3524,7 @@ def test_yes_when_unknown(self): self.assertEqual(inferred, util.Uninferable) def test_other_nodes(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' def test(): pass test and 0 #@ 1 and test #@ @@ -3555,12 +3555,12 @@ def meth(self): pass callable(C1) #@''', True), ] for code, expected_value in expected: - node = test_utils.extract_node(code) + node = extract_node(code) inferred = next(node.infer()) self.assertEqual(inferred.value, expected_value) def test_callable_methods(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class C: def test(self): pass @staticmethod @@ -3592,7 +3592,7 @@ class NotReallyCallableDueToPythonMisfeature(object): self.assertTrue(inferred) def test_inference_errors(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' from unknown import unknown callable(unknown) #@ def test(): @@ -3604,7 +3604,7 @@ def test(): self.assertEqual(inferred, util.Uninferable) def test_not_callable(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' callable("") #@ callable(1) #@ callable(True) #@ @@ -3630,7 +3630,7 @@ def test_bool(self): ('from unknown import Unknown; __(bool(Unknown))', util.Uninferable), ] for code, expected in pairs: - node = test_utils.extract_node(code) + node = extract_node(code) inferred = next(node.infer()) if expected is util.Uninferable: self.assertEqual(expected, inferred) @@ -3638,7 +3638,7 @@ def test_bool(self): self.assertEqual(inferred.value, expected) def test_bool_bool_special_method(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class FalseClass: def {method}(self): return False @@ -3672,7 +3672,7 @@ def foo(self): return 0 self.assertEqual(inferred.value, expected_value) def test_bool_instance_not_callable(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class BoolInvalid(object): {method} = 42 class LenInvalid(object): @@ -3698,7 +3698,7 @@ def test_type(self): ('type(frozenset())', 'frozenset'), ] for code, expected in pairs: - node = test_utils.extract_node(code) + node = extract_node(code) inferred = next(node.infer()) self.assertIsInstance(inferred, nodes.ClassDef) self.assertEqual(inferred.name, expected) @@ -3721,7 +3721,7 @@ def test_args(self): (3, ), (), (3, 4, 5), (), (), (4, ), (4, 5), (), (3, ), (), (), (3, ), (42, )] - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' def func(*args): return args func() #@ @@ -3759,7 +3759,7 @@ def test_multiple_starred_args(self): (1, 2, 3), (1, 4, 2, 3, 5, 6, 7), ] - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' def func(a, b, *args): return args func(1, 2, *(1, ), *(2, 3)) #@ @@ -3772,7 +3772,7 @@ def func(a, b, *args): def test_defaults(self): expected_values = [42, 3, 41, 42] - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' def func(a, b, c=42, *args): return c func(1, 2) #@ @@ -3788,7 +3788,7 @@ def func(a, b, c=42, *args): @test_utils.require_version('3.0') def test_kwonly_args(self): expected_values = [24, 24, 42, 23, 24, 24, 54] - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' def test(*, f, b): return f test(f=24, b=33) #@ def test(a, *, f): return f @@ -3813,7 +3813,7 @@ def test_kwargs(self): [('a', 1)], [('a', 'b')], ] - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' def test(**kwargs): return kwargs test(a=1, b=2, c=3) #@ @@ -3827,7 +3827,7 @@ def test(**kwargs): self.assertEqual(value, expected_value) def test_kwargs_and_other_named_parameters(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' def test(a=42, b=24, **kwargs): return kwargs test(42, 24, c=3, d=4) #@ @@ -3849,7 +3849,7 @@ def test(a=42, b=24, **kwargs): def test_kwargs_access_by_name(self): expected_values = [42, 42, 42, 24] - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' def test(**kwargs): return kwargs['f'] test(f=42) #@ @@ -3873,7 +3873,7 @@ def test_multiple_kwargs(self): ('d', 4), ('f', 42), ] - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' def test(**kwargs): return kwargs test(a=1, b=2, **{'c': 3}, **{'d': 4}, f=42) #@ @@ -3884,7 +3884,7 @@ def test(**kwargs): self.assertEqual(value, expected_value) def test_kwargs_are_overriden(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' def test(f): return f test(f=23, **{'f': 34}) #@ @@ -3897,7 +3897,7 @@ def test(f=None): self.assertEqual(inferred, util.Uninferable) def test_fail_to_infer_args(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' def test(a, **kwargs): return a test(*missing) #@ test(*object) #@ @@ -3940,13 +3940,13 @@ def test_slice(self): ('[1, 2, 3][slice(0, 3, 2)]', [1, 3]), ] for node, expected_value in ast_nodes: - ast_node = test_utils.extract_node("__({})".format(node)) + ast_node = extract_node("__({})".format(node)) inferred = next(ast_node.infer()) self.assertIsInstance(inferred, nodes.List) self.assertEqual([elt.value for elt in inferred.elts], expected_value) def test_slice_inference_error(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' from unknown import unknown [1, 2, 3][slice(None, unknown, unknown)] #@ [1, 2, 3][slice(None, missing, missing)] #@ @@ -3968,7 +3968,7 @@ def test_slice_attributes(self): ] for code, values in ast_nodes: lower, upper, step = values - node = test_utils.extract_node(code) + node = extract_node(code) inferred = next(node.infer()) self.assertIsInstance(inferred, nodes.Slice) lower_value = next(inferred.igetattr('start')) @@ -3983,7 +3983,7 @@ def test_slice_attributes(self): self.assertEqual(inferred.pytype(), '%s.slice' % BUILTINS) def test_slice_type(self): - ast_node = test_utils.extract_node('type(slice(None, None, None))') + ast_node = extract_node('type(slice(None, None, None))') inferred = next(ast_node.infer()) self.assertIsInstance(inferred, nodes.ClassDef) self.assertEqual(inferred.name, 'slice') @@ -3992,7 +3992,7 @@ def test_slice_type(self): class DirTest(unittest.TestCase): def test_instance_custom_ok_dir(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class A: def __dir__(self): return ['a', 'b', 'c'] @@ -4029,7 +4029,7 @@ def __init__(self): sorted(expected_values)) def test_modules_dir(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' import collections class A: def __dir__(self): @@ -4044,14 +4044,14 @@ def __dir__(self): self.assertTrue({'OrderedDict', 'deque', 'defaultdict', 'namedtuple'}.issubset(elts)) def test_list_dir(self): - ast_node = test_utils.extract_node('dir([])') + ast_node = extract_node('dir([])') inferred = next(ast_node.infer()) elts = {elt.value for elt in inferred.elts} self.assertTrue({'append', 'count', 'extend', 'index', 'insert', 'mro', 'pop', 'remove', 'reverse', 'sort'}.issubset(elts)) def test_metaclass_dir(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' import six class Meta(type): def __dir__(self): @@ -4078,7 +4078,7 @@ class B(object): self.assertEqual(elts, ['a', 'b']) def test_class_dir(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class A: x = 42 class B(A): @@ -4096,7 +4096,7 @@ class B(A): self.assertTrue({'__bases__', 'x', 'y', '__dict__', '__doc__'}.issubset(elts)) def test_no_args_dir(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A: pass class B: diff --git a/astroid/tests/unittest_lookup.py b/astroid/tests/unittest_lookup.py index a376e3be20..e7179888da 100644 --- a/astroid/tests/unittest_lookup.py +++ b/astroid/tests/unittest_lookup.py @@ -12,7 +12,6 @@ from astroid.interpreter import lookup from astroid import nodes from astroid.tree import scoped_nodes -from astroid import test_utils from astroid import util from astroid.tests import resources @@ -270,7 +269,7 @@ class foo: def test(self): pass ''' - member = test_utils.extract_node(code, __name__).targets[0] + member = builder.extract_node(code, __name__).targets[0] it = member.infer() obj = next(it) self.assertIsInstance(obj, nodes.Const) @@ -287,7 +286,7 @@ def decorator(bla): def funcA(): return 4 ''' - decname = test_utils.extract_node(code, __name__) + decname = builder.extract_node(code, __name__) it = decname.infer() obj = next(it) self.assertIsInstance(obj, nodes.FunctionDef) diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index c9d0aa0fe8..ef276291a4 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -54,7 +54,7 @@ def test(a, b, c=42, *, x=42, **kwargs): self.assertEqual(node.as_string().strip(), code.strip()) def test_as_string_for_list_containing_uninferable(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' def foo(): bar = [arg] * 1 ''') @@ -64,7 +64,7 @@ def foo(): self.assertEqual(binop.as_string(), '([arg]) * (1)') def test_frozenset_as_string(self): - nodes = test_utils.extract_node(''' + nodes = astroid.extract_node(''' frozenset((1, 2, 3)) #@ frozenset({1, 2, 3}) #@ frozenset([1, 2, 3,]) #@ @@ -310,7 +310,7 @@ def setUp(self): self.module2 = resources.build_file('data/module2.py', 'data.module2') def test_do_import_module_works_for_all(self): - import_from, import_ = test_utils.extract_node(''' + import_from, import_ = astroid.extract_node(''' from collections import deque #@ import collections #@ ''') @@ -581,7 +581,7 @@ def decorated_with_lazy(self): return 42 class Python35AsyncTest(unittest.TestCase): def test_async_await_keywords(self): - async_def, async_for, async_with, await_node = test_utils.extract_node(''' + async_def, async_for, async_with, await_node = astroid.extract_node(''' async def func(): #@ async for i in range(10): #@ f = __(await i) @@ -639,7 +639,7 @@ def test_concrete_issubclass(self): class ScopeTest(unittest.TestCase): def test_decorators(self): - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' @test def foo(): pass ''') @@ -672,7 +672,7 @@ class classdef: pass self.assertIsInstance(module['listcomp'].parent.value.scope(), nodes.Module) def test_scope_of_default_argument_value(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' def test(a=__(b)): pass ''') @@ -681,7 +681,7 @@ def test(a=__(b)): @test_utils.require_version(minver='3.0') def test_scope_of_default_keyword_argument_value(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' def test(*, b=__(c)): pass ''') @@ -690,7 +690,7 @@ def test(*, b=__(c)): @test_utils.require_version(minver='3.0') def test_scope_of_annotations(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' def test(a: __(b), *args:__(f), c:__(d)=4, **kwargs: _(l))->__(x): pass ''') @@ -699,7 +699,7 @@ def test(a: __(b), *args:__(f), c:__(d)=4, **kwargs: _(l))->__(x): self.assertIsInstance(scope, nodes.Module) def test_scope_of_list_comprehension_target_composite_nodes(self): - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' [i for data in __([DATA1, DATA2]) for i in data] ''') node = ast_node.elts[0] @@ -707,7 +707,7 @@ def test_scope_of_list_comprehension_target_composite_nodes(self): self.assertIsInstance(scope, nodes.Module) def test_scope_of_nested_list_comprehensions(self): - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' [1 for data in DATA for x in __(data)] ''') scope = ast_node.scope() @@ -717,7 +717,7 @@ def test_scope_of_nested_list_comprehensions(self): self.assertIsInstance(scope, nodes.ListComp) def test_scope_of_list_comprehension_targets(self): - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' [1 for data in DATA] ''') # target is `data` from the list comprehension @@ -729,7 +729,7 @@ def test_scope_of_list_comprehension_targets(self): self.assertIsInstance(scope, nodes.ListComp) def test_scope_of_list_comprehension_value(self): - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' [__(i) for i in DATA] ''') scope = ast_node.scope() @@ -739,7 +739,7 @@ def test_scope_of_list_comprehension_value(self): self.assertIsInstance(scope, nodes.Module) def test_scope_of_dict_comprehension(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' {i: __(j) for (i, j) in DATA} {i:j for (i, j) in __(DATA)} ''') @@ -748,14 +748,14 @@ def test_scope_of_dict_comprehension(self): iter_scope = ast_nodes[1].scope() self.assertIsInstance(iter_scope, nodes.Module) - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' {i:1 for i in DATA}''') target = ast_node.generators[0].target target_scope = target.scope() self.assertIsInstance(target_scope, nodes.DictComp) def test_scope_elt_of_generator_exp(self): - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' list(__(i) for i in range(10)) ''') scope = ast_node.scope() @@ -765,47 +765,47 @@ def test_scope_elt_of_generator_exp(self): class ContextTest(unittest.TestCase): def test_subscript_load(self): - node = test_utils.extract_node('f[1]') + node = astroid.extract_node('f[1]') self.assertIs(node.ctx, astroid.Load) def test_subscript_del(self): - node = test_utils.extract_node('del f[1]') + node = astroid.extract_node('del f[1]') self.assertIs(node.targets[0].ctx, astroid.Del) def test_subscript_store(self): - node = test_utils.extract_node('f[1] = 2') + node = astroid.extract_node('f[1] = 2') subscript = node.targets[0] self.assertIs(subscript.ctx, astroid.Store) def test_list_load(self): - node = test_utils.extract_node('[]') + node = astroid.extract_node('[]') self.assertIs(node.ctx, astroid.Load) def test_list_del(self): - node = test_utils.extract_node('del []') + node = astroid.extract_node('del []') self.assertIs(node.targets[0].ctx, astroid.Del) def test_list_store(self): with self.assertRaises(exceptions.AstroidSyntaxError): - test_utils.extract_node('[0] = 2') + astroid.extract_node('[0] = 2') def test_tuple_load(self): - node = test_utils.extract_node('(1, )') + node = astroid.extract_node('(1, )') self.assertIs(node.ctx, astroid.Load) def test_tuple_store(self): with self.assertRaises(exceptions.AstroidSyntaxError): - test_utils.extract_node('(1, ) = 3') + astroid.extract_node('(1, ) = 3') @test_utils.require_version(minver='3.5') def test_starred_load(self): - node = test_utils.extract_node('a = *b') + node = astroid.extract_node('a = *b') starred = node.value self.assertIs(starred.ctx, astroid.Load) @test_utils.require_version(minver='3.0') def test_starred_store(self): - node = test_utils.extract_node('a, *b = 1, 2') + node = astroid.extract_node('a, *b = 1, 2') starred = node.targets[0].elts[1] self.assertIs(starred.ctx, astroid.Store) @@ -813,7 +813,7 @@ def test_starred_store(self): class FunctionTest(unittest.TestCase): def test_function_not_on_top_of_lambda(self): - lambda_, function_ = test_utils.extract_node(''' + lambda_, function_ = astroid.extract_node(''' lambda x: x #@ def func(): pass #@ ''') @@ -824,7 +824,7 @@ def func(): pass #@ class DictTest(unittest.TestCase): def test_keys_values_items(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' {1: 2, 2:3} ''') self.assertEqual([key.value for key in node.keys], [1, 2]) @@ -837,7 +837,7 @@ class ParameterTest(unittest.TestCase): def _variadics_test_helper(self, vararg_lineno, vararg_col_offset, kwarg_lineno, kwarg_col_offset): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' def test(*args, **kwargs): pass ''') args = node.args diff --git a/astroid/tests/unittest_object_model.py b/astroid/tests/unittest_object_model.py index 5d7635e616..369771aa44 100644 --- a/astroid/tests/unittest_object_model.py +++ b/astroid/tests/unittest_object_model.py @@ -25,7 +25,7 @@ class InstanceModelTest(unittest.TestCase): def test_instance_special_model(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class A: "test" def __init__(self): @@ -58,7 +58,7 @@ def __init__(self): @unittest.expectedFailure def test_instance_local_attributes_overrides_object_model(self): # The instance lookup needs to be changed in order for this to work. - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' class A: @property def __dict__(self): @@ -73,7 +73,7 @@ def __dict__(self): class BoundMethodModelTest(unittest.TestCase): def test_bound_method_model(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class A: def test(self): pass a = A() @@ -94,7 +94,7 @@ class UnboundMethodModelTest(unittest.TestCase): @unittest.skipUnless(six.PY2, "Unbound methods are available in Python 2 only.") def test_unbound_method_model(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class A: def test(self): pass t = A.test @@ -127,7 +127,7 @@ def test(self): pass class ClassModelTest(unittest.TestCase): def test_priority_to_local_defined_values(self): - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' class A: __doc__ = "first" A.__doc__ #@ @@ -138,7 +138,7 @@ class A: @test_utils.require_version(maxver='3.0') def test__mro__old_style(self): - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' class A: pass A.__mro__ @@ -148,7 +148,7 @@ class A: @test_utils.require_version(maxver='3.0') def test__subclasses__old_style(self): - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' class A: pass A.__subclasses__ @@ -157,7 +157,7 @@ class A: next(ast_node.infer()) def test_class_model_correct_mro_subclasses_proxied(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class A(object): pass A.mro #@ @@ -172,7 +172,7 @@ class A(object): @unittest.skipUnless(six.PY2, "Needs old style classes") def test_old_style_classes_no_mro(self): - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' class A: pass A.mro #@ @@ -181,7 +181,7 @@ class A: next(ast_node.infer()) def test_class_model(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class A(object): "test" @@ -252,7 +252,7 @@ def test_priority_to_local_defined_values(self): self.assertEqual(file_value.value, "mine") def test__path__not_a_package(self): - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' import sys sys.__path__ #@ ''') @@ -260,7 +260,7 @@ def test__path__not_a_package(self): next(ast_node.infer()) def test_module_model(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' import xml xml.__path__ #@ xml.__name__ #@ @@ -305,7 +305,7 @@ def test_module_model(self): class FunctionModelTest(unittest.TestCase): def test_partial_descriptor_support(self): - bound, result = test_utils.extract_node(''' + bound, result = astroid.extract_node(''' class A(object): pass def test(self): return 42 f = test.__get__(A(), A) @@ -323,7 +323,7 @@ def test(self): return 42 def test_descriptor_not_inferrring_self(self): # We can't infer __get__(X, Y)() when the bounded function # uses self, because of the tree's parent not being propagating good enough. - result = test_utils.extract_node(''' + result = astroid.extract_node(''' class A(object): x = 42 def test(self): return self.x @@ -335,7 +335,7 @@ def test(self): return self.x self.assertEqual(result.value, 42) def test_descriptors_binding_invalid(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class A: pass def test(self): return 42 test.__get__()() #@ @@ -347,7 +347,7 @@ def test(self): return 42 next(node.infer()) def test_function_model(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' def func(a=1, b=2): """test""" func.__name__ #@ @@ -392,7 +392,7 @@ def func(a=1, b=2): @test_utils.require_version(minver='3.0') def test_empty_return_annotation(self): - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' def test(): pass test.__annotations__ ''') @@ -402,7 +402,7 @@ def test(): pass @test_utils.require_version(minver='3.0') def test_annotations_kwdefaults(self): - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' def test(a: 1, *args: 2, f:4='lala', **kwarg:3)->2: pass test.__annotations__ #@ test.__kwdefaults__ #@ @@ -423,7 +423,7 @@ def test(a: 1, *args: 2, f:4='lala', **kwarg:3)->2: pass @test_utils.require_version(maxver='3.0') def test_function_model_for_python2(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' def test(a=1): "a" @@ -454,7 +454,7 @@ def test(a=1): class GeneratorModelTest(unittest.TestCase): def test_model(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' def test(): "a" yield @@ -489,7 +489,7 @@ class ExceptionModelTest(unittest.TestCase): @unittest.skipIf(six.PY2, "needs Python 3") def test_model_py3(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' try: x[42] except ValueError as err: @@ -509,7 +509,7 @@ def test_model_py3(self): @unittest.skipUnless(six.PY2, "needs Python 2") def test_model_py3(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' try: x[42] except ValueError as err: @@ -530,13 +530,13 @@ def test_model_py3(self): class DictObjectModelTest(unittest.TestCase): def test__class__(self): - ast_node = test_utils.extract_node('{}.__class__') + ast_node = astroid.extract_node('{}.__class__') inferred = next(ast_node.infer()) self.assertIsInstance(inferred, astroid.ClassDef) self.assertEqual(inferred.name, 'dict') def test_attributes_inferred_as_methods(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' {}.values #@ {}.items #@ {}.keys #@ @@ -547,7 +547,7 @@ def test_attributes_inferred_as_methods(self): @unittest.skipUnless(six.PY2, "needs Python 2") def test_concrete_objects_for_dict_methods(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' {1:1, 2:3}.values() #@ {1:1, 2:3}.keys() #@ {1:1, 2:3}.items() #@ @@ -568,7 +568,7 @@ def test_concrete_objects_for_dict_methods(self): @unittest.skipIf(six.PY2, "needs Python 3") def test_wrapper_objects_for_dict_methods_python3(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' {1:1, 2:3}.values() #@ {1:1, 2:3}.keys() #@ {1:1, 2:3}.items() #@ @@ -590,7 +590,7 @@ class LruCacheModelTest(unittest.TestCase): @unittest.skipIf(six.PY2, "needs Python 3") def test_lru_cache(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' import functools class Foo(object): @functools.lru_cache() diff --git a/astroid/tests/unittest_objects.py b/astroid/tests/unittest_objects.py index f5c70c0fd2..d78301f9df 100644 --- a/astroid/tests/unittest_objects.py +++ b/astroid/tests/unittest_objects.py @@ -6,6 +6,7 @@ import six +import astroid from astroid import exceptions from astroid import nodes from astroid.interpreter import objects @@ -18,7 +19,7 @@ class ObjectsTest(unittest.TestCase): def test_frozenset(self): - node = test_utils.extract_node(""" + node = astroid.extract_node(""" frozenset({1: 2, 2: 3}) #@ """) inferred = next(node.infer()) @@ -39,7 +40,7 @@ def test_frozenset(self): class SuperTests(unittest.TestCase): def test_inferring_super_outside_methods(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class Module(object): pass class StaticMethod(object): @@ -65,7 +66,7 @@ def static(): self.assertEqual(no_arguments.qname(), "%s.super" % BUILTINS) def test_inferring_unbound_super_doesnt_work(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' class Test(object): def __init__(self): super(Test) #@ @@ -75,7 +76,7 @@ def __init__(self): self.assertEqual(unbounded.qname(), "%s.super" % BUILTINS) def test_use_default_inference_on_not_inferring_args(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class Test(object): def __init__(self): super(Lala, self) #@ @@ -94,7 +95,7 @@ def test_super_on_old_style_class(self): # super doesn't work on old style class, but leave # that as an error for pylint. We'll infer Super objects, # but every call will result in a failure at some point. - node = test_utils.extract_node(''' + node = astroid.extract_node(''' class OldStyle: def __init__(self): super(OldStyle, self) #@ @@ -110,7 +111,7 @@ def __init__(self): @test_utils.require_version(minver='3.0') def test_no_arguments_super(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class First(object): pass class Second(First): def test(self): @@ -134,7 +135,7 @@ def test_classmethod(cls): self.assertEqual(second.mro_pointer.name, 'Second') def test_super_simple_cases(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class First(object): pass class Second(First): pass class Third(First): @@ -196,7 +197,7 @@ class Fourth(Third): self.assertEqual(fifth.mro_pointer.name, 'Fourth') def test_super_infer(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' class Super(object): def __init__(self): super(Super, self) #@ @@ -208,7 +209,7 @@ def __init__(self): self.assertIs(inferred, reinferred) def test_inferring_invalid_supers(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class Super(object): def __init__(self): # MRO pointer is not a type @@ -235,7 +236,7 @@ class Bupper(Super): self.assertIsInstance(cm.exception.super_.type, invalid_type) def test_proxied(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' class Super(object): def __init__(self): super(Super, self) #@ @@ -246,7 +247,7 @@ def __init__(self): self.assertIsInstance(proxied, nodes.ClassDef) def test_super_bound_model(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class First(object): def method(self): pass @@ -299,7 +300,7 @@ def method(self): self.assertEqual(sixth.type, 'classmethod') def test_super_getattr_single_inheritance(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class First(object): def test(self): pass class Second(First): @@ -345,7 +346,7 @@ def __init__(self): self.assertEqual(second_unbound.parent.name, 'First') def test_super_invalid_mro(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' class A(object): test = 42 class Super(A, A): @@ -357,7 +358,7 @@ def __init__(self): next(inferred.getattr('test')) def test_super_complex_mro(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class A(object): def spam(self): return "A" def foo(self): return "A" @@ -392,7 +393,7 @@ def __init__(self): self.assertEqual(static.parent.scope().name, 'A') def test_super_data_model(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class X(object): pass class A(X): def __init__(self): @@ -432,7 +433,7 @@ def assertEqualMro(self, klass, expected_mro): expected_mro) def test_super_mro(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class A(object): pass class B(A): pass class C(A): pass @@ -460,7 +461,7 @@ def __init__(self): fifth.super_mro() def test_super_yes_objects(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' from collections import Missing class A(object): def __init__(self): @@ -473,7 +474,7 @@ def __init__(self): self.assertIsInstance(second, objects.Instance) def test_super_invalid_types(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' import collections class A(object): def __init__(self): @@ -486,7 +487,7 @@ def __init__(self): inferred.super_mro() def test_super_properties(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' class Foo(object): @property def dict(self): @@ -507,7 +508,7 @@ def dict(self): class MethodTest(unittest.TestCase): def test_unbound_function_method_difference(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' class A: def test(self): pass A.test @@ -519,7 +520,7 @@ def test(self): pass self.assertIsInstance(inferred, nodes.FunctionDef) def test_unbound_function_from_classmethods(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' class A: @classmethod def test(cls): return cls.b @@ -533,7 +534,7 @@ def b(self): return self self.assertIsInstance(inferred, nodes.FunctionDef) def test_static_method(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' class A: @staticmethod def test(self): pass @@ -544,7 +545,7 @@ def test(self): pass @unittest.skipUnless(six.PY2, "Unbound methods are available only on Python 2") def test_underlying_proxied_unbound_method(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' class A: def test(self): pass A.test @@ -553,7 +554,7 @@ def test(self): pass self.assertIsInstance(inferred._proxied, nodes.FunctionDef) def test_underlying_proxied_bound_method(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' class A: def test(self): pass A().test diff --git a/astroid/tests/unittest_protocols.py b/astroid/tests/unittest_protocols.py index 75b8d65b84..82d01c7d0c 100644 --- a/astroid/tests/unittest_protocols.py +++ b/astroid/tests/unittest_protocols.py @@ -6,11 +6,12 @@ import unittest import astroid -from astroid.test_utils import extract_node, require_version +from astroid.test_utils import require_version from astroid import InferenceError from astroid import nodes from astroid import util from astroid.tree.node_classes import AssignName, Const, Name, Starred +from astroid import extract_node @contextlib.contextmanager diff --git a/astroid/tests/unittest_python3.py b/astroid/tests/unittest_python3.py index d37f73dd34..e558607875 100644 --- a/astroid/tests/unittest_python3.py +++ b/astroid/tests/unittest_python3.py @@ -7,9 +7,9 @@ from astroid import nodes from astroid.tree.node_classes import Assign, Expr, YieldFrom, Name, Const from astroid import raw_building -from astroid.builder import AstroidBuilder +from astroid.builder import AstroidBuilder, extract_node from astroid.tree.scoped_nodes import ClassDef, FunctionDef -from astroid.test_utils import require_version, extract_node +from astroid.test_utils import require_version class Python3TC(unittest.TestCase): diff --git a/astroid/tests/unittest_raw_building.py b/astroid/tests/unittest_raw_building.py index bf4b762a5d..83cffb6b42 100644 --- a/astroid/tests/unittest_raw_building.py +++ b/astroid/tests/unittest_raw_building.py @@ -7,7 +7,7 @@ from six.moves import builtins # pylint: disable=import-error -from astroid.builder import AstroidBuilder +from astroid.builder import AstroidBuilder, extract_node from astroid import nodes from astroid import raw_building from astroid import test_utils @@ -37,7 +37,7 @@ def test_open_is_inferred_correctly(self): for name, _ in inspect.getmembers(builtins, predicate=inspect.isbuiltin): if name == 'print': continue - node = test_utils.extract_node('{0} #@'.format(name)) + node = extract_node('{0} #@'.format(name)) inferred = next(node.infer()) self.assertIsInstance(inferred, nodes.FunctionDef, name) self.assertEqual(inferred.root().name, BUILTINS, name) diff --git a/astroid/tests/unittest_regrtest.py b/astroid/tests/unittest_regrtest.py index deaad3c3af..ff957dc94b 100644 --- a/astroid/tests/unittest_regrtest.py +++ b/astroid/tests/unittest_regrtest.py @@ -8,12 +8,12 @@ import six from astroid import MANAGER, Instance, nodes -from astroid.builder import AstroidBuilder +from astroid.builder import AstroidBuilder, extract_node from astroid import exceptions from astroid.interpreter import lookup from astroid.manager import AstroidManager from astroid import raw_building -from astroid.test_utils import require_version, extract_node, bootstrap +from astroid.test_utils import require_version, bootstrap from astroid.tests import resources from astroid import transforms diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index 6a8012f8d8..0fbc099abc 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -318,7 +318,7 @@ def test_is_abstract(self): self.assertFalse(func.is_abstract(pass_is_abstract=False)) def test_is_abstract_decorated(self): - methods = test_utils.extract_node(""" + methods = builder.extract_node(""" import abc class Klass(object): @@ -540,7 +540,7 @@ def long_classmethod(cls): 'classmethod') def test_igetattr(self): - func = test_utils.extract_node(''' + func = builder.extract_node(''' def test(): pass ''') @@ -576,7 +576,7 @@ def test_cls_special_attributes_1(self): if not cls.newstyle: self.assertRaises(AttributeInferenceError, cls.getattr, '__mro__') - ast_list = test_utils.extract_node("[1]") + ast_list = builder.extract_node("[1]") for cls in (ast_list._proxied, nodes.Const(1)._proxied): self.assertEqual(len(cls.getattr('__bases__')), 1) @@ -588,7 +588,7 @@ def test_cls_special_attributes_1(self): self.assertEqual(len(cls.getattr('__mro__')), 1) def test__mro__attribute(self): - node = test_utils.extract_node(''' + node = builder.extract_node(''' class A(object): pass class B(object): pass class C(A, B): pass @@ -598,7 +598,7 @@ class C(A, B): pass self.assertEqual(mro.elts, node.mro()) def test__bases__attribute(self): - node = test_utils.extract_node(''' + node = builder.extract_node(''' class A(object): pass class B(object): pass class C(A, B): pass @@ -1037,7 +1037,7 @@ class ThirdImpl(Simple, SecondMeta): self.assertEqual(meta.name, metaclass) def test_metaclass_type(self): - klass = test_utils.extract_node(""" + klass = builder.extract_node(""" def with_metaclass(meta, base=object): return meta("NewBase", (base, ), {}) @@ -1049,7 +1049,7 @@ class ClassWithMeta(with_metaclass(type)): #@ [base.name for base in klass.ancestors()]) def test_no_infinite_metaclass_loop(self): - klass = test_utils.extract_node(""" + klass = builder.extract_node(""" class SSS(object): class JJJ(object): @@ -1071,7 +1071,7 @@ class BBB(AAA.JJJ): self.assertIn('JJJ', ancestors) def test_no_infinite_metaclass_loop_with_redefine(self): - nodes = test_utils.extract_node(""" + nodes = builder.extract_node(""" import datetime class A(datetime.date): #@ @@ -1089,7 +1089,7 @@ class B(datetime.date): #@ self.assertEqual(None, klass.metaclass()) def test_metaclass_generator_hack(self): - klass = test_utils.extract_node(""" + klass = builder.extract_node(""" import six class WithMeta(six.with_metaclass(type, object)): #@ @@ -1102,7 +1102,7 @@ class WithMeta(six.with_metaclass(type, object)): #@ 'type', klass.metaclass().name) def test_using_six_add_metaclass(self): - klass = test_utils.extract_node(''' + klass = builder.extract_node(''' import six import abc @@ -1116,7 +1116,7 @@ class WithMeta(object): self.assertEqual(metaclass.qname(), 'abc.ABCMeta') def test_using_invalid_six_add_metaclass_call(self): - klass = test_utils.extract_node(''' + klass = builder.extract_node(''' import six @six.add_metaclass() class Invalid(object): @@ -1265,7 +1265,7 @@ def assertEqualMro(self, klass, expected_mro): @test_utils.require_version(maxver='3.0') def test_no_mro_for_old_style(self): - node = test_utils.extract_node(""" + node = builder.extract_node(""" class Old: pass""") with self.assertRaises(TypeError) as cm: node.mro() @@ -1274,7 +1274,7 @@ class Old: pass""") @test_utils.require_version(maxver='3.0') def test_combined_newstyle_oldstyle_in_mro(self): - node = test_utils.extract_node(''' + node = builder.extract_node(''' class Old: pass class New(object): @@ -1380,7 +1380,7 @@ class Duplicates(str, str): pass self.assertIsInstance(cm.exception, ResolveError) def test_generator_from_infer_call_result_parent(self): - func = test_utils.extract_node(""" + func = builder.extract_node(""" import contextlib @contextlib.contextmanager @@ -1392,7 +1392,7 @@ def test(): #@ self.assertEqual(result.parent, func) def test_type_three_arguments(self): - classes = test_utils.extract_node(""" + classes = builder.extract_node(""" type('A', (object, ), {"a": 1, "b": 2, missing: 3}) #@ """) first = next(classes.infer()) @@ -1409,7 +1409,7 @@ def test_type_three_arguments(self): first.getattr("missing") def test_implicit_metaclass(self): - cls = test_utils.extract_node(""" + cls = builder.extract_node(""" class A(object): pass """) @@ -1417,7 +1417,7 @@ class A(object): self.assertEqual(cls.implicit_metaclass(), type_cls) def test_implicit_metaclass_lookup(self): - cls = test_utils.extract_node(''' + cls = builder.extract_node(''' class A(object): pass ''') @@ -1428,7 +1428,7 @@ class A(object): def test_metaclass_lookup_using_same_class(self): # Check that we don't have recursive attribute access for metaclass - cls = test_utils.extract_node(''' + cls = builder.extract_node(''' class A(object): pass ''') self.assertEqual(len(cls.getattr('mro')), 1) @@ -1499,13 +1499,13 @@ class A(object): @test_utils.require_version(maxver='3.0') def test_implicit_metaclass_is_none(self): - cls = test_utils.extract_node(""" + cls = builder.extract_node(""" class A: pass """) self.assertIsNone(cls.implicit_metaclass()) def test_local_attr_invalid_mro(self): - cls = test_utils.extract_node(""" + cls = builder.extract_node(""" # A has an invalid MRO, local_attr should fallback # to using .ancestors. class A(object, object): @@ -1554,7 +1554,7 @@ class B(A): pass self.assertRaises(DuplicateBasesError, module['B'].mro) def test_instance_bound_method_lambdas(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = builder.extract_node(''' class Test(object): #@ lam = lambda self: self not_method = lambda xargs: xargs @@ -1571,7 +1571,7 @@ class Test(object): #@ self.assertIsInstance(not_method, scoped_nodes.Lambda) def test_class_extra_decorators_frame_is_not_class(self): - ast_node = test_utils.extract_node(''' + ast_node = builder.extract_node(''' def ala(): def bala(): #@ func = 42 @@ -1579,7 +1579,7 @@ def bala(): #@ self.assertEqual(ast_node.extra_decorators, []) def test_class_extra_decorators_only_callfunc_are_considered(self): - ast_node = test_utils.extract_node(''' + ast_node = builder.extract_node(''' class Ala(object): def func(self): #@ pass @@ -1588,7 +1588,7 @@ def func(self): #@ self.assertEqual(ast_node.extra_decorators, []) def test_class_extra_decorators_only_assignment_names_are_considered(self): - ast_node = test_utils.extract_node(''' + ast_node = builder.extract_node(''' class Ala(object): def func(self): #@ pass @@ -1599,7 +1599,7 @@ def __init__(self): self.assertEqual(ast_node.extra_decorators, []) def test_class_extra_decorators_only_same_name_considered(self): - ast_node = test_utils.extract_node(''' + ast_node = builder.extract_node(''' class Ala(object): def func(self): #@ pass @@ -1609,7 +1609,7 @@ def func(self): #@ self.assertEqual(ast_node.type, 'method') def test_class_extra_decorators(self): - static_method, clsmethod = test_utils.extract_node(''' + static_method, clsmethod = builder.extract_node(''' class Ala(object): def static(self): #@ pass @@ -1624,7 +1624,7 @@ def class_method(self): #@ self.assertEqual(static_method.type, 'staticmethod') def test_extra_decorators_only_class_level_assignments(self): - node = test_utils.extract_node(''' + node = builder.extract_node(''' def _bind(arg): return arg.bind @@ -1658,7 +1658,7 @@ def _call_site_from_call(call, func): return scoped_nodes.CallSite(func, call.args, keywords) def _call_site_from_code(self, code): - call = test_utils.extract_node(code) + call = builder.extract_node(code) return self._inferred_call_site_from_call(call) def _inferred_call_site_from_call(self, call): @@ -1666,7 +1666,7 @@ def _inferred_call_site_from_call(self, call): return self._call_site_from_call(call, inferred) def _test_call_site_pair(self, code, expected_args, expected_keywords): - ast_node = test_utils.extract_node(code) + ast_node = builder.extract_node(code) call_site = self._call_site_from_call(ast_node, ast_node.func) self.assertEqual(len(call_site.positional_arguments), len(expected_args)) self.assertEqual([arg.value for arg in call_site.positional_arguments], @@ -1716,7 +1716,7 @@ def test_call_site(self): def _test_call_site_valid_arguments(self, values, invalid): for value in values: - ast_node = test_utils.extract_node(value) + ast_node = builder.extract_node(value) call_site = self._call_site_from_call(ast_node, ast_node.func) self.assertEqual(call_site.has_invalid_arguments(), invalid) @@ -1731,7 +1731,7 @@ def test_call_site_valid_arguments(self): self._test_call_site_valid_arguments(values, invalid=False) def test_duplicated_keyword_arguments(self): - ast_node = test_utils.extract_node('f(f=24, **{"f": 25})') + ast_node = builder.extract_node('f(f=24, **{"f": 25})') site = self._call_site_from_call(ast_node, ast_node.func) self.assertIn('f', site.duplicated_keywords) diff --git a/astroid/tests/unittest_utils.py b/astroid/tests/unittest_utils.py index 8cef049c3f..2282575ff5 100644 --- a/astroid/tests/unittest_utils.py +++ b/astroid/tests/unittest_utils.py @@ -7,7 +7,6 @@ from astroid import InferenceError from astroid import nodes from astroid.interpreter import util -from astroid import test_utils from astroid import util as astroid_util @@ -86,7 +85,7 @@ def exclusive_func2(): self.assertEqual(util.are_exclusive(f4, f2), True) def test_unpack_infer_uninferable_nodes(self): - node = test_utils.extract_node(''' + node = builder.extract_node(''' x = [A] * 1 f = [x, [A] * 2] f @@ -98,7 +97,7 @@ def test_unpack_infer_uninferable_nodes(self): for elt in unpacked)) def test_unpack_infer_empty_tuple(self): - node = test_utils.extract_node(''' + node = builder.extract_node(''' () ''') inferred = next(node.infer()) From bb1c4af0e538e00a5bdad16c8b62260bbe139d9e Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Wed, 29 Jun 2016 17:20:41 +0100 Subject: [PATCH 265/312] Move test specific to pylint into pylint. --- astroid/tests/unittest_regrtest.py | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/astroid/tests/unittest_regrtest.py b/astroid/tests/unittest_regrtest.py index ff957dc94b..cecf6b46a5 100644 --- a/astroid/tests/unittest_regrtest.py +++ b/astroid/tests/unittest_regrtest.py @@ -105,25 +105,6 @@ class A(gobject.GObject): a = astroid['A'] self.assertTrue(a.newstyle) - - def test_pylint_config_attr(self): - try: - from pylint import lint # pylint: disable=unused-variable - except ImportError: - self.skipTest('pylint not available') - mod = MANAGER.ast_from_module_name('pylint.lint') - pylinter = mod['PyLinter'] - expect = ['OptionsManagerMixIn', 'object', 'MessagesHandlerMixIn', - 'ReportsHandlerMixIn', 'BaseTokenChecker', 'BaseChecker', - 'OptionsProviderMixIn'] - self.assertListEqual([c.name for c in pylinter.ancestors()], - expect) - self.assertTrue(list(Instance(pylinter).getattr('config'))) - inferred = list(Instance(pylinter).igetattr('config')) - self.assertEqual(len(inferred), 1) - self.assertEqual(inferred[0].root().name, 'optparse') - self.assertEqual(inferred[0].name, 'Values') - @unittest.skipIf(six.PY2, "TODO: numpy is currently infinitely looping") def test_numpy_crash(self): """test don't crash on numpy""" From 0d495d2893cb62aaa5e7a8814f185b645b1a1477 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Wed, 29 Jun 2016 18:47:45 +0100 Subject: [PATCH 266/312] Add readthedocs badge. --- README.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.rst b/README.rst index 077b3e577c..1f4f637875 100644 --- a/README.rst +++ b/README.rst @@ -11,6 +11,10 @@ Astroid .. image:: https://coveralls.io/repos/github/PyCQA/astroid/badge.svg?branch=master :target: https://coveralls.io/github/PyCQA/astroid?branch=master +.. image:: https://readthedocs.org/projects/astroid/badge/?version=latest + :target: http://astroid.readthedocs.io/en/latest/?badge=latest + :alt: Documentation Status + What's this? From ec8f393318c16df94a5bd632915c23e183631ae7 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sat, 16 Jul 2016 14:17:16 +0300 Subject: [PATCH 267/312] Make Uninferable have a false value by default. --- astroid/tests/unittest_inference.py | 20 +++++++++++++------- astroid/util.py | 5 +++++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index e517730761..766cac2e4b 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -2237,6 +2237,18 @@ def test_bool_value_recursive(self): inferred = next(node.infer()) self.assertEqual(inferred.bool_value(), expected) + def test_genexpr_bool_value(self): + node = extract_node('''(x for x in range(10))''') + self.assertTrue(node.bool_value()) + + def test_name_bool_value(self): + node = extract_node(''' + x = 42 + y = x + y + ''') + self.assertIs(node.bool_value(), util.Uninferable) + def test_bool_value(self): # Verify the truth value of nodes. module = parse(''' @@ -2245,7 +2257,6 @@ def test_bool_value(self): def function(): pass class Class(object): def method(self): pass - genexpr = (x for x in range(10)) dict_comp = {x:y for (x, y) in ((1, 2), (2, 3))} set_comp = {x for x in range(10)} list_comp = [x for x in range(10)] @@ -2258,7 +2269,6 @@ def generator_func(): def true_value(): return True generator = generator_func() - name = generator bin_op = 1 + 2 bool_op = x and y callfunc = test() @@ -2273,8 +2283,6 @@ def true_value(): self.assertTrue(function.bool_value()) klass = module['Class'] self.assertTrue(klass.bool_value()) - genexpr = next(module['genexpr'].infer()) - self.assertTrue(genexpr.bool_value()) dict_comp = next(module['dict_comp'].infer()) self.assertEqual(dict_comp, util.Uninferable) set_comp = next(module['set_comp'].infer()) @@ -2289,10 +2297,8 @@ def true_value(): self.assertTrue(bound_method) generator = next(module['generator'].infer()) self.assertTrue(generator) - name = module['name'].parent.value - self.assertTrue(name.bool_value()) bin_op = module['bin_op'].parent.value - self.assertTrue(bin_op.bool_value()) + self.assertIs(bin_op.bool_value(), util.Uninferable) bool_op = module['bool_op'].parent.value self.assertEqual(bool_op.bool_value(), util.Uninferable) callfunc = module['callfunc'].parent.value diff --git a/astroid/util.py b/astroid/util.py index c3b3fc1adc..fd50a0f2c2 100644 --- a/astroid/util.py +++ b/astroid/util.py @@ -83,6 +83,11 @@ def __getattribute__(self, name): def __call__(self, *args, **kwargs): return self + def __bool__(self): + return False + + __nonzero__ = __bool__ + def accept(self, visitor): func = getattr(visitor, "visit_uninferable") return func(self) From 153ba40f10e026fd3ec936b447147cff873a535a Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 19 Jul 2016 19:17:40 +0300 Subject: [PATCH 268/312] Keep a consistent copyright notice across the board. --- astroid/__init__.py | 2 ++ astroid/__pkginfo__.py | 2 ++ astroid/as_string.py | 2 ++ astroid/brain/brain_builtin_inference.py | 2 ++ astroid/brain/brain_collections.py | 2 ++ astroid/brain/brain_dateutil.py | 2 ++ astroid/brain/brain_functools.py | 2 ++ astroid/brain/brain_gi.py | 2 ++ astroid/brain/brain_hashlib.py | 2 ++ astroid/brain/brain_mechanize.py | 2 ++ astroid/brain/brain_multiprocessing.py | 2 ++ astroid/brain/brain_namedtuple_enum.py | 2 ++ astroid/brain/brain_nose.py | 2 ++ astroid/brain/brain_numpy.py | 2 ++ astroid/brain/brain_pkg_resources.py | 2 ++ astroid/brain/brain_pytest.py | 2 ++ astroid/brain/brain_qt.py | 2 ++ astroid/brain/brain_six.py | 2 ++ astroid/brain/brain_ssl.py | 2 ++ astroid/brain/brain_subprocess.py | 2 ++ astroid/brain/brain_threading.py | 2 ++ astroid/builder.py | 2 ++ astroid/context.py | 2 ++ astroid/decorators.py | 2 ++ astroid/exceptions.py | 2 ++ astroid/inference.py | 2 ++ astroid/interpreter/__init__.py | 2 ++ astroid/interpreter/assign.py | 2 ++ astroid/interpreter/lookup.py | 2 ++ astroid/interpreter/objectmodel.py | 2 ++ astroid/interpreter/objects.py | 2 ++ astroid/interpreter/runtimeabc.py | 2 ++ astroid/interpreter/scope.py | 2 ++ astroid/interpreter/util.py | 2 ++ astroid/manager.py | 2 ++ astroid/modutils.py | 2 ++ astroid/nodes.py | 2 ++ astroid/protocols.py | 2 ++ astroid/raw_building.py | 2 ++ astroid/test_utils.py | 2 ++ astroid/tests/__init__.py | 2 ++ astroid/tests/resources.py | 2 ++ astroid/tests/unittest_brain.py | 2 ++ astroid/tests/unittest_builder.py | 2 ++ astroid/tests/unittest_helpers.py | 2 ++ astroid/tests/unittest_inference.py | 2 ++ astroid/tests/unittest_lookup.py | 2 ++ astroid/tests/unittest_manager.py | 2 ++ astroid/tests/unittest_modutils.py | 2 ++ astroid/tests/unittest_nodes.py | 2 ++ astroid/tests/unittest_object_model.py | 2 ++ astroid/tests/unittest_objects.py | 2 ++ astroid/tests/unittest_protocols.py | 2 ++ astroid/tests/unittest_python3.py | 2 ++ astroid/tests/unittest_raw_building.py | 2 ++ astroid/tests/unittest_regrtest.py | 2 ++ astroid/tests/unittest_scoped_nodes.py | 2 ++ astroid/tests/unittest_transforms.py | 2 ++ astroid/tests/unittest_utils.py | 2 ++ astroid/transforms.py | 2 ++ astroid/tree/__init__.py | 2 ++ astroid/tree/base.py | 2 ++ astroid/tree/node_classes.py | 2 ++ astroid/tree/rebuilder.py | 2 ++ astroid/tree/scoped_nodes.py | 2 ++ astroid/tree/treeabc.py | 2 ++ astroid/util.py | 2 ++ setup.py | 22 +++++----------------- 68 files changed, 139 insertions(+), 17 deletions(-) diff --git a/astroid/__init__.py b/astroid/__init__.py index 52a9f247f6..64911c11b6 100644 --- a/astroid/__init__.py +++ b/astroid/__init__.py @@ -1,3 +1,5 @@ +# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/__pkginfo__.py b/astroid/__pkginfo__.py index b61529992f..ab34ba4683 100644 --- a/astroid/__pkginfo__.py +++ b/astroid/__pkginfo__.py @@ -1,3 +1,5 @@ +# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/as_string.py b/astroid/as_string.py index 652720a7bd..c4651646ed 100644 --- a/astroid/as_string.py +++ b/astroid/as_string.py @@ -1,3 +1,5 @@ +# Copyright (c) 2009-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index 96acbd39be..8a2ed40c6b 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -1,3 +1,5 @@ +# Copyright (c) 2014-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/brain/brain_collections.py b/astroid/brain/brain_collections.py index ad37861850..1fdd40c67c 100644 --- a/astroid/brain/brain_collections.py +++ b/astroid/brain/brain_collections.py @@ -1,3 +1,5 @@ +# Copyright (c) 2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/brain/brain_dateutil.py b/astroid/brain/brain_dateutil.py index 2c385ecc5e..3c14c72dce 100644 --- a/astroid/brain/brain_dateutil.py +++ b/astroid/brain/brain_dateutil.py @@ -1,3 +1,5 @@ +# Copyright (c) 2015-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/brain/brain_functools.py b/astroid/brain/brain_functools.py index a96fc0655c..0f7a73e4c3 100644 --- a/astroid/brain/brain_functools.py +++ b/astroid/brain/brain_functools.py @@ -1,3 +1,5 @@ +# Copyright (c) 2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/brain/brain_gi.py b/astroid/brain/brain_gi.py index 0e42330797..84a93004a3 100644 --- a/astroid/brain/brain_gi.py +++ b/astroid/brain/brain_gi.py @@ -1,3 +1,5 @@ +# Copyright (c) 2013-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/brain/brain_hashlib.py b/astroid/brain/brain_hashlib.py index 2ff438588a..297e6a5d5e 100644 --- a/astroid/brain/brain_hashlib.py +++ b/astroid/brain/brain_hashlib.py @@ -1,3 +1,5 @@ +# Copyright (c) 2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/brain/brain_mechanize.py b/astroid/brain/brain_mechanize.py index 2026d3cdb5..cfcfdae4f5 100644 --- a/astroid/brain/brain_mechanize.py +++ b/astroid/brain/brain_mechanize.py @@ -1,3 +1,5 @@ +# Copyright (c) 2012-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/brain/brain_multiprocessing.py b/astroid/brain/brain_multiprocessing.py index d0fb0724cf..86722e56ab 100644 --- a/astroid/brain/brain_multiprocessing.py +++ b/astroid/brain/brain_multiprocessing.py @@ -1,3 +1,5 @@ +# Copyright (c) 2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/brain/brain_namedtuple_enum.py b/astroid/brain/brain_namedtuple_enum.py index 38c6c5132a..eecdb50c72 100644 --- a/astroid/brain/brain_namedtuple_enum.py +++ b/astroid/brain/brain_namedtuple_enum.py @@ -1,3 +1,5 @@ +# Copyright (c) 2012-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/brain/brain_nose.py b/astroid/brain/brain_nose.py index b9adb2201f..f2a5a84ab6 100644 --- a/astroid/brain/brain_nose.py +++ b/astroid/brain/brain_nose.py @@ -1,3 +1,5 @@ +# Copyright (c) 2015-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/brain/brain_numpy.py b/astroid/brain/brain_numpy.py index 493a0dcfbd..e7ba139f41 100644 --- a/astroid/brain/brain_numpy.py +++ b/astroid/brain/brain_numpy.py @@ -1,3 +1,5 @@ +# Copyright (c) 2015-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/brain/brain_pkg_resources.py b/astroid/brain/brain_pkg_resources.py index dbbc88850d..9f1b506023 100644 --- a/astroid/brain/brain_pkg_resources.py +++ b/astroid/brain/brain_pkg_resources.py @@ -1,3 +1,5 @@ +# Copyright (c) 2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/brain/brain_pytest.py b/astroid/brain/brain_pytest.py index f20605f008..065c9521a3 100644 --- a/astroid/brain/brain_pytest.py +++ b/astroid/brain/brain_pytest.py @@ -1,3 +1,5 @@ +# Copyright (c) 2014-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/brain/brain_qt.py b/astroid/brain/brain_qt.py index 30d8de752c..0722439277 100644 --- a/astroid/brain/brain_qt.py +++ b/astroid/brain/brain_qt.py @@ -1,3 +1,5 @@ +# Copyright (c) 2015-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/brain/brain_six.py b/astroid/brain/brain_six.py index 2130807891..26f50a1df2 100644 --- a/astroid/brain/brain_six.py +++ b/astroid/brain/brain_six.py @@ -1,3 +1,5 @@ +# Copyright (c) 2014-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/brain/brain_ssl.py b/astroid/brain/brain_ssl.py index 5f33d014b2..6357c0ddb6 100644 --- a/astroid/brain/brain_ssl.py +++ b/astroid/brain/brain_ssl.py @@ -1,3 +1,5 @@ +# Copyright (c) 2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/brain/brain_subprocess.py b/astroid/brain/brain_subprocess.py index 3136f450ac..0fd78fccbd 100644 --- a/astroid/brain/brain_subprocess.py +++ b/astroid/brain/brain_subprocess.py @@ -1,3 +1,5 @@ +# Copyright (c) 2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/brain/brain_threading.py b/astroid/brain/brain_threading.py index 98f7fb0f28..e64174ce1a 100644 --- a/astroid/brain/brain_threading.py +++ b/astroid/brain/brain_threading.py @@ -1,3 +1,5 @@ +# Copyright (c) 2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/builder.py b/astroid/builder.py index b3d8b524be..4ed61a4dc2 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -1,3 +1,5 @@ +# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/context.py b/astroid/context.py index 95f1d9f5a6..c2073a0a39 100644 --- a/astroid/context.py +++ b/astroid/context.py @@ -1,3 +1,5 @@ +# Copyright (c) 2015-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/decorators.py b/astroid/decorators.py index 20bb5b7d43..bdd4ef09ab 100644 --- a/astroid/decorators.py +++ b/astroid/decorators.py @@ -1,3 +1,5 @@ +# Copyright (c) 2015-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/exceptions.py b/astroid/exceptions.py index 10e24e1b09..62fc9eeeaa 100644 --- a/astroid/exceptions.py +++ b/astroid/exceptions.py @@ -1,3 +1,5 @@ +# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/inference.py b/astroid/inference.py index 43483b1b20..819beeb446 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -1,3 +1,5 @@ +# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/interpreter/__init__.py b/astroid/interpreter/__init__.py index 7a8e4fceb2..9c77de2222 100644 --- a/astroid/interpreter/__init__.py +++ b/astroid/interpreter/__init__.py @@ -1,3 +1,5 @@ +# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/interpreter/assign.py b/astroid/interpreter/assign.py index 5b00afb859..d30bb4e1be 100644 --- a/astroid/interpreter/assign.py +++ b/astroid/interpreter/assign.py @@ -1,3 +1,5 @@ +# Copyright (c) 2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/interpreter/lookup.py b/astroid/interpreter/lookup.py index 53f59b7d10..9ad54fc651 100644 --- a/astroid/interpreter/lookup.py +++ b/astroid/interpreter/lookup.py @@ -1,3 +1,5 @@ +# Copyright (c) 2015-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/interpreter/objectmodel.py b/astroid/interpreter/objectmodel.py index 47ca442c46..d97e9edf41 100644 --- a/astroid/interpreter/objectmodel.py +++ b/astroid/interpreter/objectmodel.py @@ -1,3 +1,5 @@ +# Copyright (c) 2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/interpreter/objects.py b/astroid/interpreter/objects.py index 6ad9ab48ce..85698e33ee 100644 --- a/astroid/interpreter/objects.py +++ b/astroid/interpreter/objects.py @@ -1,3 +1,5 @@ +# Copyright (c) 2009-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/interpreter/runtimeabc.py b/astroid/interpreter/runtimeabc.py index 8ecfc4d1a5..adbcced92d 100644 --- a/astroid/interpreter/runtimeabc.py +++ b/astroid/interpreter/runtimeabc.py @@ -1,3 +1,5 @@ +# Copyright (c) 2015-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/interpreter/scope.py b/astroid/interpreter/scope.py index 6314dcc78d..aac7055505 100644 --- a/astroid/interpreter/scope.py +++ b/astroid/interpreter/scope.py @@ -1,3 +1,5 @@ +# Copyright (c) 2015-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/interpreter/util.py b/astroid/interpreter/util.py index 148804ce75..4a3769305b 100644 --- a/astroid/interpreter/util.py +++ b/astroid/interpreter/util.py @@ -1,3 +1,5 @@ +# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/manager.py b/astroid/manager.py index 24f353c833..0206f2576d 100644 --- a/astroid/manager.py +++ b/astroid/manager.py @@ -1,3 +1,5 @@ +# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/modutils.py b/astroid/modutils.py index 7143657f2a..caada4cbc2 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -1,3 +1,5 @@ +# Copyright (c) 2014-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/nodes.py b/astroid/nodes.py index 4fd5f4e705..775f617821 100644 --- a/astroid/nodes.py +++ b/astroid/nodes.py @@ -1,3 +1,5 @@ +# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/protocols.py b/astroid/protocols.py index da10b496c6..8f261252f3 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -1,3 +1,5 @@ +# Copyright (c) 2009-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/raw_building.py b/astroid/raw_building.py index b0746aeb06..d60aba3c53 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -1,3 +1,5 @@ +# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/test_utils.py b/astroid/test_utils.py index 89ed7f3082..0f2ffa794a 100644 --- a/astroid/test_utils.py +++ b/astroid/test_utils.py @@ -1,3 +1,5 @@ +# Copyright (c) 2013-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/tests/__init__.py b/astroid/tests/__init__.py index 7a8e4fceb2..9c77de2222 100644 --- a/astroid/tests/__init__.py +++ b/astroid/tests/__init__.py @@ -1,3 +1,5 @@ +# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/tests/resources.py b/astroid/tests/resources.py index 461c8df18b..9ab6636b4f 100644 --- a/astroid/tests/resources.py +++ b/astroid/tests/resources.py @@ -1,3 +1,5 @@ +# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/tests/unittest_brain.py b/astroid/tests/unittest_brain.py index b1642bdf49..1aea1fa6fa 100644 --- a/astroid/tests/unittest_brain.py +++ b/astroid/tests/unittest_brain.py @@ -1,3 +1,5 @@ +# Copyright (c) 2013-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/tests/unittest_builder.py b/astroid/tests/unittest_builder.py index 140a4eeb77..aad580ddd6 100644 --- a/astroid/tests/unittest_builder.py +++ b/astroid/tests/unittest_builder.py @@ -1,3 +1,5 @@ +# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/tests/unittest_helpers.py b/astroid/tests/unittest_helpers.py index 357a2bd94c..1b94cc478c 100644 --- a/astroid/tests/unittest_helpers.py +++ b/astroid/tests/unittest_helpers.py @@ -1,3 +1,5 @@ +# Copyright (c) 2015-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index 766cac2e4b..a5b87ae623 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -1,3 +1,5 @@ +# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/tests/unittest_lookup.py b/astroid/tests/unittest_lookup.py index e7179888da..9157f05cce 100644 --- a/astroid/tests/unittest_lookup.py +++ b/astroid/tests/unittest_lookup.py @@ -1,3 +1,5 @@ +# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/tests/unittest_manager.py b/astroid/tests/unittest_manager.py index 2cb64b9dc7..310e671a39 100644 --- a/astroid/tests/unittest_manager.py +++ b/astroid/tests/unittest_manager.py @@ -1,3 +1,5 @@ +# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/tests/unittest_modutils.py b/astroid/tests/unittest_modutils.py index 2b32422911..1b3286ac6e 100644 --- a/astroid/tests/unittest_modutils.py +++ b/astroid/tests/unittest_modutils.py @@ -1,3 +1,5 @@ +# Copyright (c) 2014-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index ef276291a4..d0bed37ed5 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -1,3 +1,5 @@ +# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/tests/unittest_object_model.py b/astroid/tests/unittest_object_model.py index 369771aa44..cdb068b399 100644 --- a/astroid/tests/unittest_object_model.py +++ b/astroid/tests/unittest_object_model.py @@ -1,3 +1,5 @@ +# Copyright (c) 2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/tests/unittest_objects.py b/astroid/tests/unittest_objects.py index d78301f9df..39f5a24c6a 100644 --- a/astroid/tests/unittest_objects.py +++ b/astroid/tests/unittest_objects.py @@ -1,3 +1,5 @@ +# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/tests/unittest_protocols.py b/astroid/tests/unittest_protocols.py index 82d01c7d0c..761f5f3a7c 100644 --- a/astroid/tests/unittest_protocols.py +++ b/astroid/tests/unittest_protocols.py @@ -1,3 +1,5 @@ +# Copyright (c) 2015-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/tests/unittest_python3.py b/astroid/tests/unittest_python3.py index e558607875..24d83c4e1b 100644 --- a/astroid/tests/unittest_python3.py +++ b/astroid/tests/unittest_python3.py @@ -1,3 +1,5 @@ +# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/tests/unittest_raw_building.py b/astroid/tests/unittest_raw_building.py index 83cffb6b42..ead4200163 100644 --- a/astroid/tests/unittest_raw_building.py +++ b/astroid/tests/unittest_raw_building.py @@ -1,3 +1,5 @@ +# Copyright (c) 2013-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/tests/unittest_regrtest.py b/astroid/tests/unittest_regrtest.py index cecf6b46a5..5ead021bb0 100644 --- a/astroid/tests/unittest_regrtest.py +++ b/astroid/tests/unittest_regrtest.py @@ -1,3 +1,5 @@ +# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index 0fbc099abc..89b74385ee 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -1,3 +1,5 @@ +# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/tests/unittest_transforms.py b/astroid/tests/unittest_transforms.py index 285aafdcb6..d34d6fa401 100644 --- a/astroid/tests/unittest_transforms.py +++ b/astroid/tests/unittest_transforms.py @@ -1,3 +1,5 @@ +# Copyright (c) 2015-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/tests/unittest_utils.py b/astroid/tests/unittest_utils.py index 2282575ff5..ce6fb5e1ed 100644 --- a/astroid/tests/unittest_utils.py +++ b/astroid/tests/unittest_utils.py @@ -1,3 +1,5 @@ +# Copyright (c) 2008-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/transforms.py b/astroid/transforms.py index 83243ad0ec..3d91b5e7ac 100644 --- a/astroid/transforms.py +++ b/astroid/transforms.py @@ -1,3 +1,5 @@ +# Copyright (c) 2015-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/tree/__init__.py b/astroid/tree/__init__.py index 7a8e4fceb2..9c77de2222 100644 --- a/astroid/tree/__init__.py +++ b/astroid/tree/__init__.py @@ -1,3 +1,5 @@ +# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/tree/base.py b/astroid/tree/base.py index 3fe989971e..33a439388a 100644 --- a/astroid/tree/base.py +++ b/astroid/tree/base.py @@ -1,3 +1,5 @@ +# Copyright (c) 2015-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index 47d9d04707..7e8c428fd5 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -1,3 +1,5 @@ +# Copyright (c) 2009-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/tree/rebuilder.py b/astroid/tree/rebuilder.py index ce98afddb6..d8167826b7 100644 --- a/astroid/tree/rebuilder.py +++ b/astroid/tree/rebuilder.py @@ -1,3 +1,5 @@ +# Copyright (c) 2009-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 0e73ec12c1..95d06b0b27 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -1,3 +1,5 @@ +# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/tree/treeabc.py b/astroid/tree/treeabc.py index 22e6aeb84b..535e399360 100644 --- a/astroid/tree/treeabc.py +++ b/astroid/tree/treeabc.py @@ -1,3 +1,5 @@ +# Copyright (c) 2015-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/astroid/util.py b/astroid/util.py index fd50a0f2c2..7543e1c9a1 100644 --- a/astroid/util.py +++ b/astroid/util.py @@ -1,3 +1,5 @@ +# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr # 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 diff --git a/setup.py b/setup.py index 3a3cc62159..a931f7e958 100644 --- a/setup.py +++ b/setup.py @@ -1,22 +1,10 @@ #!/usr/bin/env python +# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) +# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# 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 + # pylint: disable=W0404,W0622,W0704,W0613 -# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of astroid. -# -# astroid is free software: you can redistribute it and/or modify it under -# the terms of the GNU Lesser General Public License as published by the Free -# Software Foundation, either version 2.1 of the License, or (at your option) any -# later version. -# -# astroid is distributed in the hope that it will be useful, but WITHOUT -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more -# details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with astroid. If not, see . """Setup script for astroid.""" import os from setuptools import setup, find_packages From 1e0c39067a69841478605f05fc40aa434b2f2ed4 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Fri, 22 Jul 2016 23:20:45 +0300 Subject: [PATCH 269/312] Even more granular copyrights (thanks to copyrite) --- astroid/__init__.py | 7 +++++-- astroid/__pkginfo__.py | 6 ++++-- astroid/as_string.py | 7 +++++-- astroid/brain/brain_builtin_inference.py | 5 +++-- astroid/brain/brain_collections.py | 4 ++-- astroid/brain/brain_dateutil.py | 5 +++-- astroid/brain/brain_functools.py | 4 ++-- astroid/brain/brain_gi.py | 4 ++-- astroid/brain/brain_hashlib.py | 4 ++-- astroid/brain/brain_mechanize.py | 4 ++-- astroid/brain/brain_multiprocessing.py | 4 ++-- astroid/brain/brain_namedtuple_enum.py | 6 ++++-- astroid/brain/brain_nose.py | 4 ++-- astroid/brain/brain_numpy.py | 4 ++-- astroid/brain/brain_pkg_resources.py | 4 ++-- astroid/brain/brain_pytest.py | 5 +++-- astroid/brain/brain_qt.py | 4 ++-- astroid/brain/brain_six.py | 4 ++-- astroid/brain/brain_ssl.py | 4 ++-- astroid/brain/brain_subprocess.py | 4 ++-- astroid/brain/brain_threading.py | 4 ++-- astroid/builder.py | 7 +++++-- astroid/context.py | 5 +++-- astroid/decorators.py | 10 ++++------ astroid/exceptions.py | 7 +++++-- astroid/inference.py | 7 +++++-- astroid/interpreter/__init__.py | 5 ----- astroid/interpreter/assign.py | 4 ++-- astroid/interpreter/lookup.py | 4 ++-- astroid/interpreter/objectmodel.py | 7 +------ astroid/interpreter/objects.py | 6 ++++-- astroid/interpreter/runtimeabc.py | 4 ++-- astroid/interpreter/scope.py | 5 +++-- astroid/interpreter/util.py | 4 ++-- astroid/manager.py | 7 +++++-- astroid/modutils.py | 8 ++++++-- astroid/nodes.py | 7 +++++-- astroid/protocols.py | 7 +++++-- astroid/raw_building.py | 7 +++++-- astroid/test_utils.py | 6 ++++-- astroid/tests/__init__.py | 3 +-- astroid/tests/resources.py | 5 +++-- astroid/tests/unittest_brain.py | 9 +++++++-- astroid/tests/unittest_builder.py | 7 +++++-- astroid/tests/unittest_helpers.py | 5 +++-- astroid/tests/unittest_inference.py | 9 +++++++-- astroid/tests/unittest_lookup.py | 7 +++++-- astroid/tests/unittest_manager.py | 6 ++++-- astroid/tests/unittest_modutils.py | 8 ++++++-- astroid/tests/unittest_nodes.py | 8 ++++++-- astroid/tests/unittest_object_model.py | 8 ++------ astroid/tests/unittest_objects.py | 5 +++-- astroid/tests/unittest_protocols.py | 4 ++-- astroid/tests/unittest_python3.py | 6 ++++-- astroid/tests/unittest_raw_building.py | 6 ++++-- astroid/tests/unittest_regrtest.py | 7 +++++-- astroid/tests/unittest_scoped_nodes.py | 9 +++++++-- astroid/tests/unittest_transforms.py | 5 +++-- astroid/tests/unittest_utils.py | 5 +++-- astroid/transforms.py | 4 ++-- astroid/tree/__init__.py | 3 +-- astroid/tree/base.py | 5 +++-- astroid/tree/node_classes.py | 6 ++++-- astroid/tree/rebuilder.py | 6 ++++-- astroid/tree/scoped_nodes.py | 7 +++++-- astroid/tree/treeabc.py | 5 +++-- astroid/util.py | 9 +++------ setup.py | 6 ++++-- 68 files changed, 232 insertions(+), 155 deletions(-) diff --git a/astroid/__init__.py b/astroid/__init__.py index 64911c11b6..9292e463e7 100644 --- a/astroid/__init__.py +++ b/astroid/__init__.py @@ -1,5 +1,8 @@ -# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2006-2013 LOGILAB S.A. (Paris, FRANCE) +# Copyright (c) 2014 Google, Inc. +# Copyright (c) 2015-2016 Cara Vinson +# Copyright (c) 2015-2016 Claudiu Popa + # 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 diff --git a/astroid/__pkginfo__.py b/astroid/__pkginfo__.py index ab34ba4683..780d9bbd09 100644 --- a/astroid/__pkginfo__.py +++ b/astroid/__pkginfo__.py @@ -1,5 +1,7 @@ -# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2006-2014 LOGILAB S.A. (Paris, FRANCE) +# Copyright (c) 2014-2016 Claudiu Popa +# Copyright (c) 2014 Google, Inc. + # 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 diff --git a/astroid/as_string.py b/astroid/as_string.py index c4651646ed..8c49b7c03e 100644 --- a/astroid/as_string.py +++ b/astroid/as_string.py @@ -1,5 +1,8 @@ -# Copyright (c) 2009-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2009-2011, 2013-2014 LOGILAB S.A. (Paris, FRANCE) +# Copyright (c) 2013-2016 Claudiu Popa +# Copyright (c) 2013-2014 Google, Inc. +# Copyright (c) 2015-2016 Cara Vinson + # 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 diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index 8a2ed40c6b..bbcb725bc3 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -1,5 +1,6 @@ -# Copyright (c) 2014-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2014-2016 Claudiu Popa +# Copyright (c) 2015-2016 Cara Vinson + # 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 diff --git a/astroid/brain/brain_collections.py b/astroid/brain/brain_collections.py index 1fdd40c67c..cc53c5abfd 100644 --- a/astroid/brain/brain_collections.py +++ b/astroid/brain/brain_collections.py @@ -1,5 +1,5 @@ -# Copyright (c) 2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2016 Claudiu Popa + # 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 diff --git a/astroid/brain/brain_dateutil.py b/astroid/brain/brain_dateutil.py index 3c14c72dce..e9aafd2598 100644 --- a/astroid/brain/brain_dateutil.py +++ b/astroid/brain/brain_dateutil.py @@ -1,5 +1,6 @@ -# Copyright (c) 2015-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2015-2016 Claudiu Popa +# Copyright (c) 2016 Cara Vinson + # 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 diff --git a/astroid/brain/brain_functools.py b/astroid/brain/brain_functools.py index 0f7a73e4c3..7166a5b477 100644 --- a/astroid/brain/brain_functools.py +++ b/astroid/brain/brain_functools.py @@ -1,5 +1,5 @@ -# Copyright (c) 2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2016 Claudiu Popa + # 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 diff --git a/astroid/brain/brain_gi.py b/astroid/brain/brain_gi.py index 84a93004a3..1c525aadb0 100644 --- a/astroid/brain/brain_gi.py +++ b/astroid/brain/brain_gi.py @@ -1,5 +1,5 @@ -# Copyright (c) 2013-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2015-2016 Claudiu Popa + # 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 diff --git a/astroid/brain/brain_hashlib.py b/astroid/brain/brain_hashlib.py index 297e6a5d5e..aacd174379 100644 --- a/astroid/brain/brain_hashlib.py +++ b/astroid/brain/brain_hashlib.py @@ -1,5 +1,5 @@ -# Copyright (c) 2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2016 Claudiu Popa + # 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 diff --git a/astroid/brain/brain_mechanize.py b/astroid/brain/brain_mechanize.py index cfcfdae4f5..afc65059f2 100644 --- a/astroid/brain/brain_mechanize.py +++ b/astroid/brain/brain_mechanize.py @@ -1,5 +1,5 @@ -# Copyright (c) 2012-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2015-2016 Claudiu Popa + # 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 diff --git a/astroid/brain/brain_multiprocessing.py b/astroid/brain/brain_multiprocessing.py index 86722e56ab..1d7e4682b3 100644 --- a/astroid/brain/brain_multiprocessing.py +++ b/astroid/brain/brain_multiprocessing.py @@ -1,5 +1,5 @@ -# Copyright (c) 2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2016 Claudiu Popa + # 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 diff --git a/astroid/brain/brain_namedtuple_enum.py b/astroid/brain/brain_namedtuple_enum.py index eecdb50c72..952e2c6b61 100644 --- a/astroid/brain/brain_namedtuple_enum.py +++ b/astroid/brain/brain_namedtuple_enum.py @@ -1,5 +1,7 @@ -# Copyright (c) 2012-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2012-2015 LOGILAB S.A. (Paris, FRANCE) +# Copyright (c) 2014-2016 Claudiu Popa +# Copyright (c) 2015-2016 Cara Vinson + # 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 diff --git a/astroid/brain/brain_nose.py b/astroid/brain/brain_nose.py index f2a5a84ab6..4fae02d490 100644 --- a/astroid/brain/brain_nose.py +++ b/astroid/brain/brain_nose.py @@ -1,5 +1,5 @@ -# Copyright (c) 2015-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2015-2016 Claudiu Popa + # 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 diff --git a/astroid/brain/brain_numpy.py b/astroid/brain/brain_numpy.py index e7ba139f41..27a146c13a 100644 --- a/astroid/brain/brain_numpy.py +++ b/astroid/brain/brain_numpy.py @@ -1,5 +1,5 @@ -# Copyright (c) 2015-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2015-2016 Claudiu Popa + # 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 diff --git a/astroid/brain/brain_pkg_resources.py b/astroid/brain/brain_pkg_resources.py index 9f1b506023..64d980ad89 100644 --- a/astroid/brain/brain_pkg_resources.py +++ b/astroid/brain/brain_pkg_resources.py @@ -1,5 +1,5 @@ -# Copyright (c) 2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2016 Claudiu Popa + # 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 diff --git a/astroid/brain/brain_pytest.py b/astroid/brain/brain_pytest.py index 065c9521a3..6d451f1efc 100644 --- a/astroid/brain/brain_pytest.py +++ b/astroid/brain/brain_pytest.py @@ -1,5 +1,6 @@ -# Copyright (c) 2014-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2014-2016 Claudiu Popa +# Copyright (c) 2016 Cara Vinson + # 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 diff --git a/astroid/brain/brain_qt.py b/astroid/brain/brain_qt.py index 0722439277..fb16dfb034 100644 --- a/astroid/brain/brain_qt.py +++ b/astroid/brain/brain_qt.py @@ -1,5 +1,5 @@ -# Copyright (c) 2015-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2015-2016 Claudiu Popa + # 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 diff --git a/astroid/brain/brain_six.py b/astroid/brain/brain_six.py index 26f50a1df2..04007f47b7 100644 --- a/astroid/brain/brain_six.py +++ b/astroid/brain/brain_six.py @@ -1,5 +1,5 @@ -# Copyright (c) 2014-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2014-2016 Claudiu Popa + # 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 diff --git a/astroid/brain/brain_ssl.py b/astroid/brain/brain_ssl.py index 6357c0ddb6..78c20c99d4 100644 --- a/astroid/brain/brain_ssl.py +++ b/astroid/brain/brain_ssl.py @@ -1,5 +1,5 @@ -# Copyright (c) 2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2016 Claudiu Popa + # 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 diff --git a/astroid/brain/brain_subprocess.py b/astroid/brain/brain_subprocess.py index 0fd78fccbd..12f39322f5 100644 --- a/astroid/brain/brain_subprocess.py +++ b/astroid/brain/brain_subprocess.py @@ -1,5 +1,5 @@ -# Copyright (c) 2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2016 Claudiu Popa + # 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 diff --git a/astroid/brain/brain_threading.py b/astroid/brain/brain_threading.py index e64174ce1a..f2dfe60386 100644 --- a/astroid/brain/brain_threading.py +++ b/astroid/brain/brain_threading.py @@ -1,5 +1,5 @@ -# Copyright (c) 2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2016 Claudiu Popa + # 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 diff --git a/astroid/builder.py b/astroid/builder.py index 4ed61a4dc2..08f125cbba 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -1,5 +1,8 @@ -# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2006-2011, 2013-2014 LOGILAB S.A. (Paris, FRANCE) +# Copyright (c) 2014-2016 Claudiu Popa +# Copyright (c) 2014-2015 Google, Inc. +# Copyright (c) 2015-2016 Cara Vinson + # 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 diff --git a/astroid/context.py b/astroid/context.py index c2073a0a39..f693d406e7 100644 --- a/astroid/context.py +++ b/astroid/context.py @@ -1,5 +1,6 @@ -# Copyright (c) 2015-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2015-2016 Cara Vinson +# Copyright (c) 2015-2016 Claudiu Popa + # 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 diff --git a/astroid/decorators.py b/astroid/decorators.py index bdd4ef09ab..1fdde77d25 100644 --- a/astroid/decorators.py +++ b/astroid/decorators.py @@ -1,12 +1,10 @@ -# Copyright (c) 2015-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2015 Florian Bruhin +# Copyright (c) 2015-2016 Cara Vinson +# Copyright (c) 2015-2016 Claudiu Popa + # 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 -# -# The code in this file was originally part of logilab-common, licensed under -# the same license. - """ A few useful function/method decorators.""" import functools diff --git a/astroid/exceptions.py b/astroid/exceptions.py index 62fc9eeeaa..9dc807109c 100644 --- a/astroid/exceptions.py +++ b/astroid/exceptions.py @@ -1,5 +1,8 @@ -# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2007, 2009-2010, 2013 LOGILAB S.A. (Paris, FRANCE) +# Copyright (c) 2014 Google, Inc. +# Copyright (c) 2015-2016 Claudiu Popa +# Copyright (c) 2015-2016 Cara Vinson + # 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 diff --git a/astroid/inference.py b/astroid/inference.py index 819beeb446..37ee6b336a 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -1,5 +1,8 @@ -# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2006-2011, 2013-2014 LOGILAB S.A. (Paris, FRANCE) +# Copyright (c) 2013-2014 Google, Inc. +# Copyright (c) 2014-2016 Claudiu Popa +# Copyright (c) 2015-2016 Cara Vinson + # 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 diff --git a/astroid/interpreter/__init__.py b/astroid/interpreter/__init__.py index 9c77de2222..e69de29bb2 100644 --- a/astroid/interpreter/__init__.py +++ b/astroid/interpreter/__init__.py @@ -1,5 +0,0 @@ -# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr -# 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 - diff --git a/astroid/interpreter/assign.py b/astroid/interpreter/assign.py index d30bb4e1be..92d7a07a38 100644 --- a/astroid/interpreter/assign.py +++ b/astroid/interpreter/assign.py @@ -1,5 +1,5 @@ -# Copyright (c) 2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2016 Claudiu Popa + # 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 diff --git a/astroid/interpreter/lookup.py b/astroid/interpreter/lookup.py index 9ad54fc651..1a44703d72 100644 --- a/astroid/interpreter/lookup.py +++ b/astroid/interpreter/lookup.py @@ -1,5 +1,5 @@ -# Copyright (c) 2015-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2015-2016 Claudiu Popa + # 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 diff --git a/astroid/interpreter/objectmodel.py b/astroid/interpreter/objectmodel.py index d97e9edf41..bd6d1ce6ad 100644 --- a/astroid/interpreter/objectmodel.py +++ b/astroid/interpreter/objectmodel.py @@ -1,12 +1,7 @@ -# Copyright (c) 2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2016 Claudiu Popa # 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 -# -# The code in this file was originally part of logilab-common, licensed under -# the same license. - """ Data object model, as per https://docs.python.org/3/reference/datamodel.html. diff --git a/astroid/interpreter/objects.py b/astroid/interpreter/objects.py index 85698e33ee..99e4e2bef6 100644 --- a/astroid/interpreter/objects.py +++ b/astroid/interpreter/objects.py @@ -1,5 +1,7 @@ -# Copyright (c) 2009-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2009-2011, 2013-2014 LOGILAB S.A. (Paris, FRANCE) +# Copyright (c) 2014-2016 Claudiu Popa +# Copyright (c) 2015-2016 Cara Vinson + # 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 diff --git a/astroid/interpreter/runtimeabc.py b/astroid/interpreter/runtimeabc.py index adbcced92d..42940ad59d 100644 --- a/astroid/interpreter/runtimeabc.py +++ b/astroid/interpreter/runtimeabc.py @@ -1,5 +1,5 @@ -# Copyright (c) 2015-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2015-2016 Claudiu Popa + # 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 diff --git a/astroid/interpreter/scope.py b/astroid/interpreter/scope.py index aac7055505..7b49d49e16 100644 --- a/astroid/interpreter/scope.py +++ b/astroid/interpreter/scope.py @@ -1,5 +1,6 @@ -# Copyright (c) 2015-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2015-2016 Cara Vinson +# Copyright (c) 2015-2016 Claudiu Popa + # 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 diff --git a/astroid/interpreter/util.py b/astroid/interpreter/util.py index 4a3769305b..1fbfdb1a45 100644 --- a/astroid/interpreter/util.py +++ b/astroid/interpreter/util.py @@ -1,5 +1,5 @@ -# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2015-2016 Claudiu Popa + # 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 diff --git a/astroid/manager.py b/astroid/manager.py index 0206f2576d..e57377b6c5 100644 --- a/astroid/manager.py +++ b/astroid/manager.py @@ -1,5 +1,8 @@ -# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2006-2011, 2013-2014 LOGILAB S.A. (Paris, FRANCE) +# Copyright (c) 2014-2016 Claudiu Popa +# Copyright (c) 2014 Google, Inc. +# Copyright (c) 2015-2016 Cara Vinson + # 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 diff --git a/astroid/modutils.py b/astroid/modutils.py index caada4cbc2..be80ab70b5 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -1,5 +1,9 @@ -# Copyright (c) 2014-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# -*- coding: utf-8 -*- +# Copyright (c) 2014-2016 Claudiu Popa +# Copyright (c) 2014 Google, Inc. +# Copyright (c) 2015 Radosław Ganczarek +# Copyright (c) 2015 Florian Bruhin + # 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 diff --git a/astroid/nodes.py b/astroid/nodes.py index 775f617821..eb8155ffa0 100644 --- a/astroid/nodes.py +++ b/astroid/nodes.py @@ -1,5 +1,8 @@ -# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2006-2011, 2013 LOGILAB S.A. (Paris, FRANCE) +# Copyright (c) 2014 Google, Inc. +# Copyright (c) 2014-2016 Claudiu Popa +# Copyright (c) 2015-2016 Cara Vinson + # 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 diff --git a/astroid/protocols.py b/astroid/protocols.py index 8f261252f3..cdd237cc42 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -1,5 +1,8 @@ -# Copyright (c) 2009-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2009-2011, 2013-2014 LOGILAB S.A. (Paris, FRANCE) +# Copyright (c) 2014-2016 Claudiu Popa +# Copyright (c) 2014 Google, Inc. +# Copyright (c) 2015-2016 Cara Vinson + # 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 diff --git a/astroid/raw_building.py b/astroid/raw_building.py index d60aba3c53..7409611784 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -1,5 +1,8 @@ -# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2006-2014 LOGILAB S.A. (Paris, FRANCE) +# Copyright (c) 2014-2016 Claudiu Popa +# Copyright (c) 2014 Google, Inc. +# Copyright (c) 2015-2016 Cara Vinson + # 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 diff --git a/astroid/test_utils.py b/astroid/test_utils.py index 0f2ffa794a..edd414157b 100644 --- a/astroid/test_utils.py +++ b/astroid/test_utils.py @@ -1,5 +1,7 @@ -# Copyright (c) 2013-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2013-2014 Google, Inc. +# Copyright (c) 2015-2016 Claudiu Popa +# Copyright (c) 2015-2016 Cara Vinson + # 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 diff --git a/astroid/tests/__init__.py b/astroid/tests/__init__.py index 9c77de2222..f8c7bff16e 100644 --- a/astroid/tests/__init__.py +++ b/astroid/tests/__init__.py @@ -1,5 +1,4 @@ -# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr + # 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 diff --git a/astroid/tests/resources.py b/astroid/tests/resources.py index 9ab6636b4f..fa53324f8f 100644 --- a/astroid/tests/resources.py +++ b/astroid/tests/resources.py @@ -1,5 +1,6 @@ -# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2014 Google, Inc. +# Copyright (c) 2015-2016 Claudiu Popa + # 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 diff --git a/astroid/tests/unittest_brain.py b/astroid/tests/unittest_brain.py index 1aea1fa6fa..e79e5d46c6 100644 --- a/astroid/tests/unittest_brain.py +++ b/astroid/tests/unittest_brain.py @@ -1,5 +1,10 @@ -# Copyright (c) 2013-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2013-2014 Google, Inc. +# Copyright (c) 2014-2016 Claudiu Popa +# Copyright (c) 2015 Philip Lorenz +# Copyright (c) 2015 LOGILAB S.A. (Paris, FRANCE) +# Copyright (c) 2015-2016 Cara Vinson +# Copyright (c) 2015 raylu + # 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 diff --git a/astroid/tests/unittest_builder.py b/astroid/tests/unittest_builder.py index aad580ddd6..c55dc7227e 100644 --- a/astroid/tests/unittest_builder.py +++ b/astroid/tests/unittest_builder.py @@ -1,5 +1,8 @@ -# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2006-2014 LOGILAB S.A. (Paris, FRANCE) +# Copyright (c) 2014-2016 Claudiu Popa +# Copyright (c) 2014-2015 Google, Inc. +# Copyright (c) 2015-2016 Cara Vinson + # 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 diff --git a/astroid/tests/unittest_helpers.py b/astroid/tests/unittest_helpers.py index 1b94cc478c..a6bc9253db 100644 --- a/astroid/tests/unittest_helpers.py +++ b/astroid/tests/unittest_helpers.py @@ -1,5 +1,6 @@ -# Copyright (c) 2015-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2015-2016 Cara Vinson +# Copyright (c) 2015-2016 Claudiu Popa + # 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 diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index a5b87ae623..884dd830b7 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -1,5 +1,10 @@ -# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2006-2015 LOGILAB S.A. (Paris, FRANCE) +# Copyright (c) 2013-2014 Google, Inc. +# Copyright (c) 2014-2016 Claudiu Popa +# Copyright (c) 2015 Rene Zhang +# Copyright (c) 2015-2016 Cara Vinson +# Copyright (c) 2015 Dmitry Pribysh + # 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 diff --git a/astroid/tests/unittest_lookup.py b/astroid/tests/unittest_lookup.py index 9157f05cce..f3accfe8fe 100644 --- a/astroid/tests/unittest_lookup.py +++ b/astroid/tests/unittest_lookup.py @@ -1,5 +1,8 @@ -# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2007-2013 LOGILAB S.A. (Paris, FRANCE) +# Copyright (c) 2014 Google, Inc. +# Copyright (c) 2014-2016 Claudiu Popa +# Copyright (c) 2015-2016 Cara Vinson + # 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 diff --git a/astroid/tests/unittest_manager.py b/astroid/tests/unittest_manager.py index 310e671a39..72594fbd8e 100644 --- a/astroid/tests/unittest_manager.py +++ b/astroid/tests/unittest_manager.py @@ -1,5 +1,7 @@ -# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2006, 2009-2014 LOGILAB S.A. (Paris, FRANCE) +# Copyright (c) 2014-2016 Claudiu Popa +# Copyright (c) 2014 Google, Inc. + # 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 diff --git a/astroid/tests/unittest_modutils.py b/astroid/tests/unittest_modutils.py index 1b3286ac6e..6484178ae5 100644 --- a/astroid/tests/unittest_modutils.py +++ b/astroid/tests/unittest_modutils.py @@ -1,5 +1,9 @@ -# Copyright (c) 2014-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# -*- coding: utf-8 -*- +# Copyright (c) 2014-2016 Claudiu Popa +# Copyright (c) 2014 Google, Inc. +# Copyright (c) 2015 Radosław Ganczarek +# Copyright (c) 2015 Florian Bruhin + # 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 diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index d0bed37ed5..114eb15b6f 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -1,5 +1,9 @@ -# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2006-2007, 2009-2014 LOGILAB S.A. (Paris, FRANCE) +# Copyright (c) 2013-2016 Claudiu Popa +# Copyright (c) 2014 Google, Inc. +# Copyright (c) 2015 Florian Bruhin +# Copyright (c) 2015-2016 Cara Vinson + # 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 diff --git a/astroid/tests/unittest_object_model.py b/astroid/tests/unittest_object_model.py index cdb068b399..573ebbba85 100644 --- a/astroid/tests/unittest_object_model.py +++ b/astroid/tests/unittest_object_model.py @@ -1,12 +1,8 @@ -# Copyright (c) 2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2016 Claudiu Popa + # 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 -# -# The code in this file was originally part of logilab-common, licensed under -# the same license. - import unittest import types import xml diff --git a/astroid/tests/unittest_objects.py b/astroid/tests/unittest_objects.py index 39f5a24c6a..dc1f87e206 100644 --- a/astroid/tests/unittest_objects.py +++ b/astroid/tests/unittest_objects.py @@ -1,5 +1,6 @@ -# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2015-2016 Cara Vinson +# Copyright (c) 2015-2016 Claudiu Popa + # 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 diff --git a/astroid/tests/unittest_protocols.py b/astroid/tests/unittest_protocols.py index 761f5f3a7c..1d33765c83 100644 --- a/astroid/tests/unittest_protocols.py +++ b/astroid/tests/unittest_protocols.py @@ -1,5 +1,5 @@ -# Copyright (c) 2015-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2015-2016 Claudiu Popa + # 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 diff --git a/astroid/tests/unittest_python3.py b/astroid/tests/unittest_python3.py index 24d83c4e1b..a9ef11e08b 100644 --- a/astroid/tests/unittest_python3.py +++ b/astroid/tests/unittest_python3.py @@ -1,5 +1,7 @@ -# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2013-2016 Claudiu Popa +# Copyright (c) 2014 Google, Inc. +# Copyright (c) 2015-2016 Cara Vinson + # 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 diff --git a/astroid/tests/unittest_raw_building.py b/astroid/tests/unittest_raw_building.py index ead4200163..eceb6b685a 100644 --- a/astroid/tests/unittest_raw_building.py +++ b/astroid/tests/unittest_raw_building.py @@ -1,5 +1,7 @@ -# Copyright (c) 2013-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2014-2016 Claudiu Popa +# Copyright (c) 2014 Google, Inc. +# Copyright (c) 2015-2016 Cara Vinson + # 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 diff --git a/astroid/tests/unittest_regrtest.py b/astroid/tests/unittest_regrtest.py index 5ead021bb0..3a0e92eced 100644 --- a/astroid/tests/unittest_regrtest.py +++ b/astroid/tests/unittest_regrtest.py @@ -1,5 +1,8 @@ -# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2006-2008, 2010-2014 LOGILAB S.A. (Paris, FRANCE) +# Copyright (c) 2013-2014 Google, Inc. +# Copyright (c) 2014-2016 Claudiu Popa +# Copyright (c) 2015-2016 Cara Vinson + # 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 diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index 89b74385ee..7088d89aa6 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -1,5 +1,10 @@ -# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2006-2014 LOGILAB S.A. (Paris, FRANCE) +# Copyright (c) 2011, 2013-2015 Google, Inc. +# Copyright (c) 2013-2016 Claudiu Popa +# Copyright (c) 2015 Rene Zhang +# Copyright (c) 2015 Philip Lorenz +# Copyright (c) 2015-2016 Cara Vinson + # 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 diff --git a/astroid/tests/unittest_transforms.py b/astroid/tests/unittest_transforms.py index d34d6fa401..bb8d0a7f31 100644 --- a/astroid/tests/unittest_transforms.py +++ b/astroid/tests/unittest_transforms.py @@ -1,5 +1,6 @@ -# Copyright (c) 2015-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2015-2016 Cara Vinson +# Copyright (c) 2015-2016 Claudiu Popa + # 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 diff --git a/astroid/tests/unittest_utils.py b/astroid/tests/unittest_utils.py index ce6fb5e1ed..cb7aea3165 100644 --- a/astroid/tests/unittest_utils.py +++ b/astroid/tests/unittest_utils.py @@ -1,5 +1,6 @@ -# Copyright (c) 2008-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2014, 2016 Google, Inc. +# Copyright (c) 2015-2016 Claudiu Popa + # 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 diff --git a/astroid/transforms.py b/astroid/transforms.py index 3d91b5e7ac..9914ea299d 100644 --- a/astroid/transforms.py +++ b/astroid/transforms.py @@ -1,5 +1,5 @@ -# Copyright (c) 2015-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2015-2016 Claudiu Popa + # 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 diff --git a/astroid/tree/__init__.py b/astroid/tree/__init__.py index 9c77de2222..f8c7bff16e 100644 --- a/astroid/tree/__init__.py +++ b/astroid/tree/__init__.py @@ -1,5 +1,4 @@ -# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr + # 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 diff --git a/astroid/tree/base.py b/astroid/tree/base.py index 33a439388a..e4459f0f53 100644 --- a/astroid/tree/base.py +++ b/astroid/tree/base.py @@ -1,5 +1,6 @@ -# Copyright (c) 2015-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2015-2016 Cara Vinson +# Copyright (c) 2015-2016 Claudiu Popa + # 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 diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index 7e8c428fd5..547ebf31bb 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -1,5 +1,7 @@ -# Copyright (c) 2009-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2009-2011, 2013-2014 LOGILAB S.A. (Paris, FRANCE) +# Copyright (c) 2014-2016 Claudiu Popa +# Copyright (c) 2015-2016 Cara Vinson + # 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 diff --git a/astroid/tree/rebuilder.py b/astroid/tree/rebuilder.py index d8167826b7..bb963f3828 100644 --- a/astroid/tree/rebuilder.py +++ b/astroid/tree/rebuilder.py @@ -1,5 +1,7 @@ -# Copyright (c) 2009-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2009-2014 LOGILAB S.A. (Paris, FRANCE) +# Copyright (c) 2013-2016 Claudiu Popa +# Copyright (c) 2015-2016 Cara Vinson + # 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 diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 95d06b0b27..509d843845 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -1,5 +1,8 @@ -# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2006-2014 LOGILAB S.A. (Paris, FRANCE) +# Copyright (c) 2011, 2013-2015 Google, Inc. +# Copyright (c) 2013-2016 Claudiu Popa +# Copyright (c) 2015-2016 Cara Vinson + # 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 diff --git a/astroid/tree/treeabc.py b/astroid/tree/treeabc.py index 535e399360..c72d17fcf3 100644 --- a/astroid/tree/treeabc.py +++ b/astroid/tree/treeabc.py @@ -1,5 +1,6 @@ -# Copyright (c) 2015-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2015-2016 Cara Vinson +# Copyright (c) 2015-2016 Claudiu Popa + # 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 diff --git a/astroid/util.py b/astroid/util.py index 7543e1c9a1..e4ca1261e5 100644 --- a/astroid/util.py +++ b/astroid/util.py @@ -1,12 +1,9 @@ -# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2015-2016 Cara Vinson +# Copyright (c) 2015-2016 Claudiu Popa + # 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 -# -# The code in this file was originally part of logilab-common, licensed under -# the same license. - import importlib import platform import sys diff --git a/setup.py b/setup.py index a931f7e958..9c7046f17f 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,8 @@ #!/usr/bin/env python -# Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# Copyright (c) 2006, 2009-2013 LOGILAB S.A. (Paris, FRANCE) +# Copyright (c) 2014-2016 Claudiu Popa +# Copyright (c) 2014 Google, Inc. + # 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 From 72e3802ad1159a141ab97f3a64d98b2bc2ff2f5e Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sat, 23 Jul 2016 22:04:55 +0300 Subject: [PATCH 270/312] Add missing parameter to underlying bound method. --- astroid/brain/brain_functools.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astroid/brain/brain_functools.py b/astroid/brain/brain_functools.py index 7166a5b477..dc99c417bb 100644 --- a/astroid/brain/brain_functools.py +++ b/astroid/brain/brain_functools.py @@ -40,7 +40,7 @@ def infer_call_result(self, caller, context=None): @property def pycache_clear(self): - node = astroid.extract_node('''def cache_clear(): pass''') + node = astroid.extract_node('''def cache_clear(self): pass''') return objects.BoundMethod(proxy=node, bound=self._instance.parent.scope()) From 5f187fb5f98e780a8bda8d6f3b49b2085b262000 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Wed, 27 Jul 2016 01:51:15 +0300 Subject: [PATCH 271/312] Add `returns` into the proper order in FunctionDef._astroid_fields The order is important, since it determines the last child, which in turn determines the last line number of a scoped node. --- ChangeLog | 5 +++++ astroid/tests/unittest_scoped_nodes.py | 12 ++++++++++++ astroid/tree/scoped_nodes.py | 2 +- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index c903b628e7..e8bd4c717a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,11 @@ Change log for the astroid package (used to be astng) -- + * Add `returns` into the proper order in FunctionDef._astroid_fields + + The order is important, since it determines the last child, + which in turn determines the last line number of a scoped node. + * New function, astroid.extract_node, exported out from astroid.test_utils. * Fix a crash which occurred when the class of a namedtuple could not be inferred. diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index 7088d89aa6..0bdc2929d9 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -560,6 +560,18 @@ def test(): self.assertIsInstance(inferred, nodes.Const) self.assertEqual(inferred.value, 42) + @test_utils.require_version(minver='3.0') + def test_return_annotation_is_not_the_last(self): + func = builder.extract_node(''' + def test() -> bytes: + pass + pass + return + ''') + last_child = func.last_child() + self.assertIsInstance(last_child, nodes.Return) + self.assertEqual(func.tolineno, 5) + class ClassNodeTest(ModuleLoader, unittest.TestCase): diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 509d843845..79da1d4d94 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -822,7 +822,7 @@ class FunctionDef(LambdaFunctionMixin, lookup.LocalsDictNode, ''' if six.PY3: - _astroid_fields = ('decorators', 'args', 'body', 'returns') + _astroid_fields = ('decorators', 'args', 'returns', 'body') returns = None else: _astroid_fields = ('decorators', 'args', 'body') From a4a470c9277b900246b93a286e1648ab2279b2b1 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Thu, 28 Jul 2016 22:05:17 +0300 Subject: [PATCH 272/312] Left to do notice for the future. --- astroid/brain/brain_functools.py | 1 + 1 file changed, 1 insertion(+) diff --git a/astroid/brain/brain_functools.py b/astroid/brain/brain_functools.py index dc99c417bb..a0f6ee03b0 100644 --- a/astroid/brain/brain_functools.py +++ b/astroid/brain/brain_functools.py @@ -51,6 +51,7 @@ class LruWrappedFunctionDef(astroid.FunctionDef): def _transform_lru_cache(node, context=None): # TODO: this needs the zipper, because the new node's attributes # will still point to the old node. + # TODO: please check https://github.com/PyCQA/astroid/issues/354 as well. new_func = LruWrappedFunctionDef(name=node.name, doc=node.name, lineno=node.lineno, col_offset=node.col_offset, parent=node.parent) From 3929aabea71b412022151b3e0574c63ea8954e0a Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 2 Aug 2016 00:17:15 +0300 Subject: [PATCH 273/312] Use proper environment markers for version specific dependencies. --- astroid/__pkginfo__.py | 13 +++---------- setup.py | 1 + 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/astroid/__pkginfo__.py b/astroid/__pkginfo__.py index 780d9bbd09..496ac15a1b 100644 --- a/astroid/__pkginfo__.py +++ b/astroid/__pkginfo__.py @@ -6,8 +6,6 @@ # For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER """astroid packaging information""" -import sys - distname = 'astroid' modname = 'astroid' @@ -15,15 +13,10 @@ numversion = (2, 0, 0) version = '.'.join([str(num) for num in numversion]) - +extras_require = {} install_requires = ['lazy_object_proxy', 'six', 'wrapt'] - -if sys.version_info <= (3, 3): - install_requires.append('singledispatch') - install_requires.append('enum34') - - if sys.version_info < (3, 0): - install_requires.append('funcsigs') +extras_require[':python_version<"3.4"'] = ['enum34', 'singledispatch'] +extras_require[':python_version<"3.0"'] = ['funcsigs'] license = 'LGPL' diff --git a/setup.py b/setup.py index 9c7046f17f..3e8a19f25c 100644 --- a/setup.py +++ b/setup.py @@ -53,6 +53,7 @@ def install(): url = web, include_package_data = True, install_requires = install_requires, + extras_require=extras_require, packages = find_packages(), cmdclass = {'install_lib': AstroidInstallLib, 'easy_install': AstroidEasyInstallLib} From ec6e7e002b1d86682cda793f9048ef9c4133d844 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 2 Aug 2016 01:32:39 +0300 Subject: [PATCH 274/312] Try to upgrade setuptools in order to have latest environment marker support. --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index d46d986482..5d6c717d59 100644 --- a/.travis.yml +++ b/.travis.yml @@ -34,6 +34,7 @@ install: - $PYTHON_EXE -m easy_install --version - $PYTHON_EXE -m pip --version - $PYTHON_EXE -m tox --version + - $PYTHON_EXE -m pip install -U setuptools script: - python -m tox -e coverage-erase,$TOXENV after_success: From 5ff9e8f6f4d18d721df7cf50b8581f61e6094122 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 21 Aug 2016 22:09:33 +0300 Subject: [PATCH 275/312] Make InterpreterObjects be used as their underlying runtime object, by providing a way to access the attributes of the latter seamlessly. --- astroid/tree/node_classes.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index 547ebf31bb..28cf001af1 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -683,14 +683,23 @@ class Ellipsis(base.NodeNG): # pylint: disable=redefined-builtin def bool_value(self): return True + +_INTERPRETER_OBJECT_SENTINEL = object() + + @util.register_implementation(treeabc.InterpreterObject) class InterpreterObject(base.NodeNG): - '''InterpreterObjects are used in manufactured ASTs that simulate features of - real ASTs for inference, usually to handle behavior implemented in - the interpreter or in C extensions. + '''Used for connecting ASTs and runtime objects + InterpreterObjects are used in manufactured ASTs that simulate features of + real ASTs for inference, usually to handle behavior implemented in + the interpreter or in C extensions. They can be used as a "translator" + from a non-AST object, or in astroid's parlance, a runtime object + to an AST. They mimick their underlying object, which means that an + InterpreterObject can act as the object it is wrapping. ''' _other_fields = ('name', 'object') + object = _INTERPRETER_OBJECT_SENTINEL def __init__(self, object_=None, name=None, lineno=None, col_offset=None, parent=None): if object_ is not None: @@ -699,7 +708,12 @@ def __init__(self, object_=None, name=None, lineno=None, col_offset=None, parent super(InterpreterObject, self).__init__(lineno, col_offset, parent) def has_underlying_object(self): - return hasattr(self, 'object') + return self.object != _INTERPRETER_OBJECT_SENTINEL + + def __getattr__(self, attr): + if self.has_underlying_object(): + return getattr(self.object, attr) + raise AttributeError(attr) @util.register_implementation(treeabc.ExceptHandler) From 261be12ec725ba0124e4ea402f45292ae9f6c78a Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 22 Aug 2016 10:58:38 +0300 Subject: [PATCH 276/312] Add brain tips for _io.TextIOWrapper's buffer and raw attributes. --- ChangeLog | 2 + astroid/brain/brain_io.py | 71 +++++++++++++++++++++++++++++++++ astroid/tests/unittest_brain.py | 18 +++++++++ 3 files changed, 91 insertions(+) create mode 100644 astroid/brain/brain_io.py diff --git a/ChangeLog b/ChangeLog index e8bd4c717a..c19b0d38ac 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,8 @@ Change log for the astroid package (used to be astng) -- + * Add brain tips for _io.TextIOWrapper's buffer and raw attributes. + * Add `returns` into the proper order in FunctionDef._astroid_fields The order is important, since it determines the last child, diff --git a/astroid/brain/brain_io.py b/astroid/brain/brain_io.py new file mode 100644 index 0000000000..f869a4b6fb --- /dev/null +++ b/astroid/brain/brain_io.py @@ -0,0 +1,71 @@ +# Copyright (c) 2016 Claudiu Popa + +# 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 + +'''Astroid brain hints for some of the _io C objects.''' + +import astroid +from astroid import decorators +from astroid import util +from astroid.tree import treeabc + + +BUFFERED = {'BufferedWriter', 'BufferedReader'} +TextIOWrapper = 'TextIOWrapper' +FileIO = 'FileIO' +BufferedWriter = 'BufferedWriter' + + +def _parametrized_lazy_interpreter_object(cls): + '''Get an InterpreterObject which delays its object until accessed + + The class can be used when we are not able to access objects from + the _io module during transformation time. + ''' + + @util.register_implementation(treeabc.InterpreterObject) + class LazyInterpreterObject(astroid.InterpreterObject): + + @decorators.cachedproperty + def _io_module(self): + return astroid.MANAGER.ast_from_module_name('_io') + + @decorators.cachedproperty + def object(self): + attribute_object = self._io_module[cls] + return attribute_object.instantiate_class() + + return LazyInterpreterObject + + +def _generic_io_transform(node, name, cls): + '''Transform the given name, by adding the given *class* as a member of the node.''' + + module = astroid.MANAGER.ast_from_module_name('io') + interpreter_object_cls = _parametrized_lazy_interpreter_object( + cls=cls) + + # TODO: for some reason, this works only if it is inserted as the + # first value, please check why. + node.body.insert(0, interpreter_object_cls(name=name, parent=module)) + + +def _transform_text_io_wrapper(node): + # This is not always correct, since it can vary with the type of the descriptor, + # being stdout, stderr or stdin. But we cannot get access to the name of the + # stream, which is why we are using the BufferedWriter class as a default + # value + return _generic_io_transform(node, name='buffer', cls=BufferedWriter) + + +def _transform_buffered(node): + return _generic_io_transform(node, name='raw', cls=FileIO) + + +astroid.MANAGER.register_transform(astroid.ClassDef, + _transform_buffered, + lambda node: node.name in BUFFERED) +astroid.MANAGER.register_transform(astroid.ClassDef, + _transform_text_io_wrapper, + lambda node: node.name == TextIOWrapper) diff --git a/astroid/tests/unittest_brain.py b/astroid/tests/unittest_brain.py index e79e5d46c6..12258eee69 100644 --- a/astroid/tests/unittest_brain.py +++ b/astroid/tests/unittest_brain.py @@ -517,5 +517,23 @@ def test_pytest(self): self.assertIn('mark', module) +class IOBrainTest(unittest.TestCase): + + @unittest.skipUnless(six.PY3, 'Needs Python 3 io model') + def test_sys_streams(self): + for name in {'stdout', 'stderr', 'stdin'}: + node = astroid.extract_node(''' + import sys + sys.{} + '''.format(name)) + inferred = next(node.infer()) + buffer = next(inferred.igetattr('buffer')) + self.assertIsInstance(buffer, astroid.Instance) + self.assertEqual(buffer.name, 'BufferedWriter') + raw = next(buffer.igetattr('raw')) + self.assertIsInstance(raw, astroid.Instance) + self.assertEqual(raw.name, 'FileIO') + + if __name__ == '__main__': unittest.main() From 466d1cfd5ff513bed43232363962cca98d0236d2 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Wed, 24 Aug 2016 09:44:03 +0300 Subject: [PATCH 277/312] Add support for discovering .pth file created by certain namespace packages. --- astroid/modutils.py | 29 ++++++++++++++++--- .../testdata/data/foogle/fax/__init__.py | 0 astroid/tests/testdata/data/foogle/fax/a.py | 1 + .../data/foogle_fax-0.12.5-py2.7-nspkg.pth | 1 + astroid/tests/unittest_manager.py | 18 ++++++++++++ 5 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 astroid/tests/testdata/data/foogle/fax/__init__.py create mode 100644 astroid/tests/testdata/data/foogle/fax/a.py create mode 100644 astroid/tests/testdata/data/foogle_fax-0.12.5-py2.7-nspkg.pth diff --git a/astroid/modutils.py b/astroid/modutils.py index be80ab70b5..1ccf526ecf 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -741,6 +741,22 @@ def contribute_to_path(self, spec, processed): return path +class DynamicImpFinder(ImpFinder): + + def find_module(self, modname, module_parts, processed, submodule_path): + if _is_namespace(modname) and modname in sys.modules: + submodule_path = sys.modules[modname].__path__ + return ModuleSpec(name=modname, location='', + origin='namespace', + type=ModuleType.PY_NAMESPACE, + submodule_search_locations=submodule_path) + + + def contribute_to_path(self, spec, processed): + return spec.submodule_search_locations + + + class ZipFinder(Finder): def __init__(self, path): @@ -775,12 +791,17 @@ def contribute_to_path(self, spec, processed): return spec.submodule_search_locations return None +_SPEC_FINDERS = ( + ImpFinder, + ZipFinder, +) +if _HAS_MACHINERY and sys.version_info[:2] > (3, 3): + _SPEC_FINDERS += (PEP420SpecFinder, ) +_SPEC_FINDERS += (DynamicImpFinder, ) -def _find_spec_with_path(search_path, modname, module_parts, processed, submodule_path): - finders = [finder(search_path) for finder in (ImpFinder, ZipFinder)] - if _HAS_MACHINERY and sys.version_info[:2] > (3, 3): - finders.append(PEP420SpecFinder(search_path)) +def _find_spec_with_path(search_path, modname, module_parts, processed, submodule_path): + finders = [finder(search_path) for finder in _SPEC_FINDERS] for finder in finders: spec = finder.find_module(modname, module_parts, processed, submodule_path) if spec is None: diff --git a/astroid/tests/testdata/data/foogle/fax/__init__.py b/astroid/tests/testdata/data/foogle/fax/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/astroid/tests/testdata/data/foogle/fax/a.py b/astroid/tests/testdata/data/foogle/fax/a.py new file mode 100644 index 0000000000..3d2b4b14ef --- /dev/null +++ b/astroid/tests/testdata/data/foogle/fax/a.py @@ -0,0 +1 @@ +x = 1 \ No newline at end of file diff --git a/astroid/tests/testdata/data/foogle_fax-0.12.5-py2.7-nspkg.pth b/astroid/tests/testdata/data/foogle_fax-0.12.5-py2.7-nspkg.pth new file mode 100644 index 0000000000..d3cb4591d4 --- /dev/null +++ b/astroid/tests/testdata/data/foogle_fax-0.12.5-py2.7-nspkg.pth @@ -0,0 +1 @@ +import sys, types, os;p = os.path.join(sys._getframe(1).f_locals['sitedir'], *('foogle',));ie = os.path.exists(os.path.join(p,'__init__.py'));m = not ie and sys.modules.setdefault('foogle', types.ModuleType('foogle'));mp = (m or []) and m.__dict__.setdefault('__path__',[]);(p not in mp) and mp.append(p) diff --git a/astroid/tests/unittest_manager.py b/astroid/tests/unittest_manager.py index 72594fbd8e..e5b48d53b0 100644 --- a/astroid/tests/unittest_manager.py +++ b/astroid/tests/unittest_manager.py @@ -7,9 +7,11 @@ import os import platform +import site import sys import unittest +import pkg_resources import six import astroid @@ -121,6 +123,22 @@ def test_implicit_namespace_package(self): for _ in range(2): sys.path.pop(0) + def test_namespace_package_pth_support(self): + directory = os.path.join(resources.DATA_DIR, 'data') + pth = 'foogle_fax-0.12.5-py2.7-nspkg.pth' + site.addpackage(directory, pth, []) + pkg_resources._namespace_packages['foogle'] = [] + try: + module = self.manager.ast_from_module_name('foogle.fax') + submodule = next(module.igetattr('a')) + value = next(submodule.igetattr('x')) + self.assertIsInstance(value, astroid.Const) + with self.assertRaises(exceptions.AstroidImportError): + self.manager.ast_from_module_name('foogle.moogle') + finally: + del pkg_resources._namespace_packages['foogle'] + sys.modules.pop('foogle') + def _test_ast_from_zip(self, archive): origpath = sys.path[:] sys.modules.pop('mypypa', None) From 3c714be4ca1270d8c17b655199835ca50b5de4db Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Wed, 24 Aug 2016 10:48:04 +0300 Subject: [PATCH 278/312] Move the spec finder under a new namespace, interpreter._import. --- astroid/interpreter/_import/__init__.py | 0 astroid/interpreter/_import/spec.py | 242 +++++++++++++++++++ astroid/interpreter/_import/util.py | 12 + astroid/manager.py | 32 ++- astroid/modutils.py | 294 +++--------------------- astroid/tests/unittest_modutils.py | 18 +- 6 files changed, 312 insertions(+), 286 deletions(-) create mode 100644 astroid/interpreter/_import/__init__.py create mode 100644 astroid/interpreter/_import/spec.py create mode 100644 astroid/interpreter/_import/util.py diff --git a/astroid/interpreter/_import/__init__.py b/astroid/interpreter/_import/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/astroid/interpreter/_import/spec.py b/astroid/interpreter/_import/spec.py new file mode 100644 index 0000000000..6d6c228f31 --- /dev/null +++ b/astroid/interpreter/_import/spec.py @@ -0,0 +1,242 @@ +# Copyright (c) 2016 Claudiu Popa + +import abc +import collections +import enum +import imp +import os +import sys +import zipimport +try: + import importlib.machinery + _HAS_MACHINERY = True +except ImportError: + _HAS_MACHINERY = False + +from . import util + +ModuleType = enum.Enum('ModuleType', 'C_BUILTIN C_EXTENSION PKG_DIRECTORY ' + 'PY_CODERESOURCE PY_COMPILED PY_FROZEN PY_RESOURCE ' + 'PY_SOURCE PY_ZIPMODULE PY_NAMESPACE') +_ImpTypes = {imp.C_BUILTIN: ModuleType.C_BUILTIN, + imp.C_EXTENSION: ModuleType.C_EXTENSION, + imp.PKG_DIRECTORY: ModuleType.PKG_DIRECTORY, + imp.PY_COMPILED: ModuleType.PY_COMPILED, + imp.PY_FROZEN: ModuleType.PY_FROZEN, + imp.PY_SOURCE: ModuleType.PY_SOURCE, + } +if hasattr(imp, 'PY_RESOURCE'): + _ImpTypes[imp.PY_RESOURCE] = ModuleType.PY_RESOURCE +if hasattr(imp, 'PY_CODERESOURCE'): + _ImpTypes[imp.PY_CODERESOURCE] = ModuleType.PY_CODERESOURCE + +def _imp_type_to_module_type(imp_type): + return _ImpTypes[imp_type] + + + +_ModuleSpec = collections.namedtuple('_ModuleSpec', 'name type location ' + 'origin submodule_search_locations') + +class ModuleSpec(_ModuleSpec): + + def __new__(cls, name, type, location=None, origin=None, + submodule_search_locations=None): + return _ModuleSpec.__new__(cls, name=name, type=type, + location=location, origin=origin, + submodule_search_locations=submodule_search_locations) + + +class Finder(object): + + def __init__(self, path=None): + self._path = path or sys.path + + @abc.abstractmethod + def find_module(self, modname, module_parts, processed, submodule_path): + pass + + def contribute_to_path(self, filename, processed): + return None + + +class ImpFinder(Finder): + + def find_module(self, modname, _, processed, submodule_path): + try: + stream, mp_filename, mp_desc = imp.find_module(modname, submodule_path) + except ImportError: + return None + + # Close resources. + if stream: + stream.close() + + return ModuleSpec(name=modname, location=mp_filename, + type=_imp_type_to_module_type(mp_desc[2])) + + def contribute_to_path(self, spec, processed): + if spec.location is None: + # Builtin. + return None + + if _is_setuptools_namespace(spec.location): + # extend_path is called, search sys.path for module/packages + # of this name see pkgutil.extend_path documentation + path = [os.path.join(p, *processed) for p in sys.path + if os.path.isdir(os.path.join(p, *processed))] + else: + path = [spec.location] + return path + + +class DynamicImpFinder(ImpFinder): + + def find_module(self, modname, module_parts, processed, submodule_path): + if util.is_namespace(modname) and modname in sys.modules: + submodule_path = sys.modules[modname].__path__ + return ModuleSpec(name=modname, location='', + origin='namespace', + type=ModuleType.PY_NAMESPACE, + submodule_search_locations=submodule_path) + + + def contribute_to_path(self, spec, processed): + return spec.submodule_search_locations + + + +class ZipFinder(Finder): + + def __init__(self, path): + super(ZipFinder, self).__init__(path) + self._zipimporters = _precache_zipimporters(path) + + def find_module(self, modname, module_parts, processed, submodule_path): + try: + file_type, filename, path = _search_zip(module_parts, self._zipimporters) + except ImportError: + return None + + return ModuleSpec(name=modname, location=filename, + origin='egg', type=file_type, + submodule_search_locations=path) + + +class PEP420SpecFinder(Finder): + + def find_module(self, modname, module_parts, processed, submodule_path): + spec = importlib.machinery.PathFinder.find_spec(modname, path=submodule_path) + if spec: + location = spec.origin if spec.origin != 'namespace' else None + type = ModuleType.PY_NAMESPACE if spec.origin == 'namespace' else None + spec = ModuleSpec(name=spec.name, location=location, + origin=spec.origin, type=type, + submodule_search_locations=list(spec.submodule_search_locations or [])) + return spec + + def contribute_to_path(self, spec, processed): + if spec.type == ModuleType.PY_NAMESPACE: + return spec.submodule_search_locations + return None + +_SPEC_FINDERS = ( + ImpFinder, + ZipFinder, +) +if _HAS_MACHINERY and sys.version_info[:2] > (3, 3): + _SPEC_FINDERS += (PEP420SpecFinder, ) +_SPEC_FINDERS += (DynamicImpFinder, ) + + +def _is_setuptools_namespace(location): + try: + with open(os.path.join(location, '__init__.py'), 'rb') as stream: + data = stream.read(4096) + except IOError: + pass + else: + extend_path = b'pkgutil' in data and b'extend_path' in data + declare_namespace = ( + b"pkg_resources" in data + and b"declare_namespace(__name__)" in data) + return extend_path or declare_namespace + + +def _precache_zipimporters(path=None): + pic = sys.path_importer_cache + path = path or sys.path + for entry_path in path: + if entry_path not in pic: + try: + pic[entry_path] = zipimport.zipimporter(entry_path) + except zipimport.ZipImportError: + continue + return pic + + +def _search_zip(modpath, pic): + for filepath, importer in list(pic.items()): + if importer is not None: + found = importer.find_module(modpath[0]) + if found: + if not importer.find_module(os.path.sep.join(modpath)): + raise ImportError('No module named %s in %s/%s' % ( + '.'.join(modpath[1:]), filepath, modpath)) + #import code; code.interact(local=locals()) + return (ModuleType.PY_ZIPMODULE, + os.path.abspath(filepath) + os.path.sep + os.path.sep.join(modpath), + filepath) + raise ImportError('No module named %s' % '.'.join(modpath)) + + +def _find_spec_with_path(search_path, modname, module_parts, processed, submodule_path): + finders = [finder(search_path) for finder in _SPEC_FINDERS] + for finder in finders: + spec = finder.find_module(modname, module_parts, processed, submodule_path) + if spec is None: + continue + return finder, spec + + raise ImportError('No module named %s' % '.'.join(module_parts)) + + +def find_spec(modpath, path=None): + """get a module type / file path + + :type modpath: list or tuple + :param modpath: + split module's name (i.e name of a module or package split + on '.'), with leading empty strings for explicit relative import + + :type path: list or None + :param path: + optional list of path where the module or package should be + searched (use sys.path if nothing or None is given) + + + :rtype: tuple(int, str) + :return: the module type flag and the file path for a module + """ + _path = path or sys.path + + # Need a copy for not mutating the argument. + modpath = modpath[:] + + submodule_path = None + module_parts = modpath[:] + processed = [] + + while modpath: + modname = modpath.pop(0) + finder, spec = _find_spec_with_path(_path, modname, + module_parts, processed, + submodule_path or path) + processed.append(modname) + if modpath: + submodule_path = finder.contribute_to_path(spec, processed) + + if spec.type == ModuleType.PKG_DIRECTORY: + spec = spec._replace(submodule_search_locations=submodule_path) + + return spec diff --git a/astroid/interpreter/_import/util.py b/astroid/interpreter/_import/util.py new file mode 100644 index 0000000000..6dc7b0296d --- /dev/null +++ b/astroid/interpreter/_import/util.py @@ -0,0 +1,12 @@ +# Copyright (c) 2016 Claudiu Popa + +try: + import pkg_resources +except ImportError: + pkg_resources = None + + +def is_namespace(modname): + # pylint: disable=no-member; astroid issue #290, modifying globals at runtime. + return (pkg_resources is not None + and modname in pkg_resources._namespace_packages) \ No newline at end of file diff --git a/astroid/manager.py b/astroid/manager.py index e57377b6c5..19cdc4a7a0 100644 --- a/astroid/manager.py +++ b/astroid/manager.py @@ -11,7 +11,6 @@ from various source and using a cache of built modules) """ -import imp import os import sys import zipimport @@ -19,6 +18,7 @@ import six from astroid import exceptions +from astroid.interpreter._import import spec from astroid import modutils from astroid import transforms from astroid import util @@ -109,13 +109,16 @@ def ast_from_module_name(self, modname, context_file=None): if context_file: os.chdir(os.path.dirname(context_file)) try: - spec = self.file_from_module_name(modname, context_file) - if spec.type == modutils.ModuleType.PY_ZIPMODULE: - module = self.zip_import_data(spec.location) + found_spec = self.file_from_module_name(modname, context_file) + if found_spec.type == spec.ModuleType.PY_ZIPMODULE: + module = self.zip_import_data(found_spec.location) if module is not None: return module - elif spec.type in (modutils.ModuleType.C_BUILTIN, modutils.ModuleType.C_EXTENSION): - if spec.type == modutils.ModuleType.C_EXTENSION and not self._can_load_extension(modname): + + elif found_spec.type in (spec.ModuleType.C_BUILTIN, + spec.ModuleType.C_EXTENSION): + if (found_spec.type == spec.ModuleType.C_EXTENSION + and not self._can_load_extension(modname)): return self._build_stub_module(modname) try: module = modutils.load_module_from_name(modname) @@ -124,17 +127,22 @@ def ast_from_module_name(self, modname, context_file=None): 'Loading {modname} failed with:\n{error}', modname=modname, path=spec.location, error=ex)) return self.ast_from_module(module, modname) - elif spec.type == modutils.ModuleType.PY_COMPILED: + + elif found_spec.type == spec.ModuleType.PY_COMPILED: raise exceptions.AstroidImportError( "Unable to load compiled module {modname}.", - modname=modname, path=spec.location) - elif spec.type == modutils.ModuleType.PY_NAMESPACE: - return self._build_namespace_module(modname, spec.submodule_search_locations) - if spec.location is None: + modname=modname, path=found_spec.location) + + elif found_spec.type == spec.ModuleType.PY_NAMESPACE: + return self._build_namespace_module(modname, + found_spec.submodule_search_locations) + + if found_spec.location is None: raise exceptions.AstroidImportError( "Can't find a file for module {modname}.", modname=modname) - return self.ast_from_file(spec.location, modname, fallback=False) + + return self.ast_from_file(found_spec.location, modname, fallback=False) except exceptions.AstroidBuildingError as e: for hook in self._failed_import_hooks: try: diff --git a/astroid/modutils.py b/astroid/modutils.py index 1ccf526ecf..80a88e68bf 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -18,22 +18,16 @@ :type BUILTIN_MODULES: dict :var BUILTIN_MODULES: dictionary with builtin module names has key """ - -import abc -import collections -import enum import imp import os import platform import sys from distutils.sysconfig import get_python_lib from distutils.errors import DistutilsPlatformError -import zipimport -try: - import importlib.machinery - _HAS_MACHINERY = True -except ImportError: - _HAS_MACHINERY = False +# pylint: disable=wrong-import-order +# distutils is replaced by virtualenv with a module that does +# weird path manipulations in order to get to the +# real distutils module. try: import pkg_resources @@ -41,25 +35,9 @@ pkg_resources = None import six -from astroid import util - -ModuleType = enum.Enum('ModuleType', 'C_BUILTIN C_EXTENSION PKG_DIRECTORY ' - 'PY_CODERESOURCE PY_COMPILED PY_FROZEN PY_RESOURCE ' - 'PY_SOURCE PY_ZIPMODULE PY_NAMESPACE') -_ImpTypes = {imp.C_BUILTIN: ModuleType.C_BUILTIN, - imp.C_EXTENSION: ModuleType.C_EXTENSION, - imp.PKG_DIRECTORY: ModuleType.PKG_DIRECTORY, - imp.PY_COMPILED: ModuleType.PY_COMPILED, - imp.PY_FROZEN: ModuleType.PY_FROZEN, - imp.PY_SOURCE: ModuleType.PY_SOURCE, - } -if hasattr(imp, 'PY_RESOURCE'): - _ImpTypes[imp.PY_RESOURCE] = ModuleType.PY_RESOURCE -if hasattr(imp, 'PY_CODERESOURCE'): - _ImpTypes[imp.PY_CODERESOURCE] = ModuleType.PY_CODERESOURCE - -def _imp_type_to_module_type(imp_type): - return _ImpTypes[imp_type] +from .interpreter._import import spec +from .interpreter._import import util as spec_util +from . import util if sys.platform.startswith('win'): PY_SOURCE_EXTS = ('py', 'pyw') @@ -255,7 +233,7 @@ def load_module_from_modpath(parts, path=None, use_sys=1): setattr(prevmodule, part, module) _file = getattr(module, '__file__', '') prevmodule = module - if not _file and _is_namespace(curname): + if not _file and spec_util.is_namespace(curname): continue if not _file and len(modpath) != len(parts): raise ImportError('no module in %s' % '.'.join(parts[len(modpath):])) @@ -296,7 +274,7 @@ def check_modpath_has_init(path, mod_path): modpath.append(part) path = os.path.join(path, part) if not _has_init(path): - old_namespace = _is_namespace('.'.join(modpath)) + old_namespace = spec_util.is_namespace('.'.join(modpath)) if not old_namespace: return False return True @@ -396,7 +374,7 @@ def file_info_from_modpath(modpath, path=None, context_file=None): return _spec_from_modpath(modpath, path, context) elif modpath == ['os', 'path']: # FIXME: currently ignoring search_path... - return ModuleSpec(name='os.path', location=os.path.__file__, type=imp.PY_SOURCE) + return spec.ModuleSpec(name='os.path', location=os.path.__file__, type=imp.PY_SOURCE) return _spec_from_modpath(modpath, path, context) @@ -555,7 +533,7 @@ def is_standard_module(modname, std_path=None): # (sys and __builtin__ for instance) if filename is None: # we assume there are no namespaces in stdlib - return not _is_namespace(modname) + return not spec_util.is_namespace(modname) filename = _normalize_path(filename) if filename.startswith(_cache_normalize_path(EXT_LIB_DIR)): return False @@ -567,7 +545,6 @@ def is_standard_module(modname, std_path=None): return False - def is_relative(modname, from_file): """return true if the given module name is relative to the given file name @@ -611,245 +588,26 @@ def _spec_from_modpath(modpath, path=None, context=None): location = None if context is not None: try: - spec = _find_spec(modpath, [context]) - location = spec.location + found_spec = spec.find_spec(modpath, [context]) + location = found_spec.location except ImportError: - spec = _find_spec(modpath, path) - location = spec.location + found_spec = spec.find_spec(modpath, path) + location = found_spec.location else: - spec = _find_spec(modpath, path) - if spec.type == ModuleType.PY_COMPILED: + found_spec = spec.find_spec(modpath, path) + if found_spec.type == spec.ModuleType.PY_COMPILED: try: - location = get_source_file(spec.location) - return spec._replace(location=location, type=ModuleSpec.PY_SOURCE) + location = get_source_file(found_spec.location) + return found_spec._replace(location=location, type=spec.ModuleSpec.PY_SOURCE) except NoSourceFile: - return spec.replace(location=location) - elif spec.type == ModuleType.C_BUILTIN: + return found_spec.replace(location=location) + elif found_spec.type == spec.ModuleType.C_BUILTIN: # integrated builtin module - return spec._replace(location=None) - elif spec.type == ModuleType.PKG_DIRECTORY: - location = _has_init(spec.location) - return spec._replace(location=location, type=ModuleType.PY_SOURCE) - return spec - - -def _search_zip(modpath, pic): - for filepath, importer in list(pic.items()): - if importer is not None: - found = importer.find_module(modpath[0]) - if found: - if not importer.find_module(os.path.sep.join(modpath)): - raise ImportError('No module named %s in %s/%s' % ( - '.'.join(modpath[1:]), filepath, modpath)) - #import code; code.interact(local=locals()) - return (ModuleType.PY_ZIPMODULE, - os.path.abspath(filepath) + os.path.sep + os.path.sep.join(modpath), - filepath) - raise ImportError('No module named %s' % '.'.join(modpath)) - -try: - import pkg_resources -except ImportError: - pkg_resources = None - - -def _precache_zipimporters(path=None): - pic = sys.path_importer_cache - path = path or sys.path - for entry_path in path: - if entry_path not in pic: - try: - pic[entry_path] = zipimport.zipimporter(entry_path) - except zipimport.ZipImportError: - continue - return pic - - -def _is_namespace(modname): - return (pkg_resources is not None - and modname in pkg_resources._namespace_packages) - - -def _is_setuptools_namespace(location): - try: - with open(os.path.join(location, '__init__.py'), 'rb') as stream: - data = stream.read(4096) - except IOError: - pass - else: - extend_path = b'pkgutil' in data and b'extend_path' in data - declare_namespace = ( - b"pkg_resources" in data - and b"declare_namespace(__name__)" in data) - return extend_path or declare_namespace - - -# Spec finders. - -_ModuleSpec = collections.namedtuple('_ModuleSpec', 'name type location ' - 'origin submodule_search_locations') - -class ModuleSpec(_ModuleSpec): - - def __new__(cls, name, type, location=None, origin=None, - submodule_search_locations=None): - return _ModuleSpec.__new__(cls, name=name, type=type, - location=location, origin=origin, - submodule_search_locations=submodule_search_locations) - - -class Finder(object): - - def __init__(self, path=None): - self._path = path or sys.path - - @abc.abstractmethod - def find_module(self, modname, module_parts, processed, submodule_path): - pass - - def contribute_to_path(self, filename, processed): - return None - - -class ImpFinder(Finder): - - def find_module(self, modname, _, processed, submodule_path): - try: - stream, mp_filename, mp_desc = imp.find_module(modname, submodule_path) - except ImportError: - return None - - # Close resources. - if stream: - stream.close() - - return ModuleSpec(name=modname, location=mp_filename, - type=_imp_type_to_module_type(mp_desc[2])) - - def contribute_to_path(self, spec, processed): - if spec.location is None: - # Builtin. - return None - - if _is_setuptools_namespace(spec.location): - # extend_path is called, search sys.path for module/packages - # of this name see pkgutil.extend_path documentation - path = [os.path.join(p, *processed) for p in sys.path - if os.path.isdir(os.path.join(p, *processed))] - else: - path = [spec.location] - return path - - -class DynamicImpFinder(ImpFinder): - - def find_module(self, modname, module_parts, processed, submodule_path): - if _is_namespace(modname) and modname in sys.modules: - submodule_path = sys.modules[modname].__path__ - return ModuleSpec(name=modname, location='', - origin='namespace', - type=ModuleType.PY_NAMESPACE, - submodule_search_locations=submodule_path) - - - def contribute_to_path(self, spec, processed): - return spec.submodule_search_locations - - - -class ZipFinder(Finder): - - def __init__(self, path): - super(ZipFinder, self).__init__(path) - self._zipimporters = _precache_zipimporters(path) - - def find_module(self, modname, module_parts, processed, submodule_path): - try: - file_type, filename, path = _search_zip(module_parts, self._zipimporters) - except ImportError: - return None - - return ModuleSpec(name=modname, location=filename, - origin='egg', type=file_type, - submodule_search_locations=path) - - -class PEP420SpecFinder(Finder): - - def find_module(self, modname, module_parts, processed, submodule_path): - spec = importlib.machinery.PathFinder.find_spec(modname, path=submodule_path) - if spec: - location = spec.origin if spec.origin != 'namespace' else None - type = ModuleType.PY_NAMESPACE if spec.origin == 'namespace' else None - spec = ModuleSpec(name=spec.name, location=location, - origin=spec.origin, type=type, - submodule_search_locations=list(spec.submodule_search_locations or [])) - return spec - - def contribute_to_path(self, spec, processed): - if spec.type == ModuleType.PY_NAMESPACE: - return spec.submodule_search_locations - return None - -_SPEC_FINDERS = ( - ImpFinder, - ZipFinder, -) -if _HAS_MACHINERY and sys.version_info[:2] > (3, 3): - _SPEC_FINDERS += (PEP420SpecFinder, ) -_SPEC_FINDERS += (DynamicImpFinder, ) - - -def _find_spec_with_path(search_path, modname, module_parts, processed, submodule_path): - finders = [finder(search_path) for finder in _SPEC_FINDERS] - for finder in finders: - spec = finder.find_module(modname, module_parts, processed, submodule_path) - if spec is None: - continue - return finder, spec - - raise ImportError('No module named %s' % '.'.join(module_parts)) - - -def _find_spec(modpath, path=None): - """get a module type / file path - - :type modpath: list or tuple - :param modpath: - splitted module's name (i.e name of a module or package splitted - on '.'), with leading empty strings for explicit relative import - - :type path: list or None - :param path: - optional list of path where the module or package should be - searched (use sys.path if nothing or None is given) - - - :rtype: tuple(int, str) - :return: the module type flag and the file path for a module - """ - _path = path or sys.path - - # Need a copy for not mutating the argument. - modpath = modpath[:] - - submodule_path = None - module_parts = modpath[:] - processed = [] - - while modpath: - modname = modpath.pop(0) - finder, spec = _find_spec_with_path(_path, modname, - module_parts, processed, - submodule_path or path) - processed.append(modname) - if modpath: - submodule_path = finder.contribute_to_path(spec, processed) - - if spec.type == ModuleType.PKG_DIRECTORY: - spec = spec._replace(submodule_search_locations=submodule_path) - - return spec + return found_spec._replace(location=None) + elif found_spec.type == spec.ModuleType.PKG_DIRECTORY: + location = _has_init(found_spec.location) + return found_spec._replace(location=location, type=spec.ModuleType.PY_SOURCE) + return found_spec def _is_python_file(filename): diff --git a/astroid/tests/unittest_modutils.py b/astroid/tests/unittest_modutils.py index 6484178ae5..96c9591dae 100644 --- a/astroid/tests/unittest_modutils.py +++ b/astroid/tests/unittest_modutils.py @@ -14,6 +14,8 @@ import sys import unittest +import astroid +from astroid.interpreter._import import spec from astroid import modutils from astroid.tests import resources @@ -31,16 +33,20 @@ def tearDown(self): del sys.path_importer_cache[k] def test_find_zipped_module(self): - spec = modutils._find_spec( + found_spec = spec.find_spec( [self.package], [resources.find('data/MyPyPa-0.1.0-py2.5.zip')]) - self.assertEqual(spec.type, modutils.ModuleType.PY_ZIPMODULE) - self.assertEqual(spec.location.split(os.sep)[-3:], ["data", "MyPyPa-0.1.0-py2.5.zip", self.package]) + self.assertEqual(found_spec.type, + spec.ModuleType.PY_ZIPMODULE) + self.assertEqual(found_spec.location.split(os.sep)[-3:], + ["data", "MyPyPa-0.1.0-py2.5.zip", self.package]) def test_find_egg_module(self): - spec = modutils._find_spec( + found_spec = spec.find_spec( [self.package], [resources.find('data/MyPyPa-0.1.0-py2.5.egg')]) - self.assertEqual(spec.type, modutils.ModuleType.PY_ZIPMODULE) - self.assertEqual(spec.location.split(os.sep)[-3:], ["data", "MyPyPa-0.1.0-py2.5.egg", self.package]) + self.assertEqual(found_spec.type, + spec.ModuleType.PY_ZIPMODULE) + self.assertEqual(found_spec.location.split(os.sep)[-3:], + ["data", "MyPyPa-0.1.0-py2.5.egg", self.package]) class LoadModuleFromNameTest(unittest.TestCase): From ccefd3b1c3c4a712eb34313f6841ac36dc2f76da Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Wed, 24 Aug 2016 11:02:01 +0300 Subject: [PATCH 279/312] Rename a couple of finders and document them. --- astroid/interpreter/_import/spec.py | 49 +++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/astroid/interpreter/_import/spec.py b/astroid/interpreter/_import/spec.py index 6d6c228f31..001acb3a2b 100644 --- a/astroid/interpreter/_import/spec.py +++ b/astroid/interpreter/_import/spec.py @@ -33,12 +33,15 @@ def _imp_type_to_module_type(imp_type): return _ImpTypes[imp_type] - - _ModuleSpec = collections.namedtuple('_ModuleSpec', 'name type location ' 'origin submodule_search_locations') class ModuleSpec(_ModuleSpec): + """Defines a class similar to PEP 420's ModuleSpec + + A module spec defines a name of a module, its type, location + and where submodules can be found, if the module is a package. + """ def __new__(cls, name, type, location=None, origin=None, submodule_search_locations=None): @@ -48,19 +51,36 @@ def __new__(cls, name, type, location=None, origin=None, class Finder(object): + """A finder is a class which knows how to find a particular module.""" def __init__(self, path=None): self._path = path or sys.path @abc.abstractmethod def find_module(self, modname, module_parts, processed, submodule_path): - pass + """Find the given module + + Each finder is responsible for each protocol of finding, as long as + they all return a ModuleSpec. + + :param str modname: The module which needs to be searched. + :param list module_parts: It should be a list of strings, + where each part contributes to the module's + namespace. + :param list processed: What parts from the module parts were processed + so far. + :param list submodule_path: A list of paths where the module + can be looked into. + :returns: A ModuleSpec, describing how and where the module was found, + None, otherwise. + """ def contribute_to_path(self, filename, processed): - return None + """Get a list of extra paths where this finder can search.""" class ImpFinder(Finder): + """A finder based on the imp module.""" def find_module(self, modname, _, processed, submodule_path): try: @@ -90,7 +110,8 @@ def contribute_to_path(self, spec, processed): return path -class DynamicImpFinder(ImpFinder): +class ExplicitNamespacePackageFinder(ImpFinder): + """A finder for the explicit namespace packages, generated through pkg_resources.""" def find_module(self, modname, module_parts, processed, submodule_path): if util.is_namespace(modname) and modname in sys.modules: @@ -105,8 +126,8 @@ def contribute_to_path(self, spec, processed): return spec.submodule_search_locations - class ZipFinder(Finder): + """Finder that knows how to find a module inside zip files.""" def __init__(self, path): super(ZipFinder, self).__init__(path) @@ -123,7 +144,8 @@ def find_module(self, modname, module_parts, processed, submodule_path): submodule_search_locations=path) -class PEP420SpecFinder(Finder): +class PathSpecFinder(Finder): + """Finder based on importlib.machinery.PathFinder.""" def find_module(self, modname, module_parts, processed, submodule_path): spec = importlib.machinery.PathFinder.find_spec(modname, path=submodule_path) @@ -140,13 +162,14 @@ def contribute_to_path(self, spec, processed): return spec.submodule_search_locations return None + _SPEC_FINDERS = ( ImpFinder, ZipFinder, ) if _HAS_MACHINERY and sys.version_info[:2] > (3, 3): - _SPEC_FINDERS += (PEP420SpecFinder, ) -_SPEC_FINDERS += (DynamicImpFinder, ) + _SPEC_FINDERS += (PathSpecFinder, ) +_SPEC_FINDERS += (ExplicitNamespacePackageFinder, ) def _is_setuptools_namespace(location): @@ -202,7 +225,7 @@ def _find_spec_with_path(search_path, modname, module_parts, processed, submodul def find_spec(modpath, path=None): - """get a module type / file path + """Find a spec for the given module. :type modpath: list or tuple :param modpath: @@ -214,9 +237,9 @@ def find_spec(modpath, path=None): optional list of path where the module or package should be searched (use sys.path if nothing or None is given) - - :rtype: tuple(int, str) - :return: the module type flag and the file path for a module + :rtype: ModuleSpec + :return: A module spec, which describes how the module was + found and where. """ _path = path or sys.path From e9e2f54f27baeecc009c1f6a30baf80143d9ca2d Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Wed, 24 Aug 2016 11:08:05 +0300 Subject: [PATCH 280/312] Export two helpers for verifying the type of a spec. --- astroid/modutils.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/astroid/modutils.py b/astroid/modutils.py index 80a88e68bf..bbfaf81b62 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -630,3 +630,10 @@ def _has_init(directory): if os.path.exists(mod_or_pack + '.' + ext): return mod_or_pack + '.' + ext return None + + +def is_namespace(specobj): + return specobj.type == spec.ModuleType.PY_NAMESPACE + +def is_directory(specobj): + return specobj.type == spec.ModuleType.PKG_DIRECTORY From 80a4642ffbbb88307408ba6e1bfbc4fd36d78e1e Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Wed, 24 Aug 2016 11:08:45 +0300 Subject: [PATCH 281/312] Add changelog entry for the explicit namespace package support. --- ChangeLog | 1 + 1 file changed, 1 insertion(+) diff --git a/ChangeLog b/ChangeLog index c19b0d38ac..4ed6762f2a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,7 @@ Change log for the astroid package (used to be astng) ===================================================== -- + * Add support for explicit namespace packages, created with pkg_resources. * Add brain tips for _io.TextIOWrapper's buffer and raw attributes. From 03aafda287413122309eb7762eef499bc8229ade Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Wed, 24 Aug 2016 11:16:21 +0300 Subject: [PATCH 282/312] Simplify an if expression. --- astroid/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astroid/util.py b/astroid/util.py index e4ca1261e5..8197c15e1b 100644 --- a/astroid/util.py +++ b/astroid/util.py @@ -13,7 +13,7 @@ import six import wrapt -JYTHON = True if platform.python_implementation() == 'Jython' else False +JYTHON = platform.python_implementation() == 'Jython' try: from functools import singledispatch as _singledispatch From 3f9e2efb27478bd77cbcd62bd9a3a5653ffc1e55 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Wed, 24 Aug 2016 11:23:19 +0300 Subject: [PATCH 283/312] Remove infered and instantiate_class from the code base --- astroid/tests/unittest_inference.py | 15 --------------- astroid/tree/base.py | 15 --------------- 2 files changed, 30 deletions(-) diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index 884dd830b7..facc51e563 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -368,21 +368,6 @@ def f(f=1): self.assertEqual(a_inferred[0].value, 1) self.assertEqual(len(a_inferred), 1) - def test_infered_warning(self): - code = ''' - def f(f=1): - return f - - a = f() - ''' - ast = parse(code, __name__) - a = ast['a'] - - with warnings.catch_warnings(record=True) as w: - with test_utils.enable_warning(PendingDeprecationWarning): - a.infered() - self.assertIsInstance(w[0].message, PendingDeprecationWarning) - def test_exc_ancestors(self): code = ''' def f(): diff --git a/astroid/tree/base.py b/astroid/tree/base.py index e4459f0f53..d6e2a54f36 100644 --- a/astroid/tree/base.py +++ b/astroid/tree/base.py @@ -7,7 +7,6 @@ import abc import pprint -import warnings import six @@ -269,24 +268,10 @@ 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): """instanciate a node if it is a ClassDef node, else return self""" return self - def instanciate_class(self): - warnings.warn('%s.instanciate_class() is deprecated and slated for ' - ' removal in astroid 2.0, use %s.instantiate_class() ' - ' instead.' % (type(self).__name__, type(self).__name__), - PendingDeprecationWarning, stacklevel=2) - return self.instantiate_class() - def has_base(self, node): return False From cafbc87f063bacf72d98f51d61c37b6671b0c045 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mois=C3=A9s=20L=C3=B3pez?= Date: Mon, 22 Aug 2016 14:14:06 -0500 Subject: [PATCH 284/312] Export __version__ in __init__. --- astroid/__init__.py | 1 + astroid/__pkginfo__.py | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/astroid/__init__.py b/astroid/__init__.py index 9292e463e7..f97dc38148 100644 --- a/astroid/__init__.py +++ b/astroid/__init__.py @@ -46,6 +46,7 @@ del _Context +from .__pkginfo__ import version as __version__ # WARNING: internal imports order matters ! # pylint: disable=redefined-builtin, wildcard-import diff --git a/astroid/__pkginfo__.py b/astroid/__pkginfo__.py index 496ac15a1b..2132e6a37e 100644 --- a/astroid/__pkginfo__.py +++ b/astroid/__pkginfo__.py @@ -10,8 +10,8 @@ modname = 'astroid' -numversion = (2, 0, 0) -version = '.'.join([str(num) for num in numversion]) +version = '2.0.0' +numversion = tuple(map(int, version.split('.'))) extras_require = {} install_requires = ['lazy_object_proxy', 'six', 'wrapt'] From 3b95628c7aa2b2c5ae173d0982edec0749760873 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 24 Oct 2016 19:21:56 +0300 Subject: [PATCH 285/312] Use the correct _replace member. Thanks to @dirthead for finding the bug. Close #368 --- astroid/modutils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astroid/modutils.py b/astroid/modutils.py index bbfaf81b62..b39d03d9b4 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -600,7 +600,7 @@ def _spec_from_modpath(modpath, path=None, context=None): location = get_source_file(found_spec.location) return found_spec._replace(location=location, type=spec.ModuleSpec.PY_SOURCE) except NoSourceFile: - return found_spec.replace(location=location) + return found_spec._replace(location=location) elif found_spec.type == spec.ModuleType.C_BUILTIN: # integrated builtin module return found_spec._replace(location=None) From ef965b545bdf765ea0dff1624a3258b2f1ba482d Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 1 Nov 2016 11:17:40 +0200 Subject: [PATCH 286/312] Add test for underscores in numeral literal (Python 3.6) --- astroid/tests/unittest_python3.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/astroid/tests/unittest_python3.py b/astroid/tests/unittest_python3.py index a9ef11e08b..d49fd99e7f 100644 --- a/astroid/tests/unittest_python3.py +++ b/astroid/tests/unittest_python3.py @@ -247,6 +247,25 @@ def test_positional_only_parameters(self): for name, arg in zip(('cls', 'class_or_tuple'), ast.args.positional_only): self.assertEqual(arg.name, name) + @require_version('3.6') + def test_format_string(self): + code = "f'{greetings} {person}'" + node = extract_node(code) + self.assertEqual(node.as_string(), code) + + @require_version('3.6') + def test_underscores_in_numeral_literal(self): + pairs = [ + ('10_1000', 101000), + ('10_000_000', 10000000), + ('0x_FF_FF', 65535), + ] + for value, expected in pairs: + node = extract_node(value) + inferred = next(node.infer()) + self.assertIsInstance(inferred, nodes.Const) + self.assertEqual(inferred.value, expected) + if __name__ == '__main__': unittest.main() From 7557f8f3af764d58a8fcdeecb248190b79a508cc Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 1 Nov 2016 17:51:28 +0200 Subject: [PATCH 287/312] Add the full path to the resources's data directory when testing the namespace package pth support. --- astroid/tests/resources.py | 2 ++ astroid/tests/unittest_manager.py | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/astroid/tests/resources.py b/astroid/tests/resources.py index fa53324f8f..d1271989c2 100644 --- a/astroid/tests/resources.py +++ b/astroid/tests/resources.py @@ -16,9 +16,11 @@ from astroid import builder from astroid import MANAGER +from astroid import tests DATA_DIR = 'testdata' +RESOURCE_PATH = os.path.join(tests.__path__[0], DATA_DIR, 'data') BUILTINS = six.moves.builtins.__name__ diff --git a/astroid/tests/unittest_manager.py b/astroid/tests/unittest_manager.py index e5b48d53b0..e3702f0bb2 100644 --- a/astroid/tests/unittest_manager.py +++ b/astroid/tests/unittest_manager.py @@ -124,10 +124,11 @@ def test_implicit_namespace_package(self): sys.path.pop(0) def test_namespace_package_pth_support(self): - directory = os.path.join(resources.DATA_DIR, 'data') pth = 'foogle_fax-0.12.5-py2.7-nspkg.pth' - site.addpackage(directory, pth, []) + site.addpackage(resources.RESOURCE_PATH, pth, []) + # pylint: disable=no-member; can't infer _namespace_packages, created at runtime. pkg_resources._namespace_packages['foogle'] = [] + try: module = self.manager.ast_from_module_name('foogle.fax') submodule = next(module.igetattr('a')) From 8aa364e8a672bf596f0b3bb62a6b72c72e188dbc Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 21 Nov 2016 10:45:14 +0200 Subject: [PATCH 288/312] Skip the current class when deducing its mro. Close #361. --- astroid/tree/scoped_nodes.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 79da1d4d94..e9c6a26dee 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -1925,6 +1925,8 @@ def mro(self, context=None): bases = list(self._inferred_bases(context=context)) bases_mro = [] for base in bases: + if base is self: + continue try: mro = base.mro(context=context) bases_mro.append(mro) From 9b75d6fb5255abe49305a62b346fb11ebfcbf956 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 21 Nov 2016 11:13:08 +0200 Subject: [PATCH 289/312] Drop support for Jython in the CI --- .travis.yml | 26 ++++++++++++-------------- tox.ini | 6 +++--- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5d6c717d59..b9ca4bf700 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,28 +14,27 @@ matrix: env: TOXENV=pypy - python: 3.5 env: TOXENV=pylint - - python: 2.7 - env: JYTHON=true; TOXENV=jython + - python: 3.6-dev + env: TOXENV=py36 before_install: - - if [ "$JYTHON" == "true" ]; then hg clone http://hg.python.org/jython; cd jython; ant; cd ..; chmod +x "$(pwd)/jython/dist/bin/jython"; fi - - if [ "$JYTHON" == "true" ]; then export PYTHON_EXE="$(pwd)/jython/dist/bin/jython"; else export PYTHON_EXE=python; fi - - if [ "$JYTHON" == "true" ]; then alias jython="$(pwd)/jython/dist/bin/jython"; export PATH="$(pwd)/jython/dist/bin:$PATH"; fi - - if [ "$JYTHON" == "true" ]; then wget https://bootstrap.pypa.io/get-pip.py; $PYTHON_EXE get-pip.py; fi - python --version - uname -a - lsb_release -a install: - - $PYTHON_EXE -m pip install pip -U + - python -m pip install pip -U - python -m pip install tox - python -m pip install pip -U - - $PYTHON_EXE -m pip install tox coverage coveralls - - $PYTHON_EXE -m virtualenv --version - - $PYTHON_EXE -m easy_install --version - - $PYTHON_EXE -m pip --version - - $PYTHON_EXE -m tox --version - - $PYTHON_EXE -m pip install -U setuptools + - python -m pip install tox coverage coveralls + - python -m virtualenv --version + - python -m easy_install --version + - python -m pip --version + - python -m tox --version script: + # Test install with current version of setuptools + - python -m pip install . + # Run the tests with newest version of setuptools + - python -m pip install -U setuptools - python -m tox -e coverage-erase,$TOXENV after_success: - tox -e coveralls @@ -46,4 +45,3 @@ notifications: email: on_success: always on_failure: always - diff --git a/tox.ini b/tox.ini index 656893bad2..4c035152da 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = coverage-clean, py27, py33, py34, py35, pypy, jython, pylint +envlist = py27, py33, py34, py35, py36, pypy, pylint skip_missing_interpreters = true [testenv:pylint] @@ -9,13 +9,13 @@ commands = pylint -rn --rcfile={toxinidir}/pylintrc {envsitepackagesdir}/astroid deps = # Temporary hack, ignore this. dictproxyhack - py27,py33,pypy,jython: enum34 + py27,py33,pypy: enum34 lazy-object-proxy nose #py27,py33,py34,py35: numpy pytest python-dateutil - py27,py33,pypy,jython: singledispatch + py27,py33,pypy: singledispatch six wrapt pylint: git+https://github.com/pycqa/pylint@master From 54a7cc708998cd27371f138ac0a335d28bfb02a4 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 21 Nov 2016 11:22:43 +0200 Subject: [PATCH 290/312] Hardcode the names of the expected unbound method names. --- astroid/tests/unittest_object_model.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/astroid/tests/unittest_object_model.py b/astroid/tests/unittest_object_model.py index 573ebbba85..141354f78c 100644 --- a/astroid/tests/unittest_object_model.py +++ b/astroid/tests/unittest_object_model.py @@ -106,8 +106,12 @@ def test(self): pass cls = next(ast_nodes[0].infer()) self.assertIsInstance(cls, astroid.ClassDef) - unbound = BUILTINS.locals[types.MethodType.__name__][0] - self.assertIs(cls, unbound) + if six.PY2: + unbound_name = 'instancemethod' + else: + unbound_name = 'function' + + self.assertEqual(cls.name, unbound_name) func = next(ast_nodes[1].infer()) self.assertIsInstance(func, astroid.FunctionDef) From 58c424a24252bfaebe93617f14f0bac10064c117 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 21 Nov 2016 16:25:10 +0200 Subject: [PATCH 291/312] getitem() method accepts nodes now, instead of Python objects. --- ChangeLog | 2 + astroid/inference.py | 51 +++--------------- astroid/protocols.py | 36 +++++++++---- astroid/tests/unittest_inference.py | 6 +-- astroid/tests/unittest_object_model.py | 16 +++--- astroid/tests/unittest_python3.py | 2 +- astroid/tree/node_classes.py | 74 ++++++++++++++++++++++---- 7 files changed, 111 insertions(+), 76 deletions(-) diff --git a/ChangeLog b/ChangeLog index 4ed6762f2a..9e4cc598d9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,8 @@ Change log for the astroid package (used to be astng) ===================================================== -- + * getitem() method accepts nodes now, instead of Python objects. + * Add support for explicit namespace packages, created with pkg_resources. * Add brain tips for _io.TextIOWrapper's buffer and raw attributes. diff --git a/astroid/inference.py b/astroid/inference.py index 37ee6b336a..f87adc9d41 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -212,32 +212,7 @@ def infer_global(self, context=None): util.reraise(structured) -_SLICE_SENTINEL = object() - -def _slice_value(index, context=None): - """Get the value of the given slice index.""" - if isinstance(index, treeabc.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, treeabc.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 +_SUBSCRIPT_SENTINEL = object() @decorators.raise_if_nothing_inferred @@ -264,27 +239,17 @@ def infer_subscript(self, context=None): yield util.Uninferable return + index_value = _SUBSCRIPT_SENTINEL if isinstance(value, runtimeabc.Instance): index_value = index else: - index_value = _SLICE_SENTINEL - if isinstance(index, treeabc.Const): - index_value = index.value - elif isinstance(index, treeabc.Slice): - # Infer slices from the original object. - lower = _slice_value(index.lower, context) - upper = _slice_value(index.upper, context) - step = _slice_value(index.step, context) - if all(elem is not _SLICE_SENTINEL for elem in (lower, upper, step)): - index_value = slice(lower, upper, step) - elif isinstance(index, runtimeabc.Instance): - index = inferenceutil.class_instance_as_index(index) - if index: - index_value = index.value + if isinstance(index, runtimeabc.Instance): + instance_as_index = inferenceutil.class_instance_as_index(index) + if instance_as_index: + index_value = instance_as_index else: - raise exceptions.InferenceError(node=self, context=context) - - if index_value is _SLICE_SENTINEL: + index_value = index + if index_value is _SUBSCRIPT_SENTINEL: raise exceptions.InferenceError(node=self, context=context) try: diff --git a/astroid/protocols.py b/astroid/protocols.py index cdd237cc42..e72ea69bc0 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -228,8 +228,9 @@ def assigned_stmts(self, nodes, node=None, context=None, assign_path=None): raise exceptions.NotSupportedError -def _resolve_looppart(parts, assign_path, context): +def _resolve_looppart(parts, assign_path, context, nodes): """recursive function to resolve multiple assignments on loops""" + assign_path = assign_path[:] index = assign_path.pop(0) for part in parts: @@ -243,8 +244,9 @@ def _resolve_looppart(parts, assign_path, context): except TypeError: continue # XXX log error for stmt in itered: + index_node = nodes.Const(index) try: - assigned = stmt.getitem(index, context) + assigned = stmt.getitem(index_node, context) except (AttributeError, IndexError): continue except TypeError: # stmt is unsubscriptable Const @@ -259,8 +261,10 @@ def _resolve_looppart(parts, assign_path, context): # we are not yet on the last part of the path # search on each possibly inferred value try: - for inferred in _resolve_looppart(assigned.infer(context), - assign_path, context): + for inferred in _resolve_looppart( + assigned.infer(context), + assign_path, context, + nodes=nodes): yield inferred except exceptions.InferenceError: break @@ -276,8 +280,10 @@ def for_assigned_stmts(self, nodes, node=None, context=None, assign_path=None): for item in lst.elts: yield item else: - for inferred in _resolve_looppart(self.iter.infer(context), - assign_path, context): + for inferred in _resolve_looppart( + self.iter.infer(context), + assign_path, context, + nodes=nodes): yield inferred # Explicit StopIteration to return error information, see comment # in raise_if_nothing_inferred. @@ -374,7 +380,11 @@ def assign_assigned_stmts(self, nodes, node=None, context=None, assign_path=None if not assign_path: yield self.value return - for inferred in _resolve_asspart(self.value.infer(context), assign_path, context): + for inferred in _resolve_asspart( + self.value.infer(context), + assign_path, + context, + nodes=nodes): yield inferred # Explicit StopIteration to return error information, see comment @@ -383,14 +393,15 @@ def assign_assigned_stmts(self, nodes, node=None, context=None, assign_path=None assign_path=assign_path, context=context)) -def _resolve_asspart(parts, assign_path, context): +def _resolve_asspart(parts, assign_path, context, nodes): """recursive function to resolve multiple assignments""" assign_path = assign_path[:] index = assign_path.pop(0) for part in parts: if hasattr(part, 'getitem'): + index_node = nodes.Const(index) try: - assigned = part.getitem(index, context) + assigned = part.getitem(index_node, context) # XXX raise a specific exception to avoid potential hiding of # unexpected exception ? except (TypeError, IndexError): @@ -405,8 +416,11 @@ def _resolve_asspart(parts, assign_path, context): # we are not yet on the last part of the path search on each # possibly inferred value try: - for inferred in _resolve_asspart(assigned.infer(context), - assign_path, context): + for inferred in _resolve_asspart( + assigned.infer(context), + assign_path, + context, + nodes=nodes): yield inferred except exceptions.InferenceError: return diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index facc51e563..0b05bdd5af 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -452,7 +452,7 @@ def test_builtin_types(self): inferred = next(n.infer()) self.assertIsInstance(inferred, nodes.List) self.assertIsInstance(inferred, runtimeabc.BuiltinInstance) - self.assertEqual(inferred.getitem(0).value, 1) + self.assertEqual(inferred.getitem(nodes.Const(0)).value, 1) self.assertIsInstance(inferred._proxied, nodes.ClassDef) self.assertEqual(inferred._proxied.name, 'list') self.assertIn('append', inferred._proxied.locals) @@ -460,7 +460,7 @@ def test_builtin_types(self): inferred = next(n.infer()) self.assertIsInstance(inferred, nodes.Tuple) self.assertIsInstance(inferred, runtimeabc.BuiltinInstance) - self.assertEqual(inferred.getitem(0).value, 2) + self.assertEqual(inferred.getitem(nodes.Const(0)).value, 2) self.assertIsInstance(inferred._proxied, nodes.ClassDef) self.assertEqual(inferred._proxied.name, 'tuple') n = ast['d'] @@ -478,7 +478,7 @@ def test_builtin_types(self): self.assertIn('lower', inferred._proxied.locals) n = ast['s2'] inferred = next(n.infer()) - self.assertEqual(inferred.getitem(0).value, '_') + self.assertEqual(inferred.getitem(nodes.Const(0)).value, '_') code = 's = {1}' ast = parse(code, __name__) diff --git a/astroid/tests/unittest_object_model.py b/astroid/tests/unittest_object_model.py index 141354f78c..fd0622b24f 100644 --- a/astroid/tests/unittest_object_model.py +++ b/astroid/tests/unittest_object_model.py @@ -49,7 +49,7 @@ def __init__(self): dunder_dict = next(ast_nodes[3].infer()) self.assertIsInstance(dunder_dict, astroid.Dict) - attr = next(dunder_dict.getitem('a').infer()) + attr = next(dunder_dict.getitem(astroid.Const('a')).infer()) self.assertIsInstance(attr, astroid.Const) self.assertEqual(attr.value, 42) @@ -411,13 +411,13 @@ def test(a: 1, *args: 2, f:4='lala', **kwarg:3)->2: pass ''') annotations = next(ast_node[0].infer()) self.assertIsInstance(annotations, astroid.Dict) - self.assertIsInstance(annotations.getitem('return'), astroid.Const) - self.assertEqual(annotations.getitem('return').value, 2) - self.assertIsInstance(annotations.getitem('a'), astroid.Const) - self.assertEqual(annotations.getitem('a').value, 1) - self.assertEqual(annotations.getitem('args').value, 2) - self.assertEqual(annotations.getitem('kwarg').value, 3) - self.assertEqual(annotations.getitem('f').value, 4) + self.assertIsInstance(annotations.getitem(astroid.Const('return')), astroid.Const) + self.assertEqual(annotations.getitem(astroid.Const('return')).value, 2) + self.assertIsInstance(annotations.getitem(astroid.Const('a')), astroid.Const) + self.assertEqual(annotations.getitem(astroid.Const('a')).value, 1) + self.assertEqual(annotations.getitem(astroid.Const('args')).value, 2) + self.assertEqual(annotations.getitem(astroid.Const('kwarg')).value, 3) + self.assertEqual(annotations.getitem(astroid.Const('f')).value, 4) kwdefaults = next(ast_node[1].infer()) self.assertIsInstance(kwdefaults, astroid.Dict) diff --git a/astroid/tests/unittest_python3.py b/astroid/tests/unittest_python3.py index d49fd99e7f..0aa82a47eb 100644 --- a/astroid/tests/unittest_python3.py +++ b/astroid/tests/unittest_python3.py @@ -236,7 +236,7 @@ def test_nested_unpacking_in_dicts(self): def test_unpacking_in_dict_getitem(self): node = extract_node('{1:2, **{2:3, 3:4}, **{5: 6}}') for key, expected in ((1, 2), (2, 3), (3, 4), (5, 6)): - value = node.getitem(key) + value = node.getitem(nodes.Const(key)) self.assertIsInstance(value, nodes.Const) self.assertEqual(value.value, expected) diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index 28cf001af1..a9460337d9 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -32,16 +32,60 @@ MANAGER = manager.AstroidManager() -def _container_getitem(instance, elts, index): +# 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 TypeError('Could not infer slice used in subscript.') + + +def _container_getitem(instance, elts, index, context=None): """Get a slice or an item, using the given *index*, for the given sequence.""" - if isinstance(index, slice): + if isinstance(index, Slice): + index_slice = _infer_slice(index, context=context) new_cls = instance.__class__() - new_cls.elts = elts[index] + new_cls.elts = elts[index_slice] new_cls.parent = instance.parent return new_cls - else: - return elts[index] + elif isinstance(index, Const): + return elts[index.value] + raise TypeError('Could not use %s as subscript index' % index) @util.register_implementation(treeabc.Statement) class Statement(base.NodeNG): @@ -504,13 +548,23 @@ def __init__(self, value, lineno=None, col_offset=None, parent=None): 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 TypeError( + 'Could not use type {} as subscript index'.format(type(index)) + ) + if isinstance(self.value, six.string_types): - return Const(self.value[index]) + 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]) + return Const(self.value[index_value]) + raise TypeError('%r (value=%s)' % (self, self.value)) def has_dynamic_getattr(self): @@ -650,9 +704,9 @@ def getitem(self, lookup_key, context=None): for inferredkey in key.infer(context): if inferredkey is util.Uninferable: continue - if isinstance(inferredkey, Const) \ - and inferredkey.value == lookup_key: - return value + if isinstance(inferredkey, Const) and isinstance(lookup_key, Const): + if inferredkey.value == lookup_key.value: + return value # This should raise KeyError, but all call sites only catch # IndexError. Let's leave it like that for now. raise IndexError(lookup_key) From e9e5c7fc61748b8b7cc73c948556fc9a830c162c Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sat, 3 Dec 2016 11:47:46 +0200 Subject: [PATCH 292/312] Use sys.base_exec_prefix in case sys.real_prefix is not defined sys.real_prefix is an addition of the *virtualenv* library, but it is not added when a virtual environment is created with the builtin *venv* library. In the latter's case, sys.base_exec_prefix is the one pointing to the original installation of Python. Now, in case sys.real_prefix is not defined, we fallback to sys.base_exec_prefix. --- astroid/modutils.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/astroid/modutils.py b/astroid/modutils.py index b39d03d9b4..2854e5f1a1 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -66,10 +66,18 @@ if os.name == 'nt': STD_LIB_DIRS.add(os.path.join(sys.prefix, 'dlls')) try: - # real_prefix is defined when running inside virtualenv. + # real_prefix is defined when running inside virtual environments, + # created with the **virtualenv** library. STD_LIB_DIRS.add(os.path.join(sys.real_prefix, 'dlls')) except AttributeError: - pass + # sys.base_exec_prefix is always defined, but in a virtual environment + # created with the stdlib **venv** module, it points to the original + # installation, if the virtual env is activated. + try: + STD_LIB_DIRS.add(os.path.join(sys.base_exec_prefix, 'dlls')) + except AttributeError: + pass + if platform.python_implementation() == 'PyPy': _root = os.path.join(sys.prefix, 'lib_pypy') STD_LIB_DIRS.add(_root) From c96049a10b0ff58ed0688eb2d83cc546fd6a67d0 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sat, 3 Dec 2016 14:45:56 +0200 Subject: [PATCH 293/312] Let the type error propagate as an AstroidTypeError. --- astroid/exceptions.py | 8 +++++ astroid/inference.py | 3 +- astroid/protocols.py | 8 ++--- astroid/tree/node_classes.py | 62 +++++++++++++++++++++++------------- 4 files changed, 53 insertions(+), 28 deletions(-) diff --git a/astroid/exceptions.py b/astroid/exceptions.py index 9dc807109c..3b34ae5423 100644 --- a/astroid/exceptions.py +++ b/astroid/exceptions.py @@ -206,6 +206,14 @@ class _NonDeducibleTypeHierarchy(Exception): """Raised when is_subtype / is_supertype can't deduce the relation between two types.""" +class AstroidIndexError(AstroidError): + """Raised when an Indexable / Mapping does not have an index / key.""" + + +class AstroidTypeError(AstroidError): + """Raised when a TypeError would be expected in Python code.""" + + # Backwards-compatibility aliases OperationError = util.BadOperationMessage UnaryOperationError = util.BadUnaryOperationMessage diff --git a/astroid/inference.py b/astroid/inference.py index f87adc9d41..aeb06baf69 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -254,7 +254,8 @@ def infer_subscript(self, context=None): try: assigned = value.getitem(index_value, context) - except (IndexError, TypeError) as exc: + except (exceptions.AstroidTypeError, + exceptions.AstroidIndexError) as exc: util.reraise(exceptions.InferenceError(node=self, error=exc, context=context)) diff --git a/astroid/protocols.py b/astroid/protocols.py index e72ea69bc0..cb071a0232 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -247,9 +247,9 @@ def _resolve_looppart(parts, assign_path, context, nodes): index_node = nodes.Const(index) try: assigned = stmt.getitem(index_node, context) - except (AttributeError, IndexError): - continue - except TypeError: # stmt is unsubscriptable Const + except (AttributeError, + exceptions.AstroidTypeError, + exceptions.AstroidIndexError): continue if not assign_path: # we achieved to resolved the assignment path, @@ -404,7 +404,7 @@ def _resolve_asspart(parts, assign_path, context, nodes): assigned = part.getitem(index_node, context) # XXX raise a specific exception to avoid potential hiding of # unexpected exception ? - except (TypeError, IndexError): + except (exceptions.AstroidTypeError, exceptions.AstroidIndexError): return if not assign_path: # we achieved to resolved the assignment path, don't infer the diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index a9460337d9..05f11a82b7 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -76,16 +76,27 @@ def _infer_slice(node, context=None): def _container_getitem(instance, elts, index, context=None): """Get a slice or an item, using the given *index*, for the given sequence.""" - 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] - - raise TypeError('Could not use %s as subscript index' % index) + 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.AstroidIndexError( + message='Type error {error!r}', error=exc, + node=instance, index=index, context=context)) + + raise exceptions.AstroidTypeError( + 'Could not use %s as subscript index' % index + ) @util.register_implementation(treeabc.Statement) class Statement(base.NodeNG): @@ -553,19 +564,26 @@ def getitem(self, index, context=None): elif isinstance(index, Slice): index_value = _infer_slice(index, context=context) else: - raise TypeError( + raise exceptions.AstroidTypeError( 'Could not use type {} as subscript index'.format(type(index)) ) - 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]) + 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 TypeError: + # The object does not support this operation, let the + # following error be raised instead. + pass - raise TypeError('%r (value=%s)' % (self, self.value)) + raise exceptions.AstroidTypeError( + '%r (value=%s)' % (self, self.value) + ) def has_dynamic_getattr(self): return False @@ -699,7 +717,7 @@ def getitem(self, lookup_key, context=None): if isinstance(key, DictUnpack): try: return value.getitem(lookup_key, context) - except IndexError: + except (exceptions.AstroidTypeError, exceptions.AstroidIndexError): continue for inferredkey in key.infer(context): if inferredkey is util.Uninferable: @@ -707,9 +725,7 @@ def getitem(self, lookup_key, context=None): if isinstance(inferredkey, Const) and isinstance(lookup_key, Const): if inferredkey.value == lookup_key.value: return value - # This should raise KeyError, but all call sites only catch - # IndexError. Let's leave it like that for now. - raise IndexError(lookup_key) + raise exceptions.AstroidIndexError(index) def bool_value(self): return bool(self.keys) From 3726700f2863af35742f9e048998dfed4221f864 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sat, 3 Dec 2016 14:58:52 +0200 Subject: [PATCH 294/312] Reraise the exceptions as astroid exceptions in .getitem. --- astroid/tree/node_classes.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index 05f11a82b7..d52cb2cadf 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -90,7 +90,7 @@ def _container_getitem(instance, elts, index, context=None): message='Index {index!s} out of range', node=instance, index=index, context=context)) except TypeError as exc: - util.reraise(exceptions.AstroidIndexError( + util.reraise(exceptions.AstroidTypeError( message='Type error {error!r}', error=exc, node=instance, index=index, context=context)) @@ -576,10 +576,14 @@ def getitem(self, index, context=None): # on Python 3. Also, indexing them should return # integers. return Const(self.value[index_value]) - except TypeError: - # The object does not support this operation, let the - # following error be raised instead. - pass + 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) @@ -725,7 +729,7 @@ def getitem(self, lookup_key, context=None): if isinstance(inferredkey, Const) and isinstance(lookup_key, Const): if inferredkey.value == lookup_key.value: return value - raise exceptions.AstroidIndexError(index) + raise exceptions.AstroidIndexError(lookup_key) def bool_value(self): return bool(self.keys) From 804bbbad544564a03cb59d52282177944989c5b1 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 18 Dec 2016 12:11:50 +0200 Subject: [PATCH 295/312] Force the path to be a list when passing it to find_module. --- astroid/interpreter/_import/spec.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/astroid/interpreter/_import/spec.py b/astroid/interpreter/_import/spec.py index 001acb3a2b..6a79550ed1 100644 --- a/astroid/interpreter/_import/spec.py +++ b/astroid/interpreter/_import/spec.py @@ -84,7 +84,8 @@ class ImpFinder(Finder): def find_module(self, modname, _, processed, submodule_path): try: - stream, mp_filename, mp_desc = imp.find_module(modname, submodule_path) + stream, mp_filename, mp_desc = imp.find_module( + modname, list(submodule_path)) except ImportError: return None From 4cecab0e15d5ed84cdc8f26a372069ce2189eba7 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 18 Dec 2016 12:14:03 +0200 Subject: [PATCH 296/312] Make sure we don't convert a None object to a list. --- astroid/interpreter/_import/spec.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/astroid/interpreter/_import/spec.py b/astroid/interpreter/_import/spec.py index 6a79550ed1..9c3b8cf221 100644 --- a/astroid/interpreter/_import/spec.py +++ b/astroid/interpreter/_import/spec.py @@ -82,10 +82,12 @@ def contribute_to_path(self, filename, processed): class ImpFinder(Finder): """A finder based on the imp module.""" - def find_module(self, modname, _, processed, submodule_path): + def find_module(self, modname, module_parts, processed, submodule_path): + if submodule_path is not None: + submodule_path = list(submodule_path) + try: - stream, mp_filename, mp_desc = imp.find_module( - modname, list(submodule_path)) + stream, mp_filename, mp_desc = imp.find_module(modname, submodule_path) except ImportError: return None From 3e1e0f0784a2ea46099b92d87d7d8ff6458da547 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 18 Dec 2016 15:07:07 +0200 Subject: [PATCH 297/312] Raise AstroidTypeError whenever a slice cannot be inferred, instead of raising TypeError and letting the upper context to handle it. --- astroid/tree/node_classes.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index d52cb2cadf..dca84db19b 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -71,7 +71,9 @@ def _infer_slice(node, context=None): if all(elem is not _SLICE_SENTINEL for elem in (lower, upper, step)): return slice(lower, upper, step) - raise TypeError('Could not infer slice used in subscript.') + 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): From 25c94a9b3d5f7a6080f8a820cb8708bde22e70f2 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Fri, 30 Dec 2016 13:40:34 +0200 Subject: [PATCH 298/312] Wrap values in Const, in order to find them properly. --- astroid/tests/unittest_object_model.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/astroid/tests/unittest_object_model.py b/astroid/tests/unittest_object_model.py index fd0622b24f..b6903edc9f 100644 --- a/astroid/tests/unittest_object_model.py +++ b/astroid/tests/unittest_object_model.py @@ -400,7 +400,7 @@ def test(): pass ''') annotations = next(ast_node.infer()) self.assertIsInstance(annotations, astroid.Dict) - self.assertIs(annotations.getitem('return'), astroid.Empty) + self.assertIs(annotations.getitem(astroid.Const('return')), astroid.Empty) @test_utils.require_version(minver='3.0') def test_annotations_kwdefaults(self): @@ -421,7 +421,7 @@ def test(a: 1, *args: 2, f:4='lala', **kwarg:3)->2: pass kwdefaults = next(ast_node[1].infer()) self.assertIsInstance(kwdefaults, astroid.Dict) - self.assertEqual(kwdefaults.getitem('f').value, 'lala') + self.assertEqual(kwdefaults.getitem(astroid.Const('f')).value, 'lala') @test_utils.require_version(maxver='3.0') def test_function_model_for_python2(self): From 1ba08bb3bc871eddaed1810311d8b365dc1542f5 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Fri, 30 Dec 2016 14:21:23 +0200 Subject: [PATCH 299/312] Add missing parameter to default implementation. --- astroid/protocols.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astroid/protocols.py b/astroid/protocols.py index cb071a0232..baf3762e73 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -134,7 +134,7 @@ def _infer_unary_op_dict(self, op, nodes): @util.singledispatch -def infer_binary_op(self, operator, other, context, method, nodes): +def infer_binary_op(self, opnode, operator, other, context, method, nodes): raise exceptions.BinaryOperationNotSupportedError From 47779406d852d1951078e169a7210b533f7e2d03 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Fri, 30 Dec 2016 15:04:14 +0200 Subject: [PATCH 300/312] Add support for inferring unary binary op for classes --- astroid/inference.py | 2 +- astroid/tests/unittest_inference.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/astroid/inference.py b/astroid/inference.py index aeb06baf69..a5780682ef 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -363,7 +363,7 @@ def infer_unaryop(self, context=None, nodes=None): else: yield util.Uninferable else: - if not isinstance(operand, runtimeabc.Instance): + if not isinstance(operand, (treeabc.ClassDef, runtimeabc.Instance)): # The operation was used on something which # doesn't support it. yield util.BadUnaryOperationMessage(operand, self.op, exc) diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index 0b05bdd5af..dfe11ea6cf 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -2818,6 +2818,17 @@ def pos(self): self.assertIsInstance(inferred, nodes.Const) self.assertEqual(inferred.value, 42) + def test_unary_op_classes(self): + ast_node = extract_node(''' + class A(object): + def __invert__(self): + return 42 + ~A + ''') + inferred = next(ast_node.infer()) + self.assertIsInstance(inferred, nodes.Const) + self.assertEqual(inferred.value, 42) + def _slicing_test_helper(self, pairs, cls, get_elts): for code, expected in pairs: ast_node = extract_node(code) From e37acdc4bfe7e0d714e86a5c7f5d9f956cef15e5 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Fri, 30 Dec 2016 15:42:50 +0200 Subject: [PATCH 301/312] Add a new mechanism for retrieving the special methods. --- astroid/inference.py | 9 +++- astroid/interpreter/dunder_lookup.py | 74 ++++++++++++++++++++++++++++ astroid/tests/unittest_inference.py | 6 ++- 3 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 astroid/interpreter/dunder_lookup.py diff --git a/astroid/inference.py b/astroid/inference.py index a5780682ef..4e46b4e189 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -20,6 +20,7 @@ from astroid import exceptions from astroid.interpreter import runtimeabc from astroid.interpreter import util as inferenceutil +from astroid.interpreter import dunder_lookup from astroid import protocols from astroid.tree import treeabc from astroid import util @@ -370,7 +371,13 @@ def infer_unaryop(self, context=None, nodes=None): continue try: - meth = operand.getattr(meth, context=context)[0] + try: + methods = dunder_lookup.lookup(operand, meth) + except exceptions.NotSupportedError: + yield util.BadUnaryOperationMessage(operand, self.op, exc) + continue + + meth = methods[0] inferred = next(meth.infer(context=context)) if inferred is util.Uninferable or not inferred.callable(): continue diff --git a/astroid/interpreter/dunder_lookup.py b/astroid/interpreter/dunder_lookup.py new file mode 100644 index 0000000000..4e0bad9c05 --- /dev/null +++ b/astroid/interpreter/dunder_lookup.py @@ -0,0 +1,74 @@ +# Copyright (c) 2016 Claudiu Popa +# 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 + +"""Contains logic for retrieving special methods. + +This implementation does not rely on the dot attribute access +logic, found in ``.getattr()``. The difference between these two +is that the dunder methods are looked with the type slots +(you can find more about these here +http://lucumr.pocoo.org/2014/8/16/the-python-i-would-like-to-see/) +As such, the lookup for the special methods is actually simpler than +the dot attribute access. +""" +import itertools + +import astroid +from astroid import exceptions +from astroid import util +from astroid.interpreter import runtimeabc +from astroid.tree import treeabc + + +def _lookup_in_mro(node, name): + local_attrs = node.locals.get(name, []) + external_attrs = node.external_attrs.get(name, []) + attrs = itertools.chain(local_attrs, external_attrs) + + nodes = itertools.chain.from_iterable( + itertools.chain( + ancestor.locals.get(name, []), + ancestor.external_attrs.get(name, []) + ) + for ancestor in node.ancestors(recurs=True) + ) + values = list(itertools.chain(attrs, nodes)) + if not values: + raise exceptions.NotSupportedError + + return values + + +@util.singledispatch +def lookup(node, name): + """Lookup the given special method name in the given *node* + + If the special method was found, then a list of attributes + will be returned. Otherwise, `astroid.NotSupportedError` + is going to be raised. + """ + raise exceptions.NotSupportedError + + +@lookup.register(treeabc.ClassDef) +def _(node, name): + metaclass = node.metaclass() + if metaclass is None: + raise exceptions.NotSupportedError + + return _lookup_in_mro(metaclass, name) + + +@lookup.register(runtimeabc.Instance) +def _(node, name): + return _lookup_in_mro(node, name) + + +@lookup.register(runtimeabc.BuiltinInstance) +def _(node, name): + values = node.locals.get(name, []) + if not values: + raise exceptions.NotSupportedError + + return values diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index dfe11ea6cf..4c13e8411d 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -2820,9 +2820,13 @@ def pos(self): def test_unary_op_classes(self): ast_node = extract_node(''' - class A(object): + import six + class Meta(type): def __invert__(self): return 42 + @six.add_metaclass(Meta) + class A(object): + pass ~A ''') inferred = next(ast_node.infer()) From 2fca596fa72783daf315855b4877d1a94b984e0e Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Fri, 30 Dec 2016 15:57:15 +0200 Subject: [PATCH 302/312] Add support for binary operations between classes. --- astroid/inference.py | 9 +++++++-- astroid/protocols.py | 4 +++- astroid/tests/unittest_inference.py | 16 ++++++++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/astroid/inference.py b/astroid/inference.py index 4e46b4e189..112ce986f9 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -131,7 +131,7 @@ def infer_import(self, context=None, asname=True): yield inferenceutil.do_import_module(self, name) except exceptions.AstroidBuildingError as exc: util.reraise(exceptions.InferenceError(node=self, error=exc, - context=context)) + context=context)) @infer.register(treeabc.ImportFrom) @@ -424,7 +424,12 @@ def _invoke_binop_inference(instance, opnode, op, other, context, method_name, # the inference at this point. raise exceptions.BinaryOperationNotSupportedError - method = instance.getattr(method_name)[0] + try: + methods = dunder_lookup.lookup(instance, method_name) + except exceptions.NotSupportedError: + raise exceptions.BinaryOperationNotSupportedError + + method = methods[0] inferred = next(method.infer(context=context)) return protocols.infer_binary_op(instance, opnode, op, other, context, inferred, nodes) diff --git a/astroid/protocols.py b/astroid/protocols.py index baf3762e73..cd1bec7e84 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -217,8 +217,10 @@ def _get_binop_context(context, other): @infer_binary_op.register(runtimeabc.Instance) +@infer_binary_op.register(treeabc.ClassDef) @decorators.yes_if_nothing_inferred -def instance_infer_binary_op(self, opnode, operator, other, context, method, nodes): +def instance_class_infer_binary_op( + self, opnode, operator, other, context, method, nodes): context = _get_binop_context(context, other) return method.infer_call_result(self, context) diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index 4c13e8411d..9579850fae 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -2537,6 +2537,22 @@ def __radd__(self, other): for node in ast_nodes: self.assertEqual(next(node.infer()), util.Uninferable) + def test_bin_op_classes(self): + ast_node = extract_node(''' + class Meta(type): + def __or__(self, other): + return 24 + import six + @six.add_metaclass(Meta) + class A(object): + pass + + A | A + ''') + inferred = next(ast_node.infer()) + self.assertIsInstance(inferred, nodes.Const) + self.assertEqual(inferred.value, 24) + def test_bin_op_supertype_more_complicated_example(self): ast_node = extract_node(''' class A(object): From d99980253e1c2242b23572d165dd3a9ff990527c Mon Sep 17 00:00:00 2001 From: Jared Garst Date: Mon, 24 Oct 2016 09:18:01 -0700 Subject: [PATCH 303/312] add format string support (#365) Format strings require support for two new nodes, FormattedValue, respectively JoinedStr. --- astroid/as_string.py | 18 +++++++++++++++++- astroid/nodes.py | 2 ++ astroid/tests/unittest_python3.py | 4 ++-- astroid/tree/node_classes.py | 25 +++++++++++++++++++++++++ astroid/tree/rebuilder.py | 12 ++++++++++++ astroid/tree/treeabc.py | 7 +++++++ tox.ini | 2 +- 7 files changed, 66 insertions(+), 4 deletions(-) diff --git a/astroid/as_string.py b/astroid/as_string.py index 8c49b7c03e..17e701c568 100644 --- a/astroid/as_string.py +++ b/astroid/as_string.py @@ -17,6 +17,9 @@ import six +from astroid.tree import treeabc + + class AsStringVisitor(object): """Visitor to render an Astroid node as a valid python code string""" @@ -468,6 +471,19 @@ def visit_asyncwith(self, node): def visit_asyncfor(self, node): return 'async %s' % self.visit_for(node) + def visit_joinedstr(self, node): + # Special treatment for constants, + # as we want to join literals not reprs + string = ''.join( + value.value if isinstance(value, treeabc.Const) + else value.accept(self) + for value in node.values + ) + return "f'%s'" % string + + def visit_formattedvalue(self, node): + return '{%s}' % node.value.accept(self) + def _import_string(names): """return a list of (name, asname) formatted as a string""" @@ -477,7 +493,7 @@ def _import_string(names): _names.append('%s as %s' % (name, asname)) else: _names.append(name) - return ', '.join(_names) + return ', '.join(_names) if sys.version_info >= (3, 0): diff --git a/astroid/nodes.py b/astroid/nodes.py index eb8155ffa0..7a9b89fb4d 100644 --- a/astroid/nodes.py +++ b/astroid/nodes.py @@ -36,6 +36,7 @@ List, Name, NameConstant, Nonlocal, Pass, Parameter, Print, Raise, Return, Set, Slice, Starred, Subscript, TryExcept, TryFinally, Tuple, UnaryOp, While, With, WithItem, Yield, YieldFrom, AsyncFor, Await, AsyncWith, + FormattedValue, JoinedStr, # Node not present in the builtin ast module. DictUnpack, # Special nodes for building from live objects. @@ -72,4 +73,5 @@ UnaryOp, Unknown, While, With, WithItem, Yield, YieldFrom, + FormattedValue, JoinedStr, ) diff --git a/astroid/tests/unittest_python3.py b/astroid/tests/unittest_python3.py index 0aa82a47eb..02dfd58e5c 100644 --- a/astroid/tests/unittest_python3.py +++ b/astroid/tests/unittest_python3.py @@ -88,7 +88,7 @@ def test_metaclass_error(self): @require_version('3.0') def test_metaclass_imported(self): astroid = self.builder.string_build(dedent(""" - from abc import ABCMeta + from abc import ABCMeta class Test(metaclass=ABCMeta): pass""")) klass = astroid.body[1] @@ -99,7 +99,7 @@ class Test(metaclass=ABCMeta): pass""")) @require_version('3.0') def test_as_string(self): body = dedent(""" - from abc import ABCMeta + from abc import ABCMeta class Test(metaclass=ABCMeta): pass""") astroid = self.builder.string_build(body) klass = astroid.body[1] diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index dca84db19b..a71109f73a 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -1401,6 +1401,31 @@ def __bool__(self): __nonzero__ = __bool__ +@util.register_implementation(treeabc.FormattedValue) +class FormattedValue(base.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 + + +@util.register_implementation(treeabc.JoinedStr) +class JoinedStr(base.NodeNG): + """Represents a list of string expressions to be joined.""" + _astroid_fields = ('values',) + value = None + + def postinit(self, values=None): + self.values = values + + + # Register additional inference dispatched functions. We do # this here, since we need to pass this module as an argument # to these functions, in order to avoid circular dependencies diff --git a/astroid/tree/rebuilder.py b/astroid/tree/rebuilder.py index bb963f3828..c45467f843 100644 --- a/astroid/tree/rebuilder.py +++ b/astroid/tree/rebuilder.py @@ -873,6 +873,18 @@ def visit_await(self, node, parent): def visit_asyncwith(self, node, parent): return self.visit_with(node, parent, constructor=nodes.AsyncWith) + def visit_joinedstr(self, node, parent): + newnode = nodes.JoinedStr(node.lineno, node.col_offset, parent) + newnode.postinit([self.visit(child, newnode) + for child in node.values]) + return newnode + + def visit_formattedvalue(self, node, parent): + newnode = nodes.FormattedValue(node.lineno, node.col_offset, parent) + newnode.postinit(self.visit(node.value, newnode), + node.conversion, + _visit_or_none(node, 'format_spec', self, newnode)) + return newnode if sys.version_info >= (3, 0): TreeRebuilder = TreeRebuilder3 diff --git a/astroid/tree/treeabc.py b/astroid/tree/treeabc.py index c72d17fcf3..5b6ad47116 100644 --- a/astroid/tree/treeabc.py +++ b/astroid/tree/treeabc.py @@ -341,3 +341,10 @@ class Empty(NodeNG): or defaults for arguments or anything where None is a valid value. """ + +class FormattedValue(NodeNG): + """Represents a PEP 498 string formatting""" + + +class JoinedStr(NodeNG): + """Represents a list of string expressions to be joined.""" diff --git a/tox.ini b/tox.ini index 4c035152da..213c369bb3 100644 --- a/tox.ini +++ b/tox.ini @@ -12,7 +12,7 @@ deps = py27,py33,pypy: enum34 lazy-object-proxy nose - #py27,py33,py34,py35: numpy + py27,py33,py34,py35,py36: numpy pytest python-dateutil py27,py33,pypy: singledispatch From 72bcccc19ea6a4896473dad9d22f61e7893a1a62 Mon Sep 17 00:00:00 2001 From: Derek Gustafson Date: Sun, 22 Jan 2017 07:31:15 -0500 Subject: [PATCH 304/312] Make ClassDefs support keyword arguments. (#384) --- astroid/tests/unittest_inference.py | 13 +++++++++++++ astroid/tests/unittest_scoped_nodes.py | 12 ++++++++++++ astroid/tree/rebuilder.py | 4 +++- astroid/tree/scoped_nodes.py | 7 +++++-- 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index 9579850fae..497b286c06 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -3231,6 +3231,19 @@ def test(cls): return cls self.assertIsInstance(inferred, ClassDef) self.assertEqual(inferred.name, 'A') + @test_utils.require_version(minver='3.0') + def test_metaclass_with_keyword_args(self): + ast_node = extract_node(''' + class TestMetaKlass(type): + def __new__(mcs, name, bases, ns, kwo_arg): + return super().__new__(mcs, name, bases, ns) + + class TestKlass(metaclass=TestMetaKlass, kwo_arg=42): #@ + pass + ''') + inferred = next(ast_node.infer()) + self.assertIsInstance(inferred, nodes.ClassDef) + def test_delayed_attributes_without_slots(self): ast_node = extract_node(''' class A(object): diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index 0bdc2929d9..0eb2afadf1 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -1665,6 +1665,18 @@ def irelevant(self): parent = bind.scope() self.assertEqual(len(parent.extra_decorators), 0) + @test_utils.require_version(minver='3.0') + def test_class_keywords(self): + data = ''' + class TestKlass(object, metaclass=TestMetaKlass, + foo=42, bar='baz'): + pass + ''' + astroid = builder.parse(data, __name__) + cls = astroid['TestKlass'] + self.assertEqual(len(cls.keywords), 2) + self.assertEqual([x.arg for x in cls.keywords], ['foo', 'bar']) + class CallSiteTest(unittest.TestCase): diff --git a/astroid/tree/rebuilder.py b/astroid/tree/rebuilder.py index c45467f843..7f51885407 100644 --- a/astroid/tree/rebuilder.py +++ b/astroid/tree/rebuilder.py @@ -353,7 +353,9 @@ def visit_classdef(self, node, parent, newstyle=None): for child in node.bases], [self.visit(child, newnode) for child in node.body], - decorators, newstyle, metaclass) + decorators, newstyle, metaclass, + [self.visit(kwd, newnode) for kwd in node.keywords + if kwd.arg != 'metaclass'] if PY3 else []) return newnode def visit_const(self, node, parent): diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index e9c6a26dee..257b13d952 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -1224,7 +1224,8 @@ class ClassDef(QualifiedNameMixin, base.FilterStmtsMixin, _newstyle = None def __init__(self, name=None, doc=None, lineno=None, - col_offset=None, parent=None): + col_offset=None, parent=None, keywords=None): + self.keywords = keywords self.bases = [] self.body = [] self.name = name @@ -1233,7 +1234,9 @@ def __init__(self, name=None, doc=None, lineno=None, self.external_attrs = collections.defaultdict(list) super(ClassDef, self).__init__(lineno, col_offset, parent) - def postinit(self, bases, body, decorators, newstyle=None, metaclass=None): + # pylint: disable=redefined-outer-name + def postinit(self, bases, body, decorators, newstyle=None, metaclass=None, keywords=None): + self.keywords = keywords self.bases = bases self.body = body self.decorators = decorators From fbf94cdaf7ae622621c8290ac9c95fbdd5637e23 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 22 Jan 2017 14:33:59 +0200 Subject: [PATCH 305/312] Add changelog entry for class keywords --- ChangeLog | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ChangeLog b/ChangeLog index 9e4cc598d9..1baa403c1b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,11 @@ Change log for the astroid package (used to be astng) ===================================================== -- + * Classes can now know their definition-time arguments. + + Classes can support keyword arguments, which are passed when + a class is constructed using ``__new__``. + * getitem() method accepts nodes now, instead of Python objects. * Add support for explicit namespace packages, created with pkg_resources. From a1fc66a981a8d25467ed720af973aa7168343915 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 22 Jan 2017 14:39:50 +0200 Subject: [PATCH 306/312] Drop installation of numpy for Python 3.3, not supported any longer --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 213c369bb3..2281ca69aa 100644 --- a/tox.ini +++ b/tox.ini @@ -12,7 +12,7 @@ deps = py27,py33,pypy: enum34 lazy-object-proxy nose - py27,py33,py34,py35,py36: numpy + py27,py34,py35,py36: numpy pytest python-dateutil py27,py33,pypy: singledispatch From 096ad490f89c568ca0da8f15dd74457da9be377b Mon Sep 17 00:00:00 2001 From: David Euresti Date: Tue, 3 Jan 2017 20:54:36 -0800 Subject: [PATCH 307/312] Implement __getitem__ inference for classes (using the metaclass) Essentially implement the getitem method in ClassDef which returns the correct value. Fixes #348 --- ChangeLog | 2 ++ astroid/tests/unittest_inference.py | 16 ++++++++++++++++ astroid/tree/scoped_nodes.py | 25 +++++++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/ChangeLog b/ChangeLog index 1baa403c1b..558b385785 100644 --- a/ChangeLog +++ b/ChangeLog @@ -7,6 +7,8 @@ Change log for the astroid package (used to be astng) Classes can support keyword arguments, which are passed when a class is constructed using ``__new__``. + * ClassDef now supports __getitem__ inference through the metaclass. + * getitem() method accepts nodes now, instead of Python objects. * Add support for explicit namespace packages, created with pkg_resources. diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index 497b286c06..0d1ea26619 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -2537,6 +2537,22 @@ def __radd__(self, other): for node in ast_nodes: self.assertEqual(next(node.infer()), util.Uninferable) + def test_metaclass__getitem__(self): + ast_node = extract_node(''' + class Meta(type): + def __getitem__(cls, arg): + return 24 + import six + @six.add_metaclass(Meta) + class A(object): + pass + + A['Awesome'] #@ + ''') + inferred = next(ast_node.infer()) + self.assertIsInstance(inferred, nodes.Const) + self.assertEqual(inferred.value, 24) + def test_bin_op_classes(self): ast_node = extract_node(''' class Meta(type): diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 257b13d952..31dfc9cd1a 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -34,6 +34,7 @@ from astroid.interpreter import objectmodel from astroid.interpreter import runtimeabc from astroid.interpreter.util import infer_stmts +from astroid.interpreter import dunder_lookup from astroid import manager from astroid.tree import base as treebase from astroid.tree import node_classes @@ -1671,6 +1672,30 @@ def _valid_getattr(node): pass return False + def getitem(self, index, context=None): + """Return the inference of a subscript. + + This is basically looking up the method in the metaclass and calling it. + """ + try: + methods = dunder_lookup.lookup(self, '__getitem__') + except (exceptions.AttributeInferenceError, + exceptions.NotSupportedError) as error: + util.reraise(exceptions.InferenceError(**vars(error))) + + method = methods[0] + + # Create a new callcontext for providing index as an argument. + if context: + new_context = context.clone() + else: + new_context = contextmod.InferenceContext() + + new_context.callcontext = contextmod.CallContext(args=[index]) + new_context.boundnode = self + + return next(method.infer_call_result(self, new_context)) + def methods(self): """return an iterator on all methods defined in the class and its ancestors From ad20c0643e2a14f076de3a906412aa4284b36c97 Mon Sep 17 00:00:00 2001 From: Derek Gustafson Date: Sun, 22 Jan 2017 08:07:17 -0500 Subject: [PATCH 308/312] Make is_argument search kwonlyargs. (#386) --- astroid/tests/unittest_nodes.py | 9 +++++++++ astroid/tree/node_classes.py | 7 ++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index 114eb15b6f..db461c3fc4 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -505,6 +505,15 @@ def func(a, self.skipTest('FIXME http://bugs.python.org/issue10445 ' '(no line number on function args)') + @test_utils.require_version(minver='3.0') + def test_kwoargs(self): + ast = builder.parse(''' + def func(*, x): + pass + ''') + args = ast['func'].args + self.assertTrue(args.is_argument('x')) + class UnboundMethodNodeTest(unittest.TestCase): diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index a71109f73a..fc4fd673ea 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -263,7 +263,12 @@ def is_argument(self, name): return True if self.kwarg and name == self.kwarg.name: return True - return self.find_argname(name, True)[1] is not None + is_arg = self.find_argname(name, True)[1] is not None + is_kwarg = ( + self.kwonlyargs and + _find_arg(name, self.kwonlyargs, True)[1] is not None + ) + return is_arg or is_kwarg def find_argname(self, argname, rec=False): """return index and Name node with given name""" From 3bec4e61791e575204d7e482fe135d88f87082be Mon Sep 17 00:00:00 2001 From: Derek Gustafson Date: Wed, 25 Jan 2017 14:09:16 -0500 Subject: [PATCH 309/312] Add getitem to defaultdict brain. (#387) --- astroid/brain/brain_collections.py | 1 + astroid/tests/unittest_brain.py | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/astroid/brain/brain_collections.py b/astroid/brain/brain_collections.py index cc53c5abfd..c1a02e7239 100644 --- a/astroid/brain/brain_collections.py +++ b/astroid/brain/brain_collections.py @@ -11,6 +11,7 @@ def _collections_transform(): class defaultdict(dict): default_factory = None def __missing__(self, key): pass + def __getitem__(self, key): return default_factory class deque(object): maxlen = 0 diff --git a/astroid/tests/unittest_brain.py b/astroid/tests/unittest_brain.py index 12258eee69..22d7ca040a 100644 --- a/astroid/tests/unittest_brain.py +++ b/astroid/tests/unittest_brain.py @@ -160,6 +160,19 @@ def test_namedtuple_uninferable_fields(self): self.assertIs(util.Uninferable, inferred) +class DefaultDictTest(unittest.TestCase): + + def test_1(self): + node = builder.extract_node(''' + from collections import defaultdict + + X = defaultdict(int) + X[0] + ''') + inferred = next(node.infer()) + self.assertIs(util.Uninferable, inferred) + + class ModuleExtenderTest(unittest.TestCase): def testExtensionModules(self): transformer = MANAGER._transform From 7d2013b568b8229b3b43f18661b923adc94145fe Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Thu, 26 Jan 2017 00:13:30 +0200 Subject: [PATCH 310/312] Let NotSupportedError be propagated. --- astroid/tests/unittest_inference.py | 7 ++++++- astroid/tree/scoped_nodes.py | 7 +------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index 0d1ea26619..16480ad141 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -2933,7 +2933,6 @@ def test_invalid_slicing_primaries(self): examples = [ "(lambda x: x)[1:2]", "1[2]", - "enumerate[2]", "(1, 2, 3)[a:]", "(1, 2, 3)[object:object]", "(1, 2, 3)[1:object]", @@ -2942,6 +2941,12 @@ def test_invalid_slicing_primaries(self): node = extract_node(code) self.assertRaises(InferenceError, next, node.infer()) + node = extract_node("enumerate[2]") + self.assertRaises( + exceptions.NotSupportedError, + next, node.infer() + ) + def test_instance_slicing(self): ast_nodes = extract_node(''' class A(object): diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 31dfc9cd1a..7e44b27a19 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -1677,12 +1677,7 @@ def getitem(self, index, context=None): This is basically looking up the method in the metaclass and calling it. """ - try: - methods = dunder_lookup.lookup(self, '__getitem__') - except (exceptions.AttributeInferenceError, - exceptions.NotSupportedError) as error: - util.reraise(exceptions.InferenceError(**vars(error))) - + methods = dunder_lookup.lookup(self, '__getitem__') method = methods[0] # Create a new callcontext for providing index as an argument. From 61f49f4dee7cda2976866ea1441973c4ab55dda6 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Thu, 26 Jan 2017 00:14:35 +0200 Subject: [PATCH 311/312] Different name for kwonlyargs in astroid 2.0 --- astroid/tree/node_classes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index fc4fd673ea..66e802b888 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -265,8 +265,8 @@ def is_argument(self, name): return True is_arg = self.find_argname(name, True)[1] is not None is_kwarg = ( - self.kwonlyargs and - _find_arg(name, self.kwonlyargs, True)[1] is not None + self.keyword_only and + _find_arg(name, self.keyword_only, True)[1] is not None ) return is_arg or is_kwarg From ac425e60a2c243eaf0b41ba1eb33b84a49d11eb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Rogalski?= Date: Mon, 30 Jan 2017 22:01:57 +0100 Subject: [PATCH 312/312] Mark __init_subclass__ as classmethod (#388) --- astroid/tests/unittest_scoped_nodes.py | 10 ++++++++++ astroid/tree/scoped_nodes.py | 3 +++ 2 files changed, 13 insertions(+) diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index 0eb2afadf1..e6ba9ec5de 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -572,6 +572,16 @@ def test() -> bytes: self.assertIsInstance(last_child, nodes.Return) self.assertEqual(func.tolineno, 5) + @test_utils.require_version(minver='3.6') + def test_method_init_subclass(self): + klass = builder.extract_node(''' + class MyClass: + def __init_subclass__(cls): + pass + ''') + method = klass['__init_subclass__'] + self.assertEqual([n.name for n in method.args.args], ['cls']) + self.assertEqual(method.type, 'classmethod') class ClassNodeTest(ModuleLoader, unittest.TestCase): diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 7e44b27a19..626c10e8c3 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -13,6 +13,7 @@ Lambda, GeneratorExp, DictComp and SetComp to some extent). """ +import sys import collections import io import itertools @@ -915,6 +916,8 @@ def type(self): if isinstance(frame, ClassDef): if self.name == '__new__': return 'classmethod' + elif sys.version_info >= (3, 6) and self.name == '__init_subclass__': + return 'classmethod' else: type_name = 'method'