Skip to content

Commit a9d54d7

Browse files
authored
Fix pure Python version mutating existing MultiDict when creating along with kwargs (#1113)
Noticed there was no copy here and the test shows we were mutating the original.
1 parent 43f3efd commit a9d54d7

3 files changed

Lines changed: 10 additions & 0 deletions

File tree

CHANGES/1113.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed the pure Python version mutating the original :class:`multidict.MultiDict` when creating a new :class:`multidict.CIMultiDict` from an existing one when keyword arguments are also passed -- by :user:`bdraco`.

multidict/_multidict_py.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,7 @@ def _extend(
630630
if isinstance(arg, (MultiDict, MultiDictProxy)):
631631
items = arg._impl._items
632632
if kwargs:
633+
items = items.copy()
633634
for key, value in kwargs.items():
634635
items.append((self._title(key), key, value))
635636
else:

tests/test_multidict.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,3 +1189,11 @@ def test_items_case_insensitive_isdisjoint(
11891189
) -> None:
11901190
d = cls([("KEY", "one")])
11911191
assert d.items().isdisjoint(arg) == expected
1192+
1193+
1194+
def test_create_multidict_from_existing_multidict_new_pairs() -> None:
1195+
"""Test creating a MultiDict from an existing one does not mutate the original."""
1196+
original = MultiDict([("h1", "header1"), ("h2", "header2"), ("h3", "header3")])
1197+
new = MultiDict(original, h4="header4")
1198+
assert "h4" in new
1199+
assert "h4" not in original

0 commit comments

Comments
 (0)