Skip to content

Commit 0b3472c

Browse files
committed
specialize progressbar(length=...) as ProgressBar[int]
1 parent ca5e1c3 commit 0b3472c

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

CHANGES.rst

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Unreleased
3131
- When generating a command's name from a decorated function's name, the
3232
suffixes ``_command``, ``_cmd``, ``_group``, and ``_grp`` are removed.
3333
:issue:`2322`
34+
- Specialized typing of ``progressbar(length=...)`` as ``ProgressBar[int]``.
35+
:pr:`2630`
3436

3537

3638
Version 8.1.7

src/click/termui.py

+40
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,46 @@ def echo_via_pager(
284284
return pager(itertools.chain(text_generator, "\n"), color)
285285

286286

287+
@t.overload
288+
def progressbar(
289+
*,
290+
length: int,
291+
label: str | None = None,
292+
show_eta: bool = True,
293+
show_percent: bool | None = None,
294+
show_pos: bool = False,
295+
fill_char: str = "#",
296+
empty_char: str = "-",
297+
bar_template: str = "%(label)s [%(bar)s] %(info)s",
298+
info_sep: str = " ",
299+
width: int = 36,
300+
color: bool | None = None,
301+
update_min_steps: int = 1,
302+
) -> ProgressBar[int]:
303+
...
304+
305+
306+
@t.overload
307+
def progressbar(
308+
iterable: cabc.Iterable[V] | None = None,
309+
length: int | None = None,
310+
label: str | None = None,
311+
show_eta: bool = True,
312+
show_percent: bool | None = None,
313+
show_pos: bool = False,
314+
item_show_func: t.Callable[[V | None], str | None] | None = None,
315+
fill_char: str = "#",
316+
empty_char: str = "-",
317+
bar_template: str = "%(label)s [%(bar)s] %(info)s",
318+
info_sep: str = " ",
319+
width: int = 36,
320+
file: t.TextIO | None = None,
321+
color: bool | None = None,
322+
update_min_steps: int = 1,
323+
) -> ProgressBar[V]:
324+
...
325+
326+
287327
def progressbar(
288328
iterable: cabc.Iterable[V] | None = None,
289329
length: int | None = None,

tests/typing/typing_progressbar.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing_extensions import assert_type
2+
3+
from click import progressbar
4+
from click._termui_impl import ProgressBar
5+
6+
7+
def test_length_is_int() -> None:
8+
with progressbar(length=5) as bar:
9+
assert_type(bar, ProgressBar[int])
10+
for i in bar:
11+
assert_type(i, int)
12+
13+
14+
def it() -> tuple[str, ...]:
15+
return ("hello", "world")
16+
17+
18+
def test_generic_on_iterable() -> None:
19+
with progressbar(it()) as bar:
20+
assert_type(bar, ProgressBar[str])
21+
for s in bar:
22+
assert_type(s, str)

0 commit comments

Comments
 (0)