Skip to content

Commit 10469d0

Browse files
committed
Merging
2 parents 4f1efba + c66f275 commit 10469d0

18 files changed

Lines changed: 162 additions & 115 deletions

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,4 +341,4 @@
341341

342342

343343
# Example configuration for intersphinx: refer to the Python standard library.
344-
intersphinx_mapping = {'http://docs.python.org/': None}
344+
intersphinx_mapping = {'python': ('https://docs.python.org/3', None)}

progressbar/bar.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
from python_utils import converters, types
1717

18-
import progressbar.terminal
1918
import progressbar.env
19+
import progressbar.terminal
2020
import progressbar.terminal.stream
2121

2222
from . import (
@@ -30,8 +30,9 @@
3030

3131
# float also accepts integers and longs but we don't want an explicit union
3232
# due to type checking complexity
33-
T = float
33+
NumberT = float
3434

35+
T = types.TypeVar('T')
3536

3637
class ProgressBarMixinBase(abc.ABC):
3738
_started = False
@@ -76,14 +77,14 @@ class ProgressBarMixinBase(abc.ABC):
7677
next_update: int = 0
7778

7879
#: Current progress (min_value <= value <= max_value)
79-
value: T
80+
value: NumberT
8081
#: Previous progress value
81-
previous_value: types.Optional[T]
82+
previous_value: types.Optional[NumberT]
8283
#: The minimum/start value for the progress bar
83-
min_value: T
84+
min_value: NumberT
8485
#: Maximum (and final) value. Beyond this value an error will be raised
8586
#: unless the `max_error` parameter is `False`.
86-
max_value: T | types.Type[base.UnknownLength]
87+
max_value: NumberT | types.Type[base.UnknownLength]
8788
#: The time the progressbar reached `max_value` or when `finish()` was
8889
#: called.
8990
end_time: types.Optional[datetime]
@@ -156,7 +157,7 @@ def __repr__(self):
156157

157158
class DefaultFdMixin(ProgressBarMixinBase):
158159
# The file descriptor to write to. Defaults to `sys.stderr`
159-
fd: base.IO = sys.stderr
160+
fd: base.TextIO = sys.stderr
160161
#: Set the terminal to be ANSI compatible. If a terminal is ANSI
161162
#: compatible we will automatically enable `colors` and disable
162163
#: `line_breaks`.
@@ -177,11 +178,13 @@ class DefaultFdMixin(ProgressBarMixinBase):
177178
#: For true (24 bit/16M) color support you can use `COLORTERM=truecolor`.
178179
#: For 256 color support you can use `TERM=xterm-256color`.
179180
#: For 16 colorsupport you can use `TERM=xterm`.
180-
enable_colors: progressbar.env.ColorSupport | bool | None = progressbar.env.COLOR_SUPPORT
181+
enable_colors: progressbar.env.ColorSupport | bool | None = (
182+
progressbar.env.COLOR_SUPPORT
183+
)
181184

182185
def __init__(
183186
self,
184-
fd: base.IO = sys.stderr,
187+
fd: base.IO[str] = sys.stderr,
185188
is_terminal: bool | None = None,
186189
line_breaks: bool | None = None,
187190
enable_colors: progressbar.env.ColorSupport | None = None,
@@ -202,7 +205,7 @@ def __init__(
202205

203206
super().__init__(**kwargs)
204207

205-
def _apply_line_offset(self, fd: base.IO, line_offset: int) -> base.IO:
208+
def _apply_line_offset(self, fd: base.IO[T], line_offset: int) -> base.IO[T]:
206209
if line_offset:
207210
return progressbar.terminal.stream.LineOffsetStreamWrapper(
208211
line_offset,
@@ -213,7 +216,7 @@ def _apply_line_offset(self, fd: base.IO, line_offset: int) -> base.IO:
213216

214217
def _determine_is_terminal(
215218
self,
216-
fd: base.IO,
219+
fd: base.TextIO,
217220
is_terminal: bool | None,
218221
) -> bool:
219222
if is_terminal is not None:
@@ -257,8 +260,7 @@ def _determine_enable_colors(
257260
enable_colors = progressbar.env.ColorSupport.XTERM_256
258261
elif enable_colors is False:
259262
enable_colors = progressbar.env.ColorSupport.NONE
260-
elif not isinstance(enable_colors,
261-
progressbar.env.ColorSupport):
263+
elif not isinstance(enable_colors, progressbar.env.ColorSupport):
262264
raise ValueError(f'Invalid color support value: {enable_colors}')
263265

264266
return enable_colors
@@ -281,7 +283,9 @@ def update(self, *args: types.Any, **kwargs: types.Any) -> None:
281283
self.fd.write(line.encode('ascii', 'replace'))
282284

283285
def finish(
284-
self, *args: types.Any, **kwargs: types.Any,
286+
self,
287+
*args: types.Any,
288+
**kwargs: types.Any,
285289
) -> None: # pragma: no cover
286290
if self._finished:
287291
return
@@ -516,13 +520,13 @@ class ProgressBar(
516520

517521
def __init__(
518522
self,
519-
min_value: T = 0,
520-
max_value: T | types.Type[base.UnknownLength] | None = None,
523+
min_value: NumberT = 0,
524+
max_value: NumberT | types.Type[base.UnknownLength] | None = None,
521525
widgets: types.Optional[
522526
types.Sequence[widgets_module.WidgetBase | str]
523527
] = None,
524528
left_justify: bool = True,
525-
initial_value: T = 0,
529+
initial_value: NumberT = 0,
526530
poll_interval: types.Optional[float] = None,
527531
widget_kwargs: types.Optional[types.Dict[str, types.Any]] = None,
528532
custom_len: types.Callable[[str], int] = utils.len_color,
@@ -555,7 +559,7 @@ def __init__(
555559
)
556560
poll_interval = kwargs.get('poll')
557561

558-
if max_value and min_value > types.cast(T, max_value):
562+
if max_value and min_value > types.cast(NumberT, max_value):
559563
raise ValueError(
560564
'Max value needs to be bigger than the min value',
561565
)
@@ -996,7 +1000,7 @@ def _verify_max_value(self):
9961000
):
9971001
raise ValueError('max_value out of range, got %r' % self.max_value)
9981002

999-
def _calculate_poll_interval(self):
1003+
def _calculate_poll_interval(self) -> None:
10001004
self.num_intervals = max(100, self.term_width)
10011005
for widget in self.widgets:
10021006
interval: int | float | None = utils.deltas_to_seconds(

progressbar/env.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def from_env(cls):
6565
)
6666

6767
if os.environ.get('JUPYTER_COLUMNS') or os.environ.get(
68-
'JUPYTER_LINES',
68+
'JUPYTER_LINES',
6969
):
7070
# Jupyter notebook always supports true color.
7171
return cls.XTERM_TRUECOLOR
@@ -88,8 +88,8 @@ def from_env(cls):
8888

8989

9090
def is_ansi_terminal(
91-
fd: base.IO,
92-
is_terminal: bool | None = None,
91+
fd: base.IO,
92+
is_terminal: bool | None = None,
9393
) -> bool: # pragma: no cover
9494
if is_terminal is None:
9595
# Jupyter Notebooks define this variable and support progress bars
@@ -98,7 +98,7 @@ def is_ansi_terminal(
9898
# This works for newer versions of pycharm only. With older versions
9999
# there is no way to check.
100100
elif os.environ.get('PYCHARM_HOSTED') == '1' and not os.environ.get(
101-
'PYTEST_CURRENT_TEST',
101+
'PYTEST_CURRENT_TEST',
102102
):
103103
is_terminal = True
104104

progressbar/multi.py

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,22 @@ class MultiBar(typing.Dict[str, bar.ProgressBar]):
7575
_thread_closed: threading.Event = threading.Event()
7676

7777
def __init__(
78-
self,
79-
bars: typing.Iterable[tuple[str, bar.ProgressBar]] | None = None,
80-
fd=sys.stderr,
81-
prepend_label: bool = True,
82-
append_label: bool = False,
83-
label_format='{label:20.20} ',
84-
initial_format: str | None = '{label:20.20} Not yet started',
85-
finished_format: str | None = None,
86-
update_interval: float = 1 / 60.0, # 60fps
87-
show_initial: bool = True,
88-
show_finished: bool = True,
89-
remove_finished: timedelta | float = timedelta(seconds=3600),
90-
sort_key: str | SortKey = SortKey.CREATED,
91-
sort_reverse: bool = True,
92-
sort_keyfunc: SortKeyFunc | None = None,
93-
**progressbar_kwargs,
78+
self,
79+
bars: typing.Iterable[tuple[str, bar.ProgressBar]] | None = None,
80+
fd: typing.TextIO = sys.stderr,
81+
prepend_label: bool = True,
82+
append_label: bool = False,
83+
label_format='{label:20.20} ',
84+
initial_format: str | None = '{label:20.20} Not yet started',
85+
finished_format: str | None = None,
86+
update_interval: float = 1 / 60.0, # 60fps
87+
show_initial: bool = True,
88+
show_finished: bool = True,
89+
remove_finished: timedelta | float = timedelta(seconds=3600),
90+
sort_key: str | SortKey = SortKey.CREATED,
91+
sort_reverse: bool = True,
92+
sort_keyfunc: SortKeyFunc | None = None,
93+
**progressbar_kwargs,
9494
):
9595
self.fd = fd
9696

@@ -130,7 +130,7 @@ def __setitem__(self, key: str, bar: bar.ProgressBar):
130130
bar.fd = stream.LastLineStream(self.fd)
131131
bar.paused = True
132132
# Essentially `bar.print = self.print`, but `mypy` doesn't like that
133-
bar.print = self.print
133+
bar.print = self.print # type: ignore
134134

135135
# Just in case someone is using a progressbar with a custom
136136
# constructor and forgot to call the super constructor
@@ -186,7 +186,9 @@ def render(self, flush: bool = True, force: bool = False):
186186
with self._print_lock:
187187
# Clear the previous output if progressbars have been removed
188188
for i in range(len(output), len(self._previous_output)):
189-
self._buffer.write(terminal.clear_line(i + 1)) # pragma: no cover
189+
self._buffer.write(
190+
terminal.clear_line(i + 1),
191+
) # pragma: no cover
190192

191193
# Add empty lines to the end of the output if progressbars have
192194
# been added
@@ -195,13 +197,13 @@ def render(self, flush: bool = True, force: bool = False):
195197
self._buffer.write('\n')
196198

197199
for i, (previous, current) in enumerate(
198-
itertools.zip_longest(
199-
self._previous_output,
200-
output,
201-
fillvalue='',
202-
),
200+
itertools.zip_longest(
201+
self._previous_output,
202+
output,
203+
fillvalue='',
204+
),
203205
):
204-
if previous != current or force: # pragma: no branch
206+
if previous != current or force: # pragma: no branch
205207
self.print(
206208
'\r' + current.strip(),
207209
offset=i + 1,
@@ -212,11 +214,14 @@ def render(self, flush: bool = True, force: bool = False):
212214

213215
self._previous_output = output
214216

215-
if flush: # pragma: no branch
217+
if flush: # pragma: no branch
216218
self.flush()
217219

218220
def _render_bar(
219-
self, bar_: bar.ProgressBar, now, expired,
221+
self,
222+
bar_: bar.ProgressBar,
223+
now,
224+
expired,
220225
) -> typing.Iterable[str]:
221226
def update(force=True, write=True):
222227
self._label_bar(bar_)
@@ -237,21 +242,21 @@ def update(force=True, write=True):
237242
yield self.initial_format.format(label=bar_.label)
238243

239244
def _render_finished_bar(
240-
self,
241-
bar_: bar.ProgressBar,
242-
now,
243-
expired,
244-
update,
245+
self,
246+
bar_: bar.ProgressBar,
247+
now,
248+
expired,
249+
update,
245250
) -> typing.Iterable[str]:
246251
if bar_ not in self._finished_at:
247252
self._finished_at[bar_] = now
248253
# Force update to get the finished format
249254
update(write=False)
250255

251256
if (
252-
self.remove_finished
253-
and expired is not None
254-
and expired >= self._finished_at[bar_]
257+
self.remove_finished
258+
and expired is not None
259+
and expired >= self._finished_at[bar_]
255260
):
256261
del self[bar_.label]
257262
return
@@ -266,13 +271,13 @@ def _render_finished_bar(
266271
yield self.finished_format.format(label=bar_.label)
267272

268273
def print(
269-
self,
270-
*args,
271-
end='\n',
272-
offset=None,
273-
flush=True,
274-
clear=True,
275-
**kwargs,
274+
self,
275+
*args,
276+
end='\n',
277+
offset=None,
278+
flush=True,
279+
clear=True,
280+
**kwargs,
276281
):
277282
'''
278283
Print to the progressbar stream without overwriting the progressbars.

progressbar/terminal/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from __future__ import annotations
22

33
from .base import * # noqa F403
4-
from .stream import TextIOOutputWrapper, LineOffsetStreamWrapper, LastLineStream
4+
from .stream import * # noqa F403

progressbar/terminal/base.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212

1313
from python_utils import converters, types
1414

15-
from .. import base as pbase, env
15+
from .. import (
16+
base as pbase,
17+
env,
18+
)
1619
from .os_specific import getch
1720

1821
ESC = '\x1B'
@@ -218,14 +221,19 @@ class HSL(collections.namedtuple('HSL', ['hue', 'saturation', 'lightness'])):
218221
and 100(%).
219222
220223
'''
224+
221225
__slots__ = ()
222226

223227
@classmethod
224228
def from_rgb(cls, rgb: RGB) -> HSL:
225229
'''
226230
Convert a 0-255 RGB color to a 0-255 HLS color.
227231
'''
228-
hls = colorsys.rgb_to_hls(rgb.red/255,rgb.green/255,rgb.blue/255)
232+
hls = colorsys.rgb_to_hls(
233+
rgb.red / 255,
234+
rgb.green / 255,
235+
rgb.blue / 255,
236+
)
229237
return cls(
230238
round(hls[0] * 360),
231239
round(hls[2] * 100),
@@ -288,12 +296,16 @@ def underline(self):
288296

289297
@property
290298
def ansi(self) -> types.Optional[str]:
291-
if env.COLOR_SUPPORT is env.ColorSupport.XTERM_TRUECOLOR: # pragma: no branch
299+
if (
300+
env.COLOR_SUPPORT is env.ColorSupport.XTERM_TRUECOLOR
301+
): # pragma: no branch
292302
return f'2;{self.rgb.red};{self.rgb.green};{self.rgb.blue}'
293303

294304
if self.xterm: # pragma: no branch
295305
color = self.xterm
296-
elif env.COLOR_SUPPORT is env.ColorSupport.XTERM_256: # pragma: no branch
306+
elif (
307+
env.COLOR_SUPPORT is env.ColorSupport.XTERM_256
308+
): # pragma: no branch
297309
color = self.rgb.to_ansi_256
298310
elif env.COLOR_SUPPORT is env.ColorSupport.XTERM: # pragma: no branch
299311
color = self.rgb.to_ansi_16
@@ -395,7 +407,8 @@ def get_color(self, value: float) -> Color:
395407
elif self.interpolate:
396408
if max_color_idx > 1:
397409
index = round(
398-
converters.remap(value, 0, 1, 0, max_color_idx - 1))
410+
converters.remap(value, 0, 1, 0, max_color_idx - 1),
411+
)
399412
else:
400413
index = 0
401414

0 commit comments

Comments
 (0)