6
6
import sys
7
7
from io import UnsupportedOperation
8
8
from tempfile import TemporaryFile
9
+ from types import TracebackType
9
10
from typing import Any
10
11
from typing import AnyStr
12
+ from typing import BinaryIO
11
13
from typing import Generator
12
14
from typing import Generic
15
+ from typing import Iterable
16
+ from typing import Iterator
17
+ from typing import List
13
18
from typing import NamedTuple
14
19
from typing import Optional
15
20
from typing import TextIO
16
21
from typing import Tuple
22
+ from typing import Type
17
23
from typing import TYPE_CHECKING
18
24
from typing import Union
19
25
@@ -185,19 +191,27 @@ def write(self, s: str) -> int:
185
191
return self ._other .write (s )
186
192
187
193
188
- class DontReadFromInput :
189
- encoding = None
194
+ class DontReadFromInput (TextIO ):
195
+ @property
196
+ def encoding (self ) -> str :
197
+ return sys .__stdin__ .encoding
190
198
191
- def read (self , * args ) :
199
+ def read (self , size : int = - 1 ) -> str :
192
200
raise OSError (
193
201
"pytest: reading from stdin while output is captured! Consider using `-s`."
194
202
)
195
203
196
204
readline = read
197
- readlines = read
198
- __next__ = read
199
205
200
- def __iter__ (self ):
206
+ def __next__ (self ) -> str :
207
+ return self .readline ()
208
+
209
+ def readlines (self , hint : Optional [int ] = - 1 ) -> List [str ]:
210
+ raise OSError (
211
+ "pytest: reading from stdin while output is captured! Consider using `-s`."
212
+ )
213
+
214
+ def __iter__ (self ) -> Iterator [str ]:
201
215
return self
202
216
203
217
def fileno (self ) -> int :
@@ -215,7 +229,7 @@ def close(self) -> None:
215
229
def readable (self ) -> bool :
216
230
return False
217
231
218
- def seek (self , offset : int ) -> int :
232
+ def seek (self , offset : int , whence : int = 0 ) -> int :
219
233
raise UnsupportedOperation ("redirected stdin is pseudofile, has no seek(int)" )
220
234
221
235
def seekable (self ) -> bool :
@@ -224,22 +238,34 @@ def seekable(self) -> bool:
224
238
def tell (self ) -> int :
225
239
raise UnsupportedOperation ("redirected stdin is pseudofile, has no tell()" )
226
240
227
- def truncate (self , size : int ) -> None :
241
+ def truncate (self , size : Optional [ int ] = None ) -> int :
228
242
raise UnsupportedOperation ("cannont truncate stdin" )
229
243
230
- def write (self , * args ) -> None :
244
+ def write (self , data : str ) -> int :
231
245
raise UnsupportedOperation ("cannot write to stdin" )
232
246
233
- def writelines (self , * args ) -> None :
247
+ def writelines (self , lines : Iterable [ str ] ) -> None :
234
248
raise UnsupportedOperation ("Cannot write to stdin" )
235
249
236
250
def writable (self ) -> bool :
237
251
return False
238
252
239
- @property
240
- def buffer (self ):
253
+ def __enter__ (self ) -> "DontReadFromInput" :
241
254
return self
242
255
256
+ def __exit__ (
257
+ self ,
258
+ type : Optional [Type [BaseException ]],
259
+ value : Optional [BaseException ],
260
+ traceback : Optional [TracebackType ],
261
+ ) -> None :
262
+ pass
263
+
264
+ @property
265
+ def buffer (self ) -> BinaryIO :
266
+ # The str/bytes doesn't actually matter in this type, so OK to fake.
267
+ return self # type: ignore[return-value]
268
+
243
269
244
270
# Capture classes.
245
271
0 commit comments