From 40b6832b7cec0af5de124592d3e95c09eb3fd1ec Mon Sep 17 00:00:00 2001 From: Pedro Crespo-Valero <32402063+pcrespov@users.noreply.github.com> Date: Fri, 19 Jan 2024 18:32:08 +0100 Subject: [PATCH] @sanderegg review: refactor types and assert --- .../src/pytest_simcore/helpers/typing_env.py | 2 +- .../src/pytest_simcore/helpers/utils_envs.py | 34 +++++++++++-------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/packages/pytest-simcore/src/pytest_simcore/helpers/typing_env.py b/packages/pytest-simcore/src/pytest_simcore/helpers/typing_env.py index a6c49b10a79..fd9dd81f778 100644 --- a/packages/pytest-simcore/src/pytest_simcore/helpers/typing_env.py +++ b/packages/pytest-simcore/src/pytest_simcore/helpers/typing_env.py @@ -2,7 +2,7 @@ from typing import TypeAlias EnvVarsDict: TypeAlias = dict[str, str] -EnvVarsList: TypeAlias = Iterable[str] +EnvVarsIterable: TypeAlias = Iterable[str] # SEE packages/pytest-simcore/tests/test_helpers_utils_envs.py diff --git a/packages/pytest-simcore/src/pytest_simcore/helpers/utils_envs.py b/packages/pytest-simcore/src/pytest_simcore/helpers/utils_envs.py index 3d72ca45481..25eff962f73 100644 --- a/packages/pytest-simcore/src/pytest_simcore/helpers/utils_envs.py +++ b/packages/pytest-simcore/src/pytest_simcore/helpers/utils_envs.py @@ -3,14 +3,13 @@ """ import os -from copy import deepcopy from io import StringIO from pathlib import Path import dotenv import pytest -from .typing_env import EnvVarsDict, EnvVarsList +from .typing_env import EnvVarsDict, EnvVarsIterable # # monkeypatch using dict @@ -18,22 +17,28 @@ def setenvs_from_dict( - monkeypatch: pytest.MonkeyPatch, envs: EnvVarsDict + monkeypatch: pytest.MonkeyPatch, envs: dict[str, str | bool] ) -> EnvVarsDict: + env_vars = {} + for key, value in envs.items(): assert isinstance(key, str) - assert ( - value is not None - ), f"{key=},{value=}" # None keys cannot be is defined w/o value - converted_value = value + assert value is not None, f"{key=},{value=}" + + v = value + if isinstance(value, bool): - converted_value = f"{'true' if value else 'false'}" - assert isinstance( - converted_value, str - ), f"client MUST explicitly stringify values since some cannot be done automatically e.g. json-like values. problematic {key=},{value=}" + v = "true" if value else "false" + + assert isinstance(v, str), ( + "caller MUST explicitly stringify values since some cannot be done automatically" + f"e.g. json-like values. Check {key=},{value=}" + ) + + monkeypatch.setenv(key, v) + env_vars[key] = v - monkeypatch.setenv(key, converted_value) - return deepcopy(envs) + return env_vars def load_dotenv(envfile_content_or_path: Path | str, **options) -> EnvVarsDict: @@ -52,13 +57,12 @@ def load_dotenv(envfile_content_or_path: Path | str, **options) -> EnvVarsDict: def delenvs_from_dict( monkeypatch: pytest.MonkeyPatch, - envs: EnvVarsList, + envs: EnvVarsIterable, *, raising: bool = True, ) -> None: for key in envs: assert isinstance(key, str) - assert key is not None # None keys cannot be is defined w/o value monkeypatch.delenv(key, raising)