Skip to content

Commit

Permalink
specialize progressbar(length=...) as ProgressBar[int]
Browse files Browse the repository at this point in the history
  • Loading branch information
asottile committed Nov 7, 2023
1 parent ca5e1c3 commit b0a8bf9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/click/termui.py
Expand Up @@ -284,6 +284,46 @@ def echo_via_pager(
return pager(itertools.chain(text_generator, "\n"), color)


@t.overload
def progressbar(
*,
length: int,
label: str | None = None,
show_eta: bool = True,
show_percent: bool | None = None,
show_pos: bool = False,
fill_char: str = "#",
empty_char: str = "-",
bar_template: str = "%(label)s [%(bar)s] %(info)s",
info_sep: str = " ",
width: int = 36,
color: bool | None = None,
update_min_steps: int = 1,
) -> ProgressBar[int]:
...


@t.overload
def progressbar(
iterable: cabc.Iterable[V] | None = None,
length: int | None = None,
label: str | None = None,
show_eta: bool = True,
show_percent: bool | None = None,
show_pos: bool = False,
item_show_func: t.Callable[[V | None], str | None] | None = None,
fill_char: str = "#",
empty_char: str = "-",
bar_template: str = "%(label)s [%(bar)s] %(info)s",
info_sep: str = " ",
width: int = 36,
file: t.TextIO | None = None,
color: bool | None = None,
update_min_steps: int = 1,
) -> ProgressBar[V]:
...


def progressbar(
iterable: cabc.Iterable[V] | None = None,
length: int | None = None,
Expand Down
18 changes: 18 additions & 0 deletions tests/typing/typing_progressbar.py
@@ -0,0 +1,18 @@
from typing_extensions import assert_type

from click import progressbar
from click._termui_impl import ProgressBar


def test_length_is_int() -> None:
with progressbar(length=5) as bar:
assert_type(bar, ProgressBar[int])
for i in bar:
assert_type(i, int)


def test_generic_on_iterable() -> None:
with progressbar(("hello", "world")) as bar:
assert_type(bar, ProgressBar[str])
for s in bar:
assert_type(s, str)

0 comments on commit b0a8bf9

Please sign in to comment.