https://github.com/python/cpython/commit/c6b570e5e3b214d2038645c5fa7806e0fb3f7dcd
commit: c6b570e5e3b214d2038645c5fa7806e0fb3f7dcd
branch: main
author: Serhiy Storchaka <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2025-01-20T17:02:39+05:30
summary:

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

files:
M Lib/test/test_asyncio/test_base_events.py
M Lib/test/test_asyncio/test_events.py
M Lib/test/test_asyncio/test_futures.py
M Lib/test/test_asyncio/test_locks.py
M Lib/test/test_asyncio/test_protocols.py
M Lib/test/test_asyncio/test_queues.py
M Lib/test/test_asyncio/test_sock_lowlevel.py
M Lib/test/test_asyncio/test_streams.py
M Lib/test/test_asyncio/test_tasks.py
M Lib/test/test_asyncio/test_windows_utils.py

diff --git a/Lib/test/test_asyncio/test_base_events.py 
b/Lib/test/test_asyncio/test_base_events.py
index 1e063c1352ecb9..102c9be0ecf031 100644
--- a/Lib/test/test_asyncio/test_base_events.py
+++ b/Lib/test/test_asyncio/test_base_events.py
@@ -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(
diff --git a/Lib/test/test_asyncio/test_events.py 
b/Lib/test/test_asyncio/test_events.py
index ed75b909317357..ada049e9c7d387 100644
--- a/Lib/test/test_asyncio/test_events.py
+++ b/Lib/test/test_asyncio/test_events.py
@@ -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()
@@ -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()
diff --git a/Lib/test/test_asyncio/test_futures.py 
b/Lib/test/test_asyncio/test_futures.py
index 84b44011b9a844..01d6230e6dd9a3 100644
--- a/Lib/test/test_asyncio/test_futures.py
+++ b/Lib/test/test_asyncio/test_futures.py
@@ -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')
diff --git a/Lib/test/test_asyncio/test_locks.py 
b/Lib/test/test_asyncio/test_locks.py
index aabfcd418829b2..3bb3e5c4ca0658 100644
--- a/Lib/test/test_asyncio/test_locks.py
+++ b/Lib/test/test_asyncio/test_locks.py
@@ -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):
@@ -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())
@@ -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)))
 
diff --git a/Lib/test/test_asyncio/test_protocols.py 
b/Lib/test/test_asyncio/test_protocols.py
index a8627b5b5b87f2..4484a031988533 100644
--- a/Lib/test/test_asyncio/test_protocols.py
+++ b/Lib/test/test_asyncio/test_protocols.py
@@ -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()
@@ -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()
@@ -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()
@@ -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()
@@ -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__':
diff --git a/Lib/test/test_asyncio/test_queues.py 
b/Lib/test/test_asyncio/test_queues.py
index 1a8d604faea1fd..090b9774c2289f 100644
--- a/Lib/test/test_asyncio/test_queues.py
+++ b/Lib/test/test_asyncio/test_queues.py
@@ -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)
 
diff --git a/Lib/test/test_asyncio/test_sock_lowlevel.py 
b/Lib/test/test_asyncio/test_sock_lowlevel.py
index 5b1e5143820cad..4f7b9a1dda6b78 100644
--- a/Lib/test/test_asyncio/test_sock_lowlevel.py
+++ b/Lib/test/test_asyncio/test_sock_lowlevel.py
@@ -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
@@ -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:
@@ -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)
@@ -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
 
@@ -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.
@@ -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
@@ -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])
diff --git a/Lib/test/test_asyncio/test_streams.py 
b/Lib/test/test_asyncio/test_streams.py
index 047ada8c5d23df..673c6b46c647f3 100644
--- a/Lib/test/test_asyncio/test_streams.py
+++ b/Lib/test/test_asyncio/test_streams.py
@@ -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, [])
 
@@ -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, [])
@@ -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())
@@ -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()
 
@@ -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')
@@ -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))
@@ -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()
@@ -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)
diff --git a/Lib/test/test_asyncio/test_tasks.py 
b/Lib/test/test_asyncio/test_tasks.py
index b5363226ad79f4..7a052817766a07 100644
--- a/Lib/test/test_asyncio/test_tasks.py
+++ b/Lib/test/test_asyncio/test_tasks.py
@@ -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')
@@ -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):
diff --git a/Lib/test/test_asyncio/test_windows_utils.py 
b/Lib/test/test_asyncio/test_windows_utils.py
index be70720707cea7..a6b207567c4f00 100644
--- a/Lib/test/test_asyncio/test_windows_utils.py
+++ b/Lib/test/test_asyncio/test_windows_utils.py
@@ -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:

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to