feat: parse env var strings to expected config value types (#3107)

* fix: add try_parse_bool for env var strings to enable config overrides of boolean values

* fix: fallback to given value if not parseable

* feat: extend eval to all valid types

* fix: remove return type

* fix: prevent strange type conversions by providing expected type

* feat: add tests
This commit is contained in:
Manuel Schmid
2024-06-06 19:29:08 +02:00
committed by GitHub
parent 04d764820e
commit 5abae220c5
3 changed files with 168 additions and 39 deletions
+15
View File
@@ -1,4 +1,6 @@
import os
from ast import literal_eval
def makedirs_with_log(path):
try:
@@ -24,3 +26,16 @@ def get_files_from_folder(folder_path, extensions=None, name_filter=None):
filenames.append(path)
return filenames
def try_eval_env_var(value: str, expected_type=None):
try:
value_eval = value
if expected_type is bool:
value_eval = value.title()
value_eval = literal_eval(value_eval)
if expected_type is not None and not isinstance(value_eval, expected_type):
return value
return value_eval
except:
return value