Skip to content

Commit 1069a68

Browse files
committed
fix: actually deep-merge configs
1 parent 8de9fc8 commit 1069a68

File tree

4 files changed

+21
-21
lines changed

4 files changed

+21
-21
lines changed

src/pdfbaker/config/__init__.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -186,27 +186,27 @@ def user_defined_settings(self) -> dict[str, Any]:
186186
"""Return dictionary of user-defined settings."""
187187
return getattr(self, "__pydantic_extra__", {}) or {}
188188

189+
@staticmethod
190+
def deep_merge_dicts(
191+
base: dict[Any, Any], update: dict[Any, Any]
192+
) -> dict[Any, Any]:
193+
"""Deep merge two dictionaries."""
194+
result = base.copy()
195+
for key, value in update.items():
196+
if (
197+
key in result
198+
and isinstance(result[key], dict)
199+
and isinstance(value, dict)
200+
):
201+
result[key] = BaseConfig.deep_merge_dicts(result[key], value)
202+
else:
203+
result[key] = value
204+
return result
205+
189206
def merge(self, update: dict[str, Any]) -> "BaseConfig":
190207
"""Deep merge a dictionary into a config, returning a new config instance."""
191-
192-
def _deep_merge(
193-
base_dict: dict[str, Any], update_dict: dict[str, Any]
194-
) -> dict[str, Any]:
195-
"""Deep merge two dictionaries."""
196-
result = base_dict.copy()
197-
for key, value in update_dict.items():
198-
if (
199-
key in result
200-
and isinstance(result[key], dict)
201-
and isinstance(value, dict)
202-
):
203-
result[key] = _deep_merge(result[key], value)
204-
else:
205-
result[key] = value
206-
return result
207-
208208
base_dict = self.model_dump()
209-
merged = _deep_merge(base_dict, update)
209+
merged = self.deep_merge_dicts(base_dict, update)
210210
return self.__class__(**merged)
211211

212212
# ruff: noqa: C901

src/pdfbaker/config/baker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def load_config(cls, data: Any) -> Any:
3838
data["config_file"] = data["config_file"].resolve()
3939

4040
config_data = YAML().load(data["config_file"].read_text())
41-
data.update(config_data) # YAML values override kwargs
41+
data = BaseConfig.deep_merge_dicts(data, config_data)
4242

4343
# Set default directories
4444
if "directories" not in data:

src/pdfbaker/config/document.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def load_config(cls, data: Any) -> Any:
4646

4747
config_path = data["config_path"]
4848
config_data = YAML().load(config_path.path.read_text())
49-
data.update(config_data) # YAML values override kwargs
49+
data = BaseConfig.deep_merge_dicts(data, config_data)
5050
data["directories"]["base"] = config_path.path.parent
5151

5252
return data

src/pdfbaker/config/page.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def load_config(cls, data: Any) -> Any:
2626
if isinstance(data["config_path"], dict):
2727
data["config_path"] = PathSpec(**data["config_path"])
2828
config_data = YAML().load(data["config_path"].path.read_text())
29-
data.update(config_data) # YAML values override kwargs
29+
data = BaseConfig.deep_merge_dicts(data, config_data)
3030
data["directories"]["base"] = data["config_path"].path.parent
3131
return data
3232

0 commit comments

Comments
 (0)