Skip to content
This repository was archived by the owner on Jul 16, 2024. It is now read-only.

Commit e1cba87

Browse files
committed
test: Let test_utils type-checked and workaround python/mypy#11578
1 parent 33d656c commit e1cba87

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

tests/test_utils.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,22 @@
2626
)
2727

2828

29-
def test_odict():
29+
def test_odict() -> None:
3030
assert odict(('a', 1), ('b', 2)) == OrderedDict([('a', 1), ('b', 2)])
3131

3232

33-
def test_dict2kvlist():
33+
def test_dict2kvlist() -> None:
3434
ret = list(dict2kvlist({'a': 1, 'b': 2}))
3535
assert set(ret) == {'a', 1, 'b', 2}
3636

3737

38-
def test_generate_uuid():
38+
def test_generate_uuid() -> None:
3939
u = generate_uuid()
4040
assert len(u) == 22
4141
assert isinstance(u, str)
4242

4343

44-
def test_random_seq():
44+
def test_random_seq() -> None:
4545
assert [*get_random_seq(10, 11, 1)] == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
4646
assert [*get_random_seq(10, 6, 2)] == [0, 2, 4, 6, 8, 10]
4747
with pytest.raises(AssertionError):
@@ -57,7 +57,7 @@ def test_random_seq():
5757
assert x > last_x + 1
5858

5959

60-
def test_nmget():
60+
def test_nmget() -> None:
6161
o = {'a': {'b': 1}, 'x': None}
6262
assert nmget(o, 'a', 0) == {'b': 1}
6363
assert nmget(o, 'a.b', 0) == 1
@@ -68,7 +68,7 @@ def test_nmget():
6868
assert nmget(o, 'x', 0, null_as_default=False) is None
6969

7070

71-
def test_readable_size_to_bytes():
71+
def test_readable_size_to_bytes() -> None:
7272
assert readable_size_to_bytes(2) == 2
7373
assert readable_size_to_bytes('2') == 2
7474
assert readable_size_to_bytes('2K') == 2 * (2 ** 10)
@@ -93,7 +93,7 @@ def test_readable_size_to_bytes():
9393
readable_size_to_bytes('TT')
9494

9595

96-
def test_str_to_timedelta():
96+
def test_str_to_timedelta() -> None:
9797
assert str_to_timedelta('1d2h3m4s') == timedelta(days=1, hours=2, minutes=3, seconds=4)
9898
assert str_to_timedelta('1d2h3m') == timedelta(days=1, hours=2, minutes=3)
9999
assert str_to_timedelta('1d2h') == timedelta(days=1, hours=2)
@@ -128,7 +128,7 @@ def test_str_to_timedelta():
128128

129129

130130
@pytest.mark.asyncio
131-
async def test_curl_returns_stripped_body(mocker):
131+
async def test_curl_returns_stripped_body(mocker) -> None:
132132
mock_get = mocker.patch.object(aiohttp.ClientSession, 'get')
133133
mock_resp = {'status': 200, 'text': mock_corofunc(b'success ')}
134134
mock_get.return_value = AsyncContextManagerMock(**mock_resp)
@@ -140,7 +140,7 @@ async def test_curl_returns_stripped_body(mocker):
140140

141141

142142
@pytest.mark.asyncio
143-
async def test_curl_returns_default_value_if_not_success(mocker):
143+
async def test_curl_returns_default_value_if_not_success(mocker) -> None:
144144
mock_get = mocker.patch.object(aiohttp.ClientSession, 'get')
145145
mock_resp = {'status': 400, 'text': mock_corofunc(b'bad request')}
146146
mock_get.return_value = AsyncContextManagerMock(**mock_resp)
@@ -154,9 +154,11 @@ async def test_curl_returns_default_value_if_not_success(mocker):
154154
assert resp == 'default'
155155

156156

157-
def test_string_set_flag():
157+
def test_string_set_flag() -> None:
158158

159-
class MyFlags(StringSetFlag):
159+
# FIXME: Remove "type: ignore" when mypy gets released with
160+
# python/mypy#11579.
161+
class MyFlags(StringSetFlag): # type: ignore
160162
A = 'a'
161163
B = 'b'
162164

@@ -193,14 +195,14 @@ class MyFlags(StringSetFlag):
193195

194196

195197
class TestAsyncBarrier:
196-
def test_async_barrier_initialization(self):
198+
def test_async_barrier_initialization(self) -> None:
197199
barrier = AsyncBarrier(num_parties=5)
198200

199201
assert barrier.num_parties == 5
200202
assert barrier.cond is not None # default condition
201203

202204
@pytest.mark.asyncio
203-
async def test_wait_notify_all_if_cound_eq_num_parties(self, mocker):
205+
async def test_wait_notify_all_if_cound_eq_num_parties(self, mocker) -> None:
204206
mock_cond = mocker.patch.object(asyncio, 'Condition')
205207
mock_resp = {
206208
'notify_all': mock.Mock(),
@@ -214,8 +216,9 @@ async def test_wait_notify_all_if_cound_eq_num_parties(self, mocker):
214216
await barrier.wait()
215217

216218
assert barrier.count == 1
217-
mock_cond.return_value.notify_all.assert_called_once_with()
218-
mock_cond.return_value.wait.assert_not_called()
219+
# The methods are added at runtime.
220+
mock_cond.return_value.notify_all.assert_called_once_with() # type: ignore
221+
mock_cond.return_value.wait.assert_not_called() # type: ignore
219222

220223
def test_async_barrier_reset(self):
221224
barrier = AsyncBarrier(num_parties=5)
@@ -227,7 +230,7 @@ def test_async_barrier_reset(self):
227230

228231

229232
@pytest.mark.asyncio
230-
async def test_run_through():
233+
async def test_run_through() -> None:
231234

232235
i = 0
233236

@@ -270,7 +273,7 @@ def do_sync():
270273

271274

272275
@pytest.mark.asyncio
273-
async def test_async_file_writer_str():
276+
async def test_async_file_writer_str() -> None:
274277
# 1. Get temporary filename
275278
with NamedTemporaryFile() as temp_file:
276279
file_name = temp_file.name
@@ -298,7 +301,7 @@ async def test_async_file_writer_str():
298301

299302

300303
@pytest.mark.asyncio
301-
async def test_async_file_writer_bytes():
304+
async def test_async_file_writer_bytes() -> None:
302305
# 1. Get temporary filename
303306
with NamedTemporaryFile() as temp_file:
304307
file_name = temp_file.name

0 commit comments

Comments
 (0)