diff options
author | Emile Anclin <emile.anclin@logilab.fr> | 2010-12-01 16:30:19 +0100 |
---|---|---|
committer | Emile Anclin <emile.anclin@logilab.fr> | 2010-12-01 16:30:19 +0100 |
commit | 3d463da455e33e7ddc53a295b6a33db7b9e4288b (patch) | |
tree | f6fb6a5a8ea7c7e8d8231d77d7c03998c4000d33 | |
parent | 403a13eb005df0c88f1345b4de37af83c1f654b6 (diff) | |
download | astroid-git-3d463da455e33e7ddc53a295b6a33db7b9e4288b.tar.gz |
instance attribute support for functions
-rw-r--r-- | builder.py | 2 | ||||
-rw-r--r-- | scoped_nodes.py | 5 | ||||
-rw-r--r-- | test/unittest_scoped_nodes.py | 18 |
3 files changed, 23 insertions, 2 deletions
@@ -175,6 +175,8 @@ class ASTNGBuilder: # Const, Tuple, ... we may be wrong, may be not, but # anyway we don't want to pollute builtin's namespace continue + elif infered.is_function: + iattrs = infered.instance_attrs else: iattrs = infered.locals except AttributeError: diff --git a/scoped_nodes.py b/scoped_nodes.py index 527fea96..64057318 100644 --- a/scoped_nodes.py +++ b/scoped_nodes.py @@ -521,6 +521,7 @@ class Function(StmtMixIn, Lambda): self.name = name self.doc = doc self.extra_decorators = [] + self.instance_attrs = {} def set_line_info(self, lastchild): self.fromlineno = self.lineno @@ -543,6 +544,8 @@ class Function(StmtMixIn, Lambda): """ if name == '__module__': return [cf(self.root().qname())] + if name in self.instance_attrs: + return self.instance_attrs[name] return std_special_attributes(self, name, False) def is_method(self): @@ -669,10 +672,8 @@ class Class(StmtMixIn, LocalsDictNodeNG, FilterStmtsMixin): _astng_fields = ('decorators', 'bases', 'body') # name decorators = None - instance_attrs = None special_attributes = set(('__name__', '__doc__', '__dict__', '__module__', '__bases__', '__mro__', '__subclasses__')) - blockstart_tolineno = None _type = None diff --git a/test/unittest_scoped_nodes.py b/test/unittest_scoped_nodes.py index 215729e8..ae6ef5ce 100644 --- a/test/unittest_scoped_nodes.py +++ b/test/unittest_scoped_nodes.py @@ -286,6 +286,24 @@ a = func() self.assertIsInstance(func_vals[0], nodes.Const) self.assertEqual(func_vals[0].value, None) + def test_func_instance_attr(self): + """test instance attributes for functions""" + data= """ +def test(): + print test.bar + +test.bar = 1 +test() + """ + astng = abuilder.string_build(data, 'mod', __file__) + func = astng.body[2].value.func.infered()[0] + self.assertIsInstance(func, nodes.Function) + self.assertEqual(func.name, 'test') + one = func.getattr('bar')[0].infered()[0] + self.assertIsInstance(one, nodes.Const) + self.assertEqual(one.value, 1) + + class ClassNodeTC(TestCase): def test_dict_interface(self): |