gh-114091: Reword error message for unawaitable types (#114090)

Reword error message for unawaitable types.
This commit is contained in:
Steele Farnsworth 2024-06-17 10:48:17 -04:00 committed by GitHub
parent a26d27e7ee
commit 2c7209a3bd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 10 additions and 9 deletions

View file

@ -39,7 +39,7 @@ class LockTests(unittest.IsolatedAsyncioTestCase):
with self.assertRaisesRegex( with self.assertRaisesRegex(
TypeError, TypeError,
"object Lock can't be used in 'await' expression" "'Lock' object can't be awaited"
): ):
await lock await lock
@ -77,7 +77,7 @@ class LockTests(unittest.IsolatedAsyncioTestCase):
self.assertFalse(lock.locked()) self.assertFalse(lock.locked())
with self.assertRaisesRegex( with self.assertRaisesRegex(
TypeError, TypeError,
r"object \w+ can't be used in 'await' expression" r"'\w+' object can't be awaited"
): ):
with await lock: with await lock:
pass pass
@ -941,7 +941,7 @@ class SemaphoreTests(unittest.IsolatedAsyncioTestCase):
with self.assertRaisesRegex( with self.assertRaisesRegex(
TypeError, TypeError,
"object Semaphore can't be used in 'await' expression", "'Semaphore' object can't be awaited",
): ):
await sem await sem
@ -1270,7 +1270,7 @@ class BarrierTests(unittest.IsolatedAsyncioTestCase):
self.assertIn("filling", repr(barrier)) self.assertIn("filling", repr(barrier))
with self.assertRaisesRegex( with self.assertRaisesRegex(
TypeError, TypeError,
"object Barrier can't be used in 'await' expression", "'Barrier' object can't be awaited",
): ):
await barrier await barrier

View file

@ -77,7 +77,7 @@ class LockTests(BaseTest):
self.assertFalse(lock.locked()) self.assertFalse(lock.locked())
with self.assertRaisesRegex( with self.assertRaisesRegex(
TypeError, TypeError,
"can't be used in 'await' expression" "can't be awaited"
): ):
with await lock: with await lock:
pass pass

View file

@ -974,13 +974,13 @@ class CoroutineTest(unittest.TestCase):
async def foo(): async def foo():
await 1 await 1
with self.assertRaisesRegex(TypeError, "object int can.t.*await"): with self.assertRaisesRegex(TypeError, "'int' object can.t be awaited"):
run_async(foo()) run_async(foo())
def test_await_2(self): def test_await_2(self):
async def foo(): async def foo():
await [] await []
with self.assertRaisesRegex(TypeError, "object list can.t.*await"): with self.assertRaisesRegex(TypeError, "'list' object can.t be awaited"):
run_async(foo()) run_async(foo())
def test_await_3(self): def test_await_3(self):
@ -1040,7 +1040,7 @@ class CoroutineTest(unittest.TestCase):
async def foo(): return await Awaitable() async def foo(): return await Awaitable()
with self.assertRaisesRegex( with self.assertRaisesRegex(
TypeError, "object Awaitable can't be used in 'await' expression"): TypeError, "'Awaitable' object can't be awaited"):
run_async(foo()) run_async(foo())

View file

@ -0,0 +1 @@
Changed the error message for awaiting something that can't be awaited from "object <type> can't be used in an await expression" to "'<type>' object can't be awaited".

View file

@ -1047,7 +1047,7 @@ _PyCoro_GetAwaitableIter(PyObject *o)
} }
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"object %.100s can't be used in 'await' expression", "'%.100s' object can't be awaited",
ot->tp_name); ot->tp_name);
return NULL; return NULL;
} }