From fcf9a7d73b2083165f10afd648120e0acfcf3e70 Mon Sep 17 00:00:00 2001 From: Andre Anjos <andre.dos.anjos@gmail.com> Date: Tue, 25 Jun 2024 15:48:09 +0200 Subject: [PATCH] [libs.common.scripts.utils] Define a better type for JSON-able content --- src/mednet/libs/common/scripts/utils.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/mednet/libs/common/scripts/utils.py b/src/mednet/libs/common/scripts/utils.py index c11cc8fb..e9e8c087 100644 --- a/src/mednet/libs/common/scripts/utils.py +++ b/src/mednet/libs/common/scripts/utils.py @@ -8,6 +8,7 @@ import logging import pathlib import re import shutil +import typing import lightning.pytorch import lightning.pytorch.callbacks @@ -16,6 +17,17 @@ from mednet.libs.common.engine.device import SupportedPytorchDevice logger = logging.getLogger("mednet") +JSONable: typing.TypeAlias = ( + typing.Mapping[str, "JSONable"] + | typing.Sequence["JSONable"] + | str + | int + | float + | bool + | None +) +"""Defines types that can be encoded in a JSON string.""" + def model_summary( model: torch.nn.Module, @@ -198,7 +210,7 @@ def execution_metadata() -> dict[str, int | float | str | dict[str, str] | list[ } -def save_json_with_backup(path: pathlib.Path, data: dict | list) -> None: +def save_json_with_backup(path: pathlib.Path, data: JSONable) -> None: """Save a dictionary into a JSON file with path checking and backup. This function will save a dictionary into a JSON file. It will check to -- GitLab