From ab8a2eeccd14aa324a0e7aee41b64a65864c3ba9 Mon Sep 17 00:00:00 2001 From: Shahadat Islam Koyel <179203101+ShahadatTest@users.noreply.github.com> Date: Sun, 1 Nov 2026 00:49:31 +0600 Subject: [PATCH] Write MutableMapping paths through mapping keys --- src/pydash/helpers.py | 4 ++-- src/pydash/objects.py | 5 ++++- tests/test_objects.py | 27 ++++++++++++++++++++++++++- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/pydash/helpers.py b/src/pydash/helpers.py index fcf409a5..c2ec96db 100644 --- a/src/pydash/helpers.py +++ b/src/pydash/helpers.py @@ -3,7 +3,7 @@ from __future__ import annotations import builtins -from collections.abc import Hashable, Iterable, Mapping, Sequence +from collections.abc import Hashable, Iterable, Mapping, MutableMapping, Sequence from decimal import Decimal from functools import wraps import inspect @@ -237,7 +237,7 @@ def base_set(obj, key, value, allow_override=True): value: Value to assign. allow_override: Whether to allow overriding a previously set key. """ - if isinstance(obj, dict): + if isinstance(obj, MutableMapping): if allow_override or key not in obj: obj[key] = value elif isinstance(obj, list): diff --git a/src/pydash/objects.py b/src/pydash/objects.py index 55a5a17e..911e0f94 100644 --- a/src/pydash/objects.py +++ b/src/pydash/objects.py @@ -6,6 +6,7 @@ from __future__ import annotations +from collections.abc import MutableMapping import copy from functools import partial import math @@ -1866,6 +1867,8 @@ def set_(obj: T, path: PathT, value: t.Any) -> T: Sets the value of an object described by `path`. If any part of the object path doesn't exist, it will be created. + Mutable mappings are updated by key, including mappings that are not ``dict`` subclasses. + Args: obj: Object to modify. path: Target path to set value to. @@ -2475,7 +2478,7 @@ def update_with(obj, path, updater, customizer=None): # noqa: PLR0912 else: call_customizer = None - default_type = dict if isinstance(obj, dict) else list + default_type = dict if isinstance(obj, MutableMapping) else list tokens = to_path_tokens(path) last_key = pyd.last(tokens) diff --git a/tests/test_objects.py b/tests/test_objects.py index 17089654..1dedf1df 100644 --- a/tests/test_objects.py +++ b/tests/test_objects.py @@ -1,5 +1,5 @@ from argparse import Namespace -from collections import defaultdict, namedtuple +from collections import ChainMap, UserDict, defaultdict, namedtuple from copy import deepcopy import datetime as dt from decimal import Decimal @@ -19,6 +19,31 @@ SomeNamedTuple = namedtuple("SomeNamedTuple", ["a", "b"]) +@parametrize("factory", [UserDict, ChainMap]) +@parametrize("setter", [_.set_, _.set_with]) +def test_set_mutable_mapping_keys(factory, setter): + obj = factory({"keep": 1}) + assert setter(obj, "nested.value", 2) is obj + assert dict(obj) == {"keep": 1, "nested": {"value": 2}} + assert not hasattr(obj, "nested") + + +@parametrize("factory", [UserDict, ChainMap]) +@parametrize("updater", [_.update, _.update_with]) +def test_update_mutable_mapping_keys(factory, updater): + obj = factory({"value": 1}) + assert updater(obj, "value", lambda value: value + 1) is obj + assert dict(obj) == {"value": 2} + assert not hasattr(obj, "value") + + +@parametrize("factory", [UserDict, ChainMap]) +def test_set_mutable_mapping_integer_keys(factory): + obj = factory() + _.set_(obj, [0], "value") + assert dict(obj) == {0: "value"} + + @parametrize( "case,expected", [