Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

specialize progressbar(length=...) as ProgressBar[int] #2630

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
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
41 changes: 41 additions & 0 deletions src/click/termui.py
Expand Up @@ -284,6 +284,47 @@ 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,
file: t.TextIO | None = None,
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
@@ -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")
Comment on lines +14 to +15
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this separate function is entirely so pyright doesn't inference as Literal[("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)