@@ -138,7 +138,9 @@ def __init__(
138
138
if unrecognized_fields is None :
139
139
unrecognized_fields = {}
140
140
141
- self .unrecognized_fields = unrecognized_fields
141
+ self .unrecognized_fields = type (unrecognized_fields )(
142
+ sorted (unrecognized_fields .items (), key = lambda x : x [0 ])
143
+ )
142
144
143
145
def __eq__ (self , other : Any ) -> bool :
144
146
if not isinstance (other , Metadata ):
@@ -293,7 +295,7 @@ def to_bytes(
293
295
294
296
return serializer .serialize (self )
295
297
296
- def to_dict (self ) -> Dict [str , Any ]:
298
+ def to_dict (self ) -> Mapping [str , Any ]:
297
299
"""Returns the dict representation of self."""
298
300
299
301
signatures = [sig .to_dict () for sig in self .signatures .values ()]
@@ -582,7 +584,7 @@ def _common_fields_from_dict(
582
584
583
585
return version , spec_version , expires
584
586
585
- def _common_fields_to_dict (self ) -> Dict [str , Any ]:
587
+ def _common_fields_to_dict (self ) -> Mapping [str , Any ]:
586
588
"""Returns dict representation of common fields of ``Signed`` instances.
587
589
588
590
See ``{Root, Timestamp, Snapshot, Targets}.to_dict`` methods for usage.
@@ -682,7 +684,7 @@ def from_dict(cls, keyid: str, key_dict: Dict[str, Any]) -> "Key":
682
684
# All fields left in the key_dict are unrecognized.
683
685
return cls (keyid , keytype , scheme , keyval , key_dict )
684
686
685
- def to_dict (self ) -> Dict [str , Any ]:
687
+ def to_dict (self ) -> Mapping [str , Any ]:
686
688
"""Returns the dictionary representation of self."""
687
689
return {
688
690
"keytype" : self .keytype ,
@@ -840,7 +842,7 @@ def from_dict(cls, role_dict: Dict[str, Any]) -> "Role":
840
842
# All fields left in the role_dict are unrecognized.
841
843
return cls (keyids , threshold , role_dict )
842
844
843
- def to_dict (self ) -> Dict [str , Any ]:
845
+ def to_dict (self ) -> Mapping [str , Any ]:
844
846
"""Returns the dictionary representation of self."""
845
847
return {
846
848
"keyids" : self .keyids ,
@@ -927,12 +929,15 @@ def from_dict(cls, signed_dict: Dict[str, Any]) -> "Root":
927
929
# All fields left in the signed_dict are unrecognized.
928
930
return cls (* common_args , keys , roles , consistent_snapshot , signed_dict )
929
931
930
- def to_dict (self ) -> Dict [str , Any ]:
932
+ def to_dict (self ) -> Mapping [str , Any ]:
931
933
"""Returns the dict representation of self."""
932
934
root_dict = self ._common_fields_to_dict ()
933
- keys = {keyid : key .to_dict () for (keyid , key ) in self .keys .items ()}
935
+ keys = {
936
+ keyid : key .to_dict ()
937
+ for (keyid , key ) in sorted (self .keys .items (), key = lambda x : x [0 ])
938
+ }
934
939
roles = {}
935
- for role_name , role in self .roles .items ():
940
+ for role_name , role in sorted ( self .roles .items (), key = lambda x : x [ 0 ] ):
936
941
roles [role_name ] = role .to_dict ()
937
942
if self .consistent_snapshot is not None :
938
943
root_dict ["consistent_snapshot" ] = self .consistent_snapshot
@@ -1274,11 +1279,13 @@ def from_dict(cls, signed_dict: Dict[str, Any]) -> "Snapshot":
1274
1279
# All fields left in the snapshot_dict are unrecognized.
1275
1280
return cls (* common_args , meta , signed_dict )
1276
1281
1277
- def to_dict (self ) -> Dict [str , Any ]:
1282
+ def to_dict (self ) -> Mapping [str , Any ]:
1278
1283
"""Returns the dict representation of self."""
1279
1284
snapshot_dict = self ._common_fields_to_dict ()
1280
1285
meta_dict = {}
1281
- for meta_path , meta_info in self .meta .items ():
1286
+ for meta_path , meta_info in sorted (
1287
+ self .meta .items (), key = lambda x : x [0 ]
1288
+ ):
1282
1289
meta_dict [meta_path ] = meta_info .to_dict ()
1283
1290
1284
1291
snapshot_dict ["meta" ] = meta_dict
@@ -1695,9 +1702,12 @@ def from_dict(cls, delegations_dict: Dict[str, Any]) -> "Delegations":
1695
1702
# All fields left in the delegations_dict are unrecognized.
1696
1703
return cls (keys_res , roles_res , succinct_roles_info , delegations_dict )
1697
1704
1698
- def to_dict (self ) -> Dict [str , Any ]:
1705
+ def to_dict (self ) -> Mapping [str , Any ]:
1699
1706
"""Returns the dict representation of self."""
1700
- keys = {keyid : key .to_dict () for keyid , key in self .keys .items ()}
1707
+ keys = {
1708
+ keyid : key .to_dict ()
1709
+ for keyid , key in sorted (self .keys .items (), key = lambda x : x [0 ])
1710
+ }
1701
1711
res_dict : Dict [str , Any ] = {
1702
1712
"keys" : keys ,
1703
1713
** self .unrecognized_fields ,
@@ -1967,11 +1977,13 @@ def from_dict(cls, signed_dict: Dict[str, Any]) -> "Targets":
1967
1977
# All fields left in the targets_dict are unrecognized.
1968
1978
return cls (* common_args , res_targets , delegations , signed_dict )
1969
1979
1970
- def to_dict (self ) -> Dict [str , Any ]:
1980
+ def to_dict (self ) -> Mapping [str , Any ]:
1971
1981
"""Returns the dict representation of self."""
1972
1982
targets_dict = self ._common_fields_to_dict ()
1973
1983
targets = {}
1974
- for target_path , target_file_obj in self .targets .items ():
1984
+ for target_path , target_file_obj in sorted (
1985
+ self .targets .items (), key = lambda x : x [0 ]
1986
+ ):
1975
1987
targets [target_path ] = target_file_obj .to_dict ()
1976
1988
targets_dict [_TARGETS ] = targets
1977
1989
if self .delegations is not None :
0 commit comments