Common: Fix type hints in credentials.py

This commit is contained in:
Mike Salvatore 2022-07-15 12:34:42 -04:00
parent 62ce91b59b
commit 213b161d1a
1 changed files with 19 additions and 14 deletions

View File

@ -1,7 +1,7 @@
from __future__ import annotations from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
from typing import Any, Mapping, MutableMapping, Optional, Sequence, Type from typing import Any, Mapping, Optional, Type
from marshmallow import Schema, fields, post_load, pre_dump from marshmallow import Schema, fields, post_load, pre_dump
from marshmallow.exceptions import MarshmallowError from marshmallow.exceptions import MarshmallowError
@ -40,6 +40,9 @@ CREDENTIAL_COMPONENT_TYPE_TO_CLASS_SCHEMA: Mapping[CredentialComponentType, Sche
CredentialComponentType.USERNAME: UsernameSchema(), CredentialComponentType.USERNAME: UsernameSchema(),
} }
CredentialComponentMapping = Optional[Mapping[str, Any]]
CredentialsMapping = Mapping[str, CredentialComponentMapping]
class CredentialsSchema(Schema): class CredentialsSchema(Schema):
identity = fields.Mapping(allow_none=True) identity = fields.Mapping(allow_none=True)
@ -47,19 +50,23 @@ class CredentialsSchema(Schema):
@post_load @post_load
def _make_credentials( def _make_credentials(
self, data: MutableMapping, **kwargs: Mapping[str, Any] self,
) -> Mapping[str, Sequence[Mapping[str, Any]]]: credentials: CredentialsMapping,
if not any(data.values()): **kwargs: Mapping[str, Any],
) -> Mapping[str, Optional[ICredentialComponent]]:
if not any(credentials.values()):
raise InvalidCredentialsError("At least one credentials component must be defined") raise InvalidCredentialsError("At least one credentials component must be defined")
data["identity"] = CredentialsSchema._build_credential_component(data["identity"]) parsed_credentials = {
data["secret"] = CredentialsSchema._build_credential_component(data["secret"]) key: CredentialsSchema._build_credential_component(credential_component_mapping)
for key, credential_component_mapping in credentials.items()
}
return data return parsed_credentials
@staticmethod @staticmethod
def _build_credential_component( def _build_credential_component(
credential_component: Optional[Mapping[str, Any]] credential_component: CredentialComponentMapping,
) -> Optional[ICredentialComponent]: ) -> Optional[ICredentialComponent]:
if credential_component is None: if credential_component is None:
return None return None
@ -84,9 +91,7 @@ class CredentialsSchema(Schema):
raise InvalidCredentialComponentError(credential_component_class, str(err)) raise InvalidCredentialComponentError(credential_component_class, str(err))
@pre_dump @pre_dump
def _serialize_credentials( def _serialize_credentials(self, credentials: Credentials, **kwargs) -> CredentialsMapping:
self, credentials: Credentials, **kwargs
) -> Mapping[str, Sequence[Mapping[str, Any]]]:
data = {} data = {}
data["identity"] = CredentialsSchema._serialize_credential_component(credentials.identity) data["identity"] = CredentialsSchema._serialize_credential_component(credentials.identity)
@ -97,7 +102,7 @@ class CredentialsSchema(Schema):
@staticmethod @staticmethod
def _serialize_credential_component( def _serialize_credential_component(
credential_component: Optional[ICredentialComponent], credential_component: Optional[ICredentialComponent],
) -> Optional[Mapping[str, Any]]: ) -> CredentialComponentMapping:
if credential_component is None: if credential_component is None:
return None return None
@ -125,7 +130,7 @@ class Credentials(IJSONSerializable):
raise InvalidCredentialsError(err) raise InvalidCredentialsError(err)
@staticmethod @staticmethod
def from_mapping(credentials: Mapping) -> Credentials: def from_mapping(credentials: CredentialsMapping) -> Credentials:
""" """
Construct a Credentials object from a Mapping Construct a Credentials object from a Mapping
@ -167,7 +172,7 @@ class Credentials(IJSONSerializable):
raise InvalidCredentialsError(str(err)) raise InvalidCredentialsError(str(err))
@staticmethod @staticmethod
def to_mapping(credentials: Credentials) -> Mapping: def to_mapping(credentials: Credentials) -> CredentialsMapping:
""" """
Serialize a Credentials object to a Mapping Serialize a Credentials object to a Mapping