diff options
Diffstat (limited to 'Lib/unittest')
-rw-r--r-- | Lib/unittest/async_case.py | 14 | ||||
-rw-r--r-- | Lib/unittest/case.py | 13 |
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 |