diff --git a/clients/python/src/model_registry/types/pager.py b/clients/python/src/model_registry/types/pager.py index a789f248d..55cb9f493 100644 --- a/clients/python/src/model_registry/types/pager.py +++ b/clients/python/src/model_registry/types/pager.py @@ -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. @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/clients/python/tests/regression_test.py b/clients/python/tests/regression_test.py index eebc1e5de..024a6ea6a 100644 --- a/clients/python/tests/regression_test.py +++ b/clients/python/tests/regression_test.py @@ -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. @@ -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