Skip to content

Commit 5fa1fe7

Browse files
committed
typing: EncodedFile
1 parent 039d582 commit 5fa1fe7

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
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 IO
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: IO[bytes], 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):

0 commit comments

Comments
 (0)