From 710cfd9b78ef0116c20748eae1a26357ed1bcaaa Mon Sep 17 00:00:00 2001 From: Tom Close Date: Fri, 8 Dec 2023 10:29:09 +1100 Subject: [PATCH] replaced EMPTY_METADATA constant with False instead (as distinct from None which is returned when the fileset doesn't have metadata associated with it) --- fileformats/core/fileset.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/fileformats/core/fileset.py b/fileformats/core/fileset.py index 85685f8..1607aa1 100644 --- a/fileformats/core/fileset.py +++ b/fileformats/core/fileset.py @@ -44,7 +44,6 @@ FILE_CHUNK_LEN_DEFAULT = 8192 -EMPTY_METADATA = -1 logger = logging.getLogger("fileformats") @@ -69,14 +68,14 @@ class FileSet(DataType): fspaths: ty.FrozenSet[Path] = attrs.field(default=None, converter=fspaths_converter) _metadata: ty.Optional[ty.Dict[str, ty.Any]] = attrs.field( - default=EMPTY_METADATA, + default=False, eq=False, order=False, ) @_metadata.validator def metadata_validator(self, _, val): - if not (val == EMPTY_METADATA or val is None or isinstance(val, dict)): + if val and not isinstance(val, dict): raise TypeError( f"Fileset metadata value needs to be None or dict, not {val} ({self.fspaths})" ) @@ -191,7 +190,7 @@ def possible_exts(cls): def metadata(self) -> ty.Dict[str, ty.Any]: """Lazily load metadata from `read_metadata` extra if implemented, returning an empty metadata array if not""" - if self._metadata != EMPTY_METADATA: + if self._metadata is not False: return self._metadata try: self._metadata = self.read_metadata() @@ -213,6 +212,12 @@ def select_metadata(self, selected_keys: ty.Union[ty.Sequence[str], None]): selected_keys : Union[Sequence[str], None] the keys of the values to load. If None, all values are loaded """ + if ( + self._metadata + and selected_keys is not None + and set(selected_keys).issubset(self._metadata) + ): + return self._metadata = self.read_metadata(selected_keys) @hook.extra