Skip to content

Commit ce42938

Browse files
Merge pull request #6986 from RonnyPfannschmidt/fix-6951-tw.writer-writable
fix #6951: allow to write TerminalReporter.writer
2 parents 211adfc + eab2831 commit ce42938

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

changelog/6951.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow users to still set the deprecated ``TerminalReporter.writer`` attribute.

src/_pytest/deprecated.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,9 @@
5555
"The pytest_collect_directory hook is not working.\n"
5656
"Please use collect_ignore in conftests or pytest_collection_modifyitems."
5757
)
58+
59+
60+
TERMINALWRITER_WRITER = PytestDeprecationWarning(
61+
"The TerminalReporter.writer attribute is deprecated, use TerminalReporter._tw instead at your own risk.\n"
62+
"See https://docs.pytest.org/en/latest/deprecations.html#terminalreporter-writer for more information."
63+
)

src/_pytest/terminal.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from _pytest._io import TerminalWriter
3030
from _pytest.config import Config
3131
from _pytest.config import ExitCode
32+
from _pytest.deprecated import TERMINALWRITER_WRITER
3233
from _pytest.main import Session
3334
from _pytest.reports import CollectReport
3435
from _pytest.reports import TestReport
@@ -284,14 +285,14 @@ def __init__(self, config: Config, file=None) -> None:
284285

285286
@property
286287
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-
)
288+
warnings.warn(TERMINALWRITER_WRITER, stacklevel=2)
293289
return self._tw
294290

291+
@writer.setter
292+
def writer(self, value: TerminalWriter):
293+
warnings.warn(TERMINALWRITER_WRITER, stacklevel=2)
294+
self._tw = value
295+
295296
def _determine_show_progress_info(self):
296297
"""Return True if we should display progress information based on the current config"""
297298
# do not show progress if we are not capturing output (#3038)

testing/deprecated_test.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,15 @@ def test_terminal_reporter_writer_attr(pytestconfig):
3636
except ImportError:
3737
pass
3838
terminal_reporter = pytestconfig.pluginmanager.get_plugin("terminalreporter")
39+
expected_tw = terminal_reporter._tw
40+
41+
with pytest.warns(pytest.PytestDeprecationWarning):
42+
assert terminal_reporter.writer is expected_tw
43+
3944
with pytest.warns(pytest.PytestDeprecationWarning):
40-
assert terminal_reporter.writer is terminal_reporter._tw
45+
terminal_reporter.writer = expected_tw
46+
47+
assert terminal_reporter._tw is expected_tw
4148

4249

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

0 commit comments

Comments
 (0)