Skip to content

config: typing for create_terminal_writer, re-export TerminalWriter #6545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ repos:
_code\.|
builtin\.|
code\.|
io\.(BytesIO|saferepr)|
io\.(BytesIO|saferepr|TerminalWriter)|
path\.local\.sysfind|
process\.|
std\.
Expand Down
23 changes: 12 additions & 11 deletions src/_pytest/_code/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import py

import _pytest
from _pytest._io import TerminalWriter
from _pytest._io.saferepr import safeformat
from _pytest._io.saferepr import saferepr
from _pytest.compat import overload
Expand Down Expand Up @@ -915,14 +916,14 @@ def __str__(self) -> str:
# FYI this is called from pytest-xdist's serialization of exception
# information.
io = StringIO()
tw = py.io.TerminalWriter(file=io)
tw = TerminalWriter(file=io)
self.toterminal(tw)
return io.getvalue().strip()

def __repr__(self) -> str:
return "<{} instance at {:0x}>".format(self.__class__, id(self))

def toterminal(self, tw: py.io.TerminalWriter) -> None:
def toterminal(self, tw: TerminalWriter) -> None:
raise NotImplementedError()


Expand All @@ -933,7 +934,7 @@ def __init__(self) -> None:
def addsection(self, name: str, content: str, sep: str = "-") -> None:
self.sections.append((name, content, sep))

def toterminal(self, tw: py.io.TerminalWriter) -> None:
def toterminal(self, tw: TerminalWriter) -> None:
for name, content, sep in self.sections:
tw.sep(sep, name)
tw.line(content)
Expand All @@ -953,7 +954,7 @@ def __init__(
self.reprtraceback = chain[-1][0]
self.reprcrash = chain[-1][1]

def toterminal(self, tw: py.io.TerminalWriter) -> None:
def toterminal(self, tw: TerminalWriter) -> None:
for element in self.chain:
element[0].toterminal(tw)
if element[2] is not None:
Expand All @@ -970,7 +971,7 @@ def __init__(
self.reprtraceback = reprtraceback
self.reprcrash = reprcrash

def toterminal(self, tw: py.io.TerminalWriter) -> None:
def toterminal(self, tw: TerminalWriter) -> None:
self.reprtraceback.toterminal(tw)
super().toterminal(tw)

Expand All @@ -988,7 +989,7 @@ def __init__(
self.extraline = extraline
self.style = style

def toterminal(self, tw: py.io.TerminalWriter) -> None:
def toterminal(self, tw: TerminalWriter) -> None:
# the entries might have different styles
for i, entry in enumerate(self.reprentries):
if entry.style == "long":
Expand Down Expand Up @@ -1020,7 +1021,7 @@ class ReprEntryNative(TerminalRepr):
def __init__(self, tblines: Sequence[str]) -> None:
self.lines = tblines

def toterminal(self, tw: py.io.TerminalWriter) -> None:
def toterminal(self, tw: TerminalWriter) -> None:
tw.write("".join(self.lines))


Expand All @@ -1039,7 +1040,7 @@ def __init__(
self.reprfileloc = filelocrepr
self.style = style

def toterminal(self, tw: py.io.TerminalWriter) -> None:
def toterminal(self, tw: TerminalWriter) -> None:
if self.style == "short":
assert self.reprfileloc is not None
self.reprfileloc.toterminal(tw)
Expand Down Expand Up @@ -1072,7 +1073,7 @@ def __init__(self, path, lineno: int, message: str) -> None:
self.lineno = lineno
self.message = message

def toterminal(self, tw: py.io.TerminalWriter) -> None:
def toterminal(self, tw: TerminalWriter) -> None:
# filename and lineno output for each entry,
# using an output format that most editors understand
msg = self.message
Expand All @@ -1087,7 +1088,7 @@ class ReprLocals(TerminalRepr):
def __init__(self, lines: Sequence[str]) -> None:
self.lines = lines

def toterminal(self, tw: py.io.TerminalWriter) -> None:
def toterminal(self, tw: TerminalWriter) -> None:
for line in self.lines:
tw.line(line)

Expand All @@ -1096,7 +1097,7 @@ class ReprFuncArgs(TerminalRepr):
def __init__(self, args: Sequence[Tuple[str, object]]) -> None:
self.args = args

def toterminal(self, tw: py.io.TerminalWriter) -> None:
def toterminal(self, tw: TerminalWriter) -> None:
if self.args:
linesofar = ""
for name, value in self.args:
Expand Down
3 changes: 3 additions & 0 deletions src/_pytest/_io/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Reexport TerminalWriter from here instead of py, to make it easier to
# extend or swap our own implementation in the future.
from py.io import TerminalWriter as TerminalWriter # noqa: F401
3 changes: 2 additions & 1 deletion src/_pytest/cacheprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from .pathlib import resolve_from_str
from .pathlib import rm_rf
from _pytest import nodes
from _pytest._io import TerminalWriter
from _pytest.config import Config
from _pytest.main import Session

Expand Down Expand Up @@ -418,7 +419,7 @@ def pytest_report_header(config):
def cacheshow(config, session):
from pprint import pformat

tw = py.io.TerminalWriter()
tw = TerminalWriter()
tw.line("cachedir: " + str(config.cache._cachedir))
if not config.cache._cachedir.is_dir():
tw.line("cache is empty")
Expand Down
9 changes: 5 additions & 4 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from .findpaths import exists
from _pytest._code import ExceptionInfo
from _pytest._code import filter_traceback
from _pytest._io import TerminalWriter
from _pytest.compat import importlib_metadata
from _pytest.compat import TYPE_CHECKING
from _pytest.outcomes import fail
Expand Down Expand Up @@ -73,7 +74,7 @@ def main(args=None, plugins=None) -> "Union[int, _pytest.main.ExitCode]":
config = _prepareconfig(args, plugins)
except ConftestImportFailure as e:
exc_info = ExceptionInfo(e.excinfo)
tw = py.io.TerminalWriter(sys.stderr)
tw = TerminalWriter(sys.stderr)
tw.line(
"ImportError while loading conftest '{e.path}'.".format(e=e), red=True
)
Expand All @@ -99,7 +100,7 @@ def main(args=None, plugins=None) -> "Union[int, _pytest.main.ExitCode]":
finally:
config._ensure_unconfigure()
except UsageError as e:
tw = py.io.TerminalWriter(sys.stderr)
tw = TerminalWriter(sys.stderr)
for msg in e.args:
tw.line("ERROR: {}\n".format(msg), red=True)
return ExitCode.USAGE_ERROR
Expand Down Expand Up @@ -1175,12 +1176,12 @@ def setns(obj, dic):
setattr(pytest, name, value)


def create_terminal_writer(config, *args, **kwargs):
def create_terminal_writer(config: Config, *args, **kwargs) -> TerminalWriter:
"""Create a TerminalWriter instance configured according to the options
in the config object. Every code which requires a TerminalWriter object
and has access to a config object should use this function.
"""
tw = py.io.TerminalWriter(*args, **kwargs)
tw = TerminalWriter(*args, **kwargs)
if config.option.color == "yes":
tw.hasmarkup = True
if config.option.color == "no":
Expand Down
5 changes: 3 additions & 2 deletions src/_pytest/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
from typing import Tuple
from typing import Union

import py
import py.path

import pytest
from _pytest import outcomes
from _pytest._code.code import ExceptionInfo
from _pytest._code.code import ReprFileLocation
from _pytest._code.code import TerminalRepr
from _pytest._io import TerminalWriter
from _pytest.compat import safe_getattr
from _pytest.compat import TYPE_CHECKING
from _pytest.fixtures import FixtureRequest
Expand Down Expand Up @@ -139,7 +140,7 @@ def __init__(
):
self.reprlocation_lines = reprlocation_lines

def toterminal(self, tw: py.io.TerminalWriter) -> None:
def toterminal(self, tw: TerminalWriter) -> None:
for reprlocation, lines in self.reprlocation_lines:
for line in lines:
tw.line(line)
Expand Down
3 changes: 2 additions & 1 deletion src/_pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import _pytest
from _pytest._code.code import FormattedExcinfo
from _pytest._code.code import TerminalRepr
from _pytest._io import TerminalWriter
from _pytest.compat import _format_args
from _pytest.compat import _PytestWrapper
from _pytest.compat import get_real_func
Expand Down Expand Up @@ -754,7 +755,7 @@ def __init__(self, filename, firstlineno, tblines, errorstring, argname):
self.firstlineno = firstlineno
self.argname = argname

def toterminal(self, tw: py.io.TerminalWriter) -> None:
def toterminal(self, tw: TerminalWriter) -> None:
# tw.line("FixtureLookupError: %s" %(self.argname), red=True)
for tbline in self.tblines:
tw.line(tbline.rstrip())
Expand Down
3 changes: 2 additions & 1 deletion src/_pytest/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from _pytest._code.code import ReprLocals
from _pytest._code.code import ReprTraceback
from _pytest._code.code import TerminalRepr
from _pytest._io import TerminalWriter
from _pytest.compat import TYPE_CHECKING
from _pytest.nodes import Node
from _pytest.outcomes import skip
Expand Down Expand Up @@ -80,7 +81,7 @@ def longreprtext(self):

.. versionadded:: 3.0
"""
tw = py.io.TerminalWriter(stringio=True)
tw = TerminalWriter(stringio=True)
tw.hasmarkup = False
self.toterminal(tw)
exc = tw.stringio.getvalue()
Expand Down
7 changes: 4 additions & 3 deletions testing/code/test_excinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from _pytest._code.code import ExceptionChainRepr
from _pytest._code.code import ExceptionInfo
from _pytest._code.code import FormattedExcinfo
from _pytest._io import TerminalWriter


try:
Expand Down Expand Up @@ -855,7 +856,7 @@ def test_reprexcinfo_unicode(self):
from _pytest._code.code import TerminalRepr

class MyRepr(TerminalRepr):
def toterminal(self, tw: py.io.TerminalWriter) -> None:
def toterminal(self, tw: TerminalWriter) -> None:
tw.line("я")

x = str(MyRepr())
Expand Down Expand Up @@ -1005,7 +1006,7 @@ def f():
"""
)
excinfo = pytest.raises(ValueError, mod.f)
tw = py.io.TerminalWriter(stringio=True)
tw = TerminalWriter(stringio=True)
repr = excinfo.getrepr(**reproptions)
repr.toterminal(tw)
assert tw.stringio.getvalue()
Expand Down Expand Up @@ -1225,7 +1226,7 @@ def g():
getattr(excinfo.value, attr).__traceback__ = None

r = excinfo.getrepr()
tw = py.io.TerminalWriter(stringio=True)
tw = TerminalWriter(stringio=True)
tw.hasmarkup = False
r.toterminal(tw)

Expand Down
7 changes: 3 additions & 4 deletions testing/logging/test_formatter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import logging

import py.io

from _pytest._io import TerminalWriter
from _pytest.logging import ColoredLevelFormatter


Expand All @@ -22,7 +21,7 @@ class ColorConfig:
class option:
pass

tw = py.io.TerminalWriter()
tw = TerminalWriter()
tw.hasmarkup = True
formatter = ColoredLevelFormatter(tw, logfmt)
output = formatter.format(record)
Expand Down Expand Up @@ -142,7 +141,7 @@ class ColorConfig:
class option:
pass

tw = py.io.TerminalWriter()
tw = TerminalWriter()
tw.hasmarkup = True
formatter = ColoredLevelFormatter(tw, logfmt)
output = formatter.format(record)
Expand Down