File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11Remove reference to value/error when unwrapping outcome.
22Provide a ``.peek() `` method to recieve wrapped
3- value/error without invalidating the outcome.
3+ value or a copy of the wrapped error
4+ without invalidating the outcome.
Original file line number Diff line number Diff line change 11from __future__ import annotations
22
33import abc
4+ import copy
45from typing import (
56 TYPE_CHECKING ,
67 AsyncGenerator ,
@@ -125,10 +126,10 @@ class Outcome(abc.ABC, Generic[ValueT]):
125126
126127 @abc .abstractmethod
127128 def peek (self ) -> ValueT :
128- """Return or raise the contained value or exception, without
129- invalidating the outcome.
129+ """Return the contained value or raise a copy of the contained
130+ exception, without invalidating the outcome.
130131
131- These two lines of code are equivalent::
132+ These two lines of code are almost equivalent::
132133
133134 x = fn(*args)
134135 x = outcome.capture(fn, *args).peek()
@@ -245,7 +246,7 @@ def _unwrap_error(self) -> BaseException:
245246 def peek (self ) -> NoReturn :
246247 # Tracebacks show the 'raise' line below out of context, so let's give
247248 # this variable a name that makes sense out of context.
248- captured_error = self .error
249+ captured_error = copy . copy ( self .error )
249250 try :
250251 raise captured_error
251252 finally :
Original file line number Diff line number Diff line change @@ -25,11 +25,13 @@ async def raise_ValueError(x):
2525 e = await outcome .acapture (raise_ValueError , 9 )
2626 assert type (e .error ) is ValueError
2727 assert e .error .args == (9 ,)
28- with pytest .raises (ValueError ):
28+ with pytest .raises (ValueError ) as exc_info :
2929 e .peek ()
30- with pytest .raises (ValueError ):
30+ with pytest .raises (ValueError ) as exc_info2 :
3131 e .unwrap ()
3232
33+ assert exc_info .value is not exc_info2 .value
34+
3335
3436async def test_asend ():
3537 async def my_agen_func ():
Original file line number Diff line number Diff line change @@ -24,10 +24,13 @@ def test_Outcome():
2424 e = Error (exc )
2525 assert e .error is exc
2626 assert repr (e ) == f"Error({ exc !r} )"
27- with pytest .raises (RuntimeError ):
27+ with pytest .raises (RuntimeError ) as exc_info :
2828 e .peek ()
29- with pytest .raises (RuntimeError ):
29+ with pytest .raises (RuntimeError ) as exc_info2 :
3030 e .unwrap ()
31+
32+ assert exc_info .value is not exc_info2 .value
33+
3134 with pytest .raises (AlreadyUsedError ):
3235 e .unwrap ()
3336 assert repr (e ) == "Error(<AlreadyUsed>)"
You can’t perform that action at this time.
0 commit comments