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 0b3472c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Unreleased
- When generating a command's name from a decorated function's name, the
suffixes ``_command``, ``_cmd``, ``_group``, and ``_grp`` are removed.
:issue:`2322`
- Specialized typing of ``progressbar(length=...)`` as ``ProgressBar[int]``.
:pr:`2630`


Version 8.1.7
Expand Down
40 changes: 40 additions & 0 deletions src/click/termui.py
Original file line number Diff line number Diff line change
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
22 changes: 22 additions & 0 deletions tests/typing/typing_progressbar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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 it() -> tuple[str, ...]:
return ("hello", "world")


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

0 comments on commit 0b3472c

Please sign in to comment.