diff options
-rw-r--r-- | rebuilder.py | 14 | ||||
-rw-r--r-- | scoped_nodes.py | 25 |
2 files changed, 19 insertions, 20 deletions
diff --git a/rebuilder.py b/rebuilder.py index e3e1fc16..862de858 100644 --- a/rebuilder.py +++ b/rebuilder.py @@ -21,7 +21,7 @@ order to get a single Astroid representation import sys from warnings import warn -from _ast import (Expr as Discard, Str, Name, Attribute, +from _ast import (Expr as Discard, Str, # binary operators Add, Div, FloorDiv, Mod, Mult, Pow, Sub, BitAnd, BitOr, BitXor, LShift, RShift, @@ -99,13 +99,6 @@ def _init_set_doc(node, newnode): except IndexError: pass # ast built from scratch -def _infer_metaclass(node): - if isinstance(node, Name): - return node.id - elif isinstance(node, Attribute): - # TODO: should we retrieve the fully qualified name of the class? - return node.attr - def _lineno_parent(oldnode, newnode, parent): newnode.parent = parent if hasattr(oldnode, 'lineno'): @@ -251,7 +244,8 @@ class TreeRebuilder(object): except (AttributeError, KeyError): continue elif getattr(newnode.targets[0], 'name', None) == '__metaclass__': - self._metaclass[-1] = _infer_metaclass(node.value) or 'type' + # XXX check more... + self._metaclass[-1] = 'type' newnode.set_line_info(newnode.last_child()) return newnode @@ -944,7 +938,7 @@ class TreeRebuilder3k(TreeRebuilder): newnode = super(TreeRebuilder3k, self).visit_class(node, parent) for keyword in node.keywords: if keyword.arg == 'metaclass': - newnode._metaclass = _infer_metaclass(keyword.value) or 'type' + newnode._metaclass = self.visit(keyword, newnode).value break return newnode diff --git a/scoped_nodes.py b/scoped_nodes.py index 3f6e55a5..939fc74b 100644 --- a/scoped_nodes.py +++ b/scoped_nodes.py @@ -992,18 +992,23 @@ class Class(Statement, LocalsDictNodeNG, FilterStmtsMixin): raise InferenceError() _metaclass = None - def metaclass(self): - """ return the metaclass of this class """ + def _metaclass_search(self): + """ Return the metaclass of this class """ if self._metaclass: - return self._metaclass + # Expects this from Py3k TreeRebuilder + try: + return self._metaclass.infered()[0] + except InferenceError: + return try: - meta = Instance(self).getattr('__metaclass__')[0] + meta = self.getattr('__metaclass__')[0] except NotFoundError: return - - # XXX: We could return the actual class instance, not the string - # representing its name, but I don't know how to handle this - # with Py3's metaclass keyword.. - return meta.infered()[0].name -
\ No newline at end of file + try: + infered = meta.infered()[0] + except InferenceError: + return + if infered is YES: # don't expose this + return None + return infered |