Skip to content

Commit 3edbeb0

Browse files
committed
Added python 3.7 type hinting compatibility
1 parent b287602 commit 3edbeb0

4 files changed

Lines changed: 28 additions & 17 deletions

File tree

progressbar/bar.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class ProgressBarBase(types.Iterable, ProgressBarMixinBase):
133133

134134
class DefaultFdMixin(ProgressBarMixinBase):
135135
# The file descriptor to write to. Defaults to `sys.stderr`
136-
fd: types.IO = sys.stderr
136+
fd: base.IO = sys.stderr
137137
#: Set the terminal to be ANSI compatible. If a terminal is ANSI
138138
#: compatible we will automatically enable `colors` and disable
139139
#: `line_breaks`.
@@ -146,7 +146,7 @@ class DefaultFdMixin(ProgressBarMixinBase):
146146

147147
def __init__(
148148
self,
149-
fd: types.IO = sys.stderr,
149+
fd: base.IO = sys.stderr,
150150
is_terminal: bool | None = None,
151151
line_breaks: bool | None = None,
152152
enable_colors: bool | None = None,
@@ -303,10 +303,10 @@ def finish(self): # pragma: no cover
303303
class StdRedirectMixin(DefaultFdMixin):
304304
redirect_stderr: bool = False
305305
redirect_stdout: bool = False
306-
stdout: types.IO
307-
stderr: types.IO
308-
_stdout: types.IO
309-
_stderr: types.IO
306+
stdout: base.IO
307+
stderr: base.IO
308+
_stdout: base.IO
309+
_stderr: base.IO
310310

311311
def __init__(
312312
self,

progressbar/base.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# -*- mode: python; coding: utf-8 -*-
2+
from python_utils import types
23

34

45
class FalseMeta(type):
@@ -17,3 +18,10 @@ class UnknownLength(metaclass=FalseMeta):
1718

1819
class Undefined(metaclass=FalseMeta):
1920
pass
21+
22+
23+
try:
24+
IO = types.IO # type: ignore
25+
TextIO = types.TextIO # type: ignore
26+
except AttributeError:
27+
from typing.io import IO # type: ignore

progressbar/utils.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
from python_utils.terminal import get_terminal_size
1616
from python_utils.time import epoch, format_time, timedelta_to_seconds
1717

18+
from progressbar import base
19+
1820
if types.TYPE_CHECKING:
1921
from .bar import ProgressBar, ProgressBarMixinBase
2022

@@ -39,7 +41,7 @@
3941

4042

4143
def is_ansi_terminal(
42-
fd: types.IO, is_terminal: bool | None = None
44+
fd: base.IO, is_terminal: bool | None = None
4345
) -> bool: # pragma: no cover
4446
if is_terminal is None:
4547
# Jupyter Notebooks define this variable and support progress bars
@@ -74,7 +76,7 @@ def is_ansi_terminal(
7476
return bool(is_terminal)
7577

7678

77-
def is_terminal(fd: types.IO, is_terminal: bool | None = None) -> bool:
79+
def is_terminal(fd: base.IO, is_terminal: bool | None = None) -> bool:
7880
if is_terminal is None:
7981
# Full ansi support encompasses what we expect from a terminal
8082
is_terminal = is_ansi_terminal(fd) or None
@@ -188,14 +190,14 @@ def env_flag(name: str, default: bool | None = None) -> bool | None:
188190

189191
class WrappingIO:
190192
buffer: io.StringIO
191-
target: types.IO
193+
target: base.IO
192194
capturing: bool
193195
listeners: set
194196
needs_clear: bool = False
195197

196198
def __init__(
197199
self,
198-
target: types.IO,
200+
target: base.IO,
199201
capturing: bool = False,
200202
listeners: types.Set[ProgressBar] = None,
201203
) -> None:
@@ -300,8 +302,8 @@ def __exit__(
300302
class StreamWrapper:
301303
'''Wrap stdout and stderr globally'''
302304

303-
stdout: types.TextIO | WrappingIO
304-
stderr: types.TextIO | WrappingIO
305+
stdout: base.TextIO | WrappingIO
306+
stderr: base.TextIO | WrappingIO
305307
original_excepthook: types.Callable[
306308
[
307309
types.Optional[types.Type[BaseException]],
@@ -366,7 +368,7 @@ def wrap(self, stdout: bool = False, stderr: bool = False) -> None:
366368
if stderr:
367369
self.wrap_stderr()
368370

369-
def wrap_stdout(self) -> types.IO:
371+
def wrap_stdout(self) -> base.IO:
370372
self.wrap_excepthook()
371373

372374
if not self.wrapped_stdout:
@@ -377,7 +379,7 @@ def wrap_stdout(self) -> types.IO:
377379

378380
return sys.stdout
379381

380-
def wrap_stderr(self) -> types.IO:
382+
def wrap_stderr(self) -> base.IO:
381383
self.wrap_excepthook()
382384

383385
if not self.wrapped_stderr:

progressbar/widgets.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,10 @@ class WidgetBase(WidthWidgetMixin, metaclass=abc.ABCMeta):
207207
- max_width: Only display the widget if at most `max_width` is left
208208
- weight: Widgets with a higher `weigth` will be calculated before widgets
209209
with a lower one
210-
- copy: Copy this widget when initializing the progress bar so the
211-
progressbar can be reused. Some widgets such as the FormatCustomText
212-
require the shared state so this needs to be optional
210+
- copy: Copy this widget when initializing the progress bar so the
211+
progressbar can be reused. Some widgets such as the FormatCustomText
212+
require the shared state so this needs to be optional
213+
213214
'''
214215

215216
copy = True

0 commit comments

Comments
 (0)