diff --git a/lib/galaxy/datatypes/protocols.py b/lib/galaxy/datatypes/protocols.py index 276844d7fc83..05b02d22fe45 100644 --- a/lib/galaxy/datatypes/protocols.py +++ b/lib/galaxy/datatypes/protocols.py @@ -86,7 +86,7 @@ def get_converted_files_by_type(self, file_type): def get_mime(self) -> str: ... - def get_size(self) -> str: + def get_size(self) -> int: ... def has_data(self) -> bool: diff --git a/lib/galaxy/model/__init__.py b/lib/galaxy/model/__init__.py index ff042faaca85..81a10824644a 100644 --- a/lib/galaxy/model/__init__.py +++ b/lib/galaxy/model/__init__.py @@ -27,6 +27,7 @@ List, NamedTuple, Optional, + overload, Set, Tuple, Type, @@ -102,6 +103,7 @@ from sqlalchemy.orm.collections import attribute_mapped_collection from sqlalchemy.sql import exists from typing_extensions import ( + Literal, Protocol, TypedDict, ) @@ -3915,16 +3917,24 @@ def extra_files_path_name(self): def _extra_files_rel_path(self): return self._extra_files_path or self.extra_files_path_name - def _calculate_size(self): + def _calculate_size(self) -> int: if self.external_filename: try: return os.path.getsize(self.external_filename) except OSError: return 0 - else: - return self.object_store.size(self) + assert self.object_store + return self.object_store.size(self) - def get_size(self, nice_size=False, calculate_size=True): + @overload + def get_size(self, nice_size: Literal[False], calculate_size: bool = True) -> int: + ... + + @overload + def get_size(self, nice_size: Literal[True], calculate_size: bool = True) -> str: + ... + + def get_size(self, nice_size: bool = False, calculate_size: bool = True) -> Union[int, str]: """Returns the size of the data on disk""" if self.file_size: if nice_size: diff --git a/lib/galaxy/objectstore/__init__.py b/lib/galaxy/objectstore/__init__.py index d0b0e35b4c05..337123cc9dad 100644 --- a/lib/galaxy/objectstore/__init__.py +++ b/lib/galaxy/objectstore/__init__.py @@ -170,7 +170,7 @@ def empty(self, obj, base_dir=None, extra_dir=None, extra_dir_at_root=False, alt raise NotImplementedError() @abc.abstractmethod - def size(self, obj, extra_dir=None, extra_dir_at_root=False, alt_name=None, obj_dir=False): + def size(self, obj, extra_dir=None, extra_dir_at_root=False, alt_name=None, obj_dir=False) -> int: """ Return size of the object identified by `obj`. @@ -850,7 +850,7 @@ def _empty(self, obj, **kwargs): """Override `ObjectStore`'s stub by checking file size on disk.""" return self.size(obj, **kwargs) == 0 - def _size(self, obj, **kwargs): + def _size(self, obj, **kwargs) -> int: """Override `ObjectStore`'s stub by return file size on disk. Returns 0 if the object doesn't exist yet or other error. diff --git a/lib/galaxy/objectstore/cloud.py b/lib/galaxy/objectstore/cloud.py index e4a9ed5bc14e..491f9adb4615 100644 --- a/lib/galaxy/objectstore/cloud.py +++ b/lib/galaxy/objectstore/cloud.py @@ -400,8 +400,7 @@ def _get_transfer_progress(self): def _get_size_in_cloud(self, rel_path): try: obj = self.bucket.objects.get(rel_path) - if obj: - return obj.size + return obj.size except Exception: log.exception("Could not get size of key '%s' from S3", rel_path) return -1 diff --git a/lib/galaxy/objectstore/irods.py b/lib/galaxy/objectstore/irods.py index 3dde6e991a1d..0313f37a3153 100644 --- a/lib/galaxy/objectstore/irods.py +++ b/lib/galaxy/objectstore/irods.py @@ -608,7 +608,7 @@ def _empty(self, obj, **kwargs): else: raise ObjectNotFound(f"objectstore.empty, object does not exist: {obj}, kwargs: {kwargs}") - def _size(self, obj, **kwargs): + def _size(self, obj, **kwargs) -> int: ipt_timer = ExecutionTimer() rel_path = self._construct_path(obj, **kwargs) if self._in_cache(rel_path): diff --git a/lib/galaxy/objectstore/pithos.py b/lib/galaxy/objectstore/pithos.py index 9eaa113bc887..63b81bbc2815 100644 --- a/lib/galaxy/objectstore/pithos.py +++ b/lib/galaxy/objectstore/pithos.py @@ -309,7 +309,7 @@ def _empty(self, obj, **kwargs): raise ObjectNotFound(f"objectstore.empty, object does not exist: {obj}, kwargs: {kwargs}") return bool(self._size(obj, **kwargs)) - def _size(self, obj, **kwargs): + def _size(self, obj, **kwargs) -> int: """ :returns: The size of the object, or 0 if it doesn't exist (sorry for that, not our fault, the ObjectStore interface is like that some diff --git a/lib/galaxy/objectstore/s3.py b/lib/galaxy/objectstore/s3.py index 52432b3bf6bf..34a342b2cae2 100644 --- a/lib/galaxy/objectstore/s3.py +++ b/lib/galaxy/objectstore/s3.py @@ -385,9 +385,8 @@ def _get_transfer_progress(self): def _get_size_in_s3(self, rel_path): try: key = self._bucket.get_key(rel_path) - if key: - return key.size - except S3ResponseError: + return key.size + except (S3ResponseError, AttributeError): log.exception("Could not get size of key '%s' from S3", rel_path) return -1