|
9 | 9 | import sys |
10 | 10 | from io import UnsupportedOperation |
11 | 11 | from tempfile import TemporaryFile |
| 12 | +from typing import BinaryIO |
12 | 13 | from typing import List |
13 | 14 |
|
14 | 15 | import pytest |
@@ -414,29 +415,27 @@ def safe_text_dupfile(f, mode, default_encoding="UTF8"): |
414 | 415 | class EncodedFile: |
415 | 416 | errors = "strict" # possibly needed by py3 code (issue555) |
416 | 417 |
|
417 | | - def __init__(self, buffer, encoding): |
| 418 | + def __init__(self, buffer: BinaryIO, encoding: str) -> None: |
418 | 419 | self.buffer = buffer |
419 | 420 | self.encoding = encoding |
420 | 421 |
|
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): |
425 | 424 | raise TypeError( |
426 | 425 | "write() argument must be str, not {}".format(type(obj).__name__) |
427 | 426 | ) |
428 | | - return self.buffer.write(obj) |
| 427 | + return self.buffer.write(obj.encode(self.encoding, "replace")) |
429 | 428 |
|
430 | 429 | def writelines(self, linelist: List[str]) -> None: |
431 | 430 | self.buffer.writelines([x.encode(self.encoding, "replace") for x in linelist]) |
432 | 431 |
|
433 | 432 | @property |
434 | | - def name(self): |
| 433 | + def name(self) -> str: |
435 | 434 | """Ensure that file.name is a string.""" |
436 | 435 | return repr(self.buffer) |
437 | 436 |
|
438 | 437 | @property |
439 | | - def mode(self): |
| 438 | + def mode(self) -> str: |
440 | 439 | return self.buffer.mode.replace("b", "") |
441 | 440 |
|
442 | 441 | def __getattr__(self, name): |
|
0 commit comments