Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/pydash/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
5 changes: 4 additions & 1 deletion src/pydash/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from __future__ import annotations

from collections.abc import MutableMapping
import copy
from functools import partial
import math
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
27 changes: 26 additions & 1 deletion tests/test_objects.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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",
[
Expand Down