#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 <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/0107019352bfdf27-8597a76b-d04d-4bb7-8849-80f4e504eaac-000000%40eu-central-1.amazonses.com.

Reply via email to