Skip to content

Commit 62cf1e3

Browse files
authored
bugfix that pagination sets upper limit when should not! (#70)
* bugfix that pagination sets upper limit when should not! * tags can be null Signed-off-by: vsoch <vsoch@users.noreply.github.com>
1 parent 1a9e066 commit 62cf1e3

File tree

6 files changed

+16
-23
lines changed

6 files changed

+16
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and **Merged pull requests**. Critical items to know are:
1414
The versions coincide with releases on pip. Only major versions will be released as tags on Github.
1515

1616
## [0.0.x](https://github.com/oras-project/oras-py/tree/main) (0.0.x)
17+
- bugfix that pagination sets upper limit of 10K (0.1.15)
1718
- pagination for tags (and general function for pagination) (0.1.14)
1819
- expose upload_blob function to be consistent (0.1.13)
1920
- ensure we always strip path separators before pull/push (0.1.12)

oras/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@ def version(self, return_items: bool = False) -> Union[dict, str]:
9494
# Otherwise return a string that can be printed
9595
return "\n".join(["%s: %s" % (k, v) for k, v in versions.items()])
9696

97-
def get_tags(self, name: str, N: int = -1) -> List[str]:
97+
def get_tags(self, name: str, N=None) -> List[str]:
9898
"""
9999
Retrieve tags for a package.
100100
101101
:param name: container URI to parse
102102
:type name: str
103-
:param N: number of tags (-1 to get all tags)
103+
:param N: number of tags (None to get all tags)
104104
:type N: int
105105
"""
106106
return self.remote.get_tags(name, N=N)

oras/container.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ def get_blob_url(self, digest: str) -> str:
5454
def upload_blob_url(self) -> str:
5555
return f"{self.registry}/v2/{self.api_prefix}/blobs/uploads/"
5656

57-
def tags_url(self, N=10_000) -> str:
57+
def tags_url(self, N=None) -> str:
58+
if N is None:
59+
return f"{self.registry}/v2/{self.api_prefix}/tags/list"
5860
return f"{self.registry}/v2/{self.api_prefix}/tags/list?n={N}"
5961

6062
def put_manifest_url(self) -> str:

oras/provider.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -227,36 +227,33 @@ def upload_blob(
227227

228228
@ensure_container
229229
def get_tags(
230-
self, container: Union[str, oras.container.Container], N: int = -1
230+
self, container: Union[str, oras.container.Container], N=None
231231
) -> List[str]:
232232
"""
233233
Retrieve tags for a package.
234234
235235
:param container: parsed container URI
236236
:type container: oras.container.Container or str
237-
:param N: limit number of tags, -1 for all (default)
237+
:param N: limit number of tags, None for all (default)
238238
:type N: Optional[int]
239239
"""
240-
# -1 is a flag for retrieving all, if set we use arbitrarily high number
241-
retrieve_all = N == -1
242-
N = N if (N and N > 0) else 10_0000
243-
240+
retrieve_all = N is None
244241
tags_url = f"{self.prefix}://{container.tags_url(N=N)}" # type: ignore
245242
tags: List[str] = []
246243

247-
def extract_tags(response: requests.Response) -> bool:
244+
def extract_tags(response: requests.Response):
248245
"""
249246
Determine if we should continue based on new tags and under limit.
250247
"""
251248
json = response.json()
252-
new_tags = json.get("tags", [])
249+
new_tags = json.get("tags") or []
253250
tags.extend(new_tags)
254-
return bool(len(new_tags) and (retrieve_all or len(tags) < N))
251+
return len(new_tags) and (retrieve_all or len(tags) < N)
255252

256253
self._do_paginated_request(tags_url, callable=extract_tags)
257254

258255
# If we got a longer set than was asked for
259-
if len(tags) > N:
256+
if N is not None and len(tags) > N:
260257
tags = tags[:N]
261258
return tags
262259

@@ -270,7 +267,6 @@ def _do_paginated_request(
270267
the callable returns True, we continue to the next page, otherwise
271268
we stop.
272269
"""
273-
274270
# Save the base url to add parameters to, assuming only the params change
275271
parts = urllib.parse.urlparse(url)
276272
base_url = f"{parts.scheme}://{parts.netloc}"

oras/tests/test_oras.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,11 @@ def test_get_many_tags():
104104
)
105105
assert len(tags) == 1005
106106

107-
# This should retrieve all tags (defaults to -1)
107+
# This should retrieve all tags (defaults to None)
108108
tags = client.get_tags("channel-mirrors/conda-forge/linux-aarch64/arrow-cpp")
109109
assert len(tags) > 1500
110110

111-
# Same result (assuming doesn't change in small seconds between)
112-
same_tags = client.get_tags(
113-
"channel-mirrors/conda-forge/linux-aarch64/arrow-cpp", N=-1
114-
)
115-
assert not set(tags).difference(set(same_tags))
116-
117-
# None defaults to -1 too
111+
# Same result if explicitly set
118112
same_tags = client.get_tags(
119113
"channel-mirrors/conda-forge/linux-aarch64/arrow-cpp", N=None
120114
)

oras/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
__copyright__ = "Copyright The ORAS Authors."
33
__license__ = "Apache-2.0"
44

5-
__version__ = "0.1.14"
5+
__version__ = "0.1.15"
66
AUTHOR = "Vanessa Sochat"
77
EMAIL = "vsoch@users.noreply.github.com"
88
NAME = "oras"

0 commit comments

Comments
 (0)