Skip to content

Commit 515c5de

Browse files
authored
Merge pull request #36 from franccesco/dream/2026-07-23.1/finding-016
dream: use Path.open for config file I/O
2 parents 772d85c + f4a75ed commit 515c5de

2 files changed

Lines changed: 7 additions & 7 deletions

File tree

src/bloomy/configuration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def _store_api_key(self) -> None:
109109
config_file.parent.mkdir(parents=True, exist_ok=True)
110110

111111
config_data = {"version": 1, "api_key": self.api_key}
112-
with open(config_file, "w") as f:
112+
with config_file.open("w") as f:
113113
yaml.dump(config_data, f)
114114

115115
def _load_api_key(self) -> str | None:
@@ -124,7 +124,7 @@ def _load_api_key(self) -> str | None:
124124
return None
125125

126126
try:
127-
with open(config_file) as f:
127+
with config_file.open() as f:
128128
data = yaml.safe_load(f)
129129
return data.get("api_key")
130130
except Exception:

tests/test_configuration.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_init_with_config_file(self):
3131

3232
with (
3333
patch.dict(os.environ, {"BG_API_KEY": ""}), # Clear env var
34-
patch("builtins.open", mock_open(read_data=yaml.dump(mock_yaml_data))),
34+
patch("pathlib.Path.open", mock_open(read_data=yaml.dump(mock_yaml_data))),
3535
patch("pathlib.Path.exists", return_value=True),
3636
):
3737
config = Configuration()
@@ -43,7 +43,7 @@ def test_init_priority_order(self):
4343

4444
with (
4545
patch.dict(os.environ, {"BG_API_KEY": "env-key"}),
46-
patch("builtins.open", mock_open(read_data=yaml.dump(mock_yaml_data))),
46+
patch("pathlib.Path.open", mock_open(read_data=yaml.dump(mock_yaml_data))),
4747
patch("pathlib.Path.exists", return_value=True),
4848
):
4949
# Direct API key takes precedence
@@ -108,7 +108,7 @@ def test_configure_api_key_with_store(self):
108108
mock_client_class.return_value.__enter__.return_value = mock_client
109109

110110
with (
111-
patch("builtins.open", mock_open()) as mock_file,
111+
patch("pathlib.Path.open", mock_open()) as mock_file,
112112
patch("pathlib.Path.mkdir"),
113113
):
114114
config = Configuration()
@@ -147,7 +147,7 @@ def test_load_api_key_no_file(self):
147147
def test_load_api_key_invalid_yaml(self):
148148
"""Test loading API key with invalid YAML."""
149149
with (
150-
patch("builtins.open", mock_open(read_data="invalid: yaml: content:")),
150+
patch("pathlib.Path.open", mock_open(read_data="invalid: yaml: content:")),
151151
patch("pathlib.Path.exists", return_value=True),
152152
patch.dict(os.environ, {}, clear=True),
153153
):
@@ -159,7 +159,7 @@ def test_config_file_location(self):
159159
"""Test that configuration is loaded from the correct location."""
160160
with (
161161
patch("pathlib.Path.exists") as mock_exists,
162-
patch("builtins.open", mock_open(read_data="api_key: test-key")),
162+
patch("pathlib.Path.open", mock_open(read_data="api_key: test-key")),
163163
patch.dict(os.environ, {}, clear=True),
164164
):
165165
# Initialize config which should try to load from file

0 commit comments

Comments
 (0)