Skip to content

Commit 9df5122

Browse files
authored
Merge pull request #6 from RazerM/capture-kwargs
Add **kwargs to capture and acapture
2 parents 43ef9ae + eddfff8 commit 9df5122

File tree

5 files changed

+20
-20
lines changed

5 files changed

+20
-20
lines changed

docs/source/tutorial.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@ Tutorial
77
Outcome provides a function for capturing the outcome of a Python
88
function call, so that it can be passed around. The basic rule is::
99

10-
result = outcome.capture(f, *args)
10+
result = outcome.capture(f, *args, **kwargs)
1111
x = result.unwrap()
1212

1313
is the same as::
1414

15-
x = f(*args)
15+
x = f(*args, **kwargs)
1616

1717
even if ``f`` raises an error.
1818

1919
On Python 3.5+, there's also :func:`acapture`::
2020

21-
result = await outcome.acapture(f, *args)
21+
result = await outcome.acapture(f, *args, **kwargs)
2222
x = result.unwrap()
2323

2424
which, like before, is the same as::
2525

26-
x = await f(*args)
26+
x = await f(*args, **kwargs)
2727

2828
See the :ref:`api-reference` for the types involved.

src/outcome/_async.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,29 @@
77
__all__ = ['Error', 'Outcome', 'Value', 'acapture', 'capture']
88

99

10-
def capture(sync_fn, *args):
11-
"""Run ``sync_fn(*args)`` and capture the result.
10+
def capture(sync_fn, *args, **kwargs):
11+
"""Run ``sync_fn(*args, **kwargs)`` and capture the result.
1212
1313
Returns:
1414
Either a :class:`Value` or :class:`Error` as appropriate.
1515
1616
"""
1717
# _sync.capture references ErrorBase and ValueBase
1818
try:
19-
return Value(sync_fn(*args))
19+
return Value(sync_fn(*args, **kwargs))
2020
except BaseException as exc:
2121
return Error(exc)
2222

2323

24-
async def acapture(async_fn, *args):
25-
"""Run ``await async_fn(*args)`` and capture the result.
24+
async def acapture(async_fn, *args, **kwargs):
25+
"""Run ``await async_fn(*args, **kwargs)`` and capture the result.
2626
2727
Returns:
2828
Either a :class:`Value` or :class:`Error` as appropriate.
2929
3030
"""
3131
try:
32-
return Value(await async_fn(*args))
32+
return Value(await async_fn(*args, **kwargs))
3333
except BaseException as exc:
3434
return Error(exc)
3535

src/outcome/_sync.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
__all__ = ['Error', 'Outcome', 'Value', 'capture']
1010

1111

12-
def capture(sync_fn, *args):
13-
"""Run ``sync_fn(*args)`` and capture the result.
12+
def capture(sync_fn, *args, **kwargs):
13+
"""Run ``sync_fn(*args, **kwargs)`` and capture the result.
1414
1515
Returns:
1616
Either a :class:`Value` or :class:`Error` as appropriate.
1717
1818
"""
1919
try:
20-
return Value(sync_fn(*args))
20+
return Value(sync_fn(*args, **kwargs))
2121
except BaseException as exc:
2222
return Error(exc)
2323

tests/test_async.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111

1212

1313
async def test_acapture():
14-
async def return_arg(x):
14+
async def add(x, y):
1515
await trio.hazmat.checkpoint()
16-
return x
16+
return x + y
1717

18-
v = await outcome.acapture(return_arg, 7)
18+
v = await outcome.acapture(add, 3, y=4)
1919
assert v == Value(7)
2020

2121
async def raise_ValueError(x):

tests/test_sync.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ def test_Value_compare():
7777

7878

7979
def test_capture():
80-
def return_arg(x):
81-
return x
80+
def add(x, y):
81+
return x + y
8282

83-
v = outcome.capture(return_arg, 2)
83+
v = outcome.capture(add, 2, y=3)
8484
assert type(v) == Value
85-
assert v.unwrap() == 2
85+
assert v.unwrap() == 5
8686

8787
def raise_ValueError(x):
8888
raise ValueError(x)

0 commit comments

Comments
 (0)