[Python-checkins] bpo-41322: added deprecation warning for tests returning value!=None (GH-27748)

ambv webhook-mailer at python.org
Thu Aug 19 05:41:13 EDT 2021


https://github.com/python/cpython/commit/3db42fc5aca320b0cac1895bc3cb731235ede794
commit: 3db42fc5aca320b0cac1895bc3cb731235ede794
branch: main
author: andrei kulakov <andrei.avk at gmail.com>
committer: ambv <lukasz at langa.pl>
date: 2021-08-19T11:41:04+02:00
summary:

bpo-41322: added deprecation warning for tests returning value!=None (GH-27748)

files:
A Misc/NEWS.d/next/Library/2021-08-12-16-22-16.bpo-41322.utscTd.rst
M Lib/unittest/async_case.py
M Lib/unittest/case.py

diff --git a/Lib/unittest/async_case.py b/Lib/unittest/async_case.py
index 86ed7046c499a3..cc404cc06f8b65 100644
--- a/Lib/unittest/async_case.py
+++ b/Lib/unittest/async_case.py
@@ -1,5 +1,6 @@
 import asyncio
 import inspect
+import warnings
 
 from .case import TestCase
 
@@ -62,7 +63,9 @@ def _callSetUp(self):
         self._callAsync(self.asyncSetUp)
 
     def _callTestMethod(self, method):
-        self._callMaybeAsync(method)
+        if self._callMaybeAsync(method) is not None:
+            warnings.warn(f'It is deprecated to return a value!=None from a '
+                          f'test case ({method})', DeprecationWarning)
 
     def _callTearDown(self):
         self._callAsync(self.asyncTearDown)
diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py
index 872f12112755e9..3c771c0ca8e102 100644
--- a/Lib/unittest/case.py
+++ b/Lib/unittest/case.py
@@ -546,7 +546,9 @@ def _callSetUp(self):
         self.setUp()
 
     def _callTestMethod(self, method):
-        method()
+        if method() is not None:
+            warnings.warn(f'It is deprecated to return a value!=None from a '
+                          f'test case ({method})', DeprecationWarning)
 
     def _callTearDown(self):
         self.tearDown()
diff --git a/Misc/NEWS.d/next/Library/2021-08-12-16-22-16.bpo-41322.utscTd.rst b/Misc/NEWS.d/next/Library/2021-08-12-16-22-16.bpo-41322.utscTd.rst
new file mode 100644
index 00000000000000..e16efd2c7bd556
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-08-12-16-22-16.bpo-41322.utscTd.rst
@@ -0,0 +1,3 @@
+Added ``DeprecationWarning`` for tests and async tests that return a
+value!=None (as this may indicate an improperly written test, for example a
+test written as a generator function).



More information about the Python-checkins mailing list