From 54b8b40f83308dd87695d0ec596d1ad767f3f5ab Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Sat, 21 Jan 2023 10:53:28 +0200 Subject: [PATCH] capture: improve NoCapture typing --- src/_pytest/capture.py | 30 +++++++++++++++++++++++++----- testing/test_capture.py | 1 + 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/_pytest/capture.py b/src/_pytest/capture.py index 0ff583e04..4b838604e 100644 --- a/src/_pytest/capture.py +++ b/src/_pytest/capture.py @@ -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" diff --git a/testing/test_capture.py b/testing/test_capture.py index 26c1a5f74..5d6ef64ef 100644 --- a/testing/test_capture.py +++ b/testing/test_capture.py @@ -1052,6 +1052,7 @@ class TestFDCapture: ) ) # Should not crash with missing "_old". + assert isinstance(cap.syscapture, capture.SysCapture) assert repr(cap.syscapture) == ( " _state='done' tmpfile={!r}>".format( cap.syscapture.tmpfile