Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in_toto_attestation/v1: fix type hints #301

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions python/in_toto_attestation/v1/resource_descriptor.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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":
Comment on lines -22 to +33
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NB: I thought a bit about this, and I think this hint was wrong to begin with: type[T] is used an annotation for class objects themselves, which isn't what this API takes.

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")
21 changes: 11 additions & 10 deletions python/in_toto_attestation/v1/statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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")