Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: `progress_bar` type hints (#6304)

Andrew Glick828eead32015
diff --git a/marimo/_output/formatters/tqdm_formatters.py b/marimo/_output/formatters/tqdm_formatters.py
index a05c52bcf5..b01bb78381 100644
--- a/marimo/_output/formatters/tqdm_formatters.py
+++ b/marimo/_output/formatters/tqdm_formatters.py
@@ -1,14 +1,16 @@
# Copyright 2024 Marimo. All rights reserved.
from __future__ import annotations
-from typing import Any
+from typing import Any, TypeVar
from marimo._output.formatters.formatter_factory import FormatterFactory
from marimo._plugins.stateless.status._progress import progress_bar
from marimo._runtime.context.utils import running_in_notebook
+S = TypeVar("S")
-class ProgressBarTqdmPatch(progress_bar):
+
+class ProgressBarTqdmPatch(progress_bar[S]):
def __init__(self, *args: Any, **kwargs: Any):
# Partial translation from tqdm to our native progress bar;
# uses API of tqdm v4.66.4, likely backward compatible.
@@ -67,7 +69,7 @@ class ProgressBarTqdmPatch(progress_bar):
fp.write(end)
-class ProgressBarTrangePatch(ProgressBarTqdmPatch):
+class ProgressBarTrangePatch(ProgressBarTqdmPatch[S]):
def __init__(self, *args: Any, **kwargs: Any):
super().__init__(range(*args), **kwargs)
diff --git a/marimo/_plugins/stateless/status/_progress.py b/marimo/_plugins/stateless/status/_progress.py
index 7bc6d293a2..7f387ff05c 100644
--- a/marimo/_plugins/stateless/status/_progress.py
+++ b/marimo/_plugins/stateless/status/_progress.py
@@ -2,12 +2,16 @@
from __future__ import annotations
import time
+from collections.abc import Sized
from typing import (
TYPE_CHECKING,
Any,
+ Generic,
Literal,
Optional,
TypeVar,
+ cast,
+ overload,
)
import marimo._runtime.output._output as output
@@ -273,7 +277,7 @@ class spinner:
return self.spinner._mime_()
-class progress_bar:
+class progress_bar(Generic[S]):
"""Iterate over a collection and show a progress bar.
Examples:
@@ -299,7 +303,7 @@ class progress_bar:
every 150ms.
Args:
- collection (Collection[Union[S, int]], optional): Optional collection to iterate over.
+ collection (Union[Collection[S], Iterator[S]], optional): Optional collection to iterate over.
title (str, optional): Optional title.
subtitle (str, optional): Optional subtitle.
completion_title (str, optional): Optional title to show during completion.
@@ -311,9 +315,57 @@ class progress_bar:
disabled (bool, optional): If True, disable the progress bar.
"""
+ @overload
def __init__(
self,
- collection: Optional[Collection[S | int]] = None,
+ collection: Collection[S] = ...,
+ *,
+ title: Optional[str] = ...,
+ subtitle: Optional[str] = ...,
+ completion_title: Optional[str] = ...,
+ completion_subtitle: Optional[str] = ...,
+ total: Optional[int] = ...,
+ show_rate: bool = ...,
+ show_eta: bool = ...,
+ remove_on_exit: bool = ...,
+ disabled: bool = ...,
+ ): ...
+
+ @overload
+ def __init__(
+ self,
+ collection: Iterator[S] = ...,
+ *,
+ title: Optional[str] = ...,
+ subtitle: Optional[str] = ...,
+ completion_title: Optional[str] = ...,
+ completion_subtitle: Optional[str] = ...,
+ total: int = ...,
+ show_rate: bool = ...,
+ show_eta: bool = ...,
+ remove_on_exit: bool = ...,
+ disabled: bool = ...,
+ ): ...
+
+ @overload
+ def __init__(
+ self,
+ collection: None = ...,
+ *,
+ title: Optional[str] = ...,
+ subtitle: Optional[str] = ...,
+ completion_title: Optional[str] = ...,
+ completion_subtitle: Optional[str] = ...,
+ total: int = ...,
+ show_rate: bool = ...,
+ show_eta: bool = ...,
+ remove_on_exit: bool = ...,
+ disabled: bool = ...,
+ ): ...
+
+ def __init__(
+ self,
+ collection: Optional[Collection[S] | Iterator[S]] = None,
*,
title: Optional[str] = None,
subtitle: Optional[str] = None,
@@ -329,26 +381,29 @@ class progress_bar:
self.completion_subtitle = completion_subtitle
self.remove_on_exit = remove_on_exit
self.disabled = disabled
+ self.step: int = 1
if collection is not None:
- self.collection = collection
-
- try:
- total = total or len(collection)
- self.step = (
- collection.step if isinstance(collection, range) else 1
- )
- except TypeError: # if collection is a generator
+ self.collection: Optional[Collection[S] | Iterator[S]] = collection
+
+ if not isinstance(collection, Sized):
+ # if collection is a generator
raise TypeError(
"fail to determine length of collection, use `total`"
+ "to specify"
) from None
+ total = total or len(collection)
+ if isinstance(collection, range):
+ self.step = cast(range, collection).step
elif total is None:
raise ValueError(
"`total` is required when using as a context manager"
)
+ else:
+ self.collection = None
+
self.progress = ProgressBar(
title=title,
subtitle=subtitle,
@@ -359,9 +414,13 @@ class progress_bar:
if not disabled:
output.append(self.progress)
- def __iter__(self) -> Iterator[S | int]:
+ def __iter__(self) -> Iterator[S]:
+ if self.collection is None:
+ raise RuntimeError(
+ "progress_bar can only be iterated over if a collection is provided"
+ )
for item in self.collection:
- yield item # type: ignore[misc]
+ yield item
if not self.disabled:
self.progress.update(increment=self.step)
self._finish()
diff --git a/tests/_plugins/stateless/status/test_progress.py b/tests/_plugins/stateless/status/test_progress.py
index 4954f45f67..af227e7c0c 100644
--- a/tests/_plugins/stateless/status/test_progress.py
+++ b/tests/_plugins/stateless/status/test_progress.py
@@ -128,3 +128,7 @@ def test_progress_without_context():
for _ in range(10):
assert bar
bar.update()
+
+ with pytest.raises(RuntimeError):
+ for _ in progress_bar(total=10):
+ pass