#35915: QueryDict __getitem__ returns list which is surpising
-------------------------------------+-------------------------------------
Reporter: Nguyễn Hồng Quân | Type:
| Cleanup/optimization
Status: new | Component: HTTP
| handling
Version: 5.1 | Severity: Normal
Keywords: querydict | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Though [
https://docs.djangoproject.com/en/5.1/ref/request-
response/#django.http.QueryDict.__getitem__ documentation] says that
QueryDict.!__getitem!__(key)
Returns the value for the given key. If the key has more than one
value, it returns the last value.
It returns a list when the raw value is an empty list:
{{{#!python
>>> from django.http import QueryDict
>>> q = QueryDict('a=1', mutable=True)
>>> q['a']
'1'
>>> q.setlist('b', [])
>>> q['b']
[]
}}}
which surprises user and it even not mentioned in documentation.
Could we change this behavior? I know that we don't have a perfect
solution, but returning `None` in this case is less bad than empty list
`[]`, because it is easier to annotate type.
- If returns `None`, we can annotate type as `str | None`.
- If returns `[]`, Python doesn't have an exact type for empty list, and
`django-stubs` has to annotate as `str | list[object]` which is quite
broader than the actual value (empty list).
--
Ticket URL: <
https://code.djangoproject.com/ticket/35915>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.