Re: [Django] #35915: QueryDict __getitem__ returns an empty list when the value is an empty list

32 views
Skip to first unread message

Django

unread,
Nov 20, 2024, 10:53:41 AM11/20/24
to django-...@googlegroups.com
#35915: QueryDict __getitem__ returns an empty list when the value is an empty list
-------------------------------------+-------------------------------------
Reporter: Nguyễn Hồng Quân | Owner:
Type: | jburns6789
Cleanup/optimization | Status: assigned
Component: HTTP handling | Version: 5.1
Severity: Normal | Resolution:
Keywords: querydict | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by jburns6789):

* owner: (none) => jburns6789
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/35915#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 20, 2024, 11:47:20 AM11/20/24
to django-...@googlegroups.com
#35915: QueryDict __getitem__ returns an empty list when the value is an empty list
-------------------------------------+-------------------------------------
Reporter: Nguyễn Hồng Quân | Owner:
Type: | jburns6789
Cleanup/optimization | Status: assigned
Component: HTTP handling | Version: 5.1
Severity: Normal | Resolution:
Keywords: querydict | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by jburns6789):

Ok, looking over your suggestion,
I'm trying to figure out what I need to do, here is the doc section for
QuerySet

https://docs.djangoproject.com/en/5.1/ref/request-
response/#django.http.QueryDict

QueryDict.__getitem__(key)¶
Returns the value for the given key. If the key has more than one value,
it returns the last value. Raises
django.utils.datastructures.MultiValueDictKeyError if the key does not
exist. (This is a subclass of Python’s standard KeyError, so you can stick
to catching KeyError.)

I need to state when a QueryDict['key'] returns [ ], the said key is
associated with a empty list?
--
Ticket URL: <https://code.djangoproject.com/ticket/35915#comment:3>

Django

unread,
Nov 20, 2024, 11:49:20 AM11/20/24
to django-...@googlegroups.com
#35915: QueryDict __getitem__ returns an empty list when the value is an empty list
-------------------------------------+-------------------------------------
Reporter: Nguyễn Hồng Quân | Owner: Ayush
Type: | Khatri
Cleanup/optimization | Status: assigned
Component: HTTP handling | Version: 5.1
Severity: Normal | Resolution:
Keywords: querydict | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Ayush Khatri ):

* owner: jburns6789 => Ayush Khatri

--
Ticket URL: <https://code.djangoproject.com/ticket/35915#comment:4>

Django

unread,
Nov 20, 2024, 12:01:45 PM11/20/24
to django-...@googlegroups.com
#35915: QueryDict __getitem__ returns an empty list when the value is an empty list
-------------------------------------+-------------------------------------
Reporter: Nguyễn Hồng Quân | Owner: Ayush
Type: | Khatri
Cleanup/optimization | Status: assigned
Component: HTTP handling | Version: 5.1
Severity: Normal | Resolution:
Keywords: querydict | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Sarah Boyce):

Replying to [comment:3 jburns6789]:
> QueryDict.__getitem__(key)¶
> Returns the value for the given key. If the key has more than one value,
it returns the last value. Raises
django.utils.datastructures.MultiValueDictKeyError if the key does not
exist. (This is a subclass of Python’s standard KeyError, so you can stick
to catching KeyError.)
>
> I need to state when a QueryDict['key'] returns [ ], the said key is
associated with a empty list?

Yes
I would be tempted to update this to be very similar to the doc string so
maybe
{{{#!diff
diff --git a/docs/ref/request-response.txt b/docs/ref/request-response.txt
index afebd00d8b..abdf75984d 100644
--- a/docs/ref/request-response.txt
+++ b/docs/ref/request-response.txt
@@ -554,11 +554,10 @@ a subclass of dictionary. Exceptions are outlined
here:

.. method:: QueryDict.__getitem__(key)

- Returns the value for the given key. If the key has more than one
value,
- it returns the last value. Raises
- ``django.utils.datastructures.MultiValueDictKeyError`` if the key
does not
- exist. (This is a subclass of Python's standard :exc:`KeyError`, so
you can
- stick to catching ``KeyError``.)
+ Returns the last data value for the given key, or ``[]`` if it's an
empty
+ list. Raises ``django.utils.datastructures.MultiValueDictKeyError``
if the
+ key does not exist. (This is a subclass of Python's standard
+ :exc:`KeyError`, so you can stick to catching ``KeyError``.)

.. method:: QueryDict.__setitem__(key, value)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35915#comment:5>

Django

unread,
Nov 20, 2024, 12:02:33 PM11/20/24
to django-...@googlegroups.com
#35915: QueryDict __getitem__ returns an empty list when the value is an empty list
-------------------------------------+-------------------------------------
Reporter: Nguyễn Hồng Quân | Owner:
Type: | jburns6789
Cleanup/optimization | Status: assigned
Component: HTTP handling | Version: 5.1
Severity: Normal | Resolution:
Keywords: querydict | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* owner: Ayush Khatri => jburns6789

--
Ticket URL: <https://code.djangoproject.com/ticket/35915#comment:6>

Django

unread,
Nov 21, 2024, 1:46:25 PM11/21/24
to django-...@googlegroups.com
#35915: QueryDict __getitem__ returns an empty list when the value is an empty list
-------------------------------------+-------------------------------------
Reporter: Nguyễn Hồng Quân | Owner:
Type: | jburns6789
Cleanup/optimization | Status: assigned
Component: HTTP handling | Version: 5.1
Severity: Normal | Resolution:
Keywords: querydict | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by jburns6789):

Hi, here is my link to the PR, someone else was reviewing, is there
anything I need to in the ticket tracker?
--
Ticket URL: <https://code.djangoproject.com/ticket/35915#comment:7>

Django

unread,
Nov 21, 2024, 1:46:41 PM11/21/24
to django-...@googlegroups.com
#35915: QueryDict __getitem__ returns an empty list when the value is an empty list
-------------------------------------+-------------------------------------
Reporter: Nguyễn Hồng Quân | Owner:
Type: | jburns6789
Cleanup/optimization | Status: assigned
Component: HTTP handling | Version: 5.1
Severity: Normal | Resolution:
Keywords: querydict | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by jburns6789):

https://github.com/django/django/pull/18834
--
Ticket URL: <https://code.djangoproject.com/ticket/35915#comment:8>

Django

unread,
Nov 21, 2024, 2:32:17 PM11/21/24
to django-...@googlegroups.com
#35915: QueryDict __getitem__ returns an empty list when the value is an empty list
-------------------------------------+-------------------------------------
Reporter: Nguyễn Hồng Quân | Owner:
Type: | jburns6789
Cleanup/optimization | Status: assigned
Component: HTTP handling | Version: 5.1
Severity: Normal | Resolution:
Keywords: querydict | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by jburns6789):

* has_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/35915#comment:9>

Django

unread,
Nov 22, 2024, 2:22:12 AM11/22/24
to django-...@googlegroups.com
#35915: QueryDict __getitem__ returns an empty list when the value is an empty list
-------------------------------------+-------------------------------------
Reporter: Nguyễn Hồng Quân | Owner:
Type: | jburns6789
Cleanup/optimization | Status: assigned
Component: HTTP handling | Version: 5.1
Severity: Normal | Resolution:
Keywords: querydict | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by David Sanders:

Old description:

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

New description:

**Update:** forum post: https://forum.djangoproject.com/t/change-
querydict-getitem-in-case-of-empty-list/36522


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#comment:10>

Django

unread,
Nov 23, 2024, 5:30:50 PM11/23/24
to django-...@googlegroups.com
#35915: QueryDict __getitem__ returns an empty list when the value is an empty list
-------------------------------------+-------------------------------------
Reporter: Nguyễn Hồng Quân | Owner:
Type: | jburns6789
Cleanup/optimization | Status: assigned
Component: HTTP handling | Version: 5.1
Severity: Normal | Resolution:
Keywords: querydict | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by jburns6789):

Updated requested information https://github.com/django/django/pull/18841
--
Ticket URL: <https://code.djangoproject.com/ticket/35915#comment:11>

Django

unread,
Nov 25, 2024, 10:21:32 PM11/25/24
to django-...@googlegroups.com
#35915: QueryDict __getitem__ returns an empty list when the value is an empty list
-------------------------------------+-------------------------------------
Reporter: Nguyễn Hồng Quân | Owner:
Type: | jburns6789
Cleanup/optimization | Status: assigned
Component: HTTP handling | Version: 5.1
Severity: Normal | Resolution:
Keywords: querydict | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by jburns6789):

Is there anything else needed to be completed? or do I keep checking for
someone to approve/deny the merging of the PR?
--
Ticket URL: <https://code.djangoproject.com/ticket/35915#comment:12>

Django

unread,
Nov 26, 2024, 7:42:23 AM11/26/24
to django-...@googlegroups.com
#35915: QueryDict __getitem__ returns an empty list when the value is an empty list
-------------------------------------+-------------------------------------
Reporter: Nguyễn Hồng Quân | Owner:
Type: | jburns6789
Cleanup/optimization | Status: assigned
Component: HTTP handling | Version: 5.1
Severity: Normal | Resolution:
Keywords: querydict | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Tim Graham):

The yellow "According to the ticket's flags" box below the ticket
description explains the next steps.
--
Ticket URL: <https://code.djangoproject.com/ticket/35915#comment:13>

Django

unread,
Nov 27, 2024, 6:09:34 AM11/27/24
to django-...@googlegroups.com
#35915: QueryDict __getitem__ returns an empty list when the value is an empty list
-------------------------------------+-------------------------------------
Reporter: Nguyễn Hồng Quân | Owner:
Type: | jburns6789
Cleanup/optimization | Status: assigned
Component: HTTP handling | Version: 5.1
Severity: Normal | Resolution:
Keywords: querydict | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/35915#comment:14>

Django

unread,
Dec 2, 2024, 4:37:12 AM12/2/24
to django-...@googlegroups.com
#35915: QueryDict __getitem__ returns an empty list when the value is an empty list
-------------------------------------+-------------------------------------
Reporter: Nguyễn Hồng Quân | Owner:
Type: | jburns6789
Cleanup/optimization | Status: assigned
Component: HTTP handling | Version: 5.1
Severity: Normal | Resolution:
Keywords: querydict | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/35915#comment:15>

Django

unread,
Dec 2, 2024, 5:11:15 AM12/2/24
to django-...@googlegroups.com
#35915: QueryDict __getitem__ returns an empty list when the value is an empty list
-------------------------------------+-------------------------------------
Reporter: Nguyễn Hồng Quân | Owner:
Type: | jburns6789
Cleanup/optimization | Status: closed
Component: HTTP handling | Version: 5.1
Severity: Normal | Resolution: fixed
Keywords: querydict | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce <42296566+sarahboyce@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"b8f9f625a19298379dbc16d916694702cd038f52" b8f9f625]:
{{{#!CommitTicketReference repository=""
revision="b8f9f625a19298379dbc16d916694702cd038f52"
Fixed #35915 -- Clarified the empty list case in QueryDict.__getitem__()
docs.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35915#comment:16>
Reply all
Reply to author
Forward
0 new messages