#32010: Unexpected behaviour of MultiValueDict.getlist in conjunction with map
---------------------------------------+------------------------
Reporter: Etienne Ott | Owner: nobody
Type: Bug | Status: new
Component: Utilities | Version: 3.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
---------------------------------------+------------------------
When the return value of a call to `MultiValueDict.getlist` is fed into a
`map(int, ...)` call, the result has unexpected behaviour when checking
the existence of elements with the `in` operator.
Example:
{{{
from django.utils.datastructures import MultiValueDict
mvdict = MultiValueDict({"number": "54321"})
mapped = map(int, mvdict.getlist("number"))
if 1 in mapped:
print("1 is found with mvdict")
else:
print("1 isn't found with mvdict")
mapped = map(int, mvdict.getlist("number"))
if 54321 in mapped:
print("54321 is found with mvdict")
else:
print("54321 isn't found with mvdict")
single_list = ["54321"]
mapped = map(int, single_list)
if 1 in mapped:
print("1 is found with single list")
else:
print("1 isn't found with single list")
mapped = map(int, single_list)
if 54321 in mapped:
print("54321 is found with single list")
else:
print("54321 isn't found with single list")
}}}
Output:
{{{
1 is found with mvdict
54321 isn't found with mvdict
1 isn't found with single list
54321 is found with single list
}}}
Expected behaviour:
The number 1 is not expected to be found in the list of numbers (which in
this example is only one entry), but 54321 is. This assumes that the key
of the entry exists in the dict and that the mapping function `int`
neither adds nor removes elements due to being able/not able to parse
elements as ints. I'm fairly confident both assumptions are met in the
example, but the expected behaviour is not.
Remarks:
I admit this use case may seem a bit strange, but it did "naturally" come
up with a form where a user could select some objects identified by their
ID and the form, upon submission, checked for the existence of some IDs in
the selected list of IDs in order to do something. My suspicion is that
some form of string conversion takes place or that an exception is
stringified and taken as a return value. Upon checking if an int exists in
the variable, python will then check for the existence of an int in the
string. This would explain why the number 1 is found, as it is likely to
be found, while the number 54321 isn't.
--
Ticket URL: <https://code.djangoproject.com/ticket/32010>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/055.c89da5981bd1c05850aa9cbf2cf0c0e9%40djangoproject.com.