Opened 4 days ago
Closed 4 days ago
#36278 closed New feature (wontfix)
MultiValueDict should use sentinel for default values
Description (last modified by ) ¶
MultiValueDict has many methods that has a 'default' parameter, but sets the default to None. fx:
https://github.com/django/django/blob/main/django/utils/datastructures.py#L129
This makes
mvdict.getlist('key', None)
return [], instead of the expected None.
Change History (3)
comment:1 by , 4 days ago
Description: | modified (diff) |
---|
comment:3 by , 4 days ago
Component: | Uncategorized → Utilities |
---|---|
Keywords: | getlist default added; sentinel removed |
Resolution: | → wontfix |
Status: | new → closed |
Type: | Uncategorized → New feature |
Version: | 5.1 → dev |
I don't think we should change this. The behavior is exactly as documented for, QueryDict
:
Returns a list of the data with the requested key. Returns an empty list if the key doesn’t exist and default is None. It’s guaranteed to return a list unless the default value provided isn’t a list.
It makes sense to me that getlist
should always return a list. The intended usage is either to pass a non-None
default or expect an empty list as the result. Changing the semantics of default would be a new feature, requiring not only a deprecation period but also community consensus.
(Note that MultiValueDict
, on its own, is not a documented nor public data structure to reuse. It's the base structure for QueryDict
and that's the behavior I'm analyzing and prioritizing with this triage.)
Assuming we want to change this behavior we'd have to go through a deprecation period as there might be code out there that expects
[]
to be returned fromgetlist(missing_key, None)
. Slightly related to #29356.