Skip to content

Commit 3cb06e5

Browse files
committed
All type issues finally fixed? Maybe?
1 parent 9bda71d commit 3cb06e5

4 files changed

Lines changed: 50 additions & 34 deletions

File tree

progressbar/bar.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ class ProgressBarMixinBase(abc.ABC):
5151
#: are configured
5252
widget_kwargs: types.Dict[str, types.Any]
5353
#: Custom length function for multibyte characters such as CJK
54-
# custom_len: types.Callable[[str], int]
55-
custom_len: types.ClassVar[
56-
types.Callable[['ProgressBarMixinBase', str], int]
57-
]
54+
# mypy and pyright can't agree on what the correct one is... so we'll
55+
# need to use a helper function :(
56+
# custom_len: types.Callable[['ProgressBarMixinBase', str], int]
57+
custom_len: types.Callable[[str], int]
5858
#: The time the progress bar was started
5959
initial_start_time: types.Optional[datetime]
6060
#: The interval to poll for updates in seconds if there are updates
@@ -188,7 +188,7 @@ def __init__(
188188
def update(self, *args, **kwargs):
189189
ProgressBarMixinBase.update(self, *args, **kwargs)
190190

191-
line = converters.to_unicode(self._format_line())
191+
line: str = converters.to_unicode(self._format_line())
192192
if not self.enable_colors:
193193
line = utils.no_color(line)
194194

@@ -240,11 +240,11 @@ def _format_widgets(self):
240240
expanding.insert(0, index)
241241
elif isinstance(widget, str):
242242
result.append(widget)
243-
width -= self.custom_len(widget)
243+
width -= self.custom_len(widget) # type: ignore
244244
else:
245245
widget_output = converters.to_unicode(widget(self, data))
246246
result.append(widget_output)
247-
width -= self.custom_len(widget_output)
247+
width -= self.custom_len(widget_output) # type: ignore
248248

249249
count = len(expanding)
250250
while expanding:
@@ -254,7 +254,7 @@ def _format_widgets(self):
254254
count -= 1
255255

256256
widget_output = widget(self, data, portion)
257-
width -= self.custom_len(widget_output)
257+
width -= self.custom_len(widget_output) # type: ignore
258258
result[index] = widget_output
259259

260260
return result
@@ -303,8 +303,8 @@ def finish(self): # pragma: no cover
303303
class StdRedirectMixin(DefaultFdMixin):
304304
redirect_stderr: bool = False
305305
redirect_stdout: bool = False
306-
stdout: base.IO
307-
stderr: base.IO
306+
stdout: utils.WrappingIO | base.IO
307+
stderr: utils.WrappingIO | base.IO
308308
_stdout: base.IO
309309
_stderr: base.IO
310310

@@ -428,7 +428,7 @@ class ProgressBar(
428428
you from changing the ProgressBar you should treat it as read only.
429429
'''
430430

431-
_iterable: types.Optional[types.Iterable]
431+
_iterable: types.Optional[types.Iterator]
432432

433433
_DEFAULT_MAXVAL: Type[base.UnknownLength] = base.UnknownLength
434434
# update every 50 milliseconds (up to a 20 times per second)
@@ -439,11 +439,11 @@ def __init__(
439439
self,
440440
min_value: T = 0,
441441
max_value: T | types.Type[base.UnknownLength] | None = None,
442-
widgets: types.List[widgets_module.WidgetBase] = None,
442+
widgets: types.Optional[types.List[widgets_module.WidgetBase]] = None,
443443
left_justify: bool = True,
444444
initial_value: T = 0,
445445
poll_interval: types.Optional[float] = None,
446-
widget_kwargs: types.Dict[str, types.Any] = None,
446+
widget_kwargs: types.Optional[types.Dict[str, types.Any]] = None,
447447
custom_len: types.Callable[[str], int] = utils.len_color,
448448
max_error=True,
449449
prefix=None,
@@ -594,7 +594,7 @@ def percentage(self):
594594
return None
595595
elif self.max_value:
596596
todo = self.value - self.min_value
597-
total = self.max_value - self.min_value
597+
total = self.max_value - self.min_value # type: ignore
598598
percentage = 100.0 * todo / total
599599
else:
600600
percentage = 100.0
@@ -631,7 +631,7 @@ def data(self) -> types.Dict[str, types.Any]:
631631
'''
632632
self._last_update_time = time.time()
633633
self._last_update_timer = timeit.default_timer()
634-
elapsed = self.last_update_time - self.start_time
634+
elapsed = self.last_update_time - self.start_time # type: ignore
635635
# For Python 2.7 and higher we have _`timedelta.total_seconds`, but we
636636
# want to support older versions as well
637637
total_seconds_elapsed = utils.deltas_to_seconds(elapsed)
@@ -717,11 +717,16 @@ def __iter__(self):
717717

718718
def __next__(self):
719719
try:
720-
value = next(self._iterable)
720+
if self._iterable is None: # pragma: no cover
721+
value = self.value
722+
else:
723+
value = next(self._iterable)
724+
721725
if self.start_time is None:
722726
self.start()
723727
else:
724728
self.update(self.value + 1)
729+
725730
return value
726731
except StopIteration:
727732
self.finish()

progressbar/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Undefined(metaclass=FalseMeta):
2020
pass
2121

2222

23-
try:
23+
try: # pragma: no cover
2424
IO = types.IO # type: ignore
2525
TextIO = types.TextIO # type: ignore
2626
except AttributeError:

progressbar/utils.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
assert scale_1024
2727
assert epoch
2828

29+
StringT = types.TypeVar('StringT', bound=types.StringTypes)
30+
2931
ANSI_TERMS = (
3032
'([xe]|bv)term',
3133
'(sco)?ansi',
@@ -141,7 +143,7 @@ def deltas_to_seconds(
141143
return default # type: ignore
142144

143145

144-
def no_color(value: types.StringTypes) -> types.StringTypes:
146+
def no_color(value: StringT) -> StringT:
145147
'''
146148
Return the `value` without ANSI escape codes
147149
@@ -153,9 +155,10 @@ def no_color(value: types.StringTypes) -> types.StringTypes:
153155
'abc'
154156
'''
155157
if isinstance(value, bytes):
156-
return re.sub('\\\u001b\\[.*?[@-~]'.encode(), b'', value)
158+
pattern: bytes = '\\\u001b\\[.*?[@-~]'.encode()
159+
return re.sub(pattern, b'', value) # type: ignore
157160
else:
158-
return re.sub(u'\x1b\\[.*?[@-~]', '', value)
161+
return re.sub(u'\x1b\\[.*?[@-~]', '', value) # type: ignore
159162

160163

161164
def len_color(value: types.StringTypes) -> int:
@@ -199,7 +202,7 @@ def __init__(
199202
self,
200203
target: base.IO,
201204
capturing: bool = False,
202-
listeners: types.Set[ProgressBar] = None,
205+
listeners: types.Optional[types.Set[ProgressBar]] = None,
203206
) -> None:
204207
self.buffer = io.StringIO()
205208
self.target = target
@@ -306,12 +309,17 @@ class StreamWrapper:
306309
stderr: base.TextIO | WrappingIO
307310
original_excepthook: types.Callable[
308311
[
309-
types.Optional[types.Type[BaseException]],
310-
types.Optional[BaseException],
311-
types.Optional[TracebackType],
312+
types.Type[BaseException],
313+
BaseException,
314+
TracebackType | None,
312315
],
313316
None,
314317
]
318+
# original_excepthook: types.Callable[
319+
# [
320+
# types.Type[BaseException],
321+
# BaseException, TracebackType | None,
322+
# ], None] | None
315323
wrapped_stdout: int = 0
316324
wrapped_stderr: int = 0
317325
wrapped_excepthook: int = 0
@@ -368,7 +376,7 @@ def wrap(self, stdout: bool = False, stderr: bool = False) -> None:
368376
if stderr:
369377
self.wrap_stderr()
370378

371-
def wrap_stdout(self) -> base.IO:
379+
def wrap_stdout(self) -> WrappingIO:
372380
self.wrap_excepthook()
373381

374382
if not self.wrapped_stdout:
@@ -377,9 +385,9 @@ def wrap_stdout(self) -> base.IO:
377385
)
378386
self.wrapped_stdout += 1
379387

380-
return sys.stdout
388+
return sys.stdout # type: ignore
381389

382-
def wrap_stderr(self) -> base.IO:
390+
def wrap_stderr(self) -> WrappingIO:
383391
self.wrap_excepthook()
384392

385393
if not self.wrapped_stderr:
@@ -388,7 +396,7 @@ def wrap_stderr(self) -> base.IO:
388396
)
389397
self.wrapped_stderr += 1
390398

391-
return sys.stderr
399+
return sys.stderr # type: ignore
392400

393401
def unwrap_excepthook(self) -> None:
394402
if self.wrapped_excepthook:

progressbar/widgets.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def __call__(
132132
progress: ProgressBarMixinBase,
133133
data: Data,
134134
format: types.Optional[str] = None,
135-
):
135+
) -> str:
136136
'''Formats the widget into a string'''
137137
format = self.get_format(progress, data, format)
138138
try:
@@ -216,7 +216,7 @@ class WidgetBase(WidthWidgetMixin, metaclass=abc.ABCMeta):
216216
copy = True
217217

218218
@abc.abstractmethod
219-
def __call__(self, progress: ProgressBarMixinBase, data: Data):
219+
def __call__(self, progress: ProgressBarMixinBase, data: Data) -> str:
220220
'''Updates the widget.
221221
222222
progress - a reference to the calling ProgressBar
@@ -237,7 +237,7 @@ def __call__(
237237
progress: ProgressBarMixinBase,
238238
data: Data,
239239
width: int = 0,
240-
):
240+
) -> str:
241241
'''Updates the widget providing the total width the widget must fill.
242242
243243
progress - a reference to the calling ProgressBar
@@ -702,7 +702,9 @@ def __call__(self, progress: ProgressBarMixinBase, data: Data, width=None):
702702
if self.fill:
703703
# Cut the last character so we can replace it with our marker
704704
fill = self.fill(
705-
progress, data, width - progress.custom_len(marker)
705+
progress,
706+
data,
707+
width - progress.custom_len(marker), # type: ignore
706708
)
707709
else:
708710
fill = ''
@@ -767,7 +769,8 @@ class SimpleProgress(FormatWidgetMixin, WidgetBase):
767769
def __init__(self, format=DEFAULT_FORMAT, **kwargs):
768770
FormatWidgetMixin.__init__(self, format=format, **kwargs)
769771
WidgetBase.__init__(self, format=format, **kwargs)
770-
self.max_width_cache = dict(default=self.max_width)
772+
self.max_width_cache = dict()
773+
self.max_width_cache['default'] = self.max_width or 0
771774

772775
def __call__(
773776
self, progress: ProgressBarMixinBase, data: Data, format=None
@@ -950,7 +953,7 @@ class FormatCustomText(FormatWidgetMixin, WidgetBase):
950953
def __init__(
951954
self,
952955
format: str,
953-
mapping: types.Dict[str, types.Any] = None,
956+
mapping: types.Optional[types.Dict[str, types.Any]] = None,
954957
**kwargs,
955958
):
956959
self.format = format

0 commit comments

Comments
 (0)