diff options
author | Ceridwen <ceridwenv@gmail.com> | 2015-07-10 11:48:53 -0400 |
---|---|---|
committer | Ceridwen <ceridwenv@gmail.com> | 2015-07-10 11:48:53 -0400 |
commit | f75eecb94101ee67995b4066971a3d8b8363fe65 (patch) | |
tree | 6114adf807788325dc44667446e5d47b7a9b37f4 | |
parent | c301d737567bed9c54ea95f73f8f681c99d102cf (diff) | |
download | astroid-f75eecb94101ee67995b4066971a3d8b8363fe65.tar.gz |
Refactor node classes to have constructors and change some names.
I corrected the spelling of 'infered' to 'infered' and changed some to
make them more consistent with the built-in ast module and to read
better in English. I created aliases with proxy objects to maintain
backwards compatibility.
I also refactored the node classes so that they have __init__ and
postinit functions. The postinit function has to be called after the
creation of a parent with the instances of its children because the
node __init__ functions have to take a parent instance to maintain the
doubly-linked structure of the AST. This involved moving considerable
amounts of node-construction logic from rebuilder.py to
node_classes.py and scoped_nodes.py.
-rw-r--r-- | astroid/bases.py | 5 | ||||
-rw-r--r-- | astroid/brain/builtin_inference.py | 5 | ||||
-rw-r--r-- | astroid/manager.py | 2 | ||||
-rw-r--r-- | astroid/node_classes.py | 10 | ||||
-rw-r--r-- | astroid/nodes.py | 20 | ||||
-rw-r--r-- | astroid/rebuilder.py | 40 | ||||
-rw-r--r-- | astroid/scoped_nodes.py | 10 | ||||
-rw-r--r-- | astroid/tests/unittest_inference.py | 5 | ||||
-rw-r--r-- | astroid/tests/unittest_nodes.py | 3 | ||||
-rw-r--r-- | tox.ini | 2 |
10 files changed, 37 insertions, 65 deletions
diff --git a/astroid/bases.py b/astroid/bases.py index d84cdf1..363b25f 100644 --- a/astroid/bases.py +++ b/astroid/bases.py @@ -18,10 +18,6 @@ """This module contains base classes and functions for the nodes and some inference utils. """ -from __future__ import print_function -from astroid.as_string import dump -import pprint - __docformat__ = "restructuredtext en" import sys @@ -476,7 +472,6 @@ class NodeNG(object): If the instance has some explicit inference function set, it will be called instead of the default interface. """ - # pprint.pprint(vars(context), stream=sys.stderr) if self._explicit_inference is not None: # explicit_inference is not bound, give it self explicitly try: diff --git a/astroid/brain/builtin_inference.py b/astroid/brain/builtin_inference.py index 24f4aa7..0d71384 100644 --- a/astroid/brain/builtin_inference.py +++ b/astroid/brain/builtin_inference.py @@ -1,9 +1,4 @@ """Astroid hooks for various builtins.""" -from __future__ import print_function -from astroid import as_string -import pprint -import sys - from functools import partial import sys from textwrap import dedent diff --git a/astroid/manager.py b/astroid/manager.py index f0bde09..a1bda43 100644 --- a/astroid/manager.py +++ b/astroid/manager.py @@ -19,8 +19,6 @@ possible by providing a class responsible to get astroid representation from various source and using a cache of built modules) """ -from __future__ import print_function - __docformat__ = "restructuredtext en" import collections diff --git a/astroid/node_classes.py b/astroid/node_classes.py index 87ebc3d..e8fdffc 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -1295,16 +1295,6 @@ class YieldFrom(Yield): """ Class representing a YieldFrom node. """ -# Backward-compatibility aliases - -Backquote = Repr -Discard = Expr -AssName = AssignName -AssAttr = AssignAttr -GetAttr = Attribute -CallFunc = Call - - # constants ############################################################## CONST_CLS = { diff --git a/astroid/nodes.py b/astroid/nodes.py index 4325c7e..82b2fb7 100644 --- a/astroid/nodes.py +++ b/astroid/nodes.py @@ -38,6 +38,8 @@ on ImportFrom and Import : __docformat__ = "restructuredtext en" +import lazy_object_proxy + from astroid.node_classes import ( Arguments, AssignAttr, Assert, Assign, AssignName, AugAssign, Repr, BinOp, BoolOp, Break, Call, Compare, @@ -53,6 +55,7 @@ from astroid.scoped_nodes import ( ListComp, SetComp, FunctionDef, ClassDef, ) + ALL_NODE_CLASSES = ( Arguments, AssignAttr, Assert, Assign, AssignName, AugAssign, Repr, BinOp, BoolOp, Break, @@ -75,3 +78,20 @@ ALL_NODE_CLASSES = ( While, With, Yield, YieldFrom ) + + +# Backward-compatibility aliases +def proxy_alias(alias_name, node_type): + proxy = type(alias_name, (lazy_object_proxy.Proxy,), + {'__class__': object.__dict__['__class__']}) + return proxy(node_type) + +Backquote = proxy_alias('Backquote', Repr) +Discard = proxy_alias('Discard', Expr) +AssName = proxy_alias('AssName', AssignName) +AssAttr = proxy_alias('AssAttr', AssignAttr) +GetAttr = proxy_alias('GetAttr', Attribute) +CallFunc = proxy_alias('CallFunc', Call) +Class = proxy_alias('Class', ClassDef) +Function = proxy_alias('Function', FunctionDef) +GenExpr = proxy_alias('GenExpr', GeneratorExp) diff --git a/astroid/rebuilder.py b/astroid/rebuilder.py index c4c6293..e4855f0 100644 --- a/astroid/rebuilder.py +++ b/astroid/rebuilder.py @@ -18,14 +18,9 @@ """this module contains utilities for rebuilding a _ast tree in order to get a single Astroid representation """ - -from __future__ import print_function -# from ast import dump -# import as_string - import sys from _ast import ( - Expr, Str, # Expr as Discard + Expr, Str, # binary operators Add, BinOp, Div, FloorDiv, Mod, Mult, Pow, Sub, BitAnd, BitOr, BitXor, LShift, RShift, @@ -85,19 +80,11 @@ CONST_NAME_TRANSFORMS = {'None': None, } REDIRECT = {'arguments': 'Arguments', - # 'Attribute': 'Getattr', 'comprehension': 'Comprehension', - # 'Call': 'CallFunc', - # 'ClassDef': 'Class', "ListCompFor": 'Comprehension', "GenExprFor": 'Comprehension', 'excepthandler': 'ExceptHandler', - # 'Expr': 'Discard', - # 'FunctionDef': 'Function', - # 'GeneratorExp': 'GenExpr', - # 'ImportFrom': 'FromImport', 'keyword': 'Keyword', - # 'Repr': 'Backquote', } PY3 = sys.version_info >= (3, 0) PY34 = sys.version_info >= (3, 4) @@ -177,27 +164,32 @@ class TreeRebuilder(object): def visit_arguments(self, node, parent, assign_ctx=None, kwonlyargs=None, kw_defaults=None, annotations=None): """visit a Arguments node by returning a fresh instance of it""" + vararg, kwarg = node.vararg, node.kwarg if PY34: - newnode = new.Arguments(node.vararg.arg if node.vararg else None, - node.kwarg.arg if node.kwarg else None, + newnode = new.Arguments(vararg.arg if vararg else None, + kwarg.arg if kwarg else None, parent) else: - newnode = new.Arguments(node.vararg, node.kwarg, parent) + newnode = new.Arguments(vararg, kwarg, parent) args = [self.visit(child, newnode, "Assign") for child in node.args] defaults = [self.visit(child, newnode, assign_ctx) for child in node.defaults] varargannotation = None kwargannotation = None - if node.vararg: + # change added in 82732 (7c5c678e4164), vararg and kwarg + # are instances of `_ast.arg`, not strings + if vararg: if PY34: + vararg = vararg.arg if node.vararg.annotation: varargannotation = self.visit(node.vararg.annotation, newnode, assign_ctx) elif PY3 and node.vararg.annotation: varargannotation = self.visit(node.varargannotation, newnode, assign_ctx) - if node.kwarg: + if kwarg: if PY34: + kwarg = kwarg.arg if node.kwarg.annotation: kwargannotation = self.visit(node.kwarg.annotation, newnode, assign_ctx) @@ -214,13 +206,11 @@ class TreeRebuilder(object): arg.annotation else None for arg in node.args] if PY3 else [], varargannotation, kwargannotation) - # change added in 82732 (7c5c678e4164), vararg and kwarg - # are instances of `_ast.arg`, not strings # save argument names in locals: - if node.vararg: - newnode.parent.set_local(node.vararg, newnode) - if node.kwarg: - newnode.parent.set_local(node.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): diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index 004abfc..0061425 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -21,9 +21,6 @@ This module contains the classes for "scoped" node, i.e. which are opening a new local scope in the language definition : Module, ClassDef, FunctionDef (and Lambda, GeneratorExp, DictComp and SetComp to some extent). """ - -from __future__ import print_function - import functools import io import itertools @@ -1060,7 +1057,6 @@ class ClassDef(bases.Statement, LocalsDictNodeNG, mixins.FilterStmtsMixin): self._metaclass = metaclass def _newstyle_impl(self, context=None): - # import pdb; pdb.set_trace() if context is None: context = bases.InferenceContext() if self._newstyle is not None: @@ -1650,9 +1646,3 @@ class ClassDef(bases.Statement, LocalsDictNodeNG, mixins.FilterStmtsMixin): def bool_value(self): return True - - -# Backward-compatibility aliases -Class = ClassDef -Function = FunctionDef -GenExpr = GeneratorExp diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index 201a10e..c7e5052 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -17,9 +17,6 @@ # with astroid. If not, see <http://www.gnu.org/licenses/>. """tests for the astroid inference capabilities """ -from __future__ import print_function -from astroid.as_string import dump - import os import sys from functools import partial @@ -1373,8 +1370,6 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): kwarg = kwargs ''' ast = test_utils.build_module(code, __name__) - print(dump(ast), file=sys.stderr) - func = ast['test'] vararg = func.body[0].value kwarg = func.body[1].value diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index 5f6f827..4313d3f 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -17,9 +17,6 @@ # with astroid. If not, see <http://www.gnu.org/licenses/>. """tests for specific behaviour of astroid nodes """ -from __future__ import print_function -from astroid.as_string import dump - import os import sys import unittest @@ -5,6 +5,7 @@ envlist = py27, py34 [testenv:pylint]
deps =
+ lazy_object_proxy
logilab-common
six
hg+https://bitbucket.org/logilab/astroid
@@ -13,6 +14,7 @@ commands = pylint -rn --rcfile={toxinidir}/pylintrc {envsitepackagesdir}/astroid [testenv]
deps =
+ lazy_object_proxy
logilab-common
six
|