1515
1616from python_utils import converters , types
1717
18- import progressbar .terminal
1918import progressbar .env
19+ import progressbar .terminal
2020import progressbar .terminal .stream
2121
2222from . import (
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
3637class 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
157158class 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 (
0 commit comments