Skip to content

Commit c79c7c6

Browse files
author
Jussi Kukkonen
committed
Metadata: docs: Explain why Metadata is Generic
Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
1 parent 4213f46 commit c79c7c6

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

tuf/api/metadata.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ class Metadata(Generic[T]):
6969
Provides methods to convert to and from dictionary, read and write to and
7070
from file and to create and verify metadata signatures.
7171
72+
Metadata[T] is a generic container type where T can be any one type of
73+
[Root, Timestamp, Snapshot, Targets]. The purpose of this is to allow
74+
type checking of the signed attribute in code using Metadata::
75+
76+
root_md = Metadata.from_file("root.json", signed_type=Root)
77+
# root_md type is now Metadata[Root]. This means signed and its
78+
# attributes like consistent_snapshot are now statically typed and the
79+
# types can be verified by static type checkers and shown by IDEs
80+
print(root_md.signed.consistent_snapshot)
81+
82+
Using the signed_type argument in factory constructors is not required but
83+
not doing so means T is not a specific type so static typing cannot happen.
84+
7285
Attributes:
7386
signed: A subclass of Signed, which has the actual metadata payload,
7487
i.e. one of Targets, Snapshot, Timestamp or Root.

0 commit comments

Comments
 (0)