mirror of
https://github.com/python/cpython.git
synced 2025-12-11 03:20:01 +00:00
gh-120388: Improve deprecation warning message, when test returns non-None (#120401)
Co-authored-by: Alex Waygood <alex.waygood@gmail.com> Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
92c9c6ae14
commit
fabcf6bc8f
5 changed files with 44 additions and 6 deletions
|
|
@ -312,18 +312,21 @@ class TestAsyncCase(unittest.TestCase):
|
||||||
self.assertIn('It is deprecated to return a value that is not None', str(w.warning))
|
self.assertIn('It is deprecated to return a value that is not None', str(w.warning))
|
||||||
self.assertIn('test1', str(w.warning))
|
self.assertIn('test1', str(w.warning))
|
||||||
self.assertEqual(w.filename, __file__)
|
self.assertEqual(w.filename, __file__)
|
||||||
|
self.assertIn("returned 'int'", str(w.warning))
|
||||||
|
|
||||||
with self.assertWarns(DeprecationWarning) as w:
|
with self.assertWarns(DeprecationWarning) as w:
|
||||||
Test('test2').run()
|
Test('test2').run()
|
||||||
self.assertIn('It is deprecated to return a value that is not None', str(w.warning))
|
self.assertIn('It is deprecated to return a value that is not None', str(w.warning))
|
||||||
self.assertIn('test2', str(w.warning))
|
self.assertIn('test2', str(w.warning))
|
||||||
self.assertEqual(w.filename, __file__)
|
self.assertEqual(w.filename, __file__)
|
||||||
|
self.assertIn("returned 'async_generator'", str(w.warning))
|
||||||
|
|
||||||
with self.assertWarns(DeprecationWarning) as w:
|
with self.assertWarns(DeprecationWarning) as w:
|
||||||
Test('test3').run()
|
Test('test3').run()
|
||||||
self.assertIn('It is deprecated to return a value that is not None', str(w.warning))
|
self.assertIn('It is deprecated to return a value that is not None', str(w.warning))
|
||||||
self.assertIn('test3', str(w.warning))
|
self.assertIn('test3', str(w.warning))
|
||||||
self.assertEqual(w.filename, __file__)
|
self.assertEqual(w.filename, __file__)
|
||||||
|
self.assertIn(f'returned {Nothing.__name__!r}', str(w.warning))
|
||||||
|
|
||||||
def test_cleanups_interleave_order(self):
|
def test_cleanups_interleave_order(self):
|
||||||
events = []
|
events = []
|
||||||
|
|
|
||||||
|
|
@ -325,18 +325,37 @@ class Test_TestCase(unittest.TestCase, TestEquality, TestHashing):
|
||||||
self.assertIn('It is deprecated to return a value that is not None', str(w.warning))
|
self.assertIn('It is deprecated to return a value that is not None', str(w.warning))
|
||||||
self.assertIn('test1', str(w.warning))
|
self.assertIn('test1', str(w.warning))
|
||||||
self.assertEqual(w.filename, __file__)
|
self.assertEqual(w.filename, __file__)
|
||||||
|
self.assertIn("returned 'int'", str(w.warning))
|
||||||
|
|
||||||
with self.assertWarns(DeprecationWarning) as w:
|
with self.assertWarns(DeprecationWarning) as w:
|
||||||
Foo('test2').run()
|
Foo('test2').run()
|
||||||
self.assertIn('It is deprecated to return a value that is not None', str(w.warning))
|
self.assertIn('It is deprecated to return a value that is not None', str(w.warning))
|
||||||
self.assertIn('test2', str(w.warning))
|
self.assertIn('test2', str(w.warning))
|
||||||
self.assertEqual(w.filename, __file__)
|
self.assertEqual(w.filename, __file__)
|
||||||
|
self.assertIn("returned 'generator'", str(w.warning))
|
||||||
|
|
||||||
with self.assertWarns(DeprecationWarning) as w:
|
with self.assertWarns(DeprecationWarning) as w:
|
||||||
Foo('test3').run()
|
Foo('test3').run()
|
||||||
self.assertIn('It is deprecated to return a value that is not None', str(w.warning))
|
self.assertIn('It is deprecated to return a value that is not None', str(w.warning))
|
||||||
self.assertIn('test3', str(w.warning))
|
self.assertIn('test3', str(w.warning))
|
||||||
self.assertEqual(w.filename, __file__)
|
self.assertEqual(w.filename, __file__)
|
||||||
|
self.assertIn(f'returned {Nothing.__name__!r}', str(w.warning))
|
||||||
|
|
||||||
|
def test_deprecation_of_return_val_from_test_async_method(self):
|
||||||
|
class Foo(unittest.TestCase):
|
||||||
|
async def test1(self):
|
||||||
|
return 1
|
||||||
|
|
||||||
|
with self.assertWarns(DeprecationWarning) as w:
|
||||||
|
Foo('test1').run()
|
||||||
|
self.assertIn('It is deprecated to return a value that is not None', str(w.warning))
|
||||||
|
self.assertIn('test1', str(w.warning))
|
||||||
|
self.assertEqual(w.filename, __file__)
|
||||||
|
self.assertIn("returned 'coroutine'", str(w.warning))
|
||||||
|
self.assertIn(
|
||||||
|
'Maybe you forgot to use IsolatedAsyncioTestCase as the base class?',
|
||||||
|
str(w.warning),
|
||||||
|
)
|
||||||
|
|
||||||
def _check_call_order__subtests(self, result, events, expected_events):
|
def _check_call_order__subtests(self, result, events, expected_events):
|
||||||
class Foo(Test.LoggingTestCase):
|
class Foo(Test.LoggingTestCase):
|
||||||
|
|
|
||||||
|
|
@ -90,9 +90,13 @@ class IsolatedAsyncioTestCase(TestCase):
|
||||||
self._callAsync(self.asyncSetUp)
|
self._callAsync(self.asyncSetUp)
|
||||||
|
|
||||||
def _callTestMethod(self, method):
|
def _callTestMethod(self, method):
|
||||||
if self._callMaybeAsync(method) is not None:
|
result = self._callMaybeAsync(method)
|
||||||
warnings.warn(f'It is deprecated to return a value that is not None from a '
|
if result is not None:
|
||||||
f'test case ({method})', DeprecationWarning, stacklevel=4)
|
msg = (
|
||||||
|
f'It is deprecated to return a value that is not None '
|
||||||
|
f'from a test case ({method} returned {type(result).__name__!r})',
|
||||||
|
)
|
||||||
|
warnings.warn(msg, DeprecationWarning, stacklevel=4)
|
||||||
|
|
||||||
def _callTearDown(self):
|
def _callTearDown(self):
|
||||||
self._callAsync(self.asyncTearDown)
|
self._callAsync(self.asyncTearDown)
|
||||||
|
|
|
||||||
|
|
@ -603,9 +603,18 @@ class TestCase(object):
|
||||||
self.setUp()
|
self.setUp()
|
||||||
|
|
||||||
def _callTestMethod(self, method):
|
def _callTestMethod(self, method):
|
||||||
if method() is not None:
|
result = method()
|
||||||
warnings.warn(f'It is deprecated to return a value that is not None from a '
|
if result is not None:
|
||||||
f'test case ({method})', DeprecationWarning, stacklevel=3)
|
import inspect
|
||||||
|
msg = (
|
||||||
|
f'It is deprecated to return a value that is not None '
|
||||||
|
f'from a test case ({method} returned {type(result).__name__!r})'
|
||||||
|
)
|
||||||
|
if inspect.iscoroutine(result):
|
||||||
|
msg += (
|
||||||
|
'. Maybe you forgot to use IsolatedAsyncioTestCase as the base class?'
|
||||||
|
)
|
||||||
|
warnings.warn(msg, DeprecationWarning, stacklevel=3)
|
||||||
|
|
||||||
def _callTearDown(self):
|
def _callTearDown(self):
|
||||||
self.tearDown()
|
self.tearDown()
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
Improve a warning message when a test method in :mod:`unittest` returns
|
||||||
|
something other than ``None``. Now we show the returned object type and
|
||||||
|
optional asyncio-related tip.
|
||||||
Loading…
Add table
Add a link
Reference in a new issue