aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/unittest
diff options
context:
space:
mode:
authorBénédikt Tran <10796600+picnixz@users.noreply.github.com>2025-04-19 10:44:01 +0200
committerGitHub <noreply@github.com>2025-04-19 10:44:01 +0200
commit8a9c6c4d16a746eea1e000d6701d1c274c1f331b (patch)
treec7898c8103a3f99297f07151cc3dbf5e5572dfb2 /Lib/unittest
parent95800fe6e719c829acf52fbb00198135b78719b4 (diff)
downloadcpython-8a9c6c4d16a746eea1e000d6701d1c274c1f331b.tar.gz
cpython-8a9c6c4d16a746eea1e000d6701d1c274c1f331b.zip
gh-128398: improve error messages when incorrectly using `with` and `async with` (#132218)
Improve the error message with a suggestion when an object supporting the synchronous (resp. asynchronous) context manager protocol is entered using `async with` (resp. `with`) instead of `with` (resp. `async with`).
Diffstat (limited to 'Lib/unittest')
-rw-r--r--Lib/unittest/async_case.py14
-rw-r--r--Lib/unittest/case.py13
2 files changed, 22 insertions, 5 deletions
diff --git a/Lib/unittest/async_case.py b/Lib/unittest/async_case.py
index 6000af1cef0..a1c0d6c368c 100644
--- a/Lib/unittest/async_case.py
+++ b/Lib/unittest/async_case.py
@@ -75,9 +75,17 @@ class IsolatedAsyncioTestCase(TestCase):
enter = cls.__aenter__
exit = cls.__aexit__
except AttributeError:
- raise TypeError(f"'{cls.__module__}.{cls.__qualname__}' object does "
- f"not support the asynchronous context manager protocol"
- ) from None
+ msg = (f"'{cls.__module__}.{cls.__qualname__}' object does "
+ "not support the asynchronous context manager protocol")
+ try:
+ cls.__enter__
+ cls.__exit__
+ except AttributeError:
+ pass
+ else:
+ msg += (" but it supports the context manager protocol. "
+ "Did you mean to use enterContext()?")
+ raise TypeError(msg) from None
result = await enter(cm)
self.addAsyncCleanup(exit, cm, None, None, None)
return result
diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py
index 10c3b7e1223..884fc1b21f6 100644
--- a/Lib/unittest/case.py
+++ b/Lib/unittest/case.py
@@ -111,8 +111,17 @@ def _enter_context(cm, addcleanup):
enter = cls.__enter__
exit = cls.__exit__
except AttributeError:
- raise TypeError(f"'{cls.__module__}.{cls.__qualname__}' object does "
- f"not support the context manager protocol") from None
+ msg = (f"'{cls.__module__}.{cls.__qualname__}' object does "
+ "not support the context manager protocol")
+ try:
+ cls.__aenter__
+ cls.__aexit__
+ except AttributeError:
+ pass
+ else:
+ msg += (" but it supports the asynchronous context manager "
+ "protocol. Did you mean to use enterAsyncContext()?")
+ raise TypeError(msg) from None
result = enter(cm)
addcleanup(exit, cm, None, None, None)
return result