Skip to content

Commit

Permalink
Deprecate TerminalReporter.writer
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Feb 21, 2020
1 parent d1b5052 commit 435ad22
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
3 changes: 3 additions & 0 deletions 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``.
14 changes: 13 additions & 1 deletion doc/en/deprecations.rst
Expand Up @@ -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.
Expand Down Expand Up @@ -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
----------------
Expand Down
16 changes: 14 additions & 2 deletions src/_pytest/terminal.py
Expand Up @@ -8,6 +8,7 @@
import platform
import sys
import time
import warnings
from functools import partial
from typing import Any
from typing import Callable
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions testing/deprecated_test.py
Expand Up @@ -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
Expand All @@ -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))
Expand Down

0 comments on commit 435ad22

Please sign in to comment.