|
5 | 5 |
|
6 | 6 | from __future__ import annotations |
7 | 7 |
|
| 8 | +import io |
8 | 9 | import json |
9 | | -from typing import Any, Final, Iterable |
| 10 | +from types import TracebackType |
| 11 | +from typing import Any, Final, Iterable, Iterator, TextIO |
10 | 12 |
|
11 | 13 | from mypy.ipc import IPCBase |
12 | 14 |
|
@@ -40,19 +42,76 @@ def send(connection: IPCBase, data: Any) -> None: |
40 | 42 | connection.write(json.dumps(data)) |
41 | 43 |
|
42 | 44 |
|
43 | | -class WriteToConn: |
| 45 | +class WriteToConn(TextIO): |
44 | 46 | """Helper class to write to a connection instead of standard output.""" |
45 | 47 |
|
46 | | - def __init__(self, server: IPCBase, output_key: str = "stdout") -> None: |
| 48 | + def __init__(self, server: IPCBase, output_key: str, isatty: bool) -> None: |
47 | 49 | self.server = server |
48 | 50 | self.output_key = output_key |
| 51 | + self._isatty = isatty |
| 52 | + |
| 53 | + def __enter__(self) -> TextIO: |
| 54 | + return self |
| 55 | + |
| 56 | + def __exit__( |
| 57 | + self, |
| 58 | + t: type[BaseException] | None, |
| 59 | + value: BaseException | None, |
| 60 | + traceback: TracebackType | None, |
| 61 | + ) -> None: |
| 62 | + pass |
| 63 | + |
| 64 | + def __iter__(self) -> Iterator[str]: |
| 65 | + raise io.UnsupportedOperation |
| 66 | + |
| 67 | + def __next__(self) -> str: |
| 68 | + raise io.UnsupportedOperation |
| 69 | + |
| 70 | + def close(self) -> None: |
| 71 | + pass |
| 72 | + |
| 73 | + def fileno(self) -> int: |
| 74 | + raise OSError |
| 75 | + |
| 76 | + def flush(self) -> None: |
| 77 | + pass |
| 78 | + |
| 79 | + def isatty(self) -> bool: |
| 80 | + return self._isatty |
| 81 | + |
| 82 | + def read(self, n: int = 0) -> str: |
| 83 | + raise io.UnsupportedOperation |
| 84 | + |
| 85 | + def readable(self) -> bool: |
| 86 | + return False |
| 87 | + |
| 88 | + def readline(self, limit: int = 0) -> str: |
| 89 | + raise io.UnsupportedOperation |
| 90 | + |
| 91 | + def readlines(self, hint: int = 0) -> list[str]: |
| 92 | + raise io.UnsupportedOperation |
| 93 | + |
| 94 | + def seek(self, offset: int, whence: int = 0) -> int: |
| 95 | + raise io.UnsupportedOperation |
| 96 | + |
| 97 | + def seekable(self) -> bool: |
| 98 | + return False |
| 99 | + |
| 100 | + def tell(self) -> int: |
| 101 | + raise io.UnsupportedOperation |
| 102 | + |
| 103 | + def truncate(self, size: int | None = 0) -> int: |
| 104 | + raise io.UnsupportedOperation |
49 | 105 |
|
50 | 106 | def write(self, output: str) -> int: |
51 | 107 | resp: dict[str, Any] = {} |
52 | 108 | resp[self.output_key] = output |
53 | 109 | send(self.server, resp) |
54 | 110 | return len(output) |
55 | 111 |
|
| 112 | + def writable(self) -> bool: |
| 113 | + return True |
| 114 | + |
56 | 115 | def writelines(self, lines: Iterable[str]) -> None: |
57 | 116 | for s in lines: |
58 | 117 | self.write(s) |
0 commit comments