Skip to content

Commit

Permalink
py: fix paging when missing token
Browse files Browse the repository at this point in the history
Fixes: kubeflow#348

Signed-off-by: Isabella do Amaral <idoamara@redhat.com>
  • Loading branch information
isinyaaa committed Sep 5, 2024
1 parent 5755b77 commit 275eeeb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
12 changes: 9 additions & 3 deletions clients/python/src/model_registry/types/pager.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ async def _anext_page(self) -> list[T]:
return await cast(Awaitable[list[T]], self.page_fn(self.options))

def _needs_fetch(self) -> bool:
return not self._current_page or self._i >= len(self._current_page)
return not self._current_page or (
self._i >= len(self._current_page) and self._start is not None
)

def _next_item(self) -> T:
"""Get the next item in the pager.
Expand All @@ -126,6 +128,8 @@ def _next_item(self) -> T:
self._current_page = self._next_page()
self._i = 0
assert self._current_page
if self._i >= len(self._current_page):
raise StopIteration

item = self._current_page[self._i]
self._i += 1
Expand All @@ -143,6 +147,8 @@ async def _anext_item(self) -> T:
self._current_page = await self._anext_page()
self._i = 0
assert self._current_page
if self._i >= len(self._current_page):
raise StopIteration

item = self._current_page[self._i]
self._i += 1
Expand All @@ -153,7 +159,7 @@ def __next__(self) -> T:

item = self._next_item()

if not self._start:
if self._start is None:
self._start = self.options.next_page_token
elif check_looping and self.options.next_page_token == self._start:
raise StopIteration
Expand All @@ -165,7 +171,7 @@ async def __anext__(self) -> T:

item = await self._anext_item()

if not self._start:
if self._start is None:
self._start = self.options.next_page_token
elif check_looping and self.options.next_page_token == self._start:
raise StopAsyncIteration
Expand Down
27 changes: 27 additions & 0 deletions clients/python/tests/regression_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def test_create_tagged_version(client: ModelRegistry):
assert mv
assert mv.id


@pytest.mark.e2e
def test_get_model_without_user_token(setup_env_user_token, client):
"""Test regression for using client methods without an user_token in the init arguments.
Expand All @@ -44,3 +45,29 @@ def test_get_model_without_user_token(setup_env_user_token, client):
assert rm.id
assert (_rm := client.get_registered_model(name))
assert rm.id == _rm.id


@pytest.mark.e2e
def test_get_few_registered_models(client: ModelRegistry):
"""Test regression for paging without next page token.
Reported on: https://github.com/kubeflow/model-registry/issues/348
"""
models = 9

for name in [f"test_model{i}" for i in range(models)]:
client.register_model(
name,
"s3",
model_format_name="test_format",
model_format_version="test_version",
version="1.0.0",
)

i = 0
for rm in client.get_registered_models():
print(f"found {rm}")
i += 1
assert i < models + 1

assert i == models

0 comments on commit 275eeeb

Please sign in to comment.