Skip to content

Commit daddd97

Browse files
committed
config: typing for create_terminal_writer
This also imports `TerminalWriter` explicitly via `_pytest._io`, allowing for easier extending / replacing it.
1 parent 10e243d commit daddd97

File tree

9 files changed

+35
-28
lines changed

9 files changed

+35
-28
lines changed

src/_pytest/_code/code.py

+12-11
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import py
3030

3131
import _pytest
32+
from _pytest._io import TerminalWriter
3233
from _pytest._io.saferepr import safeformat
3334
from _pytest._io.saferepr import saferepr
3435
from _pytest.compat import overload
@@ -915,14 +916,14 @@ def __str__(self) -> str:
915916
# FYI this is called from pytest-xdist's serialization of exception
916917
# information.
917918
io = StringIO()
918-
tw = py.io.TerminalWriter(file=io)
919+
tw = TerminalWriter(file=io)
919920
self.toterminal(tw)
920921
return io.getvalue().strip()
921922

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

925-
def toterminal(self, tw: py.io.TerminalWriter) -> None:
926+
def toterminal(self, tw: TerminalWriter) -> None:
926927
raise NotImplementedError()
927928

928929

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

936-
def toterminal(self, tw: py.io.TerminalWriter) -> None:
937+
def toterminal(self, tw: TerminalWriter) -> None:
937938
for name, content, sep in self.sections:
938939
tw.sep(sep, name)
939940
tw.line(content)
@@ -953,7 +954,7 @@ def __init__(
953954
self.reprtraceback = chain[-1][0]
954955
self.reprcrash = chain[-1][1]
955956

956-
def toterminal(self, tw: py.io.TerminalWriter) -> None:
957+
def toterminal(self, tw: TerminalWriter) -> None:
957958
for element in self.chain:
958959
element[0].toterminal(tw)
959960
if element[2] is not None:
@@ -970,7 +971,7 @@ def __init__(
970971
self.reprtraceback = reprtraceback
971972
self.reprcrash = reprcrash
972973

973-
def toterminal(self, tw: py.io.TerminalWriter) -> None:
974+
def toterminal(self, tw: TerminalWriter) -> None:
974975
self.reprtraceback.toterminal(tw)
975976
super().toterminal(tw)
976977

@@ -988,7 +989,7 @@ def __init__(
988989
self.extraline = extraline
989990
self.style = style
990991

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

1023-
def toterminal(self, tw: py.io.TerminalWriter) -> None:
1024+
def toterminal(self, tw: TerminalWriter) -> None:
10241025
tw.write("".join(self.lines))
10251026

10261027

@@ -1039,7 +1040,7 @@ def __init__(
10391040
self.reprfileloc = filelocrepr
10401041
self.style = style
10411042

1042-
def toterminal(self, tw: py.io.TerminalWriter) -> None:
1043+
def toterminal(self, tw: TerminalWriter) -> None:
10431044
if self.style == "short":
10441045
assert self.reprfileloc is not None
10451046
self.reprfileloc.toterminal(tw)
@@ -1072,7 +1073,7 @@ def __init__(self, path, lineno: int, message: str) -> None:
10721073
self.lineno = lineno
10731074
self.message = message
10741075

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

1090-
def toterminal(self, tw: py.io.TerminalWriter) -> None:
1091+
def toterminal(self, tw: TerminalWriter) -> None:
10911092
for line in self.lines:
10921093
tw.line(line)
10931094

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

1099-
def toterminal(self, tw: py.io.TerminalWriter) -> None:
1100+
def toterminal(self, tw: TerminalWriter) -> None:
11001101
if self.args:
11011102
linesofar = ""
11021103
for name, value in self.args:

src/_pytest/_io/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Reexport TerminalWriter from here instead of py, to make it easier to
2+
# extend or swap our own implementation in the future.
3+
from py.io import TerminalWriter as TerminalWriter # noqa: F401

src/_pytest/cacheprovider.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from .pathlib import resolve_from_str
1818
from .pathlib import rm_rf
1919
from _pytest import nodes
20+
from _pytest._io import TerminalWriter
2021
from _pytest.config import Config
2122
from _pytest.main import Session
2223

@@ -418,7 +419,7 @@ def pytest_report_header(config):
418419
def cacheshow(config, session):
419420
from pprint import pformat
420421

421-
tw = py.io.TerminalWriter()
422+
tw = TerminalWriter()
422423
tw.line("cachedir: " + str(config.cache._cachedir))
423424
if not config.cache._cachedir.is_dir():
424425
tw.line("cache is empty")

src/_pytest/config/__init__.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from .findpaths import exists
3737
from _pytest._code import ExceptionInfo
3838
from _pytest._code import filter_traceback
39+
from _pytest._io import TerminalWriter
3940
from _pytest.compat import importlib_metadata
4041
from _pytest.compat import TYPE_CHECKING
4142
from _pytest.outcomes import fail
@@ -73,7 +74,7 @@ def main(args=None, plugins=None) -> "Union[int, _pytest.main.ExitCode]":
7374
config = _prepareconfig(args, plugins)
7475
except ConftestImportFailure as e:
7576
exc_info = ExceptionInfo(e.excinfo)
76-
tw = py.io.TerminalWriter(sys.stderr)
77+
tw = TerminalWriter(sys.stderr)
7778
tw.line(
7879
"ImportError while loading conftest '{e.path}'.".format(e=e), red=True
7980
)
@@ -99,7 +100,7 @@ def main(args=None, plugins=None) -> "Union[int, _pytest.main.ExitCode]":
99100
finally:
100101
config._ensure_unconfigure()
101102
except UsageError as e:
102-
tw = py.io.TerminalWriter(sys.stderr)
103+
tw = TerminalWriter(sys.stderr)
103104
for msg in e.args:
104105
tw.line("ERROR: {}\n".format(msg), red=True)
105106
return ExitCode.USAGE_ERROR
@@ -1175,12 +1176,12 @@ def setns(obj, dic):
11751176
setattr(pytest, name, value)
11761177

11771178

1178-
def create_terminal_writer(config, *args, **kwargs):
1179+
def create_terminal_writer(config: Config, *args, **kwargs) -> TerminalWriter:
11791180
"""Create a TerminalWriter instance configured according to the options
11801181
in the config object. Every code which requires a TerminalWriter object
11811182
and has access to a config object should use this function.
11821183
"""
1183-
tw = py.io.TerminalWriter(*args, **kwargs)
1184+
tw = TerminalWriter(*args, **kwargs)
11841185
if config.option.color == "yes":
11851186
tw.hasmarkup = True
11861187
if config.option.color == "no":

src/_pytest/doctest.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@
1313
from typing import Tuple
1414
from typing import Union
1515

16-
import py
17-
1816
import pytest
1917
from _pytest import outcomes
2018
from _pytest._code.code import ExceptionInfo
2119
from _pytest._code.code import ReprFileLocation
2220
from _pytest._code.code import TerminalRepr
21+
from _pytest._io import TerminalWriter
2322
from _pytest.compat import safe_getattr
2423
from _pytest.compat import TYPE_CHECKING
2524
from _pytest.fixtures import FixtureRequest
@@ -139,7 +138,7 @@ def __init__(
139138
):
140139
self.reprlocation_lines = reprlocation_lines
141140

142-
def toterminal(self, tw: py.io.TerminalWriter) -> None:
141+
def toterminal(self, tw: TerminalWriter) -> None:
143142
for reprlocation, lines in self.reprlocation_lines:
144143
for line in lines:
145144
tw.line(line)

src/_pytest/fixtures.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import _pytest
1717
from _pytest._code.code import FormattedExcinfo
1818
from _pytest._code.code import TerminalRepr
19+
from _pytest._io import TerminalWriter
1920
from _pytest.compat import _format_args
2021
from _pytest.compat import _PytestWrapper
2122
from _pytest.compat import get_real_func
@@ -751,7 +752,7 @@ def __init__(self, filename, firstlineno, tblines, errorstring, argname):
751752
self.firstlineno = firstlineno
752753
self.argname = argname
753754

754-
def toterminal(self, tw: py.io.TerminalWriter) -> None:
755+
def toterminal(self, tw: TerminalWriter) -> None:
755756
# tw.line("FixtureLookupError: %s" %(self.argname), red=True)
756757
for tbline in self.tblines:
757758
tw.line(tbline.rstrip())

src/_pytest/reports.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from _pytest._code.code import ReprLocals
1919
from _pytest._code.code import ReprTraceback
2020
from _pytest._code.code import TerminalRepr
21+
from _pytest._io import TerminalWriter
2122
from _pytest.compat import TYPE_CHECKING
2223
from _pytest.nodes import Node
2324
from _pytest.outcomes import skip
@@ -80,7 +81,7 @@ def longreprtext(self):
8081
8182
.. versionadded:: 3.0
8283
"""
83-
tw = py.io.TerminalWriter(stringio=True)
84+
tw = TerminalWriter(stringio=True)
8485
tw.hasmarkup = False
8586
self.toterminal(tw)
8687
exc = tw.stringio.getvalue()

testing/code/test_excinfo.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from _pytest._code.code import ExceptionChainRepr
1313
from _pytest._code.code import ExceptionInfo
1414
from _pytest._code.code import FormattedExcinfo
15+
from _pytest._io import TerminalWriter
1516

1617

1718
try:
@@ -855,7 +856,7 @@ def test_reprexcinfo_unicode(self):
855856
from _pytest._code.code import TerminalRepr
856857

857858
class MyRepr(TerminalRepr):
858-
def toterminal(self, tw: py.io.TerminalWriter) -> None:
859+
def toterminal(self, tw: TerminalWriter) -> None:
859860
tw.line("я")
860861

861862
x = str(MyRepr())
@@ -1005,7 +1006,7 @@ def f():
10051006
"""
10061007
)
10071008
excinfo = pytest.raises(ValueError, mod.f)
1008-
tw = py.io.TerminalWriter(stringio=True)
1009+
tw = TerminalWriter(stringio=True)
10091010
repr = excinfo.getrepr(**reproptions)
10101011
repr.toterminal(tw)
10111012
assert tw.stringio.getvalue()
@@ -1225,7 +1226,7 @@ def g():
12251226
getattr(excinfo.value, attr).__traceback__ = None
12261227

12271228
r = excinfo.getrepr()
1228-
tw = py.io.TerminalWriter(stringio=True)
1229+
tw = TerminalWriter(stringio=True)
12291230
tw.hasmarkup = False
12301231
r.toterminal(tw)
12311232

testing/logging/test_formatter.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import logging
22

3-
import py.io
4-
3+
from _pytest._io import TerminalWriter
54
from _pytest.logging import ColoredLevelFormatter
65

76

@@ -22,7 +21,7 @@ class ColorConfig:
2221
class option:
2322
pass
2423

25-
tw = py.io.TerminalWriter()
24+
tw = TerminalWriter()
2625
tw.hasmarkup = True
2726
formatter = ColoredLevelFormatter(tw, logfmt)
2827
output = formatter.format(record)
@@ -142,7 +141,7 @@ class ColorConfig:
142141
class option:
143142
pass
144143

145-
tw = py.io.TerminalWriter()
144+
tw = TerminalWriter()
146145
tw.hasmarkup = True
147146
formatter = ColoredLevelFormatter(tw, logfmt)
148147
output = formatter.format(record)

0 commit comments

Comments
 (0)