capture: improve NoCapture typing

This commit is contained in:
Ran Benita 2023-01-21 10:53:28 +02:00
parent 54911acf8d
commit 54b8b40f83
2 changed files with 26 additions and 5 deletions

View File

@ -306,9 +306,29 @@ class CaptureBase(abc.ABC, Generic[AnyStr]):
patchsysdict = {0: "stdin", 1: "stdout", 2: "stderr"}
class NoCapture:
EMPTY_BUFFER = None
__init__ = start = done = suspend = resume = lambda *args: None
class NoCapture(CaptureBase[str]):
EMPTY_BUFFER = ""
def __init__(self, fd: int) -> None:
pass
def start(self) -> None:
pass
def done(self) -> None:
pass
def suspend(self) -> None:
pass
def resume(self) -> None:
pass
def snap(self) -> str:
return ""
def writeorg(self, data: str) -> None:
pass
class SysCaptureBase(CaptureBase[AnyStr]):
@ -439,7 +459,7 @@ class FDCaptureBase(CaptureBase[AnyStr]):
if targetfd == 0:
self.tmpfile = open(os.devnull, encoding="utf-8")
self.syscapture = SysCapture(targetfd)
self.syscapture: CaptureBase[str] = SysCapture(targetfd)
else:
self.tmpfile = EncodedFile(
TemporaryFile(buffering=0),
@ -451,7 +471,7 @@ class FDCaptureBase(CaptureBase[AnyStr]):
if targetfd in patchsysdict:
self.syscapture = SysCapture(targetfd, self.tmpfile)
else:
self.syscapture = NoCapture()
self.syscapture = NoCapture(targetfd)
self._state = "initialized"

View File

@ -1052,6 +1052,7 @@ class TestFDCapture:
)
)
# Should not crash with missing "_old".
assert isinstance(cap.syscapture, capture.SysCapture)
assert repr(cap.syscapture) == (
"<SysCapture stdout _old=<UNSET> _state='done' tmpfile={!r}>".format(
cap.syscapture.tmpfile