Skip to content

Commit 612631b

Browse files
authored
Merge pull request #103 from sarahbx/remove_timeoutsampler_args
Remove deprecated TimeoutSampler arguments
2 parents b97f702 + e46adfe commit 612631b

File tree

2 files changed

+2
-219
lines changed

2 files changed

+2
-219
lines changed

ocp_resources/utils.py

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
import logging
22
import time
3-
from warnings import warn
43

54

65
LOGGER = logging.getLogger(__name__)
76

87

9-
class InvalidArgumentsError(Exception):
10-
pass
11-
12-
138
class TimeoutExpiredError(Exception):
149
def __init__(self, value):
1510
super().__init__()
@@ -29,8 +24,6 @@ class TimeoutSampler:
2924
3025
Yielding the output allows you to handle every value as you wish.
3126
32-
Feel free to set the instance variables.
33-
3427
exceptions_dict should be in the following format:
3528
{
3629
exception0: [exception0_msg0],
@@ -67,8 +60,6 @@ class BExampleError(AExampleError)
6760
wait_timeout (int): Time in seconds to wait for func to return a value equating to True
6861
sleep (int): Time in seconds between calls to func
6962
func (function): to be wrapped by TimeoutSampler
70-
exceptions (tuple): Deprecated: Tuple containing all retry exceptions
71-
exceptions_msg (str): Deprecated: String to match exception against
7263
exceptions_dict (dict): Exception handling definition
7364
print_log (bool): Print elapsed time to log
7465
"""
@@ -78,8 +69,6 @@ def __init__(
7869
wait_timeout,
7970
sleep,
8071
func,
81-
exceptions=None, # TODO: Deprecated and will be removed, use exceptions_dict
82-
exceptions_msg=None, # TODO: Deprecated and will be removed, use exceptions_dict
8372
exceptions_dict=None,
8473
print_log=True,
8574
**func_kwargs,
@@ -91,59 +80,9 @@ def __init__(
9180
self.elapsed_time = None
9281
self.print_log = print_log
9382

94-
# TODO: when exceptions arg removed replace with: self.exceptions_dict = exceptions_dict or {Exception: []}
95-
self.exceptions_dict = self._pre_process_exceptions(
96-
exceptions=exceptions,
97-
exceptions_msg=exceptions_msg,
98-
exceptions_dict=exceptions_dict,
99-
)
83+
self.exceptions_dict = exceptions_dict or {Exception: []}
10084
self._exceptions = tuple(self.exceptions_dict.keys())
10185

102-
def _pre_process_exceptions(self, exceptions, exceptions_msg, exceptions_dict):
103-
"""
104-
Convert any deprecated `exceptions` and `exceptions_msg` arguments to an `exceptions_dict`
105-
106-
TODO: Deprecation: This method should be removed when the 'exceptions' argument is removed from __init__
107-
108-
Args:
109-
exceptions (tuple): Deprecated: Tuple containing all retry exceptions
110-
exceptions_msg (str): Deprecated: String to match exception against
111-
exceptions_dict (dict): Exception handling definition
112-
113-
Returns:
114-
dict: exceptions_dict compatible input
115-
"""
116-
output = {}
117-
if exceptions_dict and (exceptions or exceptions_msg):
118-
raise InvalidArgumentsError(
119-
"Must specify either exceptions_dict or exceptions/exception_msg, not both"
120-
)
121-
122-
elif exceptions or exceptions_msg:
123-
warn(
124-
"TimeoutSampler() exception and exception_msg are now deprecated. "
125-
"Please update to use exceptions_dict by Oct 12, 2021",
126-
DeprecationWarning,
127-
)
128-
129-
if exceptions_dict:
130-
output.update(exceptions_dict)
131-
132-
elif exceptions is None:
133-
output[Exception] = []
134-
135-
else:
136-
if not isinstance(exceptions, tuple):
137-
exceptions = (exceptions,)
138-
139-
for exp in exceptions:
140-
if exceptions_msg:
141-
output[exp] = [exceptions_msg]
142-
else:
143-
output[exp] = []
144-
145-
return output
146-
14786
def _get_func_info(self, _func, type_):
14887
res = getattr(_func, type_, None)
14988
if res:

tests/unittests/test_utils.py

Lines changed: 1 addition & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
import re
2-
import warnings
32

43
import pytest
54

6-
from ocp_resources.utils import (
7-
InvalidArgumentsError,
8-
TimeoutExpiredError,
9-
TimeoutSampler,
10-
)
5+
from ocp_resources.utils import TimeoutExpiredError, TimeoutSampler
116

127

138
class TestTimeoutSampler:
@@ -150,154 +145,3 @@ def test_timeout_sampler_raises_timeout(self, test_params, expected):
150145
assert (
151146
exception_match
152147
), f"Expected Regex: {expected['exception_log_regex']!r} Exception Log: {exception_log!r}"
153-
154-
@pytest.mark.parametrize(
155-
"test_params, expected",
156-
[
157-
pytest.param(
158-
{},
159-
{
160-
"exceptions": (Exception,),
161-
"exceptions_dict": {Exception: []},
162-
},
163-
id="noargs",
164-
),
165-
pytest.param(
166-
{
167-
"exceptions": ValueError,
168-
},
169-
{
170-
"exceptions": (ValueError,),
171-
"exceptions_dict": {ValueError: []},
172-
"warning": {
173-
"class": DeprecationWarning,
174-
"text": "TimeoutSampler() exception and exception_msg are now deprecated.",
175-
},
176-
},
177-
id="init_valueerror_expect_deprecation_warning",
178-
),
179-
pytest.param(
180-
{
181-
"exceptions": ValueError,
182-
"exceptions_msg": "test message",
183-
},
184-
{
185-
"exceptions": (ValueError,),
186-
"exceptions_dict": {ValueError: ["test message"]},
187-
"warning": {
188-
"class": DeprecationWarning,
189-
"text": "TimeoutSampler() exception and exception_msg are now deprecated.",
190-
},
191-
},
192-
id="init_valueerror_with_msg_expect_deprecation_warning",
193-
),
194-
pytest.param(
195-
{
196-
"exceptions": (Exception, ValueError),
197-
},
198-
{
199-
"exceptions": (Exception, ValueError),
200-
"exceptions_dict": {
201-
Exception: [],
202-
ValueError: [],
203-
},
204-
"warning": {
205-
"class": DeprecationWarning,
206-
"text": "TimeoutSampler() exception and exception_msg are now deprecated.",
207-
},
208-
},
209-
id="init_multi_exception_with_no_msg_expect_deprecation_warning",
210-
),
211-
pytest.param(
212-
{
213-
"exceptions_dict": {
214-
Exception: ["exception msg"],
215-
ValueError: ["another exception msg"],
216-
},
217-
},
218-
{
219-
"exceptions": (Exception, ValueError),
220-
"exceptions_dict": {
221-
Exception: ["exception msg"],
222-
ValueError: ["another exception msg"],
223-
},
224-
},
225-
id="init_dict",
226-
),
227-
pytest.param(
228-
{
229-
"exceptions_dict": {
230-
Exception: [],
231-
ValueError: [],
232-
},
233-
},
234-
{
235-
"exceptions": (Exception, ValueError),
236-
"exceptions_dict": {
237-
Exception: [],
238-
ValueError: [],
239-
},
240-
},
241-
id="init_dict_no_msgs",
242-
),
243-
pytest.param(
244-
{
245-
"exceptions": TypeError,
246-
"exceptions_dict": {
247-
Exception: ["exception msg"],
248-
KeyError: ["another exception msg"],
249-
},
250-
},
251-
{
252-
"raises": InvalidArgumentsError,
253-
},
254-
id="init_deprecated_exceptions_and_new_dict_expect_raise_invalid_arguments",
255-
),
256-
pytest.param(
257-
{
258-
"exceptions_msg": "test exception message",
259-
"exceptions_dict": {
260-
Exception: ["exception msg"],
261-
KeyError: ["another exception msg"],
262-
},
263-
},
264-
{
265-
"raises": InvalidArgumentsError,
266-
},
267-
id="init_deprecated_msg_and_new_dict_expect_raise_invalid_arguments",
268-
),
269-
],
270-
)
271-
def test_timeout_sampler_pre_process_exceptions(self, test_params, expected):
272-
# TODO: Remove this test when _pre_process_exceptions() is removed from TimeoutSampler
273-
def _timeout_sampler():
274-
return TimeoutSampler(
275-
wait_timeout=1,
276-
sleep=1,
277-
func=lambda: True,
278-
exceptions=test_params.get("exceptions"),
279-
exceptions_msg=test_params.get("exceptions_msg"),
280-
exceptions_dict=test_params.get("exceptions_dict"),
281-
print_log=False,
282-
)
283-
284-
if expected.get("raises"):
285-
with pytest.raises(expected["raises"]):
286-
_timeout_sampler()
287-
else:
288-
with warnings.catch_warnings(record=True) as caught_warnings:
289-
# Note: catch_warnings is not thread safe
290-
timeout_sampler = _timeout_sampler()
291-
if "warning" in expected:
292-
assert len(caught_warnings) == 1
293-
assert issubclass(
294-
caught_warnings[-1].category, expected["warning"]["class"]
295-
)
296-
assert expected["warning"]["text"] in str(
297-
caught_warnings[-1].message
298-
)
299-
else:
300-
assert len(caught_warnings) == 0
301-
302-
assert expected["exceptions"] == timeout_sampler._exceptions
303-
assert expected["exceptions_dict"] == timeout_sampler.exceptions_dict

0 commit comments

Comments
 (0)