@v_args at function level now takes precedent over class level

This commit is contained in:
Erez Shinan 2018-12-19 18:01:14 +02:00
parent 222df5bab4
commit 641fec43ce
2 changed files with 21 additions and 0 deletions

View File

@ -69,6 +69,10 @@ class Transformer:
if name.startswith('_') or name in libmembers:
continue
# Skip if v_args already applied (at the function level)
if hasattr(cls.__dict__[name], 'vargs_applied'):
continue
static = isinstance(cls.__dict__[name], (staticmethod, classmethod))
setattr(cls, name, decorator(value, static=static, **kwargs))
return cls
@ -241,6 +245,7 @@ def _visitor_args_func_dec(func, inline=False, meta=False, whole_tree=False, sta
f = wraps(func)(create_decorator(func, False))
else:
f = smart_decorator(func, create_decorator)
f.vargs_applied = True
f.inline = inline
f.meta = meta
f.whole_tree = whole_tree

View File

@ -128,6 +128,22 @@ class TestTrees(TestCase):
x = MyTransformer().transform( Tree('hello', [2]))
self.assertEqual(x, 'hello')
def test_vargs_override(self):
t = Tree('add', [Tree('sub', [Tree('i', ['3']), Tree('f', ['1.1'])]), Tree('i', ['1'])])
@v_args(inline=True)
class T(Transformer):
i = int
f = float
sub = lambda self, a, b: a-b
@v_args(inline=False)
def add(self, values):
return sum(values)
res = T().transform(t)
self.assertEqual(res, 2.9)
if __name__ == '__main__':
unittest.main()