Skip to content

Commit

Permalink
Improve protocol usage, add overloads for pipes.
Browse files Browse the repository at this point in the history
  • Loading branch information
hoel-bagard committed Feb 21, 2024
1 parent b29f750 commit 80f410c
Showing 1 changed file with 47 additions and 11 deletions.
58 changes: 47 additions & 11 deletions stubs/wurlitzer/wurlitzer.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ from _typeshed import SupportsWrite
from collections.abc import Iterator
from contextlib import contextmanager
from types import TracebackType
from typing import Any, Final, Literal, TextIO
from typing import Any, Final, Literal, Protocol, TextIO, TypeVar, overload
from typing_extensions import TypeAlias

STDOUT: Final = 2
PIPE: Final = 3
_STDOUT: TypeAlias = Literal[2]
_PIPE: TypeAlias = Literal[3]
_T_contra = TypeVar("_T_contra", contravariant=True)
_StreamOutT = TypeVar("_StreamOutT", bound=_Stream[str] | _Stream[bytes])
_StreamErrT = TypeVar("_StreamErrT", bound=_Stream[str] | _Stream[bytes])

class _Stream(SupportsWrite[_T_contra], Protocol):
def seek(self, __offset: int, __whence: int = ...) -> int: ...

# Alias for IPython.core.interactiveshell.InteractiveShell.
# N.B. Even if we added ipython to the stub-uploader allowlist,
Expand All @@ -23,9 +29,9 @@ class Wurlitzer:

def __init__(
self,
stdout: SupportsWrite[str | bytes] | None = None,
stderr: _STDOUT | SupportsWrite[str | bytes] | None = None,
encoding: str = ...,
stdout: _Stream[str] | _Stream[bytes] | None = None,
stderr: _STDOUT | _Stream[str] | _Stream[bytes] | None = None,
encoding: str | None = ...,
bufsize: int | None = ...,
) -> None: ...
def __enter__(self): ...
Expand All @@ -34,16 +40,46 @@ class Wurlitzer:
) -> None: ...

def dup2(a: int, b: int, timeout: float = 3) -> int: ...
def sys_pipes(
encoding: str = ..., bufsize: int | None = None
) -> contextlib._GeneratorContextManager[tuple[TextIO | io.BytesIO | io.StringIO, TextIO | io.BytesIO | io.StringIO | None]]: ...
def sys_pipes(encoding: str = ..., bufsize: int | None = None) -> contextlib._GeneratorContextManager[tuple[TextIO, TextIO]]: ...
@overload
@contextmanager
def pipes(stdout: _PIPE, stderr: _STDOUT, encoding: None, bufsize: int | None = None) -> Iterator[tuple[io.BytesIO, None]]: ...
@overload
@contextmanager
def pipes(
stdout: _PIPE, stderr: _PIPE, encoding: None, bufsize: int | None = None
) -> Iterator[tuple[io.BytesIO, io.BytesIO]]: ...
@overload
@contextmanager
def pipes(
stdout: _PIPE, stderr: _StreamErrT, encoding: None, bufsize: int | None = None
) -> Iterator[tuple[io.BytesIO, _StreamErrT]]: ...
@overload
@contextmanager
def pipes(stdout: _PIPE, stderr: _STDOUT, encoding: str, bufsize: int | None = None) -> Iterator[tuple[io.StringIO, None]]: ...
@overload
@contextmanager
def pipes(
stdout: _PIPE, stderr: _PIPE, encoding: str, bufsize: int | None = None
) -> Iterator[tuple[io.StringIO, io.StringIO]]: ...
@overload
@contextmanager
def pipes(
stdout: _PIPE, stderr: _StreamErrT, encoding: str, bufsize: int | None = None
) -> Iterator[tuple[io.StringIO, _StreamErrT]]: ...
@overload
@contextmanager
def pipes(
stdout: _StreamOutT, stderr: _StreamErrT, encoding: str | None, bufsize: int | None = None
) -> Iterator[tuple[_StreamOutT, _StreamErrT]]: ...
@overload
@contextmanager
def pipes(
stdout: _PIPE | SupportsWrite[str | bytes] = 3,
stderr: _STDOUT | _PIPE | SupportsWrite[str | bytes] = 3,
encoding: str = ...,
stdout: _PIPE | _StreamOutT = 3,
stderr: _STDOUT | _PIPE | _StreamErrT = 3,
encoding: str | None = ...,
bufsize: int | None = None,
) -> Iterator[tuple[TextIO | io.BytesIO | io.StringIO, TextIO | io.BytesIO | io.StringIO | None]]: ...
) -> Iterator[tuple[io.BytesIO | io.StringIO | _StreamOutT, io.BytesIO | io.StringIO | _StreamErrT | None]]: ...
def sys_pipes_forever(encoding: str = ..., bufsize: int | None = None): ...
def stop_sys_pipes() -> None: ...
def load_ipython_extension(ip: _InteractiveShell) -> None: ...
Expand Down

0 comments on commit 80f410c

Please sign in to comment.