Handle pydantic ValidationError reading .env (#4456)

* move tradier to data providers

* handle env load errors

* improve message

* detail model in message
This commit is contained in:
montezdesousa
2023-03-10 23:07:40 +00:00
committed by GitHub
parent 740152b862
commit bea21dff28
4 changed files with 45 additions and 6 deletions

View File

@@ -36,6 +36,7 @@ class CredentialsModel:
API_TOKEN_TERMINAL_KEY: str = "REPLACE_ME"
API_STOCKSERA_KEY: str = "REPLACE_ME"
API_INTRINIO_KEY: str = "REPLACE_ME"
API_TRADIER_TOKEN: str = "REPLACE_ME"
# Socials
API_GITHUB_KEY: str = "REPLACE_ME"
@@ -57,7 +58,6 @@ class CredentialsModel:
OANDA_ACCOUNT_TYPE: str = "REPLACE_ME"
OANDA_ACCOUNT: str = "REPLACE_ME"
OANDA_TOKEN: str = "REPLACE_ME"
API_TRADIER_TOKEN: str = "REPLACE_ME"
API_BINANCE_KEY: str = "REPLACE_ME"
API_BINANCE_SECRET: str = "REPLACE_ME"
API_COINBASE_KEY: str = "REPLACE_ME"

View File

@@ -22,7 +22,6 @@ LOCAL_KEYS = [
"OANDA_ACCOUNT_TYPE",
"OANDA_ACCOUNT",
"OANDA_TOKEN",
"API_TRADIER_TOKEN",
"API_BINANCE_KEY",
"API_BINANCE_SECRET",
"API_COINBASE_KEY",

View File

@@ -9,11 +9,13 @@ from openbb_terminal.core.models import (
ProfileModel,
UserModel,
)
from openbb_terminal.core.session.env_handler import reading_env
from openbb_terminal.core.session.env_handler import load_env_to_model, reading_env
__env_dict = reading_env()
__credentials = CredentialsModel(**__env_dict)
__preferences = PreferencesModel(**__env_dict)
__credentials = load_env_to_model(__env_dict, CredentialsModel)
__preferences = load_env_to_model(__env_dict, PreferencesModel)
__profile = ProfileModel()
__local_user = UserModel( # type: ignore
credentials=__credentials,

View File

@@ -1,8 +1,9 @@
# IMPORTS STANDARD
from typing import Any, Dict, Optional
from typing import Any, Dict, Optional, Type, Union
# IMPORTS THIRDPARTY
from dotenv import dotenv_values
from pydantic import ValidationError
# IMPORTS INTERNAL
from openbb_terminal.core.config.paths import (
@@ -10,6 +11,8 @@ from openbb_terminal.core.config.paths import (
REPOSITORY_ENV_FILE,
SETTINGS_ENV_FILE,
)
from openbb_terminal.core.models.credentials_model import CredentialsModel
from openbb_terminal.core.models.preferences_model import PreferencesModel
def reading_env() -> Dict[str, Any]:
@@ -31,3 +34,38 @@ def reading_env() -> Dict[str, Any]:
}
return __env_dict_filtered
def load_env_to_model(
env_dict: dict, model: Union[Type[CredentialsModel], Type[PreferencesModel]]
) -> Union[Type[CredentialsModel], Type[PreferencesModel]]:
"""Load environment variables to model.
Parameters
----------
env_dict : dict
Environment variables dictionary.
model : Union[Type[CredentialsModel], Type[PreferencesModel]]
Model to validate.
Returns
-------
Union[Type[CredentialsModel], Type[PreferencesModel]]
Model with environment variables.
"""
try:
return model(**env_dict) # type: ignore
except ValidationError as error:
model_name = model.__name__.strip("Model").lower()
print(f"Error loading {model_name}:")
for err in error.errors():
loc = err.get("loc", None)
var_name = str(loc[0]) if loc else ""
msg = err.get("msg", "")
var = env_dict.pop(var_name, None)
fields = model.__dataclass_fields__ # type: ignore
if var and var_name in fields:
default = fields[var_name].default
print(f" {var_name}: {msg}, using default -> {default}")
return model(**env_dict) # type: ignore