Skip to content

Fix OrderedMultiDict equality to compare values against plain mappings - #404

Merged
mahmoud merged 1 commit into
mahmoud:masterfrom
DucMinhNe:fix/omd-eq-compares-values
Jun 17, 2026
Merged

mahmoud merged 1 commit into
mahmoud:masterfrom
DucMinhNe:fix/omd-eq-compares-values

Conversation

@DucMinhNe

Copy link
Copy Markdown
Contributor

OrderedMultiDict.eq has a branch for comparing against a non-OMD mapping (anything with a keys() method, like a dict). That branch computes other[selfk] == self[selfk] but discards the result, so it only checks that every key of self exists in other and never compares the values. An OMD therefore compares equal to any same-keyed mapping no matter what the values are:

>>> from boltons.dictutils import OMD
>>> omd = OMD([("a", 1), ("b", 2)])
>>> omd == {"a": 999, "b": 2}
True   # should be False

The existing test_eq does not catch this because its dict case is written as d == omd, which dispatches to dict.__eq__ (OMD subclasses dict), not OMD.__eq__. The buggy branch is only reached with the OMD on the left.

Fix: compare the values and return False on mismatch. The length check earlier in eq already handles the differing-key-count case.

The same eq is duplicated in urlutils.OrderedMultiDict (the base class of QueryParamDict), which had the identical bug, so it is fixed there too.

Added regression tests in test_dictutils.py and test_urlutils.py that put the OMD/QueryParamDict on the left so the branch is actually exercised. Both fail before the change and pass after. Full suite: 433 passed.

OrderedMultiDict.__eq__ has a branch for comparing against a non-OMD
mapping (anything with a keys() method, e.g. a dict). That branch
computes other[selfk] == self[selfk] but throws the result away, so it
only checks that every key of self exists in other and never compares
the values. As a result an OMD compares equal to any same-keyed mapping
regardless of the values:

    >>> omd = OMD([('a', 1), ('b', 2)])
    >>> omd == {'a': 999, 'b': 2}
    True   # should be False

The existing test_eq doesn't catch this because its dict case is written
as d == omd, which dispatches to dict.__eq__ rather than OMD.__eq__; the
buggy branch is only reached with the OMD on the left.

Compare the values and return False on mismatch. The same __eq__ is
duplicated in urlutils.OrderedMultiDict (used by QueryParamDict), so it's
fixed there too. Added regression tests for both.
@mahmoud

mahmoud commented Jun 17, 2026

Copy link
Copy Markdown
Owner

Wow, that is a big miss and a great catch. Good call, thank you!

@mahmoud
mahmoud merged commit c463d16 into mahmoud:master Jun 17, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants