summaryrefslogtreecommitdiff
path: root/rebuilder.py
diff options
context:
space:
mode:
authorEevee (Alex Munroe) <amunroe@yelp.com>2014-07-31 16:32:21 -0700
committerEevee (Alex Munroe) <amunroe@yelp.com>2014-07-31 16:32:21 -0700
commit3a090e2819c85abacae5dd244733408cb110e427 (patch)
tree22afd25a9e0b62afb2c456c8f63fd2b69d597f02 /rebuilder.py
parent58e5e9774ba6a5cb378f2e30eb026489d66922fd (diff)
parentbf919348a823a573ebbb7c435626172ba21b7c3c (diff)
downloadastroid-git-3a090e2819c85abacae5dd244733408cb110e427.tar.gz
merged with default
Diffstat (limited to 'rebuilder.py')
-rw-r--r--rebuilder.py59
1 files changed, 42 insertions, 17 deletions
diff --git a/rebuilder.py b/rebuilder.py
index c581eb98..95790314 100644
--- a/rebuilder.py
+++ b/rebuilder.py
@@ -20,8 +20,8 @@ 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,
@@ -47,15 +47,18 @@ _BIN_OP_CLASSES = {Add: '+',
Pow: '**',
Sub: '-',
LShift: '<<',
- RShift: '>>'}
+ RShift: '>>',
+ }
_BOOL_OP_CLASSES = {And: 'and',
- Or: 'or'}
+ Or: 'or',
+ }
_UNARY_OP_CLASSES = {UAdd: '+',
USub: '-',
Not: 'not',
- Invert: '~'}
+ Invert: '~',
+ }
_CMP_OP_CLASSES = {Eq: '==',
Gt: '>',
@@ -66,11 +69,13 @@ _CMP_OP_CLASSES = {Eq: '==',
Lt: '<',
LtE: '<=',
NotEq: '!=',
- NotIn: 'not in'}
+ NotIn: 'not in',
+ }
CONST_NAME_TRANSFORMS = {'None': None,
'True': True,
- 'False': False}
+ 'False': False,
+ }
REDIRECT = {'arguments': 'Arguments',
'Attribute': 'Getattr',
@@ -86,7 +91,7 @@ REDIRECT = {'arguments': 'Arguments',
'ImportFrom': 'From',
'keyword': 'Keyword',
'Repr': 'Backquote',
- }
+ }
PY3K = sys.version_info >= (3, 0)
PY34 = sys.version_info >= (3, 4)
@@ -174,10 +179,25 @@ class TreeRebuilder(object):
vararg, kwarg = node.vararg, node.kwarg
# change added in 82732 (7c5c678e4164), vararg and kwarg
# are instances of `_ast.arg`, not strings
- if vararg and PY34:
- vararg = vararg.arg
- if kwarg and PY34:
- kwarg = kwarg.arg
+ if vararg:
+ if PY34:
+ if vararg.annotation:
+ newnode.varargannotation = self.visit(vararg.annotation,
+ newnode)
+ vararg = vararg.arg
+ elif PY3K and node.varargannotation:
+ newnode.varargannotation = self.visit(node.varargannotation,
+ newnode)
+ if kwarg:
+ if PY34:
+ if kwarg.annotation:
+ newnode.kwargannotation = self.visit(kwarg.annotation,
+ newnode)
+ kwarg = kwarg.arg
+ elif PY3K:
+ if node.kwargannotation:
+ newnode.kwargannotation = self.visit(node.kwargannotation,
+ newnode)
newnode.vararg = vararg
newnode.kwarg = kwarg
# save argument names in locals:
@@ -217,8 +237,8 @@ class TreeRebuilder(object):
# set some function or metaclass infos XXX explain ?
klass = newnode.parent.frame()
if (isinstance(klass, new.Class)
- and isinstance(newnode.value, new.CallFunc)
- and isinstance(newnode.value.func, new.Name)):
+ and isinstance(newnode.value, new.CallFunc)
+ and isinstance(newnode.value.func, new.Name)):
func_name = newnode.value.func.name
for ass_node in newnode.targets:
try:
@@ -326,7 +346,7 @@ class TreeRebuilder(object):
_lineno_parent(node, newnode, parent)
newnode.left = self.visit(node.left, newnode)
newnode.ops = [(_CMP_OP_CLASSES[op.__class__], self.visit(expr, newnode))
- for (op, expr) in zip(node.ops, node.comparators)]
+ for (op, expr) in zip(node.ops, node.comparators)]
return newnode
def visit_comprehension(self, node, parent):
@@ -349,7 +369,7 @@ class TreeRebuilder(object):
if 'decorators' in node._fields: # py < 2.6, i.e. 2.5
decorators = node.decorators
else:
- decorators= node.decorator_list
+ decorators = node.decorator_list
newnode.nodes = [self.visit(child, newnode) for child in decorators]
return newnode
@@ -367,7 +387,7 @@ class TreeRebuilder(object):
newnode = new.Dict()
_lineno_parent(node, newnode, parent)
newnode.items = [(self.visit(key, newnode), self.visit(value, newnode))
- for key, value in zip(node.keys, node.values)]
+ for key, value in zip(node.keys, node.values)]
return newnode
def visit_dictcomp(self, node, parent):
@@ -467,6 +487,8 @@ class TreeRebuilder(object):
decorators = getattr(node, attr)
if decorators:
newnode.decorators = self.visit_decorators(node, newnode)
+ if PY3K and node.returns:
+ newnode.returns = self.visit(node.returns, newnode)
self._global_names.pop()
frame = newnode.parent.frame()
if isinstance(frame, new.Class):
@@ -782,6 +804,9 @@ class TreeRebuilder3k(TreeRebuilder):
newnode.kwonlyargs = [self.visit(child, newnode) for child in node.kwonlyargs]
self.asscontext = None
newnode.kw_defaults = [self.visit(child, newnode) if child else None for child in node.kw_defaults]
+ newnode.annotations = [
+ self.visit(arg.annotation, newnode) if arg.annotation else None
+ for arg in node.args]
return newnode
def visit_excepthandler(self, node, parent):