PR review suggestions

This commit is contained in:
Nuno Campos 2023-09-01 15:30:14 +01:00
parent 7966af1e9c
commit 63306899a2
2 changed files with 16 additions and 23 deletions

View File

@ -263,9 +263,9 @@ class Runnable(Generic[Input, Output], ABC):
bound=self, bound=self,
kwargs={}, kwargs={},
config={}, config={},
retry_if_exception_type=retry_if_exception_type, retry_exception_types=retry_if_exception_type,
wait_exponential_jitter=wait_exponential_jitter, wait_exponential_jitter=wait_exponential_jitter,
stop_after_attempt=stop_after_attempt, max_attempt_number=stop_after_attempt,
) )
def map(self) -> Runnable[List[Input], List[Output]]: def map(self) -> Runnable[List[Input], List[Output]]:
@ -388,6 +388,7 @@ class Runnable(Generic[Input, Output], ABC):
], ],
input: List[Input], input: List[Input],
config: Optional[Union[RunnableConfig, List[RunnableConfig]]] = None, config: Optional[Union[RunnableConfig, List[RunnableConfig]]] = None,
*,
return_exceptions: bool = False, return_exceptions: bool = False,
run_type: Optional[str] = None, run_type: Optional[str] = None,
) -> List[Output]: ) -> List[Output]:
@ -456,6 +457,7 @@ class Runnable(Generic[Input, Output], ABC):
], ],
input: List[Input], input: List[Input],
config: Optional[Union[RunnableConfig, List[RunnableConfig]]] = None, config: Optional[Union[RunnableConfig, List[RunnableConfig]]] = None,
*,
return_exceptions: bool = False, return_exceptions: bool = False,
run_type: Optional[str] = None, run_type: Optional[str] = None,
) -> List[Output]: ) -> List[Output]:
@ -1057,9 +1059,7 @@ class RunnableSequence(Serializable, Runnable[Input, Output]):
# If an input has failed it will be present in this map, # If an input has failed it will be present in this map,
# and the value will be the exception that was raised. # and the value will be the exception that was raised.
failed_inputs_map: Dict[int, Exception] = {} failed_inputs_map: Dict[int, Exception] = {}
stepidx = -1
for step in self.steps: for step in self.steps:
stepidx += 1
# Assemble the original indexes of the remaining inputs # Assemble the original indexes of the remaining inputs
# (i.e. the ones that haven't failed yet) # (i.e. the ones that haven't failed yet)
remaining_idxs = [ remaining_idxs = [
@ -1176,9 +1176,7 @@ class RunnableSequence(Serializable, Runnable[Input, Output]):
# If an input has failed it will be present in this map, # If an input has failed it will be present in this map,
# and the value will be the exception that was raised. # and the value will be the exception that was raised.
failed_inputs_map: Dict[int, Exception] = {} failed_inputs_map: Dict[int, Exception] = {}
stepidx = -1
for step in self.steps: for step in self.steps:
stepidx += 1
# Assemble the original indexes of the remaining inputs # Assemble the original indexes of the remaining inputs
# (i.e. the ones that haven't failed yet) # (i.e. the ones that haven't failed yet)
remaining_idxs = [ remaining_idxs = [

View File

@ -24,31 +24,32 @@ U = TypeVar("U")
class RunnableRetry(RunnableBinding[Input, Output]): class RunnableRetry(RunnableBinding[Input, Output]):
"""Retry a Runnable if it fails.""" """Retry a Runnable if it fails."""
retry_if_exception_type: Tuple[Type[BaseException]] = (Exception,) retry_exception_types: Tuple[Type[BaseException]] = (Exception,)
wait_exponential_jitter: bool = True wait_exponential_jitter: bool = True
stop_after_attempt: int = 3 max_attempt_number: int = 3
@property
def _kwargs_retrying(self) -> Dict[str, Any]: def _kwargs_retrying(self) -> Dict[str, Any]:
kwargs: Dict[str, Any] = dict() kwargs: Dict[str, Any] = dict()
if self.stop_after_attempt: if self.max_attempt_number:
kwargs["stop"] = stop_after_attempt(self.stop_after_attempt) kwargs["stop"] = stop_after_attempt(self.max_attempt_number)
if self.wait_exponential_jitter: if self.wait_exponential_jitter:
kwargs["wait"] = wait_exponential_jitter() kwargs["wait"] = wait_exponential_jitter()
if self.retry_if_exception_type: if self.retry_exception_types:
kwargs["retry"] = retry_if_exception_type(self.retry_if_exception_type) kwargs["retry"] = retry_if_exception_type(self.retry_exception_types)
return kwargs return kwargs
def _sync_retrying(self, **kwargs: Any) -> Retrying: def _sync_retrying(self, **kwargs: Any) -> Retrying:
return Retrying(**self._kwargs_retrying(), **kwargs) return Retrying(**self._kwargs_retrying, **kwargs)
def _async_retrying(self, **kwargs: Any) -> AsyncRetrying: def _async_retrying(self, **kwargs: Any) -> AsyncRetrying:
return AsyncRetrying(**self._kwargs_retrying(), **kwargs) return AsyncRetrying(**self._kwargs_retrying, **kwargs)
def _patch_config( def _patch_config(
self, self,
@ -56,15 +57,9 @@ class RunnableRetry(RunnableBinding[Input, Output]):
run_manager: T, run_manager: T,
retry_state: RetryCallState, retry_state: RetryCallState,
) -> RunnableConfig: ) -> RunnableConfig:
config = config or {} attempt = retry_state.attempt_number
return patch_config( tag = "retry:attempt:{}".format(attempt) if attempt > 1 else None
config, return patch_config(config, callbacks=run_manager.get_child(tag))
callbacks=run_manager.get_child(
"retry:attempt:{}".format(retry_state.attempt_number)
if retry_state.attempt_number > 1
else None
),
)
def _patch_config_list( def _patch_config_list(
self, self,