Repository navigation
Fix OrderedMultiDict equality to compare values against plain mappings - #404
Merged
Merged
Conversation
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.
Owner
|
Wow, that is a big miss and a great catch. Good call, thank you! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
The existing test_eq does not catch this because its dict case is written as
d == omd, which dispatches todict.__eq__(OMD subclasses dict), notOMD.__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.