From 7855fdefd25fb6cb0dc36a693ac608c0df0850b4 Mon Sep 17 00:00:00 2001 From: Cheukting Date: Fri, 13 May 2022 22:23:17 +0100 Subject: [PATCH] update changelog, warning msg and warning inherit --- changelog/7337.improvement.rst | 2 +- src/_pytest/python.py | 5 ++--- src/_pytest/warning_types.py | 14 +++++++------- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/changelog/7337.improvement.rst b/changelog/7337.improvement.rst index e2e4e5e83..1df95b612 100644 --- a/changelog/7337.improvement.rst +++ b/changelog/7337.improvement.rst @@ -1 +1 @@ -Warns for test functions that return non-None. Now in `pytest_pyfunc_call` after the `async_warn_and_skip` it will check for if the return is something other than None. +Warns for test functions that return non-None. diff --git a/src/_pytest/python.py b/src/_pytest/python.py index e793721c6..699535620 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -197,9 +197,8 @@ def pytest_pyfunc_call(pyfuncitem: "Function") -> Optional[object]: elif result is not None: warnings.warn( PytestReturnNotNoneWarning( - "Test function returning {result}, do you mean to use `assert` instead or `return`?".format( - result=result - ) + f"Expected None, but the test returned {result!r}, which will be an error in a " + "future version of Pytest. Did you mean to use `assert` instead of `return`?" ) ) return True diff --git a/src/_pytest/warning_types.py b/src/_pytest/warning_types.py index aec767cdc..ebfeb87d8 100644 --- a/src/_pytest/warning_types.py +++ b/src/_pytest/warning_types.py @@ -42,13 +42,6 @@ class PytestCollectionWarning(PytestWarning): __module__ = "pytest" -@final -class PytestReturnNotNoneWarning(PytestWarning): - """Warning emitted when a test function is returning value other than None.""" - - __module__ = "pytest" - - class PytestDeprecationWarning(PytestWarning, DeprecationWarning): """Warning class for features that will be removed in a future version.""" @@ -62,6 +55,13 @@ class PytestRemovedIn8Warning(PytestDeprecationWarning): __module__ = "pytest" +@final +class PytestReturnNotNoneWarning(PytestDeprecationWarning): + """Warning emitted when a test function is returning value other than None.""" + + __module__ = "pytest" + + @final class PytestExperimentalApiWarning(PytestWarning, FutureWarning): """Warning category used to denote experiments in pytest.