diff --git a/changelog/6779.deprecation.rst b/changelog/6779.deprecation.rst new file mode 100644 index 00000000000..d722695da14 --- /dev/null +++ b/changelog/6779.deprecation.rst @@ -0,0 +1,3 @@ +The ``TerminalReporter.writer`` attribute has been deprecated and should no longer be used. This +was inadvertently exposed as part of the public API of that plugin and ties it too much +with ``py.io.TerminalWriter``. diff --git a/doc/en/deprecations.rst b/doc/en/deprecations.rst index bb91b9a11ec..732f92985f1 100644 --- a/doc/en/deprecations.rst +++ b/doc/en/deprecations.rst @@ -37,7 +37,7 @@ display captured output when tests fail: ``no``, ``stdout``, ``stderr``, ``log`` Node Construction changed to ``Node.from_parent`` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -.. deprecated:: 5.3 +.. deprecated:: 5.4 The construction of nodes new should use the named constructor ``from_parent``. This limitation in api surface intends to enable better/simpler refactoring of the collection tree. @@ -95,6 +95,18 @@ The plan is remove the ``--result-log`` option in pytest 6.0 if ``pytest-reportl to all users and is deemed stable. The ``pytest-reportlog`` plugin might even be merged into the core at some point, depending on the plans for the plugins and number of users using it. +TerminalReporter.writer +~~~~~~~~~~~~~~~~~~~~~~~ + +.. deprecated:: 5.4 + +The ``TerminalReporter.writer`` attribute has been deprecated and should no longer be used. This +was inadvertently exposed as part of the public API of that plugin and ties it too much +with ``py.io.TerminalWriter``. + +Plugins that used ``TerminalReporter.writer`` directly should instead use ``TerminalReporter`` +methods that provide the same functionality. + Removed Features ---------------- diff --git a/src/_pytest/terminal.py b/src/_pytest/terminal.py index 4be5b85139a..cc9ec3b0f3f 100644 --- a/src/_pytest/terminal.py +++ b/src/_pytest/terminal.py @@ -8,6 +8,7 @@ import platform import sys import time +import warnings from functools import partial from typing import Any from typing import Callable @@ -25,6 +26,7 @@ import pytest from _pytest import nodes +from _pytest._io import TerminalWriter from _pytest.config import Config from _pytest.config import ExitCode from _pytest.main import Session @@ -270,7 +272,7 @@ def __init__(self, config: Config, file=None) -> None: self.startdir = config.invocation_dir if file is None: file = sys.stdout - self.writer = self._tw = _pytest.config.create_terminal_writer(config, file) + self._tw = _pytest.config.create_terminal_writer(config, file) self._screen_width = self._tw.fullwidth self.currentfspath = None # type: Any self.reportchars = getreportopt(config) @@ -280,6 +282,16 @@ def __init__(self, config: Config, file=None) -> None: self._show_progress_info = self._determine_show_progress_info() self._collect_report_last_write = None # type: Optional[float] + @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." + ) + ) + return self._tw + 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) @@ -972,7 +984,7 @@ def show_simple(stat, lines: List[str]) -> None: failed = self.stats.get(stat, []) if not failed: return - termwidth = self.writer.fullwidth + termwidth = self._tw.fullwidth config = self.config for rep in failed: line = _get_line_with_reprcrash_message(config, rep, termwidth) diff --git a/testing/deprecated_test.py b/testing/deprecated_test.py index c7ca80cbd22..b5c66d9f5f1 100644 --- a/testing/deprecated_test.py +++ b/testing/deprecated_test.py @@ -27,7 +27,7 @@ def test(): def test_terminal_reporter_writer_attr(pytestconfig): """Check that TerminalReporter._tw is also available as 'writer' (#2984) - This attribute is planned to be deprecated in 3.4. + This attribute has been deprecated in 5.4. """ try: import xdist # noqa @@ -36,7 +36,8 @@ def test_terminal_reporter_writer_attr(pytestconfig): except ImportError: pass terminal_reporter = pytestconfig.pluginmanager.get_plugin("terminalreporter") - assert terminal_reporter.writer is terminal_reporter._tw + with pytest.warns(pytest.PytestDeprecationWarning): + assert terminal_reporter.writer is terminal_reporter._tw @pytest.mark.parametrize("plugin", sorted(deprecated.DEPRECATED_EXTERNAL_PLUGINS))