Skip to content

Avoid using falsy tests for None #1390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,21 +451,23 @@ def test_delegated_role_class(self):
with self.assertRaises(ValueError):
DelegatedRole.from_dict(role.copy())

# Test creating DelegatedRole only with "path_hash_prefixes"
# Test creating DelegatedRole only with "path_hash_prefixes" (an empty one)
del role["paths"]
DelegatedRole.from_dict(role.copy())
role["paths"] = "foo"
role["path_hash_prefixes"] = []
role_obj = DelegatedRole.from_dict(role.copy())
self.assertEqual(role_obj.to_dict(), role)

# Test creating DelegatedRole only with "paths"
# Test creating DelegatedRole only with "paths" (now an empty one)
del role["path_hash_prefixes"]
DelegatedRole.from_dict(role.copy())
role["path_hash_prefixes"] = "foo"
role["paths"] = []
role_obj = DelegatedRole.from_dict(role.copy())
self.assertEqual(role_obj.to_dict(), role)

# Test creating DelegatedRole without "paths" and
# "path_hash_prefixes" set
del role["paths"]
del role["path_hash_prefixes"]
DelegatedRole.from_dict(role)
role_obj = DelegatedRole.from_dict(role.copy())
self.assertEqual(role_obj.to_dict(), role)


def test_delegation_class(self):
Expand Down Expand Up @@ -495,6 +497,21 @@ def test_delegation_class(self):
delegations = Delegations.from_dict(copy.deepcopy(delegations_dict))
self.assertEqual(delegations_dict, delegations.to_dict())

# empty keys and roles
delegations_dict = {"keys":{}, "roles":[]}
delegations = Delegations.from_dict(delegations_dict.copy())
self.assertEqual(delegations_dict, delegations.to_dict())

# Test some basic missing or broken input
invalid_delegations_dicts = [
{},
{"keys":None, "roles":None},
{"keys":{"foo":0}, "roles":[]},
{"keys":{}, "roles":["foo"]},
]
for d in invalid_delegations_dicts:
with self.assertRaises((KeyError, AttributeError)):
Delegations.from_dict(d)

def test_metadata_targets(self):
targets_path = os.path.join(
Expand Down
17 changes: 10 additions & 7 deletions tuf/api/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ def __init__(
super().__init__(keyids, threshold, unrecognized_fields)
self.name = name
self.terminating = terminating
if paths and path_hash_prefixes:
if paths is not None and path_hash_prefixes is not None:
raise ValueError(
"Only one of the attributes 'paths' and"
"'path_hash_prefixes' can be set!"
Expand Down Expand Up @@ -804,9 +804,9 @@ def to_dict(self) -> Dict[str, Any]:
"terminating": self.terminating,
**base_role_dict,
}
if self.paths:
if self.paths is not None:
res_dict["paths"] = self.paths
elif self.path_hash_prefixes:
elif self.path_hash_prefixes is not None:
res_dict["path_hash_prefixes"] = self.path_hash_prefixes
return res_dict

Expand Down Expand Up @@ -909,17 +909,20 @@ def from_dict(cls, targets_dict: Dict[str, Any]) -> "Targets":
"""Creates Targets object from its dict representation."""
common_args = cls._common_fields_from_dict(targets_dict)
targets = targets_dict.pop("targets")
delegations = targets_dict.pop("delegations", None)
if delegations:
delegations = Delegations.from_dict(delegations)
try:
delegations_dict = targets_dict.pop("delegations")
except KeyError:
delegations = None
else:
delegations = Delegations.from_dict(delegations_dict)
# All fields left in the targets_dict are unrecognized.
return cls(*common_args, targets, delegations, targets_dict)

def to_dict(self) -> Dict[str, Any]:
"""Returns the dict representation of self."""
targets_dict = self._common_fields_to_dict()
targets_dict["targets"] = self.targets
if self.delegations:
if self.delegations is not None:
targets_dict["delegations"] = self.delegations.to_dict()
return targets_dict

Expand Down