Skip to content

Commit 5d3e766

Browse files
committed
New API: remove side effect when calling from_dict
Currently, when we are calling "from_dict" from one of the classes, we are destroying the dict passed as argument. The reason is that we are using dict.pop() function to access the dict members, but that seems wasteful and unnecessary when we can access the fields the traditional way using the [] operator. Signed-off-by: Martin Vrachev <[email protected]>
1 parent e65d6a7 commit 5d3e766

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

tuf/api/metadata.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,6 @@ def from_dict(cls, metadata: Mapping[str, Any]) -> "Metadata":
7070
KeyError: The metadata dict format is invalid.
7171
ValueError: The metadata has an unrecognized signed._type field.
7272
73-
Side Effect:
74-
Destroys the metadata Mapping passed by reference.
75-
7673
Returns:
7774
A TUF Metadata object.
7875
@@ -92,12 +89,12 @@ def from_dict(cls, metadata: Mapping[str, Any]) -> "Metadata":
9289
raise ValueError(f'unrecognized metadata type "{_type}"')
9390

9491
signatures = []
95-
for signature in metadata.pop("signatures"):
92+
for signature in metadata["signatures"]:
9693
signature_obj = Signature.from_dict(signature)
9794
signatures.append(signature_obj)
9895

9996
return cls(
100-
signed=inner_cls.from_dict(metadata.pop("signed")),
97+
signed=inner_cls.from_dict(metadata["signed"]),
10198
signatures=signatures,
10299
)
103100

@@ -330,10 +327,10 @@ def _common_fields_from_dict(signed_dict: Mapping[str, Any]) -> list:
330327
See '{Root, Timestamp, Snapshot, Targets}.from_dict' methods for usage.
331328
332329
"""
333-
_type = signed_dict.pop("_type")
334-
version = signed_dict.pop("version")
335-
spec_version = signed_dict.pop("spec_version")
336-
expires_str = signed_dict.pop("expires")
330+
_type = signed_dict["_type"]
331+
version = signed_dict["version"]
332+
spec_version = signed_dict["spec_version"]
333+
expires_str = signed_dict["expires"]
337334
# Convert 'expires' TUF metadata string to a datetime object, which is
338335
# what the constructor expects and what we store. The inverse operation
339336
# is implemented in '_common_fields_to_dict'.
@@ -425,9 +422,9 @@ def __init__(
425422
def from_dict(cls, root_dict: Mapping[str, Any]) -> "Root":
426423
"""Creates Root object from its dict representation. """
427424
common_args = cls._common_fields_from_dict(root_dict)
428-
consistent_snapshot = root_dict.pop("consistent_snapshot")
429-
keys = root_dict.pop("keys")
430-
roles = root_dict.pop("roles")
425+
consistent_snapshot = root_dict["consistent_snapshot"]
426+
keys = root_dict["keys"]
427+
roles = root_dict["roles"]
431428
return cls(*common_args, consistent_snapshot, keys, roles)
432429

433430
def to_dict(self) -> Dict[str, Any]:
@@ -751,7 +748,7 @@ def from_dict(cls, targets_dict: Mapping[str, Any]) -> "Targets":
751748
**targets_dict["targets"][target_path]
752749
)
753750

754-
delegations = targets_dict.pop("delegations")
751+
delegations = targets_dict["delegations"]
755752
return cls(*common_args, targets, delegations)
756753

757754
def to_dict(self) -> Dict[str, Any]:

0 commit comments

Comments
 (0)