Opened 4 days ago

Closed 4 days ago

#36278 closed New feature (wontfix)

MultiValueDict should use sentinel for default values

Reported by: Peter Larsen - Puzzel Solutions Owned by:
Component: Utilities Version: dev
Severity: Normal Keywords: MultiValueDict getlist default
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Peter Larsen - Puzzel Solutions)

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 Peter Larsen - Puzzel Solutions, 4 days ago

Description: modified (diff)

comment:2 by Simon Charette, 4 days ago

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 from getlist(missing_key, None). Slightly related to #29356.

Last edited 4 days ago by Simon Charette (previous) (diff)

comment:3 by Natalia Bidart, 4 days ago

Component: UncategorizedUtilities
Keywords: getlist default added; sentinel removed
Resolution: wontfix
Status: newclosed
Type: UncategorizedNew feature
Version: 5.1dev

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.)

Note: See TracTickets for help on using tickets.
Back to Top