dry-python / dry-python/returns

Add a method to get intermediate return values from flow

Đang mở
#868 3 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Python
Star
4.4k
Fork
155
Merge trung bình
2 giờ 27 phút
Pull request đã merge (30 ngày)
22

Mô tả

Hi!

It would be nice for testing to have a way to retrieve the intermediate result of a flow, in a way similar to how `assert_flow()` works but with the return value.

Example:

```python
def do_stuff():
return flow(0, func_1, bind(func_2))

def test_func_1():
v = get_function_value(func_1, do_stuff)
assert v == expected_value
```

In principle this can be done with `sys.setprofile` (in the trace function and for `return` events, `arg` contains the return value).

Proof of concept example:

```python
def trace_func(function_to_search, frame, event, arg):
if event == 'return' and frame.f_code.co_name == function_to_search.__name__:
# modified exception to hold the return value under "value" attr
raise DesiredValueFound(arg)

def get_function_value(function_to_search, my_flow):
old_tracer = sys.gettrace()
sys.setprofile(partial(trace_func, function_to_search))

try:
my_flow()
except DesiredValueFound as e:
return e.value
finally:
sys.settrace(old_tracer)
```

What do you think?

Thanks :)

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.