Skip to content

Commit 8b8621d

Browse files
authored
🐛 is3372/fixes warning of alias in BaseSettings (ITISFoundation#3454)
1 parent 5ac6e4e commit 8b8621d

File tree

2 files changed

+37
-23
lines changed

2 files changed

+37
-23
lines changed

services/web/server/src/simcore_service_webserver/application_settings.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -215,18 +215,6 @@ def enable_only_if_dev_features_allowed(cls, v, values, field: ModelField):
215215
log.warning("%s still under development and will be disabled.", field.name)
216216
return None if field.allow_none else False
217217

218-
class Config(BaseCustomSettings.Config):
219-
# NOTE: FutureWarning: aliases are no longer used by BaseSettings to define which environment variables to read.
220-
# Instead use the "env" field setting. See https://pydantic-docs.helpmanual.io/usage/settings/#environment-variable-names
221-
# NOTE: These alias are ONLY used in export, not in constructor
222-
fields = {
223-
"SC_VCS_URL": "vcs_url",
224-
"SC_VCS_REF": "vcs_ref",
225-
"SC_BUILD_DATE": "build_date",
226-
"SWARM_STACK_NAME": "stack_name",
227-
}
228-
alias_generator = lambda s: s.lower()
229-
230218
@cached_property
231219
def log_level(self) -> int:
232220
return getattr(logging, self.WEBSERVER_LOGLEVEL.upper())
@@ -273,6 +261,31 @@ def _get_disabled_public_plugins(self) -> list[str]:
273261
plugins_disabled.append(field_name)
274262
return plugins_disabled
275263

264+
def _export_by_alias(self, **kwargs) -> dict[str, Any]:
265+
# This is a small helper to assist export functions since aliases are no longer used by
266+
# BaseSettings to define which environment variables to read.
267+
# SEE https://github.com/ITISFoundation/osparc-simcore/issues/3372
268+
#
269+
# NOTE: This is a copy from pydantic's Config.fields and Config.alias_generator
270+
# SEE https://pydantic-docs.helpmanual.io/usage/model_config/#options
271+
# SEE https://pydantic-docs.helpmanual.io/usage/model_config/#alias-generator
272+
#
273+
config_fields = {
274+
"SC_VCS_URL": "vcs_url",
275+
"SC_VCS_REF": "vcs_ref",
276+
"SC_BUILD_DATE": "build_date",
277+
"SWARM_STACK_NAME": "stack_name",
278+
}
279+
config_alias_generator = lambda s: s.lower()
280+
281+
data = self.dict(**kwargs)
282+
current_keys = list(data.keys())
283+
284+
for key in current_keys:
285+
if new_key := (config_fields.get(key) or config_alias_generator(key)):
286+
data[new_key] = data.pop(key)
287+
return data
288+
276289
def public_dict(self) -> dict[str, Any]:
277290
"""Data publicaly available"""
278291

@@ -284,7 +297,7 @@ def public_dict(self) -> dict[str, Any]:
284297
data["login_2fa_required"] = self.WEBSERVER_LOGIN.LOGIN_2FA_REQUIRED
285298

286299
data.update(
287-
self.dict(
300+
self._export_by_alias(
288301
include={
289302
"APP_NAME",
290303
"API_VERSION",
@@ -293,13 +306,12 @@ def public_dict(self) -> dict[str, Any]:
293306
"SC_BUILD_DATE",
294307
},
295308
exclude_none=True,
296-
by_alias=True,
297309
)
298310
)
299311
return data
300312

301313
def to_client_statics(self) -> dict[str, Any]:
302-
data = self.dict(
314+
data = self._export_by_alias(
303315
include={
304316
"APP_NAME",
305317
"API_VERSION",
@@ -309,7 +321,6 @@ def to_client_statics(self) -> dict[str, Any]:
309321
"SWARM_STACK_NAME",
310322
},
311323
exclude_none=True,
312-
by_alias=True,
313324
)
314325
data["plugins_disabled"] = self._get_disabled_public_plugins()
315326

services/web/server/src/simcore_service_webserver/cli.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,14 @@
1313
1414
"""
1515

16-
1716
import logging
1817
import os
1918

2019
import typer
2120
from aiohttp import web
2221
from settings_library.utils_cli import create_settings_command
2322

24-
from .application import create_application, run_service
2523
from .application_settings import ApplicationSettings
26-
from .application_settings_utils import convert_to_app_config
27-
from .log import setup_logging
2824
from .login import cli as login_cli
2925

3026
# ptsvd cause issues with ProcessPoolExecutor
@@ -40,8 +36,12 @@
4036
def _setup_app_from_settings(
4137
settings: ApplicationSettings,
4238
) -> tuple[web.Application, dict]:
39+
# NOTE: keeping imports here to reduce CLI load time
40+
from .application import create_application
41+
from .application_settings_utils import convert_to_app_config
42+
from .log import setup_logging
4343

44-
# NOTE: keeping an equivalent config allows us
44+
# NOTE: By having an equivalent config allows us
4545
# to keep some of the code from the previous
4646
# design whose starting point was a validated
4747
# config. E.g. many test fixtures were based on
@@ -60,7 +60,7 @@ def _setup_app_from_settings(
6060

6161
async def app_factory() -> web.Application:
6262
"""Created to launch app from gunicorn (see docker/boot.sh)"""
63-
app_settings = ApplicationSettings()
63+
app_settings = ApplicationSettings.create_from_envs()
6464
assert app_settings.SC_BUILD_TARGET # nosec
6565

6666
log.info("Application settings: %s", app_settings.json(indent=2, sort_keys=True))
@@ -82,6 +82,9 @@ async def app_factory() -> web.Application:
8282
@main.command()
8383
def run():
8484
"""Runs web server"""
85-
app_settings = ApplicationSettings()
85+
# NOTE: keeping imports here to reduce CLI load time
86+
from .application import run_service
87+
88+
app_settings = ApplicationSettings.create_from_envs()
8689
app, cfg = _setup_app_from_settings(app_settings)
8790
run_service(app, cfg)

0 commit comments

Comments
 (0)