Skip to content

Commit d3307c1

Browse files
authored
Add backwards-compat alias for CONFIG singleton (#2351)
1 parent e926d2e commit d3307c1

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

optimade/server/config.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ class ServerConfig(BaseSettings):
368368
provider_fields: Annotated[
369369
dict[
370370
Literal["links", "references", "structures"],
371-
list[str | dict[Literal["name", "type", "unit", "description"], str]],
371+
list[str | dict],
372372
],
373373
Field(
374374
default_factory=dict,
@@ -597,3 +597,26 @@ def settings_customise_sources(
597597
ConfigFileSettingsSource(settings_cls),
598598
file_secret_settings,
599599
)
600+
601+
602+
_CONFIG: ServerConfig | None = None
603+
604+
605+
def __getattr__(name: str):
606+
"""Module-level __getattr__ to provide a deprecated CONFIG singleton.
607+
608+
This preserves backwards compatibility with code that imports
609+
``from optimade.server.config import CONFIG``.
610+
"""
611+
if name == "CONFIG":
612+
global _CONFIG
613+
warnings.warn(
614+
"Importing the CONFIG singleton from optimade.server.config is deprecated. "
615+
"Use create_app() and access config via request.app.state.config instead.",
616+
DeprecationWarning,
617+
stacklevel=2,
618+
)
619+
if _CONFIG is None:
620+
_CONFIG = ServerConfig()
621+
return _CONFIG
622+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")

0 commit comments

Comments
 (0)