[patch] QueryDict items() and mutability protection fix with doctests
|Reported by:||django@…||Owned by:||adrian|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
A few issues with httpwrappers.QueryDict. The docs say:
items() -- Just like the standard dictionary items() method, except this retains the order for values of duplicate keys, if any. For example, if the original query string was "a=1&b=2&b=3", items() will return [("a", ["1"]), ("b", ["2", "3"])], where the order of ["2", "3"] is guaranteed, but the order of a vs. b isn't.
items() actually returns [("a", "1"), ("b", "2")], rather than lists. QueryDict.getitem() returns the last item in the list so it would seem most consistent to me for items() to return the last item in the list too (as does the superclass MultiValueDict), but instead the existing implementation is returning the first item in the list.
Additionally, existing non-mutability protection is a bit weak, allowing mutation via some methods (appendlist) and not others.
This patch makes the behaviour of QueryDict consistent with the documentation and adds mutability-checks where appropriate. I have replaced MultiValueDict with a dict subclass which simplifies the implementation and also provides useful things like setdefault and iteration over keys. I have added a lists() method to MultiValueDict that returns lists for each key and my QueryDict.items() delegates to this to achieve the documented behaviour, though I would prefer that QueryDict was consistent with its superclass here - is there a reason to have it otherwise?
I have provided doctest coverage so someone can check that my expectation of the behaviour fits with Django's. I have beed running this patch without problems.