You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There's two case I'd like to see improved in metadata signed handling:
Usually I call Metadata factory constructors (e.g. Metadata.from_bytes()) knowing what signed type should be, yet I can't enforce this
Using metadata.signed in a type annotated fashion (so that it's annotated as the actual type: Root/Snapshot/Timestamp/Targets) would be fantastic.
Item 1 could be fixed with just adding an optional type argument to the constructors (Metadata.from_bytes(data, type=Root)) and raising if it does not match.
But I wonder if some Python wizard could fix both by making Metadata generic over Root/Snapshot/Timestamp/Targets:
try:
root = Metadata[Root].from_bytes(data)
except DeserializationError as e:
# this happens (among other things) if signed type does not match Root
raise exceptions.RepositoryError("Failed to load root") from e
for key in root.signed.keys.items():
# MAGIC!: keys, key and keytype are all annotated here because root
# is Metadata[Root] and so root.signed is Root
print(key.keytype)
because the "specific generic type" (Root in this case) is not an actual type available during runtime at all: there is no way to affect runtime based on that type (e.g. to raise an exception if it does not match).
It might still work if the type is given as parameter or the method contains it as constant (since then we obviously have the type available at runtime):
root = Metadata.from_bytes(data, signed_type=Root)
# or
root = Metadata.root_from_bytes(data)
Forgot to mention here: there's a linked PR that implements the Metadata.from_bytes(data: bytes, signed_type: Optional[Type[T]] = None) -> Metadata[T]
version.
It's not the prettiest thing in the world with the type argument but implementation is not too complex and using it feels quite nice.
Activity
jku commentedon Jun 15, 2021
so this does not work:
because the "specific generic type" (Root in this case) is not an actual type available during runtime at all: there is no way to affect runtime based on that type (e.g. to raise an exception if it does not match).
It might still work if the type is given as parameter or the method contains it as constant (since then we obviously have the type available at runtime):
The signatures for those alternative could be:
and I think here A) metadata.signed could be correctly typed and B) we could raise if the signed type does not match. Will have to test this...
Metadata API: Make Metadata Generic
Metadata API: Make Metadata Generic
jku commentedon Jun 22, 2021
Forgot to mention here: there's a linked PR that implements the
Metadata.from_bytes(data: bytes, signed_type: Optional[Type[T]] = None) -> Metadata[T]version.
It's not the prettiest thing in the world with the type argument but implementation is not too complex and using it feels quite nice.
Metadata API: Make Metadata Generic
Metadata API: Make Metadata Generic
Metadata API: Make Metadata Generic
Metadata API: Make Metadata Generic
Metadata API: Make Metadata Generic