Skip to content

gh-71339: Use new assertion methods in test_asyncio #129051

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -1345,7 +1345,7 @@ def getaddrinfo_task(*args, **kwds):
with self.assertRaises(OSError) as cm:
self.loop.run_until_complete(coro)

self.assertTrue(str(cm.exception).startswith('Multiple exceptions: '))
self.assertStartsWith(str(cm.exception), 'Multiple exceptions: ')
self.assertTrue(m_socket.socket.return_value.close.called)

coro = self.loop.create_connection(
Expand Down
5 changes: 2 additions & 3 deletions Lib/test/test_asyncio/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -2184,7 +2184,7 @@ def test_subprocess_stderr(self):

transp.close()
self.assertEqual(b'OUT:test', proto.data[1])
self.assertTrue(proto.data[2].startswith(b'ERR:test'), proto.data[2])
self.assertStartsWith(proto.data[2], b'ERR:test')
self.assertEqual(0, proto.returncode)

@support.requires_subprocess()
Expand All @@ -2206,8 +2206,7 @@ def test_subprocess_stderr_redirect_to_stdout(self):

stdin.write(b'test')
self.loop.run_until_complete(proto.completed)
self.assertTrue(proto.data[1].startswith(b'OUT:testERR:test'),
proto.data[1])
self.assertStartsWith(proto.data[1], b'OUT:testERR:test')
self.assertEqual(b'', proto.data[2])

transp.close()
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def test_uninitialized(self):

def test_future_cancel_message_getter(self):
f = self._new_future(loop=self.loop)
self.assertTrue(hasattr(f, '_cancel_message'))
self.assertHasAttr(f, '_cancel_message')
self.assertEqual(f._cancel_message, None)

f.cancel('my message')
Expand Down
12 changes: 6 additions & 6 deletions Lib/test/test_asyncio/test_locks.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ class LockTests(unittest.IsolatedAsyncioTestCase):

async def test_repr(self):
lock = asyncio.Lock()
self.assertTrue(repr(lock).endswith('[unlocked]>'))
self.assertEndsWith(repr(lock), '[unlocked]>')
self.assertTrue(RGX_REPR.match(repr(lock)))

await lock.acquire()
self.assertTrue(repr(lock).endswith('[locked]>'))
self.assertEndsWith(repr(lock), '[locked]>')
self.assertTrue(RGX_REPR.match(repr(lock)))

async def test_lock(self):
Expand Down Expand Up @@ -286,12 +286,12 @@ class EventTests(unittest.IsolatedAsyncioTestCase):

def test_repr(self):
ev = asyncio.Event()
self.assertTrue(repr(ev).endswith('[unset]>'))
self.assertEndsWith(repr(ev), '[unset]>')
match = RGX_REPR.match(repr(ev))
self.assertEqual(match.group('extras'), 'unset')

ev.set()
self.assertTrue(repr(ev).endswith('[set]>'))
self.assertEndsWith(repr(ev), '[set]>')
self.assertTrue(RGX_REPR.match(repr(ev)))

ev._waiters.append(mock.Mock())
Expand Down Expand Up @@ -916,11 +916,11 @@ def test_initial_value_zero(self):

async def test_repr(self):
sem = asyncio.Semaphore()
self.assertTrue(repr(sem).endswith('[unlocked, value:1]>'))
self.assertEndsWith(repr(sem), '[unlocked, value:1]>')
self.assertTrue(RGX_REPR.match(repr(sem)))

await sem.acquire()
self.assertTrue(repr(sem).endswith('[locked]>'))
self.assertEndsWith(repr(sem), '[locked]>')
self.assertTrue('waiters' not in repr(sem))
self.assertTrue(RGX_REPR.match(repr(sem)))

Expand Down
10 changes: 5 additions & 5 deletions Lib/test/test_asyncio/test_protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_base_protocol(self):
self.assertIsNone(p.connection_lost(f))
self.assertIsNone(p.pause_writing())
self.assertIsNone(p.resume_writing())
self.assertFalse(hasattr(p, '__dict__'))
self.assertNotHasAttr(p, '__dict__')

def test_protocol(self):
f = mock.Mock()
Expand All @@ -30,7 +30,7 @@ def test_protocol(self):
self.assertIsNone(p.eof_received())
self.assertIsNone(p.pause_writing())
self.assertIsNone(p.resume_writing())
self.assertFalse(hasattr(p, '__dict__'))
self.assertNotHasAttr(p, '__dict__')

def test_buffered_protocol(self):
f = mock.Mock()
Expand All @@ -41,7 +41,7 @@ def test_buffered_protocol(self):
self.assertIsNone(p.buffer_updated(150))
self.assertIsNone(p.pause_writing())
self.assertIsNone(p.resume_writing())
self.assertFalse(hasattr(p, '__dict__'))
self.assertNotHasAttr(p, '__dict__')

def test_datagram_protocol(self):
f = mock.Mock()
Expand All @@ -50,7 +50,7 @@ def test_datagram_protocol(self):
self.assertIsNone(dp.connection_lost(f))
self.assertIsNone(dp.error_received(f))
self.assertIsNone(dp.datagram_received(f, f))
self.assertFalse(hasattr(dp, '__dict__'))
self.assertNotHasAttr(dp, '__dict__')

def test_subprocess_protocol(self):
f = mock.Mock()
Expand All @@ -60,7 +60,7 @@ def test_subprocess_protocol(self):
self.assertIsNone(sp.pipe_data_received(1, f))
self.assertIsNone(sp.pipe_connection_lost(1, f))
self.assertIsNone(sp.process_exited())
self.assertFalse(hasattr(sp, '__dict__'))
self.assertNotHasAttr(sp, '__dict__')


if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async def _test_repr_or_str(self, fn, expect_id):
appear in fn(Queue()).
"""
q = asyncio.Queue()
self.assertTrue(fn(q).startswith('<Queue'), fn(q))
self.assertStartsWith(fn(q), '<Queue')
id_is_present = hex(id(q)) in fn(q)
self.assertEqual(expect_id, id_is_present)

Expand Down
14 changes: 7 additions & 7 deletions Lib/test/test_asyncio/test_sock_lowlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def _basetest_sock_client_ops(self, httpd, sock):
self.loop.run_until_complete(
self.loop.sock_recv(sock, 1024))
sock.close()
self.assertTrue(data.startswith(b'HTTP/1.0 200 OK'))
self.assertStartsWith(data, b'HTTP/1.0 200 OK')

def _basetest_sock_recv_into(self, httpd, sock):
# same as _basetest_sock_client_ops, but using sock_recv_into
Expand All @@ -127,7 +127,7 @@ def _basetest_sock_recv_into(self, httpd, sock):
self.loop.run_until_complete(
self.loop.sock_recv_into(sock, buf[nbytes:]))
sock.close()
self.assertTrue(data.startswith(b'HTTP/1.0 200 OK'))
self.assertStartsWith(data, b'HTTP/1.0 200 OK')

def test_sock_client_ops(self):
with test_utils.run_test_server() as httpd:
Expand All @@ -150,7 +150,7 @@ async def _basetest_sock_recv_racing(self, httpd, sock):
# consume data
await self.loop.sock_recv(sock, 1024)

self.assertTrue(data.startswith(b'HTTP/1.0 200 OK'))
self.assertStartsWith(data, b'HTTP/1.0 200 OK')

async def _basetest_sock_recv_into_racing(self, httpd, sock):
sock.setblocking(False)
Expand All @@ -168,7 +168,7 @@ async def _basetest_sock_recv_into_racing(self, httpd, sock):
nbytes = await self.loop.sock_recv_into(sock, buf[:1024])
# consume data
await self.loop.sock_recv_into(sock, buf[nbytes:])
self.assertTrue(data.startswith(b'HTTP/1.0 200 OK'))
self.assertStartsWith(data, b'HTTP/1.0 200 OK')

await task

Expand Down Expand Up @@ -217,7 +217,7 @@ async def recv_all():
sock.shutdown(socket.SHUT_WR)
data = await task
# ProactorEventLoop could deliver hello, so endswith is necessary
self.assertTrue(data.endswith(b'world'))
self.assertEndsWith(data, b'world')

# After the first connect attempt before the listener is ready,
# the socket needs time to "recover" to make the next connect call.
Expand Down Expand Up @@ -298,7 +298,7 @@ async def _basetest_huge_content(self, address):
data = await self.loop.sock_recv(sock, DATA_SIZE)
# HTTP headers size is less than MTU,
# they are sent by the first packet always
self.assertTrue(data.startswith(b'HTTP/1.0 200 OK'))
self.assertStartsWith(data, b'HTTP/1.0 200 OK')
while data.find(b'\r\n\r\n') == -1:
data += await self.loop.sock_recv(sock, DATA_SIZE)
# Strip headers
Expand Down Expand Up @@ -351,7 +351,7 @@ async def _basetest_huge_content_recvinto(self, address):
data = bytes(buf[:nbytes])
# HTTP headers size is less than MTU,
# they are sent by the first packet always
self.assertTrue(data.startswith(b'HTTP/1.0 200 OK'))
self.assertStartsWith(data, b'HTTP/1.0 200 OK')
while data.find(b'\r\n\r\n') == -1:
nbytes = await self.loop.sock_recv_into(sock, buf)
data = bytes(buf[:nbytes])
Expand Down
18 changes: 9 additions & 9 deletions Lib/test/test_asyncio/test_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def _basetest_open_connection(self, open_connection_fut):
self.assertEqual(data, b'HTTP/1.0 200 OK\r\n')
f = reader.read()
data = self.loop.run_until_complete(f)
self.assertTrue(data.endswith(b'\r\n\r\nTest message'))
self.assertEndsWith(data, b'\r\n\r\nTest message')
writer.close()
self.assertEqual(messages, [])

Expand All @@ -75,7 +75,7 @@ def _basetest_open_connection_no_loop_ssl(self, open_connection_fut):
writer.write(b'GET / HTTP/1.0\r\n\r\n')
f = reader.read()
data = self.loop.run_until_complete(f)
self.assertTrue(data.endswith(b'\r\n\r\nTest message'))
self.assertEndsWith(data, b'\r\n\r\nTest message')

writer.close()
self.assertEqual(messages, [])
Expand Down Expand Up @@ -1002,7 +1002,7 @@ def test_wait_closed_on_close(self):
self.assertEqual(data, b'HTTP/1.0 200 OK\r\n')
f = rd.read()
data = self.loop.run_until_complete(f)
self.assertTrue(data.endswith(b'\r\n\r\nTest message'))
self.assertEndsWith(data, b'\r\n\r\nTest message')
self.assertFalse(wr.is_closing())
wr.close()
self.assertTrue(wr.is_closing())
Expand All @@ -1028,7 +1028,7 @@ async def inner(httpd):
data = await rd.readline()
self.assertEqual(data, b'HTTP/1.0 200 OK\r\n')
data = await rd.read()
self.assertTrue(data.endswith(b'\r\n\r\nTest message'))
self.assertEndsWith(data, b'\r\n\r\nTest message')
wr.close()
await wr.wait_closed()

Expand All @@ -1048,7 +1048,7 @@ async def inner(httpd):
data = await rd.readline()
self.assertEqual(data, b'HTTP/1.0 200 OK\r\n')
data = await rd.read()
self.assertTrue(data.endswith(b'\r\n\r\nTest message'))
self.assertEndsWith(data, b'\r\n\r\nTest message')
wr.close()
with self.assertRaises(ConnectionResetError):
wr.write(b'data')
Expand Down Expand Up @@ -1089,12 +1089,12 @@ async def inner(httpd):
data = await rd.readline()
self.assertEqual(data, b'HTTP/1.0 200 OK\r\n')
data = await rd.read()
self.assertTrue(data.endswith(b'\r\n\r\nTest message'))
self.assertEndsWith(data, b'\r\n\r\nTest message')
with self.assertWarns(ResourceWarning) as cm:
del wr
gc.collect()
self.assertEqual(len(cm.warnings), 1)
self.assertTrue(str(cm.warnings[0].message).startswith("unclosed <StreamWriter"))
self.assertStartsWith(str(cm.warnings[0].message), "unclosed <StreamWriter")

messages = []
self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx))
Expand All @@ -1112,7 +1112,7 @@ async def inner(httpd):
data = await rd.readline()
self.assertEqual(data, b'HTTP/1.0 200 OK\r\n')
data = await rd.read()
self.assertTrue(data.endswith(b'\r\n\r\nTest message'))
self.assertEndsWith(data, b'\r\n\r\nTest message')

# Make "loop is closed" occur first before "del wr" for this test.
self.loop.stop()
Expand Down Expand Up @@ -1144,7 +1144,7 @@ async def inner(rd, wr):
del wr
gc.collect()
self.assertEqual(len(cm.warnings), 1)
self.assertTrue(str(cm.warnings[0].message).startswith("unclosed <StreamWriter"))
self.assertStartsWith(str(cm.warnings[0].message), "unclosed <StreamWriter")

async def outer():
srv = await asyncio.start_server(inner, socket_helper.HOSTv4, 0)
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def test_task_cancel_message_getter(self):
async def coro():
pass
t = self.new_task(self.loop, coro())
self.assertTrue(hasattr(t, '_cancel_message'))
self.assertHasAttr(t, '_cancel_message')
self.assertEqual(t._cancel_message, None)

t.cancel('my message')
Expand Down Expand Up @@ -3131,7 +3131,7 @@ def new_task(self, coro):
class GenericTaskTests(test_utils.TestCase):

def test_future_subclass(self):
self.assertTrue(issubclass(asyncio.Task, asyncio.Future))
self.assertIsSubclass(asyncio.Task, asyncio.Future)

@support.cpython_only
def test_asyncio_module_compiled(self):
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_asyncio/test_windows_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ def test_popen(self):
self.assertGreater(len(out), 0)
self.assertGreater(len(err), 0)
# allow for partial reads...
self.assertTrue(msg.upper().rstrip().startswith(out))
self.assertTrue(b"stderr".startswith(err))
self.assertStartsWith(msg.upper().rstrip(), out)
self.assertStartsWith(b"stderr", err)

# The context manager calls wait() and closes resources
with p:
Expand Down
Loading