Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 35 additions & 35 deletions baybe/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,38 @@ def inner(*args, **kwds):
return inner


def _make_default_factory(fld: Attribute, /) -> Any:
"""Make the default factory for the given attribute."""

def get_default_value(self: Settings) -> Any:
"""Dynamically retrieve the default value for the field.

Depending on the control flags, the value is retrieved either from the
field specification itself, from the corresponding environment variable,
or from the active settings object.
"""
if self._restore_defaults:
default = fld.default
else:
# Here, the active settings value is used as default, to
# enable updating settings one attribute at a time (the fallback to
# the default happens when the active settings object is itself
# being created)
default = getattr(active_settings, fld.name, fld.default)

if self._restore_environment:
# If enabled, the environment values take precedence for the default
env_name = f"BAYBE_{fld.alias.upper()}"
value = os.getenv(env_name, default)
if fld.type == "bool":
value = to_bool(value)
return value

return default

return Factory(get_default_value, takes_self=True)


def adjust_defaults(cls: type[Settings], fields: list[Attribute]) -> list[Attribute]:
"""Replace default values with the appropriate source, controlled via flags."""
results = []
Expand All @@ -84,39 +116,7 @@ def adjust_defaults(cls: type[Settings], fields: list[Attribute]) -> list[Attrib

# We use a factory here because the environment variables should be looked up
# at instantiation time, not at class definition time
def make_default_factory(fld: Attribute) -> Any:
# TODO: https://github.com/python-attrs/attrs/issues/1479
name = fld.alias or fld.name

def get_default_value(self: Settings) -> Any:
"""Dynamically retrieve the default value for the field.

Depending on the control flags, the value is retrieved either from the
field specification itself, from the corresponding environment variable,
or from the active settings object.
"""
if self._restore_defaults:
default = fld.default
else:
# Here, the active settings value is used as default, to
# enable updating settings one attribute at a time (the fallback to
# the default happens when the active settings object is itself
# being created)
default = getattr(active_settings, fld.name, fld.default)

if self._restore_environment:
# If enabled, the environment values take precedence for the default
env_name = f"BAYBE_{name.upper()}"
value = os.getenv(env_name, default)
if fld.type == "bool":
value = to_bool(value)
return value

return default

return Factory(get_default_value, takes_self=True)

results.append(fld.evolve(default=make_default_factory(fld)))
results.append(fld.evolve(default=_make_default_factory(fld)))

return results

Expand Down Expand Up @@ -228,15 +228,15 @@ def __attrs_pre_init__(self) -> None:
warnings.warn(
f"The environment variable '{env_var}' has "
f"been deprecated and support will be dropped in a future version. "
f"Please use 'BAYBE_{(fld.alias or fld.name).upper()}' instead. "
f"Please use 'BAYBE_{fld.alias.upper()}' instead. "
f"For now, we've automatically handled the translation for you.",
DeprecationWarning,
)
if env_var.endswith("POLARS"):
value = "false" if to_bool(value) else "true"
elif env_var.endswith("SIMULATION_RUNS"):
value = "true" if to_bool(value) else "false"
os.environ[f"BAYBE_{(fld.alias or fld.name).upper()}"] = value
os.environ[f"BAYBE_{fld.alias.upper()}"] = value
# <<<<< Deprecation

known_env_vars = {
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ keywords = [
]
dynamic = ['version']
dependencies = [
"attrs>=25.4.0",
"attrs>=26.1.0",
"botorch>=0.13.0,<1",
"cattrs>=25.2.0",
"exceptiongroup",
Expand Down
8 changes: 4 additions & 4 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading