diff options
| author | Sylvain Thénault <sylvain.thenault@logilab.fr> | 2009-03-06 15:14:20 +0100 |
|---|---|---|
| committer | Sylvain Thénault <sylvain.thenault@logilab.fr> | 2009-03-06 15:14:20 +0100 |
| commit | dba9f1c2ea725079905941d0ac92f0ba9af9f978 (patch) | |
| tree | 3854d96cdc5afc024add46aee404a4311ca664a9 | |
| parent | ea1f3c714637ee810c169345c678520caa4b0456 (diff) | |
| download | astroid-git-dba9f1c2ea725079905941d0ac92f0ba9af9f978.tar.gz | |
remove NoneType and Bool special classes, use Const instead
--HG--
branch : _ast_compat
| -rw-r--r-- | inference.py | 23 | ||||
| -rw-r--r-- | nodes.py | 65 | ||||
| -rw-r--r-- | rebuilder.py | 8 | ||||
| -rw-r--r-- | test/unittest_builder.py | 8 | ||||
| -rw-r--r-- | test/unittest_inference.py | 6 |
5 files changed, 54 insertions, 56 deletions
diff --git a/inference.py b/inference.py index 647c2b1f..eb09d539 100644 --- a/inference.py +++ b/inference.py @@ -50,8 +50,8 @@ nodes.Tuple.pytype = lambda x: '__builtin__.tuple' nodes.Dict.__bases__ += (Instance,) nodes.Dict._proxied = MANAGER.astng_from_class(dict) nodes.Dict.pytype = lambda x: '__builtin__.dict' -nodes.NoneType.pytype = lambda x: 'types.NoneType' -nodes.Bool._proxied = MANAGER.astng_from_class(bool) +# nodes.NoneType.pytype = lambda x: 'types.NoneType' +# nodes.Bool._proxied = MANAGER.astng_from_class(bool) builtin_astng = nodes.Dict._proxied.root() @@ -470,13 +470,22 @@ nodes.EmptyNode.infer = path_wrapper(infer_empty_node) nodes.Const.__bases__ += (Instance,) +from types import NoneType + +_CONST_PROXY = { + NoneType: MANAGER.astng_from_class(NoneType, 'types'), + bool: MANAGER.astng_from_class(bool), + int: MANAGER.astng_from_class(int), + float: MANAGER.astng_from_class(float), + complex: MANAGER.astng_from_class(complex), + str: MANAGER.astng_from_class(str), + unicode: MANAGER.astng_from_class(unicode), + } + def _set_proxied(const): - if const.value is None: - raise Exception('bad const') if not hasattr(const, '__proxied'): - const.__proxied = MANAGER.astng_from_class(const.value.__class__) + const.__proxied = _CONST_PROXY[const.value.__class__] return const.__proxied - nodes.Const._proxied = property(_set_proxied) def Const_getattr(self, name, context=None, lookupclass=None): @@ -746,5 +755,3 @@ def dict_iter_stmts(self): return self.items[::2] nodes.Dict.iter_stmts = dict_iter_stmts - -nodes.NoneType._proxied = MANAGER.astng_from_module_name('types').getattr('NoneType') @@ -340,8 +340,7 @@ def const_factory(value): except KeyError: pass try: - cls, value = CONST_VALUE_TRANSFORMS[value] - node = cls(value) + node = Const(value) except KeyError: node = _const_factory(value) return node @@ -585,38 +584,38 @@ class Generator(Proxy): # XXX why not using Const node ? Or reintroduce Num / Str -class NoneType(Instance, NodeNG): - """None value (instead of Name('None')""" - _astng_fields = () - _proxied_class = None.__class__ - _proxied = None - def __init__(self, value): - self.value = value - def __repr__(self): - return 'None' - def get_children(self): - return iter(()) - __str__ = __repr__ +# class NoneType(Instance, NodeNG): +# """None value (instead of Name('None')""" +# _astng_fields = () +# _proxied_class = None.__class__ +# _proxied = None +# def __init__(self, value): +# self.value = value +# def __repr__(self): +# return 'None' +# def get_children(self): +# return iter(()) +# __str__ = __repr__ -class Bool(Instance, NodeNG): - """None value (instead of Name('True') / Name('False')""" - _astng_fields = () - _proxied_class = bool - _proxied = None - def __init__(self, value): - self.value = value - def __repr__(self): - return str(self.value) - def get_children(self): - return iter(()) - __str__ = __repr__ - -CONST_NAME_TRANSFORMS = {'None': (NoneType, None), - 'True': (Bool, True), - 'False': (Bool, False)} -CONST_VALUE_TRANSFORMS = {None: (NoneType, None), - True: (Bool, True), - False: (Bool, False)} +# class Bool(Instance, NodeNG): +# """None value (instead of Name('True') / Name('False')""" +# _astng_fields = () +# _proxied_class = bool +# _proxied = None +# def __init__(self, value): +# self.value = value +# def __repr__(self): +# return str(self.value) +# def get_children(self): +# return iter(()) +# __str__ = __repr__ + +CONST_NAME_TRANSFORMS = {'None': (Const, None), + 'True': (Const, True), + 'False': (Const, False)} +# CONST_VALUE_TRANSFORMS = {None: (NoneType, None), +# True: (Bool, True), +# False: (Bool, False)} # inference utilities ######################################################### diff --git a/rebuilder.py b/rebuilder.py index 51077390..0fa36bea 100644 --- a/rebuilder.py +++ b/rebuilder.py @@ -242,14 +242,6 @@ class RebuildVisitor(ASTVisitor): const = nodes.const_factory(value) const.parent = node node.locals['__path__'] = [const] - - def visit_const(self, node): - """visit an Name node to become astng""" - try: - cls, value = nodes.CONST_VALUE_TRANSFORMS[node.value] - node.__class__ = cls - except KeyError: - pass def visit_name(self, node): """visit an Name node to become astng""" diff --git a/test/unittest_builder.py b/test/unittest_builder.py index d6235688..941c49cb 100644 --- a/test/unittest_builder.py +++ b/test/unittest_builder.py @@ -21,7 +21,7 @@ from unittest_inference import get_name_node from pprint import pprint from logilab.astng import builder, nodes, patchcomptransformer -from logilab.astng import Module, YES, InferenceError, NoneType, Bool +from logilab.astng import Module, YES, InferenceError from logilab.astng.nodes_as_string import as_string import data @@ -170,9 +170,9 @@ class BuilderTC(TestCase): self.assert_(time_astng) # unittest_astng = self.builder.inspect_build(unittest) - self.failUnless(isinstance(builtin_astng['None'], NoneType), builtin_astng['None']) - self.failUnless(isinstance(builtin_astng['True'], Bool), builtin_astng['True']) - self.failUnless(isinstance(builtin_astng['False'], Bool), builtin_astng['False']) + self.failUnless(isinstance(builtin_astng['None'], nodes.Const), builtin_astng['None']) + self.failUnless(isinstance(builtin_astng['True'], nodes.Const), builtin_astng['True']) + self.failUnless(isinstance(builtin_astng['False'], nodes.Const), builtin_astng['False']) self.failUnless(isinstance(builtin_astng['Exception'], nodes.From), builtin_astng['Exception']) self.failUnless(isinstance(builtin_astng['NotImplementedError'], nodes.From)) diff --git a/test/unittest_inference.py b/test/unittest_inference.py index 40c9d1d6..4bac54f2 100644 --- a/test/unittest_inference.py +++ b/test/unittest_inference.py @@ -464,7 +464,7 @@ xxx = load_module_from_name('__pkginfo__') xxx = astng['xxx'] infered = list(xxx.infer()) self.failUnlessEqual(sorted([n.__class__ for n in infered]), - sorted([nodes.NoneType, YES.__class__])) + sorted([nodes.Const, YES.__class__])) def test_method_argument(self): data = ''' @@ -553,7 +553,7 @@ if __name__ == '__main__': 3) self.failUnlessEqual([str(infered).replace('FunctionDef', 'Function') for infered in astng['fct'].infer()], - ['Function(first_word)', 'Function(last_word)', 'None']) + ['Function(first_word)', 'Function(last_word)', 'Const(None)']) def test_float_complex_ambiguity(self): data = ''' @@ -820,7 +820,7 @@ def f(g = lambda: None): callfuncnode = astng['f'].body[0].value.expr infered = list(callfuncnode.infer()) self.failUnlessEqual(len(infered), 1) - self.assertIsInstance(infered[0], nodes.NoneType) + self.assertIsInstance(infered[0], nodes.Const) self.failUnlessEqual(infered[0].value, None) def test_nonregr_getitem_empty_tuple(self): |
