summaryrefslogtreecommitdiff
path: root/astroid/rebuilder.py
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2018-10-02 09:43:56 +0200
committerClaudiu Popa <pcmanticore@gmail.com>2018-10-02 09:43:56 +0200
commitadd5f7b8eba427de9d39caae864bbc6dc37ef980 (patch)
tree86bb7df77f389062879365e97df0c3d53d38a8a6 /astroid/rebuilder.py
parentbb46cd198b93f3715da94089d42f39fc910892ff (diff)
downloadastroid-git-add5f7b8eba427de9d39caae864bbc6dc37ef980.tar.gz
Initial formatting of astroid
Diffstat (limited to 'astroid/rebuilder.py')
-rw-r--r--astroid/rebuilder.py639
1 files changed, 354 insertions, 285 deletions
diff --git a/astroid/rebuilder.py b/astroid/rebuilder.py
index 3cac7083..4a2d52ab 100644
--- a/astroid/rebuilder.py
+++ b/astroid/rebuilder.py
@@ -27,18 +27,16 @@ from astroid._ast import _parse, _get_parser_module, parse_function_type_comment
from astroid import nodes
-CONST_NAME_TRANSFORMS = {'None': None,
- 'True': True,
- 'False': False,
- }
-
-REDIRECT = {'arguments': 'Arguments',
- 'comprehension': 'Comprehension',
- "ListCompFor": 'Comprehension',
- "GenExprFor": 'Comprehension',
- 'excepthandler': 'ExceptHandler',
- 'keyword': 'Keyword',
- }
+CONST_NAME_TRANSFORMS = {"None": None, "True": True, "False": False}
+
+REDIRECT = {
+ "arguments": "Arguments",
+ "comprehension": "Comprehension",
+ "ListCompFor": "Comprehension",
+ "GenExprFor": "Comprehension",
+ "excepthandler": "ExceptHandler",
+ "keyword": "Keyword",
+}
PY3 = sys.version_info >= (3, 0)
PY34 = sys.version_info >= (3, 4)
PY37 = sys.version_info >= (3, 7)
@@ -46,52 +44,44 @@ PY37 = sys.version_info >= (3, 7)
def _binary_operators_from_module(module):
binary_operators = {
- module.Add: '+',
- module.BitAnd: '&',
- module.BitOr: '|',
- module.BitXor: '^',
- module.Div: '/',
- module.FloorDiv: '//',
- module.Mod: '%',
- module.Mult: '*',
- module.Pow: '**',
- module.Sub: '-',
- module.LShift: '<<',
- module.RShift: '>>',
+ module.Add: "+",
+ module.BitAnd: "&",
+ module.BitOr: "|",
+ module.BitXor: "^",
+ module.Div: "/",
+ module.FloorDiv: "//",
+ module.Mod: "%",
+ module.Mult: "*",
+ module.Pow: "**",
+ module.Sub: "-",
+ module.LShift: "<<",
+ module.RShift: ">>",
}
if sys.version_info >= (3, 5):
- binary_operators[module.MatMult] = '@'
+ binary_operators[module.MatMult] = "@"
return binary_operators
def _bool_operators_from_module(module):
- return {
- module.And: 'and',
- module.Or: 'or',
- }
+ return {module.And: "and", module.Or: "or"}
def _unary_operators_from_module(module):
- return {
- module.UAdd: '+',
- module.USub: '-',
- module.Not: 'not',
- module.Invert: '~',
- }
+ return {module.UAdd: "+", module.USub: "-", module.Not: "not", module.Invert: "~"}
def _compare_operators_from_module(module):
return {
- module.Eq: '==',
- module.Gt: '>',
- module.GtE: '>=',
- module.In: 'in',
- module.Is: 'is',
- module.IsNot: 'is not',
- module.Lt: '<',
- module.LtE: '<=',
- module.NotEq: '!=',
- module.NotIn: 'not in',
+ module.Eq: "==",
+ module.Gt: ">",
+ module.GtE: ">=",
+ module.In: "in",
+ module.Is: "is",
+ module.IsNot: "is not",
+ module.Lt: "<",
+ module.LtE: "<=",
+ module.NotEq: "!=",
+ module.NotIn: "not in",
}
@@ -104,8 +94,7 @@ def _contexts_from_module(module):
}
-def _visit_or_none(node, attr, visitor, parent, visit='visit',
- **kws):
+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.
@@ -137,12 +126,14 @@ class TreeRebuilder:
def _get_doc(self, node):
try:
- if PY37 and hasattr(node, 'docstring'):
+ if PY37 and hasattr(node, "docstring"):
doc = node.docstring
return node, doc
- if (node.body
- and isinstance(node.body[0], self._parser_module.Expr)
- and isinstance(node.body[0].value, self._parser_module.Str)):
+ if (
+ node.body
+ and isinstance(node.body[0], self._parser_module.Expr)
+ and isinstance(node.body[0].value, self._parser_module.Str)
+ ):
doc = node.body[0].value.s
node.body = node.body[1:]
return node, doc
@@ -156,9 +147,14 @@ class TreeRebuilder:
def visit_module(self, node, modname, modpath, package):
"""visit a Module node by returning a fresh instance of it"""
node, doc = self._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,
+ file=modpath,
+ path=[modpath],
+ package=package,
+ parent=None,
+ )
newnode.postinit([self.visit(child, newnode) for child in node.body])
return newnode
@@ -168,7 +164,7 @@ class TreeRebuilder:
visit_method = self._visit_meths[cls]
else:
cls_name = cls.__name__
- visit_name = 'visit_' + REDIRECT.get(cls_name, cls_name).lower()
+ 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)
@@ -184,14 +180,13 @@ class TreeRebuilder:
"""visit an 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)
+ 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]
+ defaults = [self.visit(child, newnode) for child in node.defaults]
varargannotation = None
kwargannotation = None
# change added in 82732 (7c5c678e4164), vararg and kwarg
@@ -199,22 +194,23 @@ class TreeRebuilder:
if vararg:
if PY34:
if node.vararg.annotation:
- varargannotation = self.visit(node.vararg.annotation,
- newnode)
+ varargannotation = self.visit(node.vararg.annotation, newnode)
vararg = vararg.arg
if kwarg:
if PY34:
if node.kwarg.annotation:
- kwargannotation = self.visit(node.kwarg.annotation,
- newnode)
+ kwargannotation = self.visit(node.kwarg.annotation, newnode)
kwarg = kwarg.arg
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]
+ 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
+ ]
kwonlyargs_annotations = [
self.visit(arg.annotation, newnode) if arg.annotation else None
for arg in node.kwonlyargs
@@ -233,7 +229,7 @@ class TreeRebuilder:
annotations=annotations,
kwonlyargs_annotations=kwonlyargs_annotations,
varargannotation=varargannotation,
- kwargannotation=kwargannotation
+ kwargannotation=kwargannotation,
)
# save argument names in locals:
if vararg:
@@ -253,7 +249,7 @@ class TreeRebuilder:
return newnode
def check_type_comment(self, node):
- type_comment = getattr(node, 'type_comment', None)
+ type_comment = getattr(node, "type_comment", None)
if not type_comment:
return None
@@ -270,7 +266,7 @@ class TreeRebuilder:
return type_object.value
def check_function_type_comment(self, node):
- type_comment = getattr(node, 'type_comment', None)
+ type_comment = getattr(node, "type_comment", None)
if not type_comment:
return None
@@ -281,7 +277,9 @@ class TreeRebuilder:
return None
returns = None
- argtypes = [self.visit(elem, node) for elem in (type_comment_ast.argtypes or [])]
+ argtypes = [
+ self.visit(elem, node) for elem in (type_comment_ast.argtypes or [])
+ ]
if type_comment_ast.returns:
returns = self.visit(type_comment_ast.returns, node)
@@ -299,18 +297,27 @@ class TreeRebuilder:
return newnode
def visit_assignname(self, node, parent, node_name=None):
- '''visit a node and return a AssignName node'''
- newnode = nodes.AssignName(node_name, getattr(node, 'lineno', None),
- getattr(node, 'col_offset', None), parent)
+ """visit a node and return a AssignName node"""
+ 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):
"""visit a AugAssign node by returning a fresh instance of it"""
- newnode = nodes.AugAssign(self._bin_op_classes[type(node.op)] + "=",
- node.lineno, node.col_offset, parent)
- newnode.postinit(self.visit(node.target, newnode),
- self.visit(node.value, newnode))
+ newnode = nodes.AugAssign(
+ self._bin_op_classes[type(node.op)] + "=",
+ node.lineno,
+ node.col_offset,
+ parent,
+ )
+ newnode.postinit(
+ self.visit(node.target, newnode), self.visit(node.value, newnode)
+ )
return newnode
def visit_repr(self, node, parent):
@@ -321,112 +328,129 @@ class TreeRebuilder:
def visit_binop(self, node, parent):
"""visit a BinOp node by returning a fresh instance of it"""
- newnode = nodes.BinOp(self._bin_op_classes[type(node.op)],
- node.lineno, node.col_offset, parent)
- newnode.postinit(self.visit(node.left, newnode),
- self.visit(node.right, newnode))
+ newnode = nodes.BinOp(
+ self._bin_op_classes[type(node.op)], node.lineno, node.col_offset, parent
+ )
+ newnode.postinit(
+ self.visit(node.left, newnode), self.visit(node.right, newnode)
+ )
return newnode
def visit_boolop(self, node, parent):
"""visit a BoolOp node by returning a fresh instance of it"""
- newnode = nodes.BoolOp(self._bool_op_classes[type(node.op)],
- node.lineno, node.col_offset, parent)
- newnode.postinit([self.visit(child, newnode)
- for child in node.values])
+ newnode = nodes.BoolOp(
+ self._bool_op_classes[type(node.op)], node.lineno, node.col_offset, parent
+ )
+ newnode.postinit([self.visit(child, newnode) for child in node.values])
return newnode
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)
+ return nodes.Break(
+ getattr(node, "lineno", None), getattr(node, "col_offset", None), parent
+ )
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)
- kwargs = _visit_or_none(node, 'kwargs', self, newnode)
- args = [self.visit(child, newnode)
- for child in node.args]
+ 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)
- for child in node.keywords]
+ keywords = [self.visit(child, newnode) for child in node.keywords]
else:
keywords = None
if starargs:
- new_starargs = nodes.Starred(col_offset=starargs.col_offset,
- lineno=starargs.lineno,
- parent=starargs.parent)
+ new_starargs = nodes.Starred(
+ col_offset=starargs.col_offset,
+ lineno=starargs.lineno,
+ parent=starargs.parent,
+ )
new_starargs.postinit(value=starargs)
args.append(new_starargs)
if kwargs:
- new_kwargs = nodes.Keyword(arg=None, col_offset=kwargs.col_offset,
- lineno=kwargs.lineno,
- parent=kwargs.parent)
+ new_kwargs = nodes.Keyword(
+ arg=None,
+ col_offset=kwargs.col_offset,
+ lineno=kwargs.lineno,
+ parent=kwargs.parent,
+ )
new_kwargs.postinit(value=kwargs)
if keywords:
keywords.append(new_kwargs)
else:
keywords = [new_kwargs]
- newnode.postinit(self.visit(node.func, newnode),
- args, keywords)
+ newnode.postinit(self.visit(node.func, newnode), args, keywords)
return newnode
def visit_classdef(self, node, parent, newstyle=None):
"""visit a ClassDef node to become astroid"""
node, doc = self._get_doc(node)
- newnode = nodes.ClassDef(node.name, doc, node.lineno,
- node.col_offset, parent)
+ newnode = nodes.ClassDef(node.name, doc, node.lineno, node.col_offset, parent)
metaclass = None
if PY3:
for keyword in node.keywords:
- if keyword.arg == 'metaclass':
+ if keyword.arg == "metaclass":
metaclass = self.visit(keyword, newnode).value
break
if node.decorator_list:
decorators = self.visit_decorators(node, newnode)
else:
decorators = None
- newnode.postinit([self.visit(child, newnode)
- for child in node.bases],
- [self.visit(child, newnode)
- for child in node.body],
- decorators, newstyle, metaclass,
- [self.visit(kwd, newnode) for kwd in node.keywords
- if kwd.arg != 'metaclass'] if PY3 else [])
+ newnode.postinit(
+ [self.visit(child, newnode) for child in node.bases],
+ [self.visit(child, newnode) for child in node.body],
+ 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):
"""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)
+ return nodes.Const(
+ node.value,
+ getattr(node, "lineno", None),
+ getattr(node, "col_offset", None),
+ parent,
+ )
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)
+ return nodes.Continue(
+ getattr(node, "lineno", None), getattr(node, "col_offset", None), 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.postinit(self.visit(node.left, newnode),
- [(self._cmp_op_classes[op.__class__],
- self.visit(expr, newnode))
- for (op, expr) in zip(node.ops, node.comparators)])
+ newnode.postinit(
+ self.visit(node.left, newnode),
+ [
+ (self._cmp_op_classes[op.__class__], self.visit(expr, newnode))
+ for (op, expr) in zip(node.ops, node.comparators)
+ ],
+ )
return newnode
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),
- self.visit(node.iter, newnode),
- [self.visit(child, newnode)
- for child in node.ifs],
- getattr(node, 'is_async', None))
+ newnode.postinit(
+ self.visit(node.target, newnode),
+ self.visit(node.iter, newnode),
+ [self.visit(child, newnode) for child in node.ifs],
+ getattr(node, "is_async", None),
+ )
return newnode
def visit_decorators(self, node, parent):
@@ -434,15 +458,13 @@ class TreeRebuilder:
# /!\ node is actually a _ast.FunctionDef node while
# parent is an astroid.nodes.FunctionDef node
newnode = nodes.Decorators(node.lineno, node.col_offset, parent)
- newnode.postinit([self.visit(child, newnode)
- for child in node.decorator_list])
+ newnode.postinit([self.visit(child, newnode) for child in node.decorator_list])
return newnode
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)
- for child in node.targets])
+ newnode.postinit([self.visit(child, newnode) for child in node.targets])
return newnode
def _visit_dict_items(self, node, parent, newnode):
@@ -450,9 +472,9 @@ class TreeRebuilder:
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)
+ rebuilt_key = nodes.DictUnpack(
+ rebuilt_value.lineno, rebuilt_value.col_offset, parent
+ )
else:
rebuilt_key = self.visit(key, newnode)
yield rebuilt_key, rebuilt_value
@@ -467,10 +489,11 @@ class TreeRebuilder:
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),
- self.visit(node.value, newnode),
- [self.visit(child, newnode)
- for child in node.generators])
+ 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):
@@ -482,40 +505,42 @@ class TreeRebuilder:
# Not used in Python 3.8+.
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)
-
+ return nodes.Ellipsis(
+ getattr(node, "lineno", None), getattr(node, "col_offset", None), parent
+ )
def visit_emptynode(self, node, parent):
"""visit an EmptyNode node by returning a fresh instance of it"""
- return nodes.EmptyNode(getattr(node, 'lineno', None),
- getattr(node, 'col_offset', None), parent)
-
+ return nodes.EmptyNode(
+ getattr(node, "lineno", None), getattr(node, "col_offset", None), parent
+ )
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),
- _visit_or_none(node, 'name', self, newnode),
- [self.visit(child, newnode)
- for child in node.body])
+ 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):
"""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),
- _visit_or_none(node, 'globals', self, newnode),
- _visit_or_none(node, 'locals', self, newnode))
+ newnode.postinit(
+ self.visit(node.body, newnode),
+ _visit_or_none(node, "globals", self, newnode),
+ _visit_or_none(node, "locals", self, newnode),
+ )
return newnode
# Not used in Python 3.8+.
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)
- for dim in node.dims])
+ newnode.postinit([self.visit(dim, newnode) for dim in node.dims])
return newnode
def _visit_for(self, cls, node, parent):
@@ -537,9 +562,14 @@ class TreeRebuilder:
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,
- getattr(node, 'lineno', None),
- getattr(node, 'col_offset', None), parent)
+ 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
@@ -548,8 +578,7 @@ class TreeRebuilder:
"""visit an FunctionDef node to become astroid"""
self._global_names.append({})
node, doc = self._get_doc(node)
- newnode = cls(node.name, doc, node.lineno,
- node.col_offset, parent)
+ newnode = cls(node.name, doc, node.lineno, node.col_offset, parent)
if node.decorator_list:
decorators = self.visit_decorators(node, newnode)
else:
@@ -580,9 +609,10 @@ class TreeRebuilder:
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),
- [self.visit(child, newnode)
- for child in node.generators])
+ newnode.postinit(
+ self.visit(node.elt, newnode),
+ [self.visit(child, newnode) for child in node.generators],
+ )
return newnode
def visit_attribute(self, node, parent):
@@ -591,25 +621,26 @@ class TreeRebuilder:
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)
+ newnode = nodes.DelAttr(node.attr, node.lineno, node.col_offset, parent)
elif context == astroid.Store:
- newnode = nodes.AssignAttr(node.attr, node.lineno, node.col_offset,
- parent)
+ newnode = nodes.AssignAttr(node.attr, node.lineno, node.col_offset, parent)
# Prohibit a local save if we are in an ExceptHandler.
if not isinstance(parent, astroid.ExceptHandler):
self._delayed_assattr.append(newnode)
else:
- newnode = nodes.Attribute(node.attr, node.lineno, node.col_offset,
- parent)
+ newnode = nodes.Attribute(node.attr, node.lineno, node.col_offset, parent)
newnode.postinit(self.visit(node.value, newnode))
return newnode
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)
- if self._global_names: # global at the module level, no effect
+ newnode = nodes.Global(
+ node.names,
+ getattr(node, "lineno", None),
+ getattr(node, "col_offset", None),
+ parent,
+ )
+ if self._global_names: # global at the module level, no effect
for name in node.names:
self._global_names[-1].setdefault(name, []).append(newnode)
return newnode
@@ -617,30 +648,36 @@ class TreeRebuilder:
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),
- [self.visit(child, newnode)
- for child in node.body],
- [self.visit(child, newnode)
- for child in node.orelse])
+ newnode.postinit(
+ self.visit(node.test, newnode),
+ [self.visit(child, newnode) for child in node.body],
+ [self.visit(child, newnode) for child in node.orelse],
+ )
return newnode
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),
- self.visit(node.body, newnode),
- self.visit(node.orelse, newnode))
+ 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):
"""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)
+ 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)
+ parent.set_local(name.split(".")[0], newnode)
return newnode
# Not used in Python 3.8+.
@@ -659,27 +696,25 @@ class TreeRebuilder:
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),
- self.visit(node.body, newnode))
+ newnode.postinit(self.visit(node.args, newnode), self.visit(node.body, newnode))
return newnode
def visit_list(self, node, parent):
"""visit a List node by returning a fresh instance of it"""
context = self._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])
+ 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):
"""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),
- [self.visit(child, newnode)
- for child in node.generators])
+ newnode.postinit(
+ self.visit(node.elt, newnode),
+ [self.visit(child, newnode) for child in node.generators],
+ )
return newnode
def visit_name(self, node, parent):
@@ -688,40 +723,54 @@ class TreeRebuilder:
# True and False can be assigned to something in py2x, so we have to
# check first the context.
if context == astroid.Del:
- newnode = nodes.DelName(node.id, node.lineno, node.col_offset,
- parent)
+ newnode = nodes.DelName(node.id, node.lineno, node.col_offset, parent)
elif context == astroid.Store:
- newnode = nodes.AssignName(node.id, node.lineno, node.col_offset,
- parent)
+ 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)
+ newnode = nodes.Const(
+ CONST_NAME_TRANSFORMS[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)
# XXX REMOVE me :
- if context in (astroid.Del, astroid.Store): # 'Aug' ??
+ if context in (astroid.Del, astroid.Store): # 'Aug' ??
self._save_assignment(newnode)
return newnode
def visit_constant(self, node, parent):
"""visit a Constant node by returning a fresh instance of Const"""
- return nodes.Const(node.value, getattr(node, 'lineno', None),
- getattr(node, 'col_offset', None), parent)
+ return nodes.Const(
+ node.value,
+ getattr(node, "lineno", None),
+ getattr(node, "col_offset", None),
+ parent,
+ )
# Not used in Python 3.8+.
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)
+ return nodes.Const(
+ node.s,
+ getattr(node, "lineno", None),
+ getattr(node, "col_offset", None),
+ parent,
+ )
+
visit_bytes = visit_str
# Not used in Python 3.8+.
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)
+ return nodes.Const(
+ node.n,
+ getattr(node, "lineno", None),
+ getattr(node, "col_offset", None),
+ parent,
+ )
def visit_pass(self, node, parent):
"""visit a Pass node by returning a fresh instance of it"""
@@ -730,18 +779,21 @@ class TreeRebuilder:
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),
- [self.visit(child, newnode)
- for child in node.values])
+ 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):
"""visit a Raise node by returning a fresh instance of it"""
newnode = nodes.Raise(node.lineno, node.col_offset, parent)
# pylint: disable=too-many-function-args
- newnode.postinit(_visit_or_none(node, 'type', self, newnode),
- _visit_or_none(node, 'inst', self, newnode),
- _visit_or_none(node, 'tback', self, newnode))
+ 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):
@@ -754,83 +806,86 @@ class TreeRebuilder:
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)
- for child in node.elts])
+ newnode.postinit([self.visit(child, newnode) for child in node.elts])
return newnode
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),
- [self.visit(child, newnode)
- for child in node.generators])
+ newnode.postinit(
+ self.visit(node.elt, newnode),
+ [self.visit(child, newnode) for child in node.generators],
+ )
return newnode
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),
- _visit_or_none(node, 'upper', self, newnode),
- _visit_or_none(node, 'step', self, newnode))
+ 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):
"""visit a Subscript node by returning a fresh instance of it"""
context = self._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))
+ 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):
"""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)
- for child in node.body],
- [self.visit(child, newnode)
- for child in node.handlers],
- [self.visit(child, newnode)
- for child in node.orelse])
+ newnode.postinit(
+ [self.visit(child, newnode) for child in node.body],
+ [self.visit(child, newnode) for child in node.handlers],
+ [self.visit(child, newnode) for child in node.orelse],
+ )
return newnode
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)
- for child in node.body],
- [self.visit(n, newnode)
- for n in node.finalbody])
+ newnode.postinit(
+ [self.visit(child, newnode) for child in node.body],
+ [self.visit(n, newnode) for n in node.finalbody],
+ )
return newnode
def visit_tuple(self, node, parent):
"""visit a Tuple node by returning a fresh instance of it"""
context = self._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])
+ 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):
"""visit a UnaryOp node by returning a fresh instance of it"""
- newnode = nodes.UnaryOp(self._unary_op_classes[node.op.__class__],
- node.lineno, node.col_offset, parent)
+ newnode = nodes.UnaryOp(
+ self._unary_op_classes[node.op.__class__],
+ node.lineno,
+ node.col_offset,
+ parent,
+ )
newnode.postinit(self.visit(node.operand, newnode))
return newnode
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),
- [self.visit(child, newnode)
- for child in node.body],
- [self.visit(child, newnode)
- for child in node.orelse])
+ newnode.postinit(
+ self.visit(node.test, newnode),
+ [self.visit(child, newnode) for child in node.body],
+ [self.visit(child, newnode) for child in node.orelse],
+ )
return newnode
def visit_with(self, node, parent):
@@ -867,8 +922,12 @@ class TreeRebuilder3(TreeRebuilder):
# Not used in Python 3.8+.
def visit_nameconstant(self, node, parent):
# 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)
+ return nodes.Const(
+ node.value,
+ getattr(node, "lineno", None),
+ getattr(node, "col_offset", None),
+ parent,
+ )
def visit_excepthandler(self, node, parent):
"""visit an ExceptHandler node by returning a fresh instance of it"""
@@ -877,31 +936,38 @@ class TreeRebuilder3(TreeRebuilder):
name = self.visit_assignname(node, newnode, node.name)
else:
name = None
- newnode.postinit(_visit_or_none(node, 'type', self, newnode),
- name,
- [self.visit(child, newnode)
- for child in node.body])
+ newnode.postinit(
+ _visit_or_none(node, "type", self, newnode),
+ name,
+ [self.visit(child, newnode) for child in node.body],
+ )
return newnode
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)
+ return nodes.Nonlocal(
+ node.names,
+ getattr(node, "lineno", None),
+ getattr(node, "col_offset", None),
+ parent,
+ )
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),
- _visit_or_none(node, 'cause', self, newnode))
+ newnode.postinit(
+ _visit_or_none(node, "exc", self, newnode),
+ _visit_or_none(node, "cause", self, newnode),
+ )
return newnode
def visit_starred(self, node, parent):
"""visit a Starred node and return a new instance of it"""
context = self._get_context(node)
- newnode = nodes.Starred(ctx=context, lineno=node.lineno,
- col_offset=node.col_offset,
- parent=parent)
+ newnode = nodes.Starred(
+ ctx=context, lineno=node.lineno, col_offset=node.col_offset, parent=parent
+ )
newnode.postinit(self.visit(node.value, newnode))
return newnode
@@ -913,11 +979,8 @@ class TreeRebuilder3(TreeRebuilder):
if node.handlers:
body = [self.visit_tryexcept(node, newnode)]
else:
- body = [self.visit(child, newnode)
- for child in node.body]
- newnode.postinit(body,
- [self.visit(n, newnode)
- for n in node.finalbody])
+ body = [self.visit(child, newnode) for child in node.body]
+ newnode.postinit(body, [self.visit(n, newnode) for n in node.finalbody])
return newnode
elif node.handlers:
return self.visit_tryexcept(node, parent)
@@ -926,22 +989,25 @@ class TreeRebuilder3(TreeRebuilder):
def visit_annassign(self, node, parent):
"""visit an AnnAssign node by returning a fresh instance of it"""
newnode = nodes.AnnAssign(node.lineno, node.col_offset, parent)
- annotation = _visit_or_none(node, 'annotation', self, newnode)
- newnode.postinit(target=self.visit(node.target, newnode),
- annotation=annotation,
- simple=node.simple,
- value=_visit_or_none(node, 'value', self, newnode))
+ annotation = _visit_or_none(node, "annotation", self, newnode)
+ newnode.postinit(
+ target=self.visit(node.target, newnode),
+ annotation=annotation,
+ simple=node.simple,
+ value=_visit_or_none(node, "value", self, newnode),
+ )
return newnode
def _visit_with(self, cls, node, parent):
- if 'items' not in node._fields:
+ 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)
+ var = _visit_or_none(child, "optional_vars", self, newnode)
return expr, var
type_annotation = self.check_type_comment(node)
@@ -962,8 +1028,9 @@ class TreeRebuilder3(TreeRebuilder):
return newnode
def visit_classdef(self, node, parent, newstyle=True):
- return super(TreeRebuilder3, self).visit_classdef(node, parent,
- newstyle=newstyle)
+ return super(TreeRebuilder3, self).visit_classdef(
+ node, parent, newstyle=newstyle
+ )
# Async structs added in Python 3.5
def visit_asyncfunctiondef(self, node, parent):
@@ -982,16 +1049,18 @@ class TreeRebuilder3(TreeRebuilder):
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])
+ 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))
+ 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