Skip to content

Commit d002dcd

Browse files
author
Jussi Kukkonen
authored
Merge pull request #1390 from jku/no-falsy-tests-for-none
Avoid using falsy tests for None
2 parents 9a397b9 + 14f5957 commit d002dcd

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

tests/test_api.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -451,21 +451,23 @@ def test_delegated_role_class(self):
451451
with self.assertRaises(ValueError):
452452
DelegatedRole.from_dict(role.copy())
453453

454-
# Test creating DelegatedRole only with "path_hash_prefixes"
454+
# Test creating DelegatedRole only with "path_hash_prefixes" (an empty one)
455455
del role["paths"]
456-
DelegatedRole.from_dict(role.copy())
457-
role["paths"] = "foo"
456+
role["path_hash_prefixes"] = []
457+
role_obj = DelegatedRole.from_dict(role.copy())
458+
self.assertEqual(role_obj.to_dict(), role)
458459

459-
# Test creating DelegatedRole only with "paths"
460+
# Test creating DelegatedRole only with "paths" (now an empty one)
460461
del role["path_hash_prefixes"]
461-
DelegatedRole.from_dict(role.copy())
462-
role["path_hash_prefixes"] = "foo"
462+
role["paths"] = []
463+
role_obj = DelegatedRole.from_dict(role.copy())
464+
self.assertEqual(role_obj.to_dict(), role)
463465

464466
# Test creating DelegatedRole without "paths" and
465467
# "path_hash_prefixes" set
466468
del role["paths"]
467-
del role["path_hash_prefixes"]
468-
DelegatedRole.from_dict(role)
469+
role_obj = DelegatedRole.from_dict(role.copy())
470+
self.assertEqual(role_obj.to_dict(), role)
469471

470472

471473
def test_delegation_class(self):
@@ -495,6 +497,21 @@ def test_delegation_class(self):
495497
delegations = Delegations.from_dict(copy.deepcopy(delegations_dict))
496498
self.assertEqual(delegations_dict, delegations.to_dict())
497499

500+
# empty keys and roles
501+
delegations_dict = {"keys":{}, "roles":[]}
502+
delegations = Delegations.from_dict(delegations_dict.copy())
503+
self.assertEqual(delegations_dict, delegations.to_dict())
504+
505+
# Test some basic missing or broken input
506+
invalid_delegations_dicts = [
507+
{},
508+
{"keys":None, "roles":None},
509+
{"keys":{"foo":0}, "roles":[]},
510+
{"keys":{}, "roles":["foo"]},
511+
]
512+
for d in invalid_delegations_dicts:
513+
with self.assertRaises((KeyError, AttributeError)):
514+
Delegations.from_dict(d)
498515

499516
def test_metadata_targets(self):
500517
targets_path = os.path.join(

tuf/api/metadata.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ def __init__(
768768
super().__init__(keyids, threshold, unrecognized_fields)
769769
self.name = name
770770
self.terminating = terminating
771-
if paths and path_hash_prefixes:
771+
if paths is not None and path_hash_prefixes is not None:
772772
raise ValueError(
773773
"Only one of the attributes 'paths' and"
774774
"'path_hash_prefixes' can be set!"
@@ -804,9 +804,9 @@ def to_dict(self) -> Dict[str, Any]:
804804
"terminating": self.terminating,
805805
**base_role_dict,
806806
}
807-
if self.paths:
807+
if self.paths is not None:
808808
res_dict["paths"] = self.paths
809-
elif self.path_hash_prefixes:
809+
elif self.path_hash_prefixes is not None:
810810
res_dict["path_hash_prefixes"] = self.path_hash_prefixes
811811
return res_dict
812812

@@ -909,17 +909,20 @@ def from_dict(cls, targets_dict: Dict[str, Any]) -> "Targets":
909909
"""Creates Targets object from its dict representation."""
910910
common_args = cls._common_fields_from_dict(targets_dict)
911911
targets = targets_dict.pop("targets")
912-
delegations = targets_dict.pop("delegations", None)
913-
if delegations:
914-
delegations = Delegations.from_dict(delegations)
912+
try:
913+
delegations_dict = targets_dict.pop("delegations")
914+
except KeyError:
915+
delegations = None
916+
else:
917+
delegations = Delegations.from_dict(delegations_dict)
915918
# All fields left in the targets_dict are unrecognized.
916919
return cls(*common_args, targets, delegations, targets_dict)
917920

918921
def to_dict(self) -> Dict[str, Any]:
919922
"""Returns the dict representation of self."""
920923
targets_dict = self._common_fields_to_dict()
921924
targets_dict["targets"] = self.targets
922-
if self.delegations:
925+
if self.delegations is not None:
923926
targets_dict["delegations"] = self.delegations.to_dict()
924927
return targets_dict
925928

0 commit comments

Comments
 (0)