Skip to content

Commit c9eb0a6

Browse files
committed
removed redundat load .env
1 parent af073b2 commit c9eb0a6

File tree

2 files changed

+15
-25
lines changed

2 files changed

+15
-25
lines changed

server-utils/server_utils/settings_utils.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Shared helpers for server settings."""
22

3-
from dotenv import load_dotenv
43
from pydantic import Field
54
from pydantic_settings import BaseSettings, SettingsConfigDict
65

@@ -13,8 +12,9 @@ def get_dot_env_path(env_prefix: str) -> str | None:
1312
When unset, no ``.env`` file is loaded and all settings come from
1413
environment variables or their defaults.
1514
16-
If a path is found, ``load_dotenv`` is called so the values are
17-
available to anything that reads ``os.environ`` directly.
15+
The returned path is intended to be passed as ``_env_file`` to a
16+
Pydantic ``BaseSettings`` constructor so that Pydantic handles the
17+
actual file loading (prefix matching, type coercion, etc.).
1818
"""
1919

2020
class _Environment(BaseSettings):
@@ -26,7 +26,4 @@ class _Environment(BaseSettings):
2626
)
2727
model_config = SettingsConfigDict(env_prefix=env_prefix)
2828

29-
path = _Environment().dot_env_path
30-
if path:
31-
load_dotenv(path)
32-
return path
29+
return _Environment().dot_env_path

server-utils/tests/test_settings_utils.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
@pytest.fixture
1313
def env_file(tmp_path: Path) -> Path:
14-
"""Create a temporary .env file with a test variable."""
14+
"""Create a temporary .env file."""
1515
p = tmp_path / ".env"
1616
p.write_text("MY_TEST_VAR=from_dotenv\n")
1717
return p
@@ -32,30 +32,23 @@ def test_returns_path_when_env_var_is_set(env_file: Path) -> None:
3232
assert result == str(env_file)
3333

3434

35-
def test_loads_dotenv_when_path_is_set(env_file: Path) -> None:
36-
"""When a path is returned, load_dotenv should populate os.environ."""
35+
def test_respects_env_prefix() -> None:
36+
"""Only the env var with the correct prefix is read."""
3737
with patch.dict(
3838
os.environ,
39-
{"OT_TEST_SERVER_dot_env_path": str(env_file)},
39+
{"OT_OTHER_SERVER_dot_env_path": "/some/path"},
4040
clear=False,
4141
):
42-
get_dot_env_path("OT_TEST_SERVER_")
43-
assert os.environ.get("MY_TEST_VAR") == "from_dotenv"
44-
45-
46-
def test_does_not_load_dotenv_when_no_path() -> None:
47-
"""When no path is set, load_dotenv should not be called."""
48-
with patch("server_utils.settings_utils.load_dotenv") as mock_load:
49-
get_dot_env_path("OT_NONEXISTENT_PREFIX_")
50-
mock_load.assert_not_called()
42+
result = get_dot_env_path("OT_MY_SERVER_")
43+
assert result is None
5144

5245

53-
def test_respects_env_prefix() -> None:
54-
"""Only the env var with the correct prefix is read."""
46+
def test_does_not_modify_os_environ(env_file: Path) -> None:
47+
"""The function should not call load_dotenv or modify os.environ."""
5548
with patch.dict(
5649
os.environ,
57-
{"OT_OTHER_SERVER_dot_env_path": "/some/path"},
50+
{"OT_TEST_SERVER_dot_env_path": str(env_file)},
5851
clear=False,
5952
):
60-
result = get_dot_env_path("OT_MY_SERVER_")
61-
assert result is None
53+
get_dot_env_path("OT_TEST_SERVER_")
54+
assert "MY_TEST_VAR" not in os.environ

0 commit comments

Comments
 (0)