-
Notifications
You must be signed in to change notification settings - Fork 47
feat(collection): optional progress bar for Collection.upsert (#10) #118
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
Open
srijanarya
wants to merge
2
commits into
supabase:main
Choose a base branch
from
srijanarya:feat/upsert-progress
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import sys | ||
| import types | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
|
|
||
| import vecs | ||
|
|
||
|
|
||
| def _records(n: int, dim: int = 4): | ||
| return [(f"vec{ix}", vec, {}) for ix, vec in enumerate(np.random.random((n, dim)))] | ||
|
|
||
|
|
||
| class _FakeBar: | ||
| """Records tqdm calls so tests don't need a TTY or notebook.""" | ||
|
|
||
| instances = [] | ||
|
|
||
| def __init__(self, total=None, **_): | ||
| self.total = total | ||
| self.updates = [] | ||
| self.closed = False | ||
| _FakeBar.instances.append(self) | ||
|
|
||
| def update(self, n): | ||
| self.updates.append(n) | ||
|
|
||
| def close(self): | ||
| self.closed = True | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def fake_tqdm(monkeypatch): | ||
| _FakeBar.instances.clear() | ||
| auto = types.ModuleType("tqdm.auto") | ||
| auto.tqdm = _FakeBar | ||
| pkg = types.ModuleType("tqdm") | ||
| pkg.auto = auto | ||
| monkeypatch.setitem(sys.modules, "tqdm", pkg) | ||
| monkeypatch.setitem(sys.modules, "tqdm.auto", auto) | ||
| return _FakeBar | ||
|
|
||
|
|
||
| def test_upsert_default_has_no_progress(client: vecs.Client, fake_tqdm) -> None: | ||
| coll = client.get_or_create_collection(name="prog_default", dimension=4) | ||
| coll.upsert(_records(10)) | ||
| assert fake_tqdm.instances == [] | ||
| assert len(coll) == 10 | ||
|
|
||
|
|
||
| def test_upsert_progress_sized_reports_total(client: vecs.Client, fake_tqdm) -> None: | ||
| coll = client.get_or_create_collection(name="prog_sized", dimension=4) | ||
| coll.upsert(_records(1200), show_progress=True) | ||
| (bar,) = fake_tqdm.instances | ||
| assert bar.total == 1200 | ||
| assert bar.updates == [500, 500, 200] | ||
| assert bar.closed | ||
| assert len(coll) == 1200 | ||
|
|
||
|
|
||
| def test_upsert_progress_generator_is_indeterminate( | ||
| client: vecs.Client, fake_tqdm | ||
| ) -> None: | ||
| coll = client.get_or_create_collection(name="prog_gen", dimension=4) | ||
| coll.upsert((r for r in _records(7)), show_progress=True) | ||
| (bar,) = fake_tqdm.instances | ||
| assert bar.total is None | ||
| assert sum(bar.updates) == 7 | ||
| assert bar.closed | ||
|
|
||
|
|
||
| def test_upsert_progress_without_tqdm_raises(client: vecs.Client, monkeypatch) -> None: | ||
| monkeypatch.setitem(sys.modules, "tqdm", None) | ||
| monkeypatch.setitem(sys.modules, "tqdm.auto", None) | ||
| coll = client.get_or_create_collection(name="prog_missing", dimension=4) | ||
| with pytest.raises(ImportError, match="vecs\\[progress\\]"): | ||
| coll.upsert(_records(3), show_progress=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work overall — the show_progress scoping is clean, and tqdm.auto resolves the notebook/terminal split exactly as discussed in the issue.
One correctness note on this hunk: progress.close() (and the update() calls) only run if the per-chunk loop finishes without error. If sess.execute(stmt) raises partway through, the function exits via the exception and the bar never closes — harmless in a plain terminal, but tqdm.auto's notebook widget can be left half-rendered if that happens in Jupyter.
Since the fix needs to wrap the whole per-chunk loop rather than just these lines, something like a try/finally around the loop body (with progress.close() in the finally), or using tqdm as a context manager, would make this exception-safe. Happy to sketch it out further if useful — not blocking, but worth tightening before merge.