Skip to content

Commit 5e65d67

Browse files
author
Jussi Kukkonen
committed
Metadata API: Make Metadata constructor Generic
In from_dict() we cannot at static type check time tell what the specific signed type so have to cast, but the constructor can be properly Generic so in the following md is automatically annotated as "Metadata[Root]" and md.signed as "Root": root = Root(...) md = Metadata(root, OrderedDict()) Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
1 parent 0baff95 commit 5e65d67

1 file changed

Lines changed: 4 additions & 6 deletions

File tree

tuf/api/metadata.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,8 @@ class Metadata(Generic[T]):
7777
signing the canonical serialized representation of 'signed'.
7878
"""
7979

80-
def __init__(
81-
self, signed: "Signed", signatures: "OrderedDict[str, Signature]"
82-
):
83-
# init does not verify type constraint T: only from_bytes() does
84-
self.signed: T = cast(T, signed)
80+
def __init__(self, signed: T, signatures: "OrderedDict[str, Signature]"):
81+
self.signed: T = signed
8582
self.signatures = signatures
8683

8784
@classmethod
@@ -127,7 +124,8 @@ def from_dict(cls, metadata: Dict[str, Any]) -> "Metadata":
127124
signatures[sig.keyid] = sig
128125

129126
return cls(
130-
signed=inner_cls.from_dict(metadata.pop("signed")),
127+
# Specific type T is not known at static type check time: cast
128+
signed=cast(T, inner_cls.from_dict(metadata.pop("signed"))),
131129
signatures=signatures,
132130
)
133131

0 commit comments

Comments
 (0)