Skip to content

Commit ff7b5db

Browse files
authored
Merge pull request pytest-dev#6784 from nicoddemus/deprecate-terminal-writer
Deprecate TerminalReporter.writer
2 parents 4b53bbc + 435ad22 commit ff7b5db

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

changelog/6779.deprecation.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The ``TerminalReporter.writer`` attribute has been deprecated and should no longer be used. This
2+
was inadvertently exposed as part of the public API of that plugin and ties it too much
3+
with ``py.io.TerminalWriter``.

doc/en/deprecations.rst

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ display captured output when tests fail: ``no``, ``stdout``, ``stderr``, ``log``
3737
Node Construction changed to ``Node.from_parent``
3838
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3939

40-
.. deprecated:: 5.3
40+
.. deprecated:: 5.4
4141

4242
The construction of nodes new should use the named constructor ``from_parent``.
4343
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
9595
to all users and is deemed stable. The ``pytest-reportlog`` plugin might even be merged into the core
9696
at some point, depending on the plans for the plugins and number of users using it.
9797

98+
TerminalReporter.writer
99+
~~~~~~~~~~~~~~~~~~~~~~~
100+
101+
.. deprecated:: 5.4
102+
103+
The ``TerminalReporter.writer`` attribute has been deprecated and should no longer be used. This
104+
was inadvertently exposed as part of the public API of that plugin and ties it too much
105+
with ``py.io.TerminalWriter``.
106+
107+
Plugins that used ``TerminalReporter.writer`` directly should instead use ``TerminalReporter``
108+
methods that provide the same functionality.
109+
98110

99111
Removed Features
100112
----------------

src/_pytest/terminal.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import platform
99
import sys
1010
import time
11+
import warnings
1112
from functools import partial
1213
from typing import Any
1314
from typing import Callable
@@ -25,6 +26,7 @@
2526

2627
import pytest
2728
from _pytest import nodes
29+
from _pytest._io import TerminalWriter
2830
from _pytest.config import Config
2931
from _pytest.config import ExitCode
3032
from _pytest.main import Session
@@ -270,7 +272,7 @@ def __init__(self, config: Config, file=None) -> None:
270272
self.startdir = config.invocation_dir
271273
if file is None:
272274
file = sys.stdout
273-
self.writer = self._tw = _pytest.config.create_terminal_writer(config, file)
275+
self._tw = _pytest.config.create_terminal_writer(config, file)
274276
self._screen_width = self._tw.fullwidth
275277
self.currentfspath = None # type: Any
276278
self.reportchars = getreportopt(config)
@@ -280,6 +282,16 @@ def __init__(self, config: Config, file=None) -> None:
280282
self._show_progress_info = self._determine_show_progress_info()
281283
self._collect_report_last_write = None # type: Optional[float]
282284

285+
@property
286+
def writer(self) -> TerminalWriter:
287+
warnings.warn(
288+
pytest.PytestDeprecationWarning(
289+
"TerminalReporter.writer attribute is deprecated, use TerminalReporter._tw instead at your own risk.\n"
290+
"See https://docs.pytest.org/en/latest/deprecations.html#terminalreporter-writer for more information."
291+
)
292+
)
293+
return self._tw
294+
283295
def _determine_show_progress_info(self):
284296
"""Return True if we should display progress information based on the current config"""
285297
# do not show progress if we are not capturing output (#3038)
@@ -972,7 +984,7 @@ def show_simple(stat, lines: List[str]) -> None:
972984
failed = self.stats.get(stat, [])
973985
if not failed:
974986
return
975-
termwidth = self.writer.fullwidth
987+
termwidth = self._tw.fullwidth
976988
config = self.config
977989
for rep in failed:
978990
line = _get_line_with_reprcrash_message(config, rep, termwidth)

testing/deprecated_test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def test():
2727

2828
def test_terminal_reporter_writer_attr(pytestconfig):
2929
"""Check that TerminalReporter._tw is also available as 'writer' (#2984)
30-
This attribute is planned to be deprecated in 3.4.
30+
This attribute has been deprecated in 5.4.
3131
"""
3232
try:
3333
import xdist # noqa
@@ -36,7 +36,8 @@ def test_terminal_reporter_writer_attr(pytestconfig):
3636
except ImportError:
3737
pass
3838
terminal_reporter = pytestconfig.pluginmanager.get_plugin("terminalreporter")
39-
assert terminal_reporter.writer is terminal_reporter._tw
39+
with pytest.warns(pytest.PytestDeprecationWarning):
40+
assert terminal_reporter.writer is terminal_reporter._tw
4041

4142

4243
@pytest.mark.parametrize("plugin", sorted(deprecated.DEPRECATED_EXTERNAL_PLUGINS))

0 commit comments

Comments
 (0)