Skip to content

Commit c93b479

Browse files
Move _deep_merge to _io.py to avoid duplication
Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com> Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/ee88789e-37df-4ed0-95b9-b5149bf1b2ee
1 parent e4a2860 commit c93b479

3 files changed

Lines changed: 25 additions & 44 deletions

File tree

src/usethis/_file/toml/io_.py

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import copy
44
import re
5-
from collections.abc import MutableMapping
65
from typing import TYPE_CHECKING, Any
76

87
import tomlkit.api
@@ -27,6 +26,7 @@
2726
KeyValueFileManager,
2827
UnexpectedFileIOError,
2928
UnexpectedFileOpenError,
29+
_deep_merge,
3030
print_keys,
3131
)
3232

@@ -354,26 +354,6 @@ def remove_from_list(self, *, keys: Sequence[Key], values: Collection[Any]) -> N
354354
self.commit(toml_document)
355355

356356

357-
def _deep_merge(
358-
target: MutableMapping[Any, Any], source: MutableMapping[Any, Any]
359-
) -> MutableMapping[Any, Any]:
360-
"""Recursively merge source into target in place, returning target.
361-
362-
For keys present in both mappings, if both values are mappings the merge is
363-
applied recursively; otherwise the source value replaces the target value.
364-
"""
365-
for key, value in source.items():
366-
if (
367-
key in target
368-
and isinstance(target[key], MutableMapping)
369-
and isinstance(value, MutableMapping)
370-
):
371-
_deep_merge(target[key], value)
372-
else:
373-
target[key] = value
374-
return target
375-
376-
377357
def _set_value_in_existing(
378358
*,
379359
toml_document: TOMLDocument,

src/usethis/_file/yaml/io_.py

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import copy
44
import re
5-
from collections.abc import MutableMapping
65
from contextlib import contextmanager
76
from dataclasses import dataclass
87
from io import StringIO
@@ -30,6 +29,7 @@
3029
KeyValueFileManager,
3130
UnexpectedFileIOError,
3231
UnexpectedFileOpenError,
32+
_deep_merge,
3333
print_keys,
3434
)
3535

@@ -375,26 +375,6 @@ def remove_from_list(self, *, keys: Sequence[Key], values: list[Any]) -> None:
375375
self.commit(self._content)
376376

377377

378-
def _deep_merge(
379-
target: MutableMapping[Any, Any], source: MutableMapping[Any, Any]
380-
) -> MutableMapping[Any, Any]:
381-
"""Recursively merge source into target in place, returning target.
382-
383-
For keys present in both mappings, if both values are mappings the merge is
384-
applied recursively; otherwise the source value replaces the target value.
385-
"""
386-
for key, value in source.items():
387-
if (
388-
key in target
389-
and isinstance(target[key], MutableMapping)
390-
and isinstance(value, MutableMapping)
391-
):
392-
_deep_merge(target[key], value)
393-
else:
394-
target[key] = value
395-
return target
396-
397-
398378
def _set_value_in_existing(
399379
*, content: YAMLLiteral, keys: Sequence[Key], value: Any
400380
) -> None:

src/usethis/_io.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import re
44
from abc import abstractmethod
5-
from typing import TYPE_CHECKING, Generic, TypeAlias, TypeVar
5+
from collections.abc import MutableMapping
6+
from typing import TYPE_CHECKING, Any, Generic, TypeAlias, TypeVar
67

78
from typing_extensions import assert_never
89

@@ -13,7 +14,7 @@
1314
from collections.abc import Sequence
1415
from pathlib import Path
1516
from types import TracebackType
16-
from typing import Any, ClassVar
17+
from typing import ClassVar
1718

1819
from typing_extensions import Self
1920

@@ -246,3 +247,23 @@ def print_keys(keys: Sequence[Key]) -> str:
246247
assert_never(key)
247248

248249
return ".".join(components)
250+
251+
252+
def _deep_merge(
253+
target: MutableMapping[Any, Any], source: MutableMapping[Any, Any]
254+
) -> MutableMapping[Any, Any]:
255+
"""Recursively merge source into target in place, returning target.
256+
257+
For keys present in both mappings, if both values are mappings the merge is
258+
applied recursively; otherwise the source value replaces the target value.
259+
"""
260+
for key, value in source.items():
261+
if (
262+
key in target
263+
and isinstance(target[key], MutableMapping)
264+
and isinstance(value, MutableMapping)
265+
):
266+
_deep_merge(target[key], value)
267+
else:
268+
target[key] = value
269+
return target

0 commit comments

Comments
 (0)