Skip to content

Commit 5b39955

Browse files
committed
util(refactor[typing]): Make update_dict mapping-generic
why: Preserve return type while accepting any mapping inputs safely. what: - Use MutableMapping-bounded TypeVar and Mapping input - Handle existing nested mappings with safe fallbacks
1 parent 50e788d commit 5b39955

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

src/vcspull/util.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import os
66
import pathlib
77
import typing as t
8-
from collections.abc import Mapping
8+
from collections.abc import Mapping, MutableMapping
99

1010
LEGACY_CONFIG_DIR = pathlib.Path("~/.vcspull/").expanduser() # remove dupes of this
1111

@@ -42,12 +42,12 @@ def get_config_dir() -> pathlib.Path:
4242
return path
4343

4444

45-
T = t.TypeVar("T", bound=dict[str, t.Any])
45+
T = t.TypeVar("T", bound=MutableMapping[str, object])
4646

4747

4848
def update_dict(
4949
d: T,
50-
u: T,
50+
u: Mapping[str, object],
5151
) -> T:
5252
"""Return updated dict.
5353
@@ -67,7 +67,13 @@ def update_dict(
6767
"""
6868
for k, v in u.items():
6969
if isinstance(v, Mapping):
70-
r = update_dict(d.get(k, {}), v)
70+
current = d.get(k)
71+
if isinstance(current, MutableMapping):
72+
r = update_dict(current, t.cast("Mapping[str, object]", v))
73+
elif isinstance(current, Mapping):
74+
r = update_dict(dict(current), t.cast("Mapping[str, object]", v))
75+
else:
76+
r = update_dict({}, t.cast("Mapping[str, object]", v))
7177
d[k] = r
7278
else:
7379
d[k] = v

0 commit comments

Comments
 (0)