Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pip/_vendor/tenacity/stop.py: 63%
46 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:48 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:48 +0000
1# Copyright 2016–2021 Julien Danjou
2# Copyright 2016 Joshua Harlow
3# Copyright 2013-2014 Ray Holder
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16import abc
17import typing
19from pip._vendor.tenacity import _utils
21if typing.TYPE_CHECKING:
22 import threading
24 from pip._vendor.tenacity import RetryCallState
27class stop_base(abc.ABC):
28 """Abstract base class for stop strategies."""
30 @abc.abstractmethod
31 def __call__(self, retry_state: "RetryCallState") -> bool:
32 pass
34 def __and__(self, other: "stop_base") -> "stop_all":
35 return stop_all(self, other)
37 def __or__(self, other: "stop_base") -> "stop_any":
38 return stop_any(self, other)
41StopBaseT = typing.Union[stop_base, typing.Callable[["RetryCallState"], bool]]
44class stop_any(stop_base):
45 """Stop if any of the stop condition is valid."""
47 def __init__(self, *stops: stop_base) -> None:
48 self.stops = stops
50 def __call__(self, retry_state: "RetryCallState") -> bool:
51 return any(x(retry_state) for x in self.stops)
54class stop_all(stop_base):
55 """Stop if all the stop conditions are valid."""
57 def __init__(self, *stops: stop_base) -> None:
58 self.stops = stops
60 def __call__(self, retry_state: "RetryCallState") -> bool:
61 return all(x(retry_state) for x in self.stops)
64class _stop_never(stop_base):
65 """Never stop."""
67 def __call__(self, retry_state: "RetryCallState") -> bool:
68 return False
71stop_never = _stop_never()
74class stop_when_event_set(stop_base):
75 """Stop when the given event is set."""
77 def __init__(self, event: "threading.Event") -> None:
78 self.event = event
80 def __call__(self, retry_state: "RetryCallState") -> bool:
81 return self.event.is_set()
84class stop_after_attempt(stop_base):
85 """Stop when the previous attempt >= max_attempt."""
87 def __init__(self, max_attempt_number: int) -> None:
88 self.max_attempt_number = max_attempt_number
90 def __call__(self, retry_state: "RetryCallState") -> bool:
91 return retry_state.attempt_number >= self.max_attempt_number
94class stop_after_delay(stop_base):
95 """Stop when the time from the first attempt >= limit."""
97 def __init__(self, max_delay: _utils.time_unit_type) -> None:
98 self.max_delay = _utils.to_seconds(max_delay)
100 def __call__(self, retry_state: "RetryCallState") -> bool:
101 if retry_state.seconds_since_start is None:
102 raise RuntimeError("__call__() called but seconds_since_start is not set")
103 return retry_state.seconds_since_start >= self.max_delay