bpo-12022: Change error type for bad objects in "with" and "async with" (GH-26809)

A TypeError is now raised instead of an AttributeError in
"with" and "async with" statements for objects which do not
support the context manager or asynchronous context manager
protocols correspondingly.
This commit is contained in:
Serhiy Storchaka 2021-06-29 11:27:04 +03:00 committed by GitHub
parent 48e3a1d95a
commit 20a88004ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 48 additions and 26 deletions

View file

@ -1212,7 +1212,7 @@ class CoroutineTest(unittest.TestCase):
async with CM():
body_executed = True
with self.assertRaisesRegex(AttributeError, '__aexit__'):
with self.assertRaisesRegex(TypeError, 'asynchronous context manager.*__aexit__'):
run_async(foo())
self.assertIs(body_executed, False)
@ -1228,7 +1228,7 @@ class CoroutineTest(unittest.TestCase):
async with CM():
body_executed = True
with self.assertRaisesRegex(AttributeError, '__aenter__'):
with self.assertRaisesRegex(TypeError, 'asynchronous context manager'):
run_async(foo())
self.assertIs(body_executed, False)
@ -1243,7 +1243,7 @@ class CoroutineTest(unittest.TestCase):
async with CM():
body_executed = True
with self.assertRaisesRegex(AttributeError, '__aenter__'):
with self.assertRaisesRegex(TypeError, 'asynchronous context manager'):
run_async(foo())
self.assertIs(body_executed, False)