summaryrefslogtreecommitdiff
path: root/astroid/scoped_nodes.py
diff options
context:
space:
mode:
Diffstat (limited to 'astroid/scoped_nodes.py')
-rw-r--r--astroid/scoped_nodes.py64
1 files changed, 30 insertions, 34 deletions
diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py
index 3b1e18a..4f5e39d 100644
--- a/astroid/scoped_nodes.py
+++ b/astroid/scoped_nodes.py
@@ -86,20 +86,6 @@ def _verify_duplicates_mro(sequences, cls, context):
mros=sequences, cls=cls, context=context)
-def remove_nodes(cls):
- @wrapt.decorator
- def decorator(func, instance, args, kwargs):
- nodes = [n for n in func(*args, **kwargs) if not isinstance(n, cls)]
- if not nodes:
- # TODO: no way to access the context when raising this error.
- raise exceptions.AttributeInferenceError(
- 'No nodes left after removing all {remove_type!r} from '
- 'nodes inferred for {node!r}.',
- node=instance, remove_type=cls)
- return nodes
- return decorator
-
-
def function_to_method(n, klass):
if isinstance(n, FunctionDef):
if n.type == 'classmethod':
@@ -359,23 +345,28 @@ class Module(LocalsDictNodeNG):
def display_type(self):
return 'Module'
- @remove_nodes(node_classes.DelName)
def getattr(self, name, context=None, ignore_locals=False):
+ result = []
if name in self.special_attributes:
if name == '__file__':
- return [node_classes.const_factory(self.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)
- if not ignore_locals and name in self.locals:
- return self.locals[name]
- if self.package:
+ result = ([node_classes.const_factory(self.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)
+ elif not ignore_locals and name in self.locals:
+ result = self.locals[name]
+ elif self.package:
try:
- return [self.import_module(name, relative_only=True)]
+ result = [self.import_module(name, relative_only=True)]
except (exceptions.AstroidBuildingException, SyntaxError):
util.reraise(exceptions.AttributeInferenceError(target=self,
attribute=name,
context=context))
+ result = [n for n in result if not isinstance(n, node_classes.DelName)]
+ if result:
+ return result
raise exceptions.AttributeInferenceError(target=self, attribute=name,
context=context)
@@ -1311,7 +1302,6 @@ class ClassDef(mixins.FilterStmtsMixin, LocalsDictNodeNG,
def has_base(self, node):
return node in self.bases
- @remove_nodes(node_classes.DelAttr)
def local_attr(self, name, context=None):
"""return the list of assign node associated to name in this class
locals or in its parents
@@ -1320,15 +1310,20 @@ class ClassDef(mixins.FilterStmtsMixin, LocalsDictNodeNG,
if no attribute with this name has been find in this class or
its parent classes
"""
- try:
- return self.locals[name]
- except KeyError:
- for class_node in self.local_attr_ancestors(name, context):
- return class_node.locals[name]
+ result = []
+ if name in self.locals:
+ result = self.locals[name]
+ else:
+ try:
+ result = next(self.local_attr_ancestors(name, context)).locals[name]
+ except StopIteration:
+ pass
+ result = [n for n in result if not isinstance(n, node_classes.DelAttr)]
+ if result:
+ return result
raise exceptions.AttributeInferenceError(target=self, attribute=name,
context=context)
- @remove_nodes(node_classes.DelAttr)
def instance_attr(self, name, context=None):
"""return the astroid nodes associated to name in this class instance
attributes dictionary and in its parents
@@ -1343,10 +1338,11 @@ class ClassDef(mixins.FilterStmtsMixin, LocalsDictNodeNG,
# get all values from parents
for class_node in self.instance_attr_ancestors(name, context):
values += class_node.instance_attrs[name]
- if not values:
- raise exceptions.AttributeInferenceError(target=self, attribute=name,
- context=context)
- return values
+ values = [n for n in values if not isinstance(n, node_classes.DelAttr)]
+ if values:
+ return values
+ raise exceptions.AttributeInferenceError(target=self, attribute=name,
+ context=context)
def instanciate_class(self):
"""return Instance of ClassDef node, else return self"""