Skip to content

Fix creating CIMultiDict from MultiDict not making keys case-insentive #1112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 1 addition & 1 deletion multidict/_multidict.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ _multidict_extend(MultiDictObject *self, PyObject *arg,
}

if (arg != NULL) {
if (MultiDict_CheckExact(arg) || CIMultiDict_CheckExact(arg)) {
if (CIMultiDict_CheckExact(arg) || (!CIMultiDict_CheckExact(self) && MultiDict_CheckExact(arg))) {
list = &((MultiDictObject*)arg)->pairs;
if (pair_list_update_from_pair_list(&self->pairs, used, list) < 0) {
goto fail;
Expand Down
8 changes: 7 additions & 1 deletion multidict/_multidict_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,8 @@ def _title(self, key: str) -> str:


class _CIMixin:
_ci: bool = True

def _key(self, key: str) -> str:
if type(key) is istr:
return key
Expand All @@ -474,6 +476,7 @@ def _title(self, key: str) -> str:

class _Base(MultiMapping[_V]):
_impl: _Impl[_V]
_ci: bool = False

@abstractmethod
def _key(self, key: str) -> str: ...
Expand Down Expand Up @@ -628,7 +631,10 @@ def _extend(
) -> None:
if arg:
if isinstance(arg, (MultiDict, MultiDictProxy)):
items = arg._impl._items
if self._ci and not arg._ci:
items = [(self._title(k), k, v) for _, k, v in arg._impl._items]
else:
items = arg._impl._items
if kwargs:
for key, value in kwargs.items():
items.append((self._title(key), key, value))
Expand Down
37 changes: 37 additions & 0 deletions tests/test_multidict.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
MultiMapping,
MutableMultiMapping,
)
from multidict._multidict_py import CIMultiDict as PyCIMultiDict
from multidict._multidict_py import MultiDict as PyMultiDict

_T = TypeVar("_T")

Expand Down Expand Up @@ -1189,3 +1191,38 @@ def test_items_case_insensitive_isdisjoint(
) -> None:
d = cls([("KEY", "one")])
assert d.items().isdisjoint(arg) == expected


def test_convert_multidict_to_cimultidict() -> None:
"""Test conversion from MultiDict to CIMultiDict."""
start_as_md = MultiDict([("KEY", "value1"), ("key2", "value2")])
assert start_as_md.get("KEY") == "value1"
assert start_as_md["KEY"] == "value1"
assert start_as_md.get("key2") == "value2"
assert start_as_md["key2"] == "value2"
start_as_cimd = CIMultiDict([("KEY", "value1"), ("key2", "value2")])
assert start_as_cimd.get("key") == "value1"
assert start_as_cimd["key"] == "value1"
assert start_as_cimd.get("key2") == "value2"
assert start_as_cimd["key2"] == "value2"
converted_to_ci = CIMultiDict(start_as_md)
assert converted_to_ci.get("key") == "value1"
assert converted_to_ci["key"] == "value1"
assert converted_to_ci.get("key2") == "value2"
assert converted_to_ci["key2"] == "value2"


def test_pure_python_convert_multidict_to_cimultidict_eq() -> None:
"""Test compare after conversion from MultiDict to CIMultiDict."""
original = PyMultiDict([("h1", "header1"), ("h2", "header2"), ("h3", "header3")])
assert PyCIMultiDict(original) == PyCIMultiDict(
[("H1", "header1"), ("H2", "header2"), ("H3", "header3")]
)


def test_convert_multidict_to_cimultidict_eq() -> None:
"""Test compare after conversion from MultiDict to CIMultiDict."""
original = MultiDict([("h1", "header1"), ("h2", "header2"), ("h3", "header3")])
assert CIMultiDict(original) == CIMultiDict(
[("H1", "header1"), ("H2", "header2"), ("H3", "header3")]
)
Loading