Skip to content

Commit

Permalink
🏷️ Wrote an as_dict type checker for Config
Browse files Browse the repository at this point in the history
  • Loading branch information
jemrobinson committed Jul 21, 2023
1 parent a34a6e9 commit 30fdc09
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
19 changes: 10 additions & 9 deletions data_safe_haven/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion data_safe_haven/functions/__init__.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -21,6 +21,7 @@

__all__ = [
"alphanumeric",
"as_dict",
"b64decode",
"b64encode",
"hex_string",
Expand Down
12 changes: 11 additions & 1 deletion data_safe_haven/functions/miscellaneous.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down

0 comments on commit 30fdc09

Please sign in to comment.