Opened 7 days ago
Last modified 22 hours ago
#35915 assigned Cleanup/optimization
QueryDict __getitem__ returns list which is surpising — at Initial Version
Reported by: | Nguyễn Hồng Quân | Owned by: | |
---|---|---|---|
Component: | HTTP handling | Version: | 5.1 |
Severity: | Normal | Keywords: | querydict |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Though 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:
>>> 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 asstr | None
. - If returns
[]
, Python doesn't have an exact type for empty list, anddjango-stubs
has to annotate asstr | list[object]
which is quite broader than the actual value (empty list).
Note:
See TracTickets
for help on using tickets.