8000 gh-128398: improve error message when incorrectly `with` and `async with` by picnixz · Pull Request #132218 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-128398: improve error message when incorrectly with and async with #132218

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 13 commits into from
Apr 19, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Merge remote-tracking branch 'upstream/main' into feat/core/async-wit…
…h-suggesetions-128398

# Conflicts:
#	Lib/test/test_compile.py
  • Loading branch information
picnixz committed Apr 13, 2025
commit db1fcfcd14b96958478ec97dd83e836a973335e2
84 changes: 25 additions & 59 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1666,67 +1666,33 @@ async def name_4():
pass
[[]]

def test_invalid_with_usages(self):
def f(obj):
with obj:
pass
def test_globals_dict_subclass(self):
# gh-132386
class WeirdDict(dict):
pass

with self.assertRaisesRegex(TypeError, re.escape((
"object does not support the context manager protocol "
"(missed __exit__ method)"
))):
f(DummyEnter())

with self.assertRaisesRegex(TypeError, re.escape((
"object does not support the context manager protocol "
"(missed __enter__ method)"
))):
f(DummyExit())

# a missing __exit__ is reported missing before a missing __enter__
with self.assertRaisesRegex(TypeError, re.escape((
"object does not support the context manager protocol "
"(missed __exit__ method)"
))):
f(object())

with self.assertRaisesRegex(TypeError, re.escape((
"object does not support the context manager protocol "
"(missed __exit__ method) but it supports the asynchronous "
"context manager protocol. Did you mean to use 'async with'?"
))):
f(AsyncDummy())

def test_invalid_async_with_usages(self):
async def f(obj):
async with obj:
pass
ns = {}
exec('def foo(): return a', WeirdDict(), ns)

self.assertRaises(NameError, ns['foo'])

def test_compile_warnings(self):
# See gh-131927
# Compile warnings originating from the same file and
# line are now only emitted once.
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("default")
compile('1 is 1', '<stdin>', 'eval')
compile('1 is 1', '<stdin>', 'eval')

self.assertEqual(len(caught), 1)

with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
compile('1 is 1', '<stdin>', 'eval')
compile('1 is 1', '<stdin>', 'eval')

with self.assertRaisesRegex(TypeError, re.escape((
"object does not support the asynchronous context manager protocol "
"(missed __aexit__ method)"
))):
f(AsyncDummyEnter()).send(None)

with self.assertRaisesRegex(TypeError, re.escape((
"object does not support the asynchronous context manager protocol "
"(missed __aenter__ method)"
))):
f(AsyncDummyExit()).send(None)

# a missing __aexit__ is reported missing before a missing __aenter__
with self.assertRaisesRegex(TypeError, re.escape((
"object does not support the asynchronous context manager protocol "
"(missed __aexit__ method)"
))):
f(object()).send(None)

with self.assertRaisesRegex(TypeError, re.escape((
"object does not support the asynchronous context manager protocol "
"(missed __aexit__ method) but it supports the context manager "
"protocol. Did you mean to use 'with'?"
))):
f(SyncDummy()).send(None)
self.assertEqual(len(caught), 2)


class TestBooleanExpression(unittest.TestCase):
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.
0