Skip to content

Commit

Permalink
Always return dict for custom metadata (#132)
Browse files Browse the repository at this point in the history
* always return dict from TargetMeta.custom and custom_internal
* fix tests accordingly
  • Loading branch information
dennisvang committed Mar 22, 2024
1 parent 735231c commit 9650662
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
19 changes: 10 additions & 9 deletions src/tufup/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class CustomMetadataDict(TypedDict):
used by tufup internally
"""

user: Optional[dict]
tufup: Optional[dict]
user: dict
tufup: dict


def _immutable(value):
Expand Down Expand Up @@ -79,19 +79,19 @@ def __init__(
logger.critical(
f'invalid filename "{self.filename}": whitespace not allowed'
)
self._custom = custom
self._custom = custom or dict(user=dict(), tufup=dict())

@property
def custom(self) -> Optional[dict]:
"""returns user-specified custom metadata"""
def custom(self) -> dict:
"""returns user-specified custom metadata dict"""
return self._get_custom_metadata('user')

@property
def custom_internal(self) -> Optional[dict]:
"""returns tufup-internal custom metadata"""
def custom_internal(self) -> dict:
"""returns tufup-internal custom metadata dict"""
return self._get_custom_metadata('tufup')

def _get_custom_metadata(self, key: str) -> Optional[dict]:
def _get_custom_metadata(self, _key: str) -> dict:
"""
get custom metadata in a backward-compatible manner (older versions did not
distinguish between user-specified and internal metadata)
Expand All @@ -100,7 +100,8 @@ def _get_custom_metadata(self, key: str) -> Optional[dict]:
# check dict keys for backward compatibility
if get_type_hints(CustomMetadataDict).keys() != self._custom.keys():
return self._custom
return self._custom.get(key)
return self._custom.get(_key)
return dict()

def __str__(self):
return str(self.target_path_str)
Expand Down
19 changes: 16 additions & 3 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def test_immutable(self):
bytearray(b'b'),
dict(a=1),
[dict(a=[dict(c={1})], b=bytearray(b'd'))],
dict(a=dict(b=dict(c=dict()))),
]
for case in cases:
with self.subTest(msg=case):
Expand Down Expand Up @@ -66,7 +67,16 @@ def test_str(self):

def test_hashable(self):
obj = TargetMeta()
self.assertEqual(obj.__hash__(), hash(tuple(vars(obj).items())))
try:
obj.__hash__()
except Exception as e:
self.fail(f'__hash__ failed unexpectedly: {e}')
expected_obj_hashable = (
('target_path_str', 'None-None.tar.gz'),
('path', pathlib.Path('None-None.tar.gz')),
('_custom', (('user', ()), ('tufup', ()))),
)
self.assertEqual(hash(expected_obj_hashable), obj.__hash__())
# we can use the obj as a set member or as dict key
self.assertEqual({obj, obj}, {obj})

Expand Down Expand Up @@ -171,8 +181,11 @@ def test_custom_metadata_backward_compatibility(self):

def test_custom_metadata_not_specified(self):
target_meta = TargetMeta()
self.assertIsNone(target_meta.custom)
self.assertIsNone(target_meta.custom_internal)
self.assertIsInstance(target_meta.custom, dict)
self.assertIsInstance(target_meta.custom_internal, dict)
# user overrides _custom attr
target_meta._custom = None
self.assertIsInstance(target_meta.custom, dict)


class PatcherTests(TempDirTestCase):
Expand Down

0 comments on commit 9650662

Please sign in to comment.