Skip to content

Commit 7cd0b23

Browse files
authored
Subclass StringIO for _WarningStream. (#886)
This is responding to a change in readme_renderer.rst.render that expects an `IO[str]` instance. See discussion at pypa/readme_renderer#231 (review)
1 parent aa7c047 commit 7cd0b23

File tree

2 files changed

+13
-28
lines changed

2 files changed

+13
-28
lines changed

tests/test_check.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,18 @@
2424
class TestWarningStream:
2525
def setup(self):
2626
self.stream = check._WarningStream()
27-
self.stream.output = pretend.stub(
28-
write=pretend.call_recorder(lambda a: None),
29-
getvalue=lambda: "result",
30-
)
3127

3228
def test_write_match(self):
3329
self.stream.write("<string>:2: (WARNING/2) Title underline too short.")
34-
35-
assert self.stream.output.write.calls == [
36-
pretend.call("line 2: Warning: Title underline too short.\n")
37-
]
30+
assert self.stream.getvalue() == "line 2: Warning: Title underline too short.\n"
3831

3932
def test_write_nomatch(self):
4033
self.stream.write("this does not match")
41-
42-
assert self.stream.output.write.calls == [pretend.call("this does not match")]
34+
assert self.stream.getvalue() == "this does not match"
4335

4436
def test_str_representation(self):
45-
assert str(self.stream) == "result"
37+
self.stream.write("<string>:2: (WARNING/2) Title underline too short.")
38+
assert str(self.stream) == "line 2: Warning: Title underline too short."
4639

4740

4841
def test_check_no_distributions(monkeypatch, caplog):

twine/commands/check.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,19 @@
4848
)
4949

5050

51-
class _WarningStream:
52-
def __init__(self) -> None:
53-
self.output = io.StringIO()
54-
55-
def write(self, text: str) -> None:
51+
class _WarningStream(io.StringIO):
52+
def write(self, text: str) -> int:
5653
matched = _REPORT_RE.search(text)
54+
if matched:
55+
line = matched.group("line")
56+
level_text = matched.group("level").capitalize()
57+
message = matched.group("message").rstrip("\r\n")
58+
text = f"line {line}: {level_text}: {message}\n"
5759

58-
if not matched:
59-
self.output.write(text)
60-
return
61-
62-
self.output.write(
63-
"line {line}: {level_text}: {message}\n".format(
64-
level_text=matched.group("level").capitalize(),
65-
line=matched.group("line"),
66-
message=matched.group("message").rstrip("\r\n"),
67-
)
68-
)
60+
return super().write(text)
6961

7062
def __str__(self) -> str:
71-
return self.output.getvalue().strip()
63+
return self.getvalue().strip()
7264

7365

7466
def _check_file(

0 commit comments

Comments
 (0)