Skip to content

Commit

Permalink
Fixed v_args handling of staticmethod and classmethod (Issue #246, #249)
Browse files Browse the repository at this point in the history
  • Loading branch information
erezsh committed Oct 16, 2018
1 parent 0881fe1 commit 216c341
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lark/visitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ def _apply_decorator(cls, decorator, **kwargs):
if name.startswith('_') or name in libmembers:
continue

if isinstance(cls.__dict__[name], (staticmethod, classmethod)):
kwargs['static'] = True
setattr(cls, name, decorator(value, **kwargs))
return cls

Expand Down Expand Up @@ -225,7 +227,7 @@ def inline_args(obj): # XXX Deprecated



def _visitor_args_func_dec(func, inline=False, meta=False, whole_tree=False):
def _visitor_args_func_dec(func, inline=False, meta=False, whole_tree=False, static=False):
assert [whole_tree, meta, inline].count(True) <= 1
def create_decorator(_f, with_self):
if with_self:
Expand All @@ -236,7 +238,10 @@ def f(self, *args, **kwargs):
return _f(*args, **kwargs)
return f

f = smart_decorator(func, create_decorator)
if static:
f = wraps(func)(create_decorator(func, False))
else:
f = smart_decorator(func, create_decorator)
f.inline = inline
f.meta = meta
f.whole_tree = whole_tree
Expand Down
19 changes: 19 additions & 0 deletions tests/test_trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,25 @@ class T(Transformer):
res = T().transform(t)
self.assertEqual(res, 2.9)

def test_vargs(self):
@v_args()
class MyTransformer(Transformer):
@staticmethod
def integer(args):
return 1 # some code here

@classmethod
def integer2(cls, args):
return 2 # some code here

hello = staticmethod(lambda args: 'hello')

x = MyTransformer().transform( Tree('integer', [2]))
self.assertEqual(x, 1)
x = MyTransformer().transform( Tree('integer2', [2]))
self.assertEqual(x, 2)
x = MyTransformer().transform( Tree('hello', [2]))
self.assertEqual(x, 'hello')


if __name__ == '__main__':
Expand Down

0 comments on commit 216c341

Please sign in to comment.