From 38a4c7e56c61e6442070a16bc36fa1d75ccff188 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Mon, 30 Mar 2020 21:51:06 +0200 Subject: [PATCH] Merge pull request #6986 from RonnyPfannschmidt/fix-6951-tw.writer-writable fix #6951: allow to write TerminalReporter.writer --- changelog/6951.bugfix.rst | 1 + src/_pytest/deprecated.py | 6 ++++++ src/_pytest/terminal.py | 13 +++++++------ testing/deprecated_test.py | 9 ++++++++- 4 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 changelog/6951.bugfix.rst diff --git a/changelog/6951.bugfix.rst b/changelog/6951.bugfix.rst new file mode 100644 index 0000000000..984089b3a5 --- /dev/null +++ b/changelog/6951.bugfix.rst @@ -0,0 +1 @@ +Allow users to still set the deprecated ``TerminalReporter.writer`` attribute. diff --git a/src/_pytest/deprecated.py b/src/_pytest/deprecated.py index 7e241ae1b3..b11093910a 100644 --- a/src/_pytest/deprecated.py +++ b/src/_pytest/deprecated.py @@ -54,3 +54,9 @@ "The pytest_collect_directory hook is not working.\n" "Please use collect_ignore in conftests or pytest_collection_modifyitems." ) + + +TERMINALWRITER_WRITER = PytestDeprecationWarning( + "The TerminalReporter.writer attribute is deprecated, use TerminalReporter._tw instead at your own risk.\n" + "See https://docs.pytest.org/en/latest/deprecations.html#terminalreporter-writer for more information." +) diff --git a/src/_pytest/terminal.py b/src/_pytest/terminal.py index 7127ac74bc..812afd258b 100644 --- a/src/_pytest/terminal.py +++ b/src/_pytest/terminal.py @@ -29,6 +29,7 @@ from _pytest._io import TerminalWriter from _pytest.config import Config from _pytest.config import ExitCode +from _pytest.deprecated import TERMINALWRITER_WRITER from _pytest.main import Session from _pytest.reports import CollectReport from _pytest.reports import TestReport @@ -284,14 +285,14 @@ def __init__(self, config: Config, file=None) -> None: @property def writer(self) -> TerminalWriter: - warnings.warn( - pytest.PytestDeprecationWarning( - "TerminalReporter.writer attribute is deprecated, use TerminalReporter._tw instead at your own risk.\n" - "See https://docs.pytest.org/en/latest/deprecations.html#terminalreporter-writer for more information." - ) - ) + warnings.warn(TERMINALWRITER_WRITER, stacklevel=2) return self._tw + @writer.setter + def writer(self, value: TerminalWriter): + warnings.warn(TERMINALWRITER_WRITER, stacklevel=2) + self._tw = value + def _determine_show_progress_info(self): """Return True if we should display progress information based on the current config""" # do not show progress if we are not capturing output (#3038) diff --git a/testing/deprecated_test.py b/testing/deprecated_test.py index b5c66d9f5f..ce54783f40 100644 --- a/testing/deprecated_test.py +++ b/testing/deprecated_test.py @@ -36,8 +36,15 @@ def test_terminal_reporter_writer_attr(pytestconfig): except ImportError: pass terminal_reporter = pytestconfig.pluginmanager.get_plugin("terminalreporter") + expected_tw = terminal_reporter._tw + + with pytest.warns(pytest.PytestDeprecationWarning): + assert terminal_reporter.writer is expected_tw + with pytest.warns(pytest.PytestDeprecationWarning): - assert terminal_reporter.writer is terminal_reporter._tw + terminal_reporter.writer = expected_tw + + assert terminal_reporter._tw is expected_tw @pytest.mark.parametrize("plugin", sorted(deprecated.DEPRECATED_EXTERNAL_PLUGINS))