Skip to content

Commit 3f8f395

Browse files
committed
typing: EncodedFile
1 parent 039d582 commit 3f8f395

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

src/_pytest/capture.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import sys
1010
from io import UnsupportedOperation
1111
from tempfile import TemporaryFile
12+
from typing import BinaryIO
1213
from typing import List
1314

1415
import pytest
@@ -414,29 +415,27 @@ def safe_text_dupfile(f, mode, default_encoding="UTF8"):
414415
class EncodedFile:
415416
errors = "strict" # possibly needed by py3 code (issue555)
416417

417-
def __init__(self, buffer, encoding):
418+
def __init__(self, buffer: BinaryIO, encoding: str) -> None:
418419
self.buffer = buffer
419420
self.encoding = encoding
420421

421-
def write(self, obj):
422-
if isinstance(obj, str):
423-
obj = obj.encode(self.encoding, "replace")
424-
else:
422+
def write(self, obj: str) -> int:
423+
if not isinstance(obj, str):
425424
raise TypeError(
426425
"write() argument must be str, not {}".format(type(obj).__name__)
427426
)
428-
return self.buffer.write(obj)
427+
return self.buffer.write(obj.encode(self.encoding, "replace"))
429428

430429
def writelines(self, linelist: List[str]) -> None:
431430
self.buffer.writelines([x.encode(self.encoding, "replace") for x in linelist])
432431

433432
@property
434-
def name(self):
433+
def name(self) -> str:
435434
"""Ensure that file.name is a string."""
436435
return repr(self.buffer)
437436

438437
@property
439-
def mode(self):
438+
def mode(self) -> str:
440439
return self.buffer.mode.replace("b", "")
441440

442441
def __getattr__(self, name):

testing/test_capture.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import textwrap
88
from io import StringIO
99
from io import UnsupportedOperation
10+
from typing import BinaryIO
1011
from typing import List
1112
from typing import TextIO
1213

@@ -1499,7 +1500,7 @@ def test_stderr_write_returns_len(capsys):
14991500
assert sys.stderr.write("Foo") == 3
15001501

15011502

1502-
def test_encodedfile_writelines(tmpfile) -> None:
1503+
def test_encodedfile_writelines(tmpfile: BinaryIO) -> None:
15031504
ef = capture.EncodedFile(tmpfile, "utf-8")
15041505
with pytest.raises(AttributeError):
15051506
ef.writelines([b"line1", b"line2"]) # type: ignore[list-item] # noqa: F821

0 commit comments

Comments
 (0)