Skip to content

Commit

Permalink
fix(hansbug): fix bug of #82, add more unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
HansBug committed Mar 6, 2023
1 parent 15debc5 commit 80d4e9b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
27 changes: 27 additions & 0 deletions test/tree/func/test_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pytest

from treevalue import FastTreeValue
from treevalue.tree import func_treelize, TreeValue, method_treelize, classmethod_treelize, delayed


Expand Down Expand Up @@ -401,3 +402,29 @@ def total(a, b):
'v': {'a': 12, 'b': 25, 'x': {'c': 38, 'd': 51}},
})
assert cnt_1 == 4

def test_return_treevalue(self):
def func(x):
return FastTreeValue({
'x': x, 'y': x ** 2,
})

f = FastTreeValue({
'x': func,
'y': {
'z': func,
}
})
v = FastTreeValue({'x': 2, 'y': {'z': 34}})
assert f(v) == FastTreeValue({
'x': {
'x': v.x,
'y': v.x ** 2,
},
'y': {
'z': {
'x': v.y.z,
'y': v.y.z ** 2,
}
}
})
7 changes: 6 additions & 1 deletion treevalue/tree/func/cfunc.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ cdef object _c_func_treelize_run(object func, list args, dict kwargs, _e_tree_mo

cdef list _a_args
cdef dict _a_kwargs
cdef object _a_ret
if not has_tree:
_a_args = []
for v in args:
Expand All @@ -72,7 +73,11 @@ cdef object _c_func_treelize_run(object func, list args, dict kwargs, _e_tree_mo
else:
_a_kwargs[k] = missing_func()

return func(*_a_args, **_a_kwargs)
_a_ret = func(*_a_args, **_a_kwargs)
if isinstance(_a_ret, TreeValue):
return _a_ret._detach()
else:
return _a_ret

cdef dict _d_res = {}
cdef str ak
Expand Down

0 comments on commit 80d4e9b

Please sign in to comment.