Skip to content

Commit 6eccf18

Browse files
committed
Improve signatures for overtype, printr, printt and stderr_writer
1 parent 0f5f7bd commit 6eccf18

File tree

2 files changed

+79
-15
lines changed

2 files changed

+79
-15
lines changed

domdf_python_tools/terminal.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
import pprint
7070
import textwrap
7171
from shutil import get_terminal_size
72-
from typing import IO
72+
from typing import IO, Optional
7373

7474
# this package
7575
from domdf_python_tools.words import CR
@@ -124,7 +124,13 @@ def interrupt() -> None:
124124
print(f"(Press Ctrl-{'C' if os.name == 'nt' else 'D'} to quit at any time)")
125125

126126

127-
def overtype(*objects, sep: str = ' ', end: str = '', file: IO = None, flush: bool = False) -> None:
127+
def overtype(
128+
*objects,
129+
sep: str = ' ',
130+
end: str = '',
131+
file: Optional[IO] = None,
132+
flush: bool = False,
133+
) -> None:
128134
r"""
129135
Print ``*objects`` to the text stream ``file``, starting with ``'\\r'``, separated by ``sep``
130136
and followed by ``end``.

domdf_python_tools/utils.py

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
from pprint import pformat
7575
from types import MethodType
7676
from typing import (
77+
IO,
7778
TYPE_CHECKING,
7879
Any,
7980
Callable,
@@ -161,30 +162,87 @@ def list2str(the_list: Iterable[Any], sep: str = ',') -> str:
161162
return sep.join([str(x) for x in the_list])
162163

163164

164-
def printr(obj: Any, *args, **kwargs) -> None:
165-
"""
165+
def printr(
166+
obj: Any,
167+
*values: object,
168+
sep: Optional[str] = ' ',
169+
end: Optional[str] = '\n',
170+
file: Optional[IO] = None,
171+
flush: bool = False,
172+
) -> None:
173+
r"""
166174
Print the :func:`repr` of an object.
167-
"""
168175
169-
print(repr(obj), *args, **kwargs)
176+
If no objects are given, :func:`~.printr` will just write ``end``.
170177
171-
172-
def printt(obj: Any, *args, **kwargs) -> None:
173-
"""
178+
:param obj:
179+
:param \*values: Additional values to print. These are printed verbatim.
180+
:param sep: The separator between values.
181+
:param end: The final value to print.
182+
Setting to ``''`` will leave the insertion point at the end of the printed text.
183+
:param file: The file to write to.
184+
If not present or :py:obj:`None`, :py:obj:`sys.stdout` will be used.
185+
:no-default file:
186+
:param flush: If :py:obj:`True` the stream is forcibly flushed after printing.
187+
"""
188+
189+
print(repr(obj), *values, sep=sep, end=end, file=file, flush=flush)
190+
191+
192+
def printt(
193+
obj: Any,
194+
*values: object,
195+
sep: Optional[str] = ' ',
196+
end: Optional[str] = '\n',
197+
file: Optional[IO] = None,
198+
flush: bool = False,
199+
) -> None:
200+
r"""
174201
Print the type of an object.
202+
203+
If no objects are given, :func:`~.printt` will just write ``end``.
204+
205+
:param obj:
206+
:param \*values: Additional values to print. These are printed verbatim.
207+
:param sep: The separator between values.
208+
:param end: The final value to print.
209+
Setting to ``''`` will leave the insertion point at the end of the printed text.
210+
:param file: The file to write to.
211+
If not present or :py:obj:`None`, :py:obj:`sys.stdout` will be used.
212+
:no-default file:
213+
:param flush: If :py:obj:`True` the stream is forcibly flushed after printing.
175214
"""
176215

177-
print(type(obj), *args, **kwargs)
216+
print(type(obj), *values, sep=sep, end=end, file=file, flush=flush)
178217

179218

180-
def stderr_writer(*args, **kwargs) -> None:
181-
"""
182-
Write to stderr, flushing stdout beforehand and stderr afterwards.
219+
def stderr_writer(
220+
*values: object,
221+
sep: Optional[str] = ' ',
222+
end: Optional[str] = '\n',
223+
) -> None:
224+
r"""
225+
Print ``*values`` to :py:obj:`sys.stderr`, separated by ``sep`` and followed by ``end``.
226+
227+
:py:obj:`sys.stdout` is flushed before printing, and :py:obj:`sys.stderr` is flushed afterwards.
228+
229+
If no objects are given, :func:`~.stderr_writer` will just write ``end``.
230+
231+
:param \*values:
232+
:param sep: The separator between values.
233+
:param end: The final value to print.
234+
Setting to ``''`` will leave the insertion point at the end of the printed text.
235+
236+
:rtype:
237+
238+
.. versionchanged:: 3.0.0
239+
240+
The only permitted keyword arguments are ``sep`` and ``end``.
241+
Previous versions allowed other keywords arguments supported by :func:`print` but they had no effect.
183242
"""
184243

185244
sys.stdout.flush()
186-
kwargs["file"] = sys.stderr
187-
print(*args, **kwargs)
245+
print(*values, sep=sep, end=end, file=sys.stderr, flush=True)
188246
sys.stderr.flush()
189247

190248

0 commit comments

Comments
 (0)