Merge pull request #6784 from nicoddemus/deprecate-terminal-writer

Deprecate TerminalReporter.writer
This commit is contained in:
Bruno Oliveira 2020-02-28 21:07:02 -03:00 committed by GitHub
commit ff7b5dbbde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 5 deletions

View File

@ -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``.

View File

@ -37,7 +37,7 @@ display captured output when tests fail: ``no``, ``stdout``, ``stderr``, ``log``
Node Construction changed to ``Node.from_parent`` 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``. 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. 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 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. 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 Removed Features
---------------- ----------------

View File

@ -8,6 +8,7 @@ import datetime
import platform import platform
import sys import sys
import time import time
import warnings
from functools import partial from functools import partial
from typing import Any from typing import Any
from typing import Callable from typing import Callable
@ -25,6 +26,7 @@ from more_itertools import collapse
import pytest import pytest
from _pytest import nodes from _pytest import nodes
from _pytest._io import TerminalWriter
from _pytest.config import Config from _pytest.config import Config
from _pytest.config import ExitCode from _pytest.config import ExitCode
from _pytest.main import Session from _pytest.main import Session
@ -270,7 +272,7 @@ class TerminalReporter:
self.startdir = config.invocation_dir self.startdir = config.invocation_dir
if file is None: if file is None:
file = sys.stdout 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._screen_width = self._tw.fullwidth
self.currentfspath = None # type: Any self.currentfspath = None # type: Any
self.reportchars = getreportopt(config) self.reportchars = getreportopt(config)
@ -280,6 +282,16 @@ class TerminalReporter:
self._show_progress_info = self._determine_show_progress_info() self._show_progress_info = self._determine_show_progress_info()
self._collect_report_last_write = None # type: Optional[float] 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): def _determine_show_progress_info(self):
"""Return True if we should display progress information based on the current config""" """Return True if we should display progress information based on the current config"""
# do not show progress if we are not capturing output (#3038) # do not show progress if we are not capturing output (#3038)
@ -972,7 +984,7 @@ class TerminalReporter:
failed = self.stats.get(stat, []) failed = self.stats.get(stat, [])
if not failed: if not failed:
return return
termwidth = self.writer.fullwidth termwidth = self._tw.fullwidth
config = self.config config = self.config
for rep in failed: for rep in failed:
line = _get_line_with_reprcrash_message(config, rep, termwidth) line = _get_line_with_reprcrash_message(config, rep, termwidth)

View File

@ -27,7 +27,7 @@ def test_resultlog_is_deprecated(testdir):
def test_terminal_reporter_writer_attr(pytestconfig): def test_terminal_reporter_writer_attr(pytestconfig):
"""Check that TerminalReporter._tw is also available as 'writer' (#2984) """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: try:
import xdist # noqa import xdist # noqa
@ -36,7 +36,8 @@ def test_terminal_reporter_writer_attr(pytestconfig):
except ImportError: except ImportError:
pass pass
terminal_reporter = pytestconfig.pluginmanager.get_plugin("terminalreporter") 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)) @pytest.mark.parametrize("plugin", sorted(deprecated.DEPRECATED_EXTERNAL_PLUGINS))