Skip to content

Commit 98174db

Browse files
woodruffwdi
andauthored
packaging: add initial hints to storage services (#16709)
Signed-off-by: William Woodruff <[email protected]> Co-authored-by: Dustin Ingram <[email protected]>
1 parent e900d74 commit 98174db

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

warehouse/packaging/interfaces.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,25 @@ def create_service(context, request):
2626
created for, passing a name for settings.
2727
"""
2828

29-
def get(path):
29+
def get(path: str):
3030
"""
3131
Return a file like object that can be read to access the file located
3232
at the given path.
3333
"""
3434

35-
def get_metadata(path):
35+
def get_metadata(path: str):
3636
"""
3737
Return a dictionary containing any user-created metadata associated
3838
with the file at a given path. Implementations may or may not store
3939
or provide such metadata.
4040
"""
4141

42-
def get_checksum(path):
42+
def get_checksum(path: str):
4343
"""
4444
Return the md5 digest of the file at a given path as a lowercase string.
4545
"""
4646

47-
def store(path, file_path, *, meta=None):
47+
def store(path: str, file_path, *, meta=None):
4848
"""
4949
Save the file located at file_path to the file storage at the location
5050
specified by path. An additional meta keyword argument may contain

warehouse/packaging/services.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,11 @@ def remove_by_prefix(self, prefix):
169169

170170

171171
class GenericBlobStorage:
172-
def __init__(self, bucket, *, prefix=None):
172+
def __init__(self, bucket, *, prefix: str | None = None):
173173
self.bucket = bucket
174174
self.prefix = prefix
175175

176-
def _get_path(self, path):
176+
def _get_path(self, path: str) -> str:
177177
# If we have a prefix, then prepend it to our path. This will let us
178178
# store items inside of a sub directory without exposing that to end
179179
# users.
@@ -184,7 +184,7 @@ def _get_path(self, path):
184184

185185

186186
class GenericB2BlobStorage(GenericBlobStorage):
187-
def get(self, path):
187+
def get(self, path: str):
188188
path = self._get_path(path)
189189
try:
190190
file_obj = io.BytesIO()
@@ -195,14 +195,14 @@ def get(self, path):
195195
except b2sdk.v2.exception.FileNotPresent:
196196
raise FileNotFoundError(f"No such key: {path!r}") from None
197197

198-
def get_metadata(self, path):
198+
def get_metadata(self, path: str):
199199
path = self._get_path(path)
200200
try:
201201
return self.bucket.get_file_info_by_name(path).file_info
202202
except b2sdk.v2.exception.FileNotPresent:
203203
raise FileNotFoundError(f"No such key: {path!r}") from None
204204

205-
def get_checksum(self, path):
205+
def get_checksum(self, path: str):
206206
path = self._get_path(path)
207207
try:
208208
return self.bucket.get_file_info_by_id(
@@ -211,7 +211,7 @@ def get_checksum(self, path):
211211
except b2sdk.v2.exception.FileNotPresent:
212212
raise FileNotFoundError(f"No such key: {path!r}") from None
213213

214-
def store(self, path, file_path, *, meta=None):
214+
def store(self, path: str, file_path, *, meta=None):
215215
path = self._get_path(path)
216216
self.bucket.upload_local_file(
217217
local_file=file_path,
@@ -231,7 +231,7 @@ def create_service(cls, context, request):
231231

232232

233233
class GenericS3BlobStorage(GenericBlobStorage):
234-
def get(self, path):
234+
def get(self, path: str):
235235
# Note: this is not actually used to serve files, instead our CDN is
236236
# configured to connect directly to our storage bucket. See:
237237
# https://github.com/python/pypi-infra/blob/master/terraform/file-hosting/vcl/main.vcl
@@ -242,15 +242,15 @@ def get(self, path):
242242
raise
243243
raise FileNotFoundError(f"No such key: {path!r}") from None
244244

245-
def get_metadata(self, path):
245+
def get_metadata(self, path: str):
246246
try:
247247
return self.bucket.Object(self._get_path(path)).metadata
248248
except botocore.exceptions.ClientError as exc:
249249
if exc.response["Error"]["Code"] != "NoSuchKey":
250250
raise
251251
raise FileNotFoundError(f"No such key: {path!r}") from None
252252

253-
def get_checksum(self, path):
253+
def get_checksum(self, path: str):
254254
try:
255255
return (
256256
self.bucket.Object(self._get_path(path)).e_tag.rstrip('"').lstrip('"')
@@ -261,7 +261,7 @@ def get_checksum(self, path):
261261
raise
262262
raise FileNotFoundError(f"No such key: {path!r}") from None
263263

264-
def store(self, path, file_path, *, meta=None):
264+
def store(self, path: str, file_path, *, meta=None):
265265
extra_args = {}
266266
if meta is not None:
267267
extra_args["Metadata"] = meta
@@ -327,24 +327,24 @@ def remove_by_prefix(self, prefix):
327327

328328

329329
class GenericGCSBlobStorage(GenericBlobStorage):
330-
def get(self, path):
330+
def get(self, path: str):
331331
# Note: this is not actually used in to serve files, instead our CDN is
332332
# configured to connect directly to our storage bucket. See:
333333
# https://github.com/python/pypi-infra/blob/master/terraform/file-hosting/vcl/main.vcl
334334
raise NotImplementedError
335335

336-
def get_metadata(self, path):
336+
def get_metadata(self, path: str):
337337
raise NotImplementedError
338338

339-
def get_checksum(self, path):
339+
def get_checksum(self, path: str):
340340
raise NotImplementedError
341341

342342
@google.api_core.retry.Retry(
343343
predicate=google.api_core.retry.if_exception_type(
344344
google.api_core.exceptions.ServiceUnavailable
345345
)
346346
)
347-
def store(self, path, file_path, *, meta=None):
347+
def store(self, path: str, file_path, *, meta=None):
348348
path = self._get_path(path)
349349
blob = self.bucket.blob(path)
350350
if meta is not None:

0 commit comments

Comments
 (0)