Browse Source

Fixed partials (Issue #398)

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.7.4
Erez Shinan 5 years ago
committed by Erez Sh
parent
commit
59f3a5707b
2 changed files with 18 additions and 1 deletions
  1. +1
    -1
      lark/utils.py
  2. +17
    -0
      tests/test_trees.py

+ 1
- 1
lark/utils.py View File

@@ -160,7 +160,7 @@ def smart_decorator(f, create_decorator):

elif isinstance(f, partial):
# wraps does not work for partials in 2.7: https://bugs.python.org/issue3445
return create_decorator(f.__func__, True)
return wraps(f.func)(create_decorator(f.func, True))

else:
return create_decorator(f.__func__.__call__, True)


+ 17
- 0
tests/test_trees.py View File

@@ -4,6 +4,7 @@ import unittest
from unittest import TestCase
import copy
import pickle
import functools

from lark.tree import Tree
from lark.visitors import Transformer, Interpreter, visit_children_decor, v_args, Discard
@@ -146,6 +147,22 @@ class TestTrees(TestCase):
res = T().transform(t)
self.assertEqual(res, 2.9)

def test_partial(self):

tree = Tree("start", [Tree("a", ["test1"]), Tree("b", ["test2"])])

def test(t, s):
return s.upper()

@v_args(inline=True)
class T(Transformer):
a = functools.partial(test)
b = functools.partial(lambda t, s: s + "!")

res = T().transform(tree)
assert res.children == ["TEST1", "test2!"]


def test_discard(self):
class MyTransformer(Transformer):
def a(self, args):


Loading…
Cancel
Save