Skip to content

Commit e5252ae

Browse files
committed
config: typing for create_terminal_writer
This also imports `TerminalWriter` explicitly, allowing for easier phasing out / replacing of it (via the import, which then could go through the `_pytest.compat` module).
1 parent 10e243d commit e5252ae

File tree

9 files changed

+35
-26
lines changed

9 files changed

+35
-26
lines changed

src/_pytest/_code/code.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import attr
2828
import pluggy
2929
import py
30+
from py.io import TerminalWriter
3031

3132
import _pytest
3233
from _pytest._io.saferepr import safeformat
@@ -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/cacheprovider.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import attr
1313
import py
14+
from py.io import TerminalWriter
1415

1516
import pytest
1617
from .pathlib import Path
@@ -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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from pluggy import HookimplMarker
2626
from pluggy import HookspecMarker
2727
from pluggy import PluginManager
28+
from py.io import TerminalWriter
2829

2930
import _pytest._code
3031
import _pytest.assertion
@@ -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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from typing import Tuple
1414
from typing import Union
1515

16-
import py
16+
from py.io import TerminalWriter
1717

1818
import pytest
1919
from _pytest import outcomes
@@ -139,7 +139,7 @@ def __init__(
139139
):
140140
self.reprlocation_lines = reprlocation_lines
141141

142-
def toterminal(self, tw: py.io.TerminalWriter) -> None:
142+
def toterminal(self, tw: TerminalWriter) -> None:
143143
for reprlocation, lines in self.reprlocation_lines:
144144
for line in lines:
145145
tw.line(line)

src/_pytest/fixtures.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import attr
1414
import py
15+
from py.io import TerminalWriter
1516

1617
import _pytest
1718
from _pytest._code.code import FormattedExcinfo
@@ -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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from typing import Union
88

99
import py
10+
from py.io import TerminalWriter
1011

1112
from _pytest._code.code import ExceptionChainRepr
1213
from _pytest._code.code import ExceptionInfo
@@ -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()

src/_pytest/terminal.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,9 @@ def rewrite(self, line, **markup):
357357

358358
def write_sep(self, sep, title=None, **markup):
359359
self.ensure_newline()
360+
if sep == "_":
361+
sep = " "
362+
markup.setdefault("underline", True)
360363
self._tw.sep(sep, title, **markup)
361364

362365
def section(self, title, sep="=", **kw):

testing/code/test_excinfo.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from typing import Union
77

88
import py
9+
from py.io import TerminalWriter
910

1011
import _pytest
1112
import pytest
@@ -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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22

3-
import py.io
3+
from py.io import TerminalWriter
44

55
from _pytest.logging import ColoredLevelFormatter
66

@@ -22,7 +22,7 @@ class ColorConfig:
2222
class option:
2323
pass
2424

25-
tw = py.io.TerminalWriter()
25+
tw = TerminalWriter()
2626
tw.hasmarkup = True
2727
formatter = ColoredLevelFormatter(tw, logfmt)
2828
output = formatter.format(record)
@@ -142,7 +142,7 @@ class ColorConfig:
142142
class option:
143143
pass
144144

145-
tw = py.io.TerminalWriter()
145+
tw = TerminalWriter()
146146
tw.hasmarkup = True
147147
formatter = ColoredLevelFormatter(tw, logfmt)
148148
output = formatter.format(record)

0 commit comments

Comments
 (0)