From 30fdc09f334a1f0e09e863eb0d603d930c9aebbc Mon Sep 17 00:00:00 2001 From: James Robinson Date: Fri, 21 Jul 2023 18:51:37 +0100 Subject: [PATCH] :label: Wrote an as_dict type checker for Config --- data_safe_haven/config/config.py | 19 ++++++++++--------- data_safe_haven/functions/__init__.py | 3 ++- data_safe_haven/functions/miscellaneous.py | 12 +++++++++++- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/data_safe_haven/config/config.py b/data_safe_haven/config/config.py index 858eb306e9..21beca9be5 100644 --- a/data_safe_haven/config/config.py +++ b/data_safe_haven/config/config.py @@ -17,6 +17,7 @@ from data_safe_haven.external import AzureApi from data_safe_haven.functions import ( alphanumeric, + as_dict, b64decode, b64encode, validate_aad_guid, @@ -26,7 +27,7 @@ validate_ip_address, validate_timezone, ) -from data_safe_haven.utility import SoftwarePackageCategory, YamlType +from data_safe_haven.utility import SoftwarePackageCategory from .backend_settings import BackendSettings @@ -66,7 +67,7 @@ def validate(self) -> None: def to_dict(self) -> Dict[str, str]: self.validate() - return chili.encode(self) # type: ignore + return as_dict(chili.encode(self)) @dataclass @@ -102,7 +103,7 @@ def validate(self) -> None: def to_dict(self) -> Dict[str, str]: self.validate() - return chili.encode(self) # type: ignore + return as_dict(chili.encode(self)) @dataclass @@ -121,7 +122,7 @@ def validate(self) -> None: def to_dict(self) -> Dict[str, Any]: self.validate() - return chili.encode(self) # type: ignore + return as_dict(chili.encode(self)) @dataclass @@ -167,7 +168,7 @@ def validate(self) -> None: def to_dict(self) -> Dict[str, Any]: self.validate() - return chili.encode(self) # type: ignore + return as_dict(chili.encode(self)) @dataclass @@ -188,7 +189,7 @@ def validate(self) -> None: def to_dict(self) -> Dict[str, bool]: self.validate() - return chili.encode(self) # type: ignore + return as_dict(chili.encode(self)) @dataclass class ConfigSectionResearchDesktopOpts: @@ -203,7 +204,7 @@ def validate(self) -> None: def to_dict(self) -> Dict[str, str]: self.validate() - return chili.encode(self) # type: ignore + return as_dict(chili.encode(self)) data_provider_ip_addresses: List[str] = field(default_factory=list) index: int = 0 @@ -242,7 +243,7 @@ def validate(self) -> None: def to_dict(self) -> Dict[str, Any]: self.validate() - return chili.encode(self) # type: ignore + return as_dict(chili.encode(self)) @dataclass @@ -259,7 +260,7 @@ def validate(self) -> None: def to_dict(self) -> Dict[str, str]: self.validate() - return chili.encode(self) # type: ignore + return as_dict(chili.encode(self)) class Config: diff --git a/data_safe_haven/functions/__init__.py b/data_safe_haven/functions/__init__.py index 3b4b54517a..33fcf2b6ee 100644 --- a/data_safe_haven/functions/__init__.py +++ b/data_safe_haven/functions/__init__.py @@ -1,4 +1,4 @@ -from .miscellaneous import ordered_private_dns_zones, time_as_string +from .miscellaneous import as_dict, ordered_private_dns_zones, time_as_string from .strings import ( alphanumeric, b64decode, @@ -21,6 +21,7 @@ __all__ = [ "alphanumeric", + "as_dict", "b64decode", "b64encode", "hex_string", diff --git a/data_safe_haven/functions/miscellaneous.py b/data_safe_haven/functions/miscellaneous.py index 696d37c918..ab435a6207 100644 --- a/data_safe_haven/functions/miscellaneous.py +++ b/data_safe_haven/functions/miscellaneous.py @@ -1,11 +1,21 @@ # Standard library imports import datetime -from typing import List, Optional +from typing import Any, Dict, List, Optional # Third-party imports import pytz +def as_dict(object: Any) -> Dict[str, Any]: + if ( + not isinstance(object, dict) + and hasattr(object, "keys") + and all(isinstance(x, str) for x in object.keys()) + ): + raise TypeError(f"{object} {type(object)} is not a valid Dict[str, Any]") + return object + + def ordered_private_dns_zones(resource_type: Optional[str] = None) -> List[str]: """ Return required DNS zones for a given resource type.