diff --git a/lark/visitors.py b/lark/visitors.py index 0cd5e05..5f8ae5d 100644 --- a/lark/visitors.py +++ b/lark/visitors.py @@ -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 diff --git a/tests/test_trees.py b/tests/test_trees.py index 7e1d841..72cfbf1 100644 --- a/tests/test_trees.py +++ b/tests/test_trees.py @@ -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()