From 204202130b3679575d2ae8aaf77a30f73d50339c Mon Sep 17 00:00:00 2001 From: Ruslan Kuprieiev Date: Fri, 28 Feb 2025 03:57:06 +0200 Subject: [PATCH] fix(fal): fix 3.8 support and deserialization errors (#435) * fix: support 3.8 * fix: fix deserialization * fix typing * lazy import tomli --- projects/fal/pyproject.toml | 4 ++-- projects/fal/src/fal/api.py | 1 + projects/fal/src/fal/config.py | 25 +++++++++++++------------ 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/projects/fal/pyproject.toml b/projects/fal/pyproject.toml index 143481f3..c66752d7 100644 --- a/projects/fal/pyproject.toml +++ b/projects/fal/pyproject.toml @@ -57,8 +57,8 @@ dependencies = [ "pyjwt[crypto]>=2.8.0,<3", "uvicorn>=0.29.0,<1", "cookiecutter", - "tomli==2.2.1", - "tomli-w==1.2.0", + "tomli>2,<3", + "tomli-w>=1,<2", ] [project.optional-dependencies] diff --git a/projects/fal/src/fal/api.py b/projects/fal/src/fal/api.py index 96d88ac1..a11fcbf3 100644 --- a/projects/fal/src/fal/api.py +++ b/projects/fal/src/fal/api.py @@ -78,6 +78,7 @@ "starlette_exporter", "structlog", "tomli", + "tomli-w", ] diff --git a/projects/fal/src/fal/config.py b/projects/fal/src/fal/config.py index 3e317b41..5beb0a5d 100644 --- a/projects/fal/src/fal/config.py +++ b/projects/fal/src/fal/config.py @@ -1,19 +1,18 @@ import os -from typing import Optional - -import tomli -import tomli_w +from typing import Dict, List, Optional SETTINGS_SECTION = "__internal__" class Config: - _config: dict[str, dict[str, str]] + _config: Dict[str, Dict[str, str]] _profile: Optional[str] DEFAULT_CONFIG_PATH = "~/.fal/config.toml" def __init__(self): + import tomli + self.config_path = os.path.expanduser( os.getenv("FAL_CONFIG_PATH", self.DEFAULT_CONFIG_PATH) ) @@ -35,13 +34,13 @@ def profile(self) -> Optional[str]: return self._profile @profile.setter - def profile(self, value: Optional[str]): + def profile(self, value: Optional[str]) -> None: if value and value not in self._config: self._config[value] = {} self._profile = value - def profiles(self): + def profiles(self) -> List[str]: keys = [] for key in self._config: if key != SETTINGS_SECTION: @@ -49,7 +48,9 @@ def profiles(self): return keys - def save(self): + def save(self) -> None: + import tomli_w + with open(self.config_path, "wb") as file: tomli_w.dump(self._config, file) @@ -59,19 +60,19 @@ def get(self, key: str) -> Optional[str]: return self._config.get(self.profile, {}).get(key) - def set(self, key: str, value: str): + def set(self, key: str, value: str) -> None: if not self.profile: raise ValueError("No profile set.") self._config[self.profile][key] = value - def get_internal(self, key): + def get_internal(self, key: str) -> Optional[str]: if SETTINGS_SECTION not in self._config: self._config[SETTINGS_SECTION] = {} return self._config[SETTINGS_SECTION].get(key) - def set_internal(self, key, value): + def set_internal(self, key: str, value: Optional[str]) -> None: if SETTINGS_SECTION not in self._config: self._config[SETTINGS_SECTION] = {} @@ -80,5 +81,5 @@ def set_internal(self, key, value): else: self._config[SETTINGS_SECTION][key] = value - def delete(self, profile): + def delete(self, profile: str) -> None: del self._config[profile]