Skip to content

Commit

Permalink
More types (2)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed Feb 29, 2024
1 parent 1e73132 commit 6108044
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/ansible_navigator/actions/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ def _collect_image_list(self) -> None:

try:
legacy_check = details["config"]["working_dir"] == "/runner"
except KeyError:
except (KeyError, TypeError):
legacy_check = False

# podman has a root label
Expand Down
5 changes: 4 additions & 1 deletion src/ansible_navigator/image_manager/inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ def parse(command: Command) -> None:
:param command: Image inspection command object
"""
obj = json.loads(command.stdout)
snake = pascal_to_snake(obj[0])
if not isinstance(obj, list):
msg = f"Unexpected {obj} parsing image inspection."
raise RuntimeError(msg)
snake = pascal_to_snake(obj[0] if len(obj) > 0 else obj)
command.details = snake


Expand Down
14 changes: 10 additions & 4 deletions src/ansible_navigator/utils/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def console_width() -> int:


# TODO: Replace this with something type-safe.
def dispatch(obj, replacements):
def dispatch(obj: object, replacements: tuple[tuple[str, str], ...]) -> object:
"""Make the replacement based on type.
:param obj: An obj in which replacements will be made
Expand All @@ -154,7 +154,10 @@ def escape_moustaches(obj: Mapping[Any, Any]) -> Mapping[Any, Any]:
:returns: The obj with replacements made
"""
replacements = (("{", "U+007B"), ("}", "U+007D"))
return dispatch(obj, replacements)
result = dispatch(obj, replacements)
if not isinstance(result, dict):
raise RuntimeError
return result


def environment_variable_is_file_path(
Expand Down Expand Up @@ -321,7 +324,7 @@ def now_iso(time_zone: str) -> str:
PASCAL_REGEX = re.compile("((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))")


def pascal_to_snake(obj) -> list[Any] | dict[Any, Any]:
def pascal_to_snake(obj: object) -> object:
"""Convert a pascal cased object into a snake cased object recursively.
:param obj: Pascal cased object
Expand Down Expand Up @@ -529,4 +532,7 @@ def unescape_moustaches(obj: Any) -> Mapping[Any, Any]:
:returns: The obj with replacements made
"""
replacements = (("U+007B", "{"), ("U+007D", "}"))
return dispatch(obj, replacements)
result = dispatch(obj, replacements)
if not isinstance(result, dict):
raise RuntimeError
return result

0 comments on commit 6108044

Please sign in to comment.