Skip to content

Commit a6386e8

Browse files
committed
don't require IO for redirect_stdout
#14903
1 parent ee5f034 commit a6386e8

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

stdlib/contextlib.pyi

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,23 @@ class suppress(AbstractContextManager[None, bool]):
141141
self, exctype: type[BaseException] | None, excinst: BaseException | None, exctb: TracebackType | None
142142
) -> bool: ...
143143

144-
class _RedirectStream(AbstractContextManager[_T_io, None]):
145-
def __init__(self, new_target: _T_io) -> None: ...
144+
# This is trying to describe what is needed for (most?) uses
145+
# of `redirect_stdout` and `redirect_stderr`.
146+
# https://github.com/python/typeshed/issues/14903
147+
class _SupportsRedirect:
148+
def write(self, s: str, /) -> int: ...
149+
def flush(self) -> None: ...
150+
151+
_SupportsRedirectT = TypeVar("_SupportsRedirectT", bound=_SupportsRedirect)
152+
153+
class _RedirectStream(AbstractContextManager[_SupportsRedirectT, None]):
154+
def __init__(self, new_target: _SupportsRedirectT) -> None: ...
146155
def __exit__(
147156
self, exctype: type[BaseException] | None, excinst: BaseException | None, exctb: TracebackType | None
148157
) -> None: ...
149158

150-
class redirect_stdout(_RedirectStream[_T_io]): ...
151-
class redirect_stderr(_RedirectStream[_T_io]): ...
159+
class redirect_stdout(_RedirectStream[_SupportsRedirectT]): ...
160+
class redirect_stderr(_RedirectStream[_SupportsRedirectT]): ...
152161

153162
class _BaseExitStack(Generic[_ExitT_co]):
154163
def enter_context(self, cm: AbstractContextManager[_T, _ExitT_co]) -> _T: ...

0 commit comments

Comments
 (0)