Skip to content

Metadata API: Improve metadata.signed "typing"  #1433

Closed

Description

@jku

There's two case I'd like to see improved in metadata signed handling:

  1. Usually I call Metadata factory constructors (e.g. Metadata.from_bytes()) knowing what signed type should be, yet I can't enforce this
  2. 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) 

Activity

  1. jku commented on Jun 15, 2021

    @jku
    MemberAuthor

    so this does not work:

    root = Metadata[Root].from_bytes(data)
    

    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)
    

    The signatures for those alternative could be:

    Metadata.from_bytes(data: bytes, signed_type: Optional[Type[T]] = None) -> Metadata[T]
    Metadata.root_from_bytes(data: bytes) -> Metadata[Root]
    

    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...

  2. added 2 commits that reference this issue on Jun 20, 2021
    b7e8d88
    0baff95
  3. jku commented on Jun 22, 2021

    @jku
    MemberAuthor

    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.

  4. added this to the weeks26-27 milestone on Jun 23, 2021
  5. added
    backlog
    on Jul 7, 2021
  6. modified the milestones: weeks26-27, Sprint 4 on Jul 7, 2021
  7. removed
    backlog
    on Jul 7, 2021
  8. modified the milestones: Sprint 4, Sprint 5 on Jul 28, 2021
  9. added 4 commits that reference this issue on Aug 16, 2021
    0e644c1
    d9fbb55
    2aeed7d
    13e20e9
  10. added a commit that references this issue on Aug 24, 2021
    34a055e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions