From 695a417dc3f626f3290504dcd587a1fb6b99f990 Mon Sep 17 00:00:00 2001 From: William Woodruff Date: Thu, 7 Dec 2023 17:12:40 -0500 Subject: [PATCH] in_toto_attestation/v1: fix type hints Signed-off-by: William Woodruff --- .../v1/resource_descriptor.py | 21 ++++++++++++++----- python/in_toto_attestation/v1/statement.py | 21 ++++++++++--------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/python/in_toto_attestation/v1/resource_descriptor.py b/python/in_toto_attestation/v1/resource_descriptor.py index 9f617390..3f53ccfd 100644 --- a/python/in_toto_attestation/v1/resource_descriptor.py +++ b/python/in_toto_attestation/v1/resource_descriptor.py @@ -1,12 +1,23 @@ # Wrapper class for in-toto attestation ResourceDescriptor protos. +import json + +import google.protobuf.json_format as pb_json import in_toto_attestation.v1.resource_descriptor_pb2 as rdpb from google.protobuf.struct_pb2 import Value -import google.protobuf.json_format as pb_json -import json + class ResourceDescriptor: - def __init__(self, name: str='', uri: str='', digest: dict=None, content: bytes=bytes(), download_location: str='', media_type: str='', annotations: dict=None) -> None: + def __init__( + self, + name: str = "", + uri: str = "", + digest: dict = None, + content: bytes = bytes(), + download_location: str = "", + media_type: str = "", + annotations: dict = None, + ) -> None: self.pb = rdpb.ResourceDescriptor() self.pb.name = name self.pb.uri = uri @@ -19,12 +30,12 @@ def __init__(self, name: str='', uri: str='', digest: dict=None, content: bytes= self.pb.annotations.update(annotations) @staticmethod - def copy_from_pb(proto: type[rdpb.ResourceDescriptor]) -> 'ResourceDescriptor': + def copy_from_pb(proto: rdpb.ResourceDescriptor) -> "ResourceDescriptor": rd = ResourceDescriptor() rd.pb.CopyFrom(proto) return rd def validate(self) -> None: # at least one of name, URI or digest are required - if self.pb.name == '' and self.pb.uri == '' and len(self.pb.digest) == 0: + if self.pb.name == "" and self.pb.uri == "" and len(self.pb.digest) == 0: raise ValueError("At least one of name, URI, or digest need to be set") diff --git a/python/in_toto_attestation/v1/statement.py b/python/in_toto_attestation/v1/statement.py index 8297cb39..3900a50b 100644 --- a/python/in_toto_attestation/v1/statement.py +++ b/python/in_toto_attestation/v1/statement.py @@ -3,7 +3,8 @@ import in_toto_attestation.v1.statement_pb2 as spb from in_toto_attestation.v1.resource_descriptor import ResourceDescriptor -STATEMENT_TYPE_URI = 'https://in-toto.io/Statement/v1' +STATEMENT_TYPE_URI = "https://in-toto.io/Statement/v1" + class Statement: def __init__(self, subjects: list, predicate_type: str, predicate: dict) -> None: @@ -14,17 +15,17 @@ def __init__(self, subjects: list, predicate_type: str, predicate: dict) -> None self.pb.predicate.update(predicate) @staticmethod - def copy_from_pb(proto: type[spb.Statement]) -> 'Statement': - stmt = Statement([], '', {}) + def copy_from_pb(proto: spb.Statement) -> "Statement": + stmt = Statement([], "", {}) stmt.pb.CopyFrom(proto) return stmt - def validate(self) -> None: + def validate(self) -> None: if self.pb.type != STATEMENT_TYPE_URI: - raise ValueError('Wrong statement type') + raise ValueError("Wrong statement type") if len(self.pb.subject) == 0: - raise ValueError('At least one subject required') + raise ValueError("At least one subject required") # check all resource descriptors in the subject subject = self.pb.subject @@ -36,10 +37,10 @@ def validate(self) -> None: if len(rd.pb.digest) == 0: # return index in the subjects list in case of failure: # can't assume any other fields in subject are set - raise ValueError('At least one digest required (subject {0})'.format(i)) + raise ValueError("At least one digest required (subject {0})".format(i)) - if self.pb.predicate_type == '': - raise ValueError('Predicate type required') + if self.pb.predicate_type == "": + raise ValueError("Predicate type required") if len(self.pb.predicate) == 0: - raise ValueError('Predicate object required') + raise ValueError("Predicate object required")