Skip to content

Commit

Permalink
python type hint fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Whitehead <[email protected]>
  • Loading branch information
andrewwhitehead committed Mar 12, 2023
1 parent 6af1e93 commit b16c68a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 65 deletions.
5 changes: 4 additions & 1 deletion wrappers/python/indy_credx/bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
LOGGER = logging.getLogger(__name__)


JsonType = Union[dict, str, bytes, memoryview]


class ObjectHandle(c_int64):
"""Index of an active IndyObject instance."""

Expand Down Expand Up @@ -455,7 +458,7 @@ def object_get_type_name(handle: ObjectHandle) -> StrBuffer:
return result


def _object_from_json(method: str, value: Union[dict, str, bytes]) -> ObjectHandle:
def _object_from_json(method: str, value: JsonType) -> ObjectHandle:
if isinstance(value, dict):
value = json.dumps(value)
result = ObjectHandle()
Expand Down
119 changes: 55 additions & 64 deletions wrappers/python/indy_credx/types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Mapping, Optional, Sequence, Tuple, Union

from . import bindings
from .bindings import JsonType


class CredentialDefinition(bindings.IndyObject):
Expand All @@ -10,7 +11,7 @@ class CredentialDefinition(bindings.IndyObject):
def create(
cls,
origin_did: str,
schema: Union[str, "Schema"],
schema: Union[JsonType, "Schema"],
signature_type: str,
tag: str,
*,
Expand All @@ -30,7 +31,7 @@ def create(
)

@classmethod
def load(cls, value: Union[dict, str, bytes, memoryview]) -> "CredentialDefinition":
def load(cls, value: JsonType) -> "CredentialDefinition":
return CredentialDefinition(
bindings._object_from_json("credx_credential_definition_from_json", value)
)
Expand Down Expand Up @@ -58,9 +59,7 @@ def schema_id(self) -> str:

class CredentialDefinitionPrivate(bindings.IndyObject):
@classmethod
def load(
cls, value: Union[dict, str, bytes, memoryview]
) -> "CredentialDefinitionPrivate":
def load(cls, value: JsonType) -> "CredentialDefinitionPrivate":
return CredentialDefinitionPrivate(
bindings._object_from_json(
"credx_credential_definition_private_from_json", value
Expand All @@ -70,7 +69,7 @@ def load(

class KeyCorrectnessProof(bindings.IndyObject):
@classmethod
def load(cls, value: Union[dict, str, bytes, memoryview]) -> "KeyCorrectnessProof":
def load(cls, value: JsonType) -> "KeyCorrectnessProof":
return KeyCorrectnessProof(
bindings._object_from_json("credx_key_correctness_proof_from_json", value)
)
Expand All @@ -81,8 +80,8 @@ class CredentialOffer(bindings.IndyObject):
def create(
cls,
schema_id: str,
cred_def: Union[str, CredentialDefinition],
key_proof: Union[str, KeyCorrectnessProof],
cred_def: Union[JsonType, CredentialDefinition],
key_proof: Union[JsonType, KeyCorrectnessProof],
) -> "CredentialOffer":
if not isinstance(cred_def, bindings.IndyObject):
cred_def = CredentialDefinition.load(cred_def)
Expand All @@ -95,7 +94,7 @@ def create(
)

@classmethod
def load(cls, value: Union[dict, str, bytes, memoryview]) -> "CredentialOffer":
def load(cls, value: JsonType) -> "CredentialOffer":
return CredentialOffer(
bindings._object_from_json("credx_credential_offer_from_json", value)
)
Expand All @@ -106,10 +105,10 @@ class CredentialRequest(bindings.IndyObject):
def create(
cls,
prover_did: str,
cred_def: Union[str, CredentialDefinition],
master_secret: Union[str, "MasterSecret"],
cred_def: Union[JsonType, CredentialDefinition],
master_secret: Union[JsonType, "MasterSecret"],
master_secret_id: str,
cred_offer: Union[str, CredentialOffer],
cred_offer: Union[JsonType, CredentialOffer],
) -> Tuple["CredentialRequest", "CredentialRequestMetadata"]:
if not isinstance(cred_def, bindings.IndyObject):
cred_def = CredentialDefinition.load(cred_def)
Expand All @@ -127,17 +126,15 @@ def create(
return CredentialRequest(cred_def), CredentialRequestMetadata(cred_def_metadata)

@classmethod
def load(cls, value: Union[dict, str, bytes, memoryview]) -> "CredentialRequest":
def load(cls, value: JsonType) -> "CredentialRequest":
return CredentialRequest(
bindings._object_from_json("credx_credential_request_from_json", value)
)


class CredentialRequestMetadata(bindings.IndyObject):
@classmethod
def load(
cls, value: Union[dict, str, bytes, memoryview]
) -> "CredentialRequestMetadata":
def load(cls, value: JsonType) -> "CredentialRequestMetadata":
return CredentialRequestMetadata(
bindings._object_from_json(
"credx_credential_request_metadata_from_json", value
Expand All @@ -151,7 +148,7 @@ def create(cls) -> "MasterSecret":
return MasterSecret(bindings.create_master_secret())

@classmethod
def load(cls, value: Union[dict, str, bytes, memoryview]) -> "MasterSecret":
def load(cls, value: JsonType) -> "MasterSecret":
return MasterSecret(
bindings._object_from_json("credx_master_secret_from_json", value)
)
Expand All @@ -175,7 +172,7 @@ def create(
)

@classmethod
def load(cls, value: Union[dict, str, bytes, memoryview]) -> "Schema":
def load(cls, value: JsonType) -> "Schema":
return Schema(bindings._object_from_json("credx_schema_from_json", value))

@property
Expand All @@ -195,10 +192,10 @@ class Credential(bindings.IndyObject):
@classmethod
def create(
cls,
cred_def: Union[str, CredentialDefinition],
cred_def_private: Union[str, CredentialDefinitionPrivate],
cred_offer: Union[str, CredentialOffer],
cred_request: Union[str, CredentialRequest],
cred_def: Union[JsonType, CredentialDefinition],
cred_def_private: Union[JsonType, CredentialDefinitionPrivate],
cred_offer: Union[JsonType, CredentialOffer],
cred_request: Union[JsonType, CredentialRequest],
attr_raw_values: Mapping[str, str],
attr_enc_values: Mapping[str, str] = None,
revocation_config: "CredentialRevocationConfig" = None,
Expand Down Expand Up @@ -232,10 +229,10 @@ def create(

def process(
self,
cred_req_metadata: Union[str, CredentialRequestMetadata],
master_secret: Union[str, MasterSecret],
cred_def: Union[str, CredentialDefinition],
rev_reg_def: Optional[Union[str, "RevocationRegistryDefinition"]] = None,
cred_req_metadata: Union[JsonType, CredentialRequestMetadata],
master_secret: Union[JsonType, MasterSecret],
cred_def: Union[JsonType, CredentialDefinition],
rev_reg_def: Optional[Union[JsonType, "RevocationRegistryDefinition"]] = None,
) -> "Credential":
if not isinstance(cred_req_metadata, bindings.IndyObject):
cred_req_metadata = CredentialRequestMetadata.load(cred_req_metadata)
Expand All @@ -256,7 +253,7 @@ def process(
)

@classmethod
def load(cls, value: Union[dict, str, bytes, memoryview]) -> "Credential":
def load(cls, value: JsonType) -> "Credential":
return Credential(
bindings._object_from_json("credx_credential_from_json", value)
)
Expand Down Expand Up @@ -303,7 +300,7 @@ def rev_reg_index(self) -> Optional[int]:

class PresentationRequest(bindings.IndyObject):
@classmethod
def load(cls, value: Union[dict, str, bytes, memoryview]) -> "PresentationRequest":
def load(cls, value: JsonType) -> "PresentationRequest":
return PresentationRequest(
bindings._object_from_json("credx_presentation_request_from_json", value)
)
Expand Down Expand Up @@ -340,7 +337,7 @@ def add_attributes(
*referents: Sequence[str],
reveal: bool = True,
timestamp: int = None,
rev_state: Union[str, "CredentialRevocationState"] = None,
rev_state: Union[JsonType, "CredentialRevocationState"] = None,
):
if not referents:
return
Expand All @@ -353,7 +350,7 @@ def add_predicates(
cred: Credential,
*referents: Sequence[str],
timestamp: int = None,
rev_state: Union[str, "CredentialRevocationState"] = None,
rev_state: Union[JsonType, "CredentialRevocationState"] = None,
):
if not referents:
return
Expand All @@ -366,12 +363,12 @@ class Presentation(bindings.IndyObject):
@classmethod
def create(
cls,
pres_req: Union[str, PresentationRequest],
pres_req: Union[JsonType, PresentationRequest],
present_creds: PresentCredentials,
self_attest: Optional[Mapping[str, str]],
master_secret: Union[str, MasterSecret],
schemas: Sequence[Union[str, Schema]],
cred_defs: Sequence[Union[str, CredentialDefinition]],
master_secret: Union[JsonType, MasterSecret],
schemas: Sequence[Union[JsonType, Schema]],
cred_defs: Sequence[Union[JsonType, CredentialDefinition]],
) -> "Presentation":
if not isinstance(pres_req, bindings.IndyObject):
pres_req = PresentationRequest.load(pres_req)
Expand Down Expand Up @@ -420,19 +417,19 @@ def create(
)

@classmethod
def load(cls, value: Union[dict, str, bytes, memoryview]) -> "Presentation":
def load(cls, value: JsonType) -> "Presentation":
return Presentation(
bindings._object_from_json("credx_presentation_from_json", value)
)

def verify(
self,
pres_req: Union[str, PresentationRequest],
schemas: Sequence[Union[str, Schema]],
cred_defs: Sequence[Union[str, CredentialDefinition]],
rev_reg_defs: Sequence[Union[str, "RevocationRegistryDefinition"]] = None,
pres_req: Union[JsonType, PresentationRequest],
schemas: Sequence[Union[JsonType, Schema]],
cred_defs: Sequence[Union[JsonType, CredentialDefinition]],
rev_reg_defs: Sequence[Union[JsonType, "RevocationRegistryDefinition"]] = None,
rev_reg_entries: Mapping[
str, Mapping[int, Union[str, "RevocationRegistry"]]
str, Mapping[int, Union[JsonType, "RevocationRegistry"]]
] = None,
) -> bool:
if not isinstance(pres_req, bindings.IndyObject):
Expand Down Expand Up @@ -483,7 +480,7 @@ class RevocationRegistryDefinition(bindings.IndyObject):
def create(
cls,
origin_did: str,
cred_def: Union[str, CredentialDefinition],
cred_def: Union[JsonType, CredentialDefinition],
tag: str,
registry_type: str,
max_cred_num: int,
Expand Down Expand Up @@ -520,9 +517,7 @@ def create(
)

@classmethod
def load(
cls, value: Union[dict, str, bytes, memoryview]
) -> "RevocationRegistryDefinition":
def load(cls, value: JsonType) -> "RevocationRegistryDefinition":
return RevocationRegistryDefinition(
bindings._object_from_json(
"credx_revocation_registry_definition_from_json", value
Expand Down Expand Up @@ -574,9 +569,7 @@ def tails_location(self) -> str:

class RevocationRegistryDefinitionPrivate(bindings.IndyObject):
@classmethod
def load(
cls, value: Union[dict, str, bytes, memoryview]
) -> "RevocationRegistryDefinitionPrivate":
def load(cls, value: JsonType) -> "RevocationRegistryDefinitionPrivate":
return RevocationRegistryDefinitionPrivate(
bindings._object_from_json(
"credx_revocation_registry_definition_private_from_json", value
Expand All @@ -586,14 +579,14 @@ def load(

class RevocationRegistry(bindings.IndyObject):
@classmethod
def load(cls, value: Union[dict, str, bytes, memoryview]) -> "RevocationRegistry":
def load(cls, value: JsonType) -> "RevocationRegistry":
return RevocationRegistry(
bindings._object_from_json("credx_revocation_registry_from_json", value)
)

def revoke_credential(
self,
rev_reg_def: Union[str, RevocationRegistryDefinition],
rev_reg_def: Union[JsonType, RevocationRegistryDefinition],
cred_rev_idx: int,
tails_path: str,
) -> "RevocationRegistryDelta":
Expand All @@ -606,7 +599,7 @@ def revoke_credential(

def update(
self,
rev_reg_def: Union[str, RevocationRegistryDefinition],
rev_reg_def: Union[JsonType, RevocationRegistryDefinition],
issued: Sequence[int],
revoked: Sequence[int],
tails_path: str,
Expand All @@ -621,17 +614,15 @@ def update(

class RevocationRegistryDelta(bindings.IndyObject):
@classmethod
def load(
cls, value: Union[dict, str, bytes, memoryview]
) -> "RevocationRegistryDelta":
def load(cls, value: JsonType) -> "RevocationRegistryDelta":
return RevocationRegistryDelta(
bindings._object_from_json(
"credx_revocation_registry_delta_from_json", value
)
)

def update_with(
self, next_delta: Union[str, "RevocationRegistryDelta"]
self, next_delta: Union[JsonType, "RevocationRegistryDelta"]
) -> "RevocationRegistryDelta":
if not isinstance(next_delta, bindings.IndyObject):
next_delta = RevocationRegistryDelta.load(next_delta)
Expand All @@ -643,9 +634,11 @@ def update_with(
class CredentialRevocationConfig:
def __init__(
self,
rev_reg_def: Union[str, "RevocationRegistryDefinition"] = None,
rev_reg_def_private: Union[str, "RevocationRegistryDefinitionPrivate"] = None,
rev_reg: Union[str, "RevocationRegistry"] = None,
rev_reg_def: Union[JsonType, "RevocationRegistryDefinition"] = None,
rev_reg_def_private: Union[
JsonType, "RevocationRegistryDefinitionPrivate"
] = None,
rev_reg: Union[JsonType, "RevocationRegistry"] = None,
rev_reg_index: int = None,
rev_reg_used: Sequence[int] = None,
tails_path: str = None,
Expand Down Expand Up @@ -681,8 +674,8 @@ class CredentialRevocationState(bindings.IndyObject):
@classmethod
def create(
cls,
rev_reg_def: Union[str, RevocationRegistryDefinition],
rev_reg_delta: Union[str, RevocationRegistryDelta],
rev_reg_def: Union[JsonType, RevocationRegistryDefinition],
rev_reg_delta: Union[JsonType, RevocationRegistryDelta],
cred_rev_id: int,
timestamp: int,
tails_path: str,
Expand All @@ -703,17 +696,15 @@ def create(
)

@classmethod
def load(
cls, value: Union[dict, str, bytes, memoryview]
) -> "CredentialRevocationState":
def load(cls, value: JsonType) -> "CredentialRevocationState":
return CredentialRevocationState(
bindings._object_from_json("credx_revocation_state_from_json", value)
)

def update(
self,
rev_reg_def: Union[str, RevocationRegistryDefinition],
rev_reg_delta: Union[str, RevocationRegistryDelta],
rev_reg_def: Union[JsonType, RevocationRegistryDefinition],
rev_reg_delta: Union[JsonType, RevocationRegistryDelta],
rev_reg_index: int,
timestamp: int,
tails_path: str,
Expand Down

0 comments on commit b16c68a

Please sign in to comment.