Skip to content

Commit

Permalink
GH-46: Fix "escape sequences are not flushed for line-buffered files" (
Browse files Browse the repository at this point in the history
…#49)

Fix "escape sequences are not flushed for line-buffered files" (#46)
  • Loading branch information
qexat authored Jan 22, 2024
1 parent 11ba8c5 commit 08f65fd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
21 changes: 11 additions & 10 deletions src/coquille/prelude.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
P = ParamSpec("P")

from coquille.typeshed import Self
from coquille.typeshed import SupportsWrite
from coquille.typeshed import SupportsWriteAndFlush


@overload
Expand Down Expand Up @@ -74,15 +74,15 @@ def prepare(
@overload
def apply(
sequence: EscapeSequence | EscapeSequenceName,
file: SupportsWrite[str] | None = None,
file: SupportsWriteAndFlush[str] | None = None,
) -> None: # pragma: no cover
pass


@overload
def apply(
sequence: Callable[P, EscapeSequence],
file: SupportsWrite[str] | None = None,
file: SupportsWriteAndFlush[str] | None = None,
*args: P.args,
**kwargs: P.kwargs,
) -> None: # pragma: no cover
Expand All @@ -91,7 +91,7 @@ def apply(

def apply(
sequence: EscapeSequence | EscapeSequenceName | Callable[P, EscapeSequence],
file: SupportsWrite[str] | None = None,
file: SupportsWriteAndFlush[str] | None = None,
*args: P.args,
**kwargs: P.kwargs,
) -> None:
Expand All @@ -102,11 +102,12 @@ def apply(
string: EscapeSequence = prepare(sequence, *args, **kwargs)
target = file or sys.stdout
target.write(string)
target.flush()


class CoquilleLike(Protocol):
sequences: list[EscapeSequence]
file: SupportsWrite[str] | None
file: SupportsWriteAndFlush[str] | None

@abstractmethod
def print(
Expand All @@ -121,7 +122,7 @@ def print(
@dataclass(slots=True)
class _ContextCoquille:
sequences: list[EscapeSequence]
file: SupportsWrite[str] | None
file: SupportsWriteAndFlush[str] | None

def apply(self, sequence: EscapeSequence | EscapeSequenceName) -> None:
"""
Expand Down Expand Up @@ -165,7 +166,7 @@ def print(
@dataclass(slots=True)
class Coquille:
sequences: list[EscapeSequence]
file: SupportsWrite[str] | None
file: SupportsWriteAndFlush[str] | None

@overload
@classmethod
Expand All @@ -180,15 +181,15 @@ def new(
def new(
cls: type[Self],
*sequences: EscapeSequence | EscapeSequenceName,
file: SupportsWrite[str],
file: SupportsWriteAndFlush[str],
) -> Self: # pragma: no cover
pass

@classmethod
def new(
cls: type[Self],
*sequences: EscapeSequence | EscapeSequenceName,
file: SupportsWrite[str] | None = None,
file: SupportsWriteAndFlush[str] | None = None,
) -> Self:
"""
Convenient constructor for a Coquille.
Expand Down Expand Up @@ -283,7 +284,7 @@ def write(
text: str,
*sequences: EscapeSequence | EscapeSequenceName,
end: str | None = "\n",
file: SupportsWrite[str] | None = None,
file: SupportsWriteAndFlush[str] | None = None,
) -> None:
"""
A function relatively similar to built-in `print`, but with
Expand Down
6 changes: 5 additions & 1 deletion src/coquille/typeshed.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
_T_contra = TypeVar("_T_contra", contravariant=True)


class SupportsWrite(Protocol[_T_contra]):
class SupportsWriteAndFlush(Protocol[_T_contra]):
@abstractmethod
def write(self, __s: _T_contra) -> object:
pass

@abstractmethod
def flush(self) -> None:
pass


Self = TypeVar("Self")

0 comments on commit 08f65fd

Please sign in to comment.