[Django] #25670: `dictsort` does not work when `arg` parameter is numeric

46 views
Skip to first unread message

Django

unread,
Nov 3, 2015, 7:34:38 AM11/3/15
to django-...@googlegroups.com
#25670: `dictsort` does not work when `arg` parameter is numeric
---------------------------------+--------------------------------------
Reporter: soon | Owner: soon
Type: Bug | Status: new
Component: Template system | Version: 1.8
Severity: Normal | Keywords: template filter dictsort
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
According to `dictsort` documentation, it orders given list of
dictionaries, using `arg` as property in each dictionary:

{{{
@register.filter(is_safe=False)
def dictsort(value, arg):
"""
Takes a list of dicts, returns that list sorted by the property given
in
the argument.
"""
try:
return sorted(value, key=Variable(arg).resolve)
except (TypeError, VariableDoesNotExist):
return ''
}}}

However, it is not possible to order list of dictionaries by a numeric
key. Let's consider the following test case:

{{{
def test_sort_list_of_tuple_like_dicts(self):
data = [{'0': 'a', '1': '42'},
{'0': 'c', '1': 'string'},
{'0': 'b', '1': 'foo'}]

sorted_data = dictsort(data, '0')

self.assertEqual([{'0': 'a', '1': '42'},
{'0': 'b', '1': 'foo'},
{'0': 'c', '1': 'string'}], sorted_data)
}}}

This test fails with the following message:

{{{
Traceback (most recent call last):
File ".../django/tests/template_tests/filter_tests/test_dictsort.py",
line 50, in test_sort_list_of_tuple_like_dicts
{'0': 'c', '1': 'string'}], sorted_data)
AssertionError: Lists differ: [{'0': 'a', '1': '42'}, {'0': 'b', '1':
'foo'}, {'0': 'c', '1': 'string'}] != [{'0': 'a', '1': '42'}, {'0': 'c',
'1': 'string'}, {'0': 'b', '1': 'foo'}]

First differing element 1:
{'0': 'b', '1': 'foo'}
{'0': 'c', '1': 'string'}

- [{'0': 'a', '1': '42'}, {'0': 'b', '1': 'foo'}, {'0': 'c', '1':
'string'}]
+ [{'0': 'a', '1': '42'}, {'0': 'c', '1': 'string'}, {'0': 'b', '1':
'foo'}]
}}}

The `dictsort` uses `sorted` function with `key=Variable(arg).resolve`.
When `arg` is `'0'`, `key` function should behave like
`operator.itemgetter('0')`, but `Variable('0').resolve(context)` returns
`0` regardless of given `context`.

There are five usages of `dictsort` with `"0"` as `arg` in
[https://github.com/django/django/blob/master/django/views/debug.py#L816
debug.py]

As [https://code.djangoproject.com/ticket/25646#comment:4 mentioned] by
''bmispelon'', this may be some kind of regression in Django 1.3.

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

Django

unread,
Nov 4, 2015, 3:21:28 AM11/4/15
to django-...@googlegroups.com
#25670: `dictsort` does not work when `arg` parameter is numeric
-------------------------------------+-------------------------------------

Reporter: soon | Owner: soon
Type: Bug | Status: new
Component: Template system | Version: 1.8
Severity: Normal | Resolution:
Keywords: template filter | Triage Stage: Accepted
dictsort |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* needs_better_patch: => 0
* needs_docs: => 0
* needs_tests: => 0
* stage: Unreviewed => Accepted


Comment:

This ticket is a spinoff of #25646.

--
Ticket URL: <https://code.djangoproject.com/ticket/25670#comment:1>

Django

unread,
Nov 5, 2015, 6:14:56 AM11/5/15
to django-...@googlegroups.com
#25670: `dictsort` does not work when `arg` parameter is numeric
-------------------------------------+-------------------------------------
Reporter: soon | Owner: soon
Type: Bug | Status: assigned

Component: Template system | Version: 1.8
Severity: Normal | Resolution:
Keywords: template filter | Triage Stage: Accepted
dictsort |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by soon):

* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/25670#comment:2>

Django

unread,
Nov 5, 2015, 10:13:57 AM11/5/15
to django-...@googlegroups.com
#25670: `dictsort` does not work when `arg` parameter is numeric
-------------------------------------+-------------------------------------
Reporter: soon | Owner: soon
Type: Bug | Status: assigned

Component: Template system | Version: 1.8
Severity: Normal | Resolution:
Keywords: template filter | Triage Stage: Accepted
dictsort |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/5551 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/25670#comment:3>

Django

unread,
Jan 23, 2016, 10:34:32 AM1/23/16
to django-...@googlegroups.com
#25670: `dictsort` does not work when `arg` parameter is numeric
-------------------------------------+-------------------------------------
Reporter: soon | Owner: soon
Type: Bug | Status: assigned

Component: Template system | Version: 1.8
Severity: Normal | Resolution:
Keywords: template filter | Triage Stage: Accepted
dictsort |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by timgraham):

I'm not sure about this. It seems like the old behavior was somewhat
accidental. Having a filter called "dictsort" work on a list of lists
seems a bit odd and unintuitive. Maybe we should ask for other opinions on
the django-developers mailing list. Maybe there is a common third-party
filter that would do the job that we could add to builtins.

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

Django

unread,
Jan 23, 2016, 10:57:52 AM1/23/16
to django-...@googlegroups.com
#25670: `dictsort` does not work when `arg` parameter is numeric
-------------------------------------+-------------------------------------
Reporter: soon | Owner: soon
Type: Bug | Status: assigned

Component: Template system | Version: 1.8
Severity: Normal | Resolution:
Keywords: template filter | Triage Stage: Accepted
dictsort |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by timgraham):

After some further thought, I changed my mind given the fact that dictsort
doesn't work with numeric string keys. That's definitely a bug that should
be fixed.

--
Ticket URL: <https://code.djangoproject.com/ticket/25670#comment:5>

Django

unread,
Jan 23, 2016, 11:04:05 AM1/23/16
to django-...@googlegroups.com
#25670: `dictsort` does not work when `arg` parameter is numeric
-------------------------------------+-------------------------------------
Reporter: soon | Owner: soon
Type: Bug | Status: assigned

Component: Template system | Version: 1.8
Severity: Normal | Resolution:
Keywords: template filter | Triage Stage: Accepted
dictsort |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* needs_better_patch: 0 => 1


Comment:

I left comments for improvement on the pull request.

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

Django

unread,
Feb 22, 2016, 5:20:16 PM2/22/16
to django-...@googlegroups.com
#25670: `dictsort` does not work when `arg` parameter is numeric
-------------------------------------+-------------------------------------
Reporter: soon | Owner: soon
Type: Bug | Status: assigned

Component: Template system | Version: 1.8
Severity: Normal | Resolution:
Keywords: template filter | Triage Stage: Accepted
dictsort |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/25670#comment:7>

Django

unread,
Feb 23, 2016, 12:16:03 PM2/23/16
to django-...@googlegroups.com
#25670: `dictsort` does not work when `arg` parameter is numeric
-------------------------------------+-------------------------------------
Reporter: soon | Owner: soon
Type: Bug | Status: closed

Component: Template system | Version: 1.8
Severity: Normal | Resolution: fixed

Keywords: template filter | Triage Stage: Accepted
dictsort |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

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


Comment:

In [changeset:"e81d1c995c0cc5573d3627de0fe6b803b2f43fb2" e81d1c99]:
{{{
#!CommitTicketReference repository=""
revision="e81d1c995c0cc5573d3627de0fe6b803b2f43fb2"
Fixed #25670 -- Allowed dictsort to sort a list of lists.

Thanks Tim Graham for the review.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25670#comment:8>

Reply all
Reply to author
Forward
0 new messages