Skip to content

Commit 77fadeb

Browse files
committed
dream: use Path.open for config file I/O
Dream dream-2026-07-23.1 finding 016.
1 parent bc89984 commit 77fadeb

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
@@ -113,7 +113,7 @@ def _store_api_key(self) -> None:
113113
config_file.parent.mkdir(parents=True, exist_ok=True)
114114

115115
config_data = {"version": 1, "api_key": self.api_key}
116-
with open(config_file, "w") as f:
116+
with config_file.open("w") as f:
117117
yaml.dump(config_data, f)
118118

119119
def _load_api_key(self) -> str | None:
@@ -128,7 +128,7 @@ def _load_api_key(self) -> str | None:
128128
return None
129129

130130
try:
131-
with open(config_file) as f:
131+
with config_file.open() as f:
132132
data = yaml.safe_load(f)
133133
return data.get("api_key")
134134
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
@@ -101,7 +101,7 @@ def test_configure_api_key_with_store(self):
101101
mock_client_class.return_value.__enter__.return_value = mock_client
102102

103103
with (
104-
patch("builtins.open", mock_open()) as mock_file,
104+
patch("pathlib.Path.open", mock_open()) as mock_file,
105105
patch("pathlib.Path.mkdir"),
106106
):
107107
config = Configuration()
@@ -140,7 +140,7 @@ def test_load_api_key_no_file(self):
140140
def test_load_api_key_invalid_yaml(self):
141141
"""Test loading API key with invalid YAML."""
142142
with (
143-
patch("builtins.open", mock_open(read_data="invalid: yaml: content:")),
143+
patch("pathlib.Path.open", mock_open(read_data="invalid: yaml: content:")),
144144
patch("pathlib.Path.exists", return_value=True),
145145
patch.dict(os.environ, {}, clear=True),
146146
):
@@ -152,7 +152,7 @@ def test_config_file_location(self):
152152
"""Test that configuration is loaded from the correct location."""
153153
with (
154154
patch("pathlib.Path.exists") as mock_exists,
155-
patch("builtins.open", mock_open(read_data="api_key: test-key")),
155+
patch("pathlib.Path.open", mock_open(read_data="api_key: test-key")),
156156
patch.dict(os.environ, {}, clear=True),
157157
):
158158
# Initialize config which should try to load from file

0 commit comments

Comments
 (0)