#27058: {% for %} tag unpacking: less strict checks
--------------------------------------+--------------------
Reporter: sergei-maertens | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Template system | Version: 1.10
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+--------------------
In 1.10 there are now checks done before unpacking in a for loop:
{{{
{% for foo, bar in items %}
...
{% endfor %}
}}}
Where it is verified that everey `item` in `items` has `len(item) == 2` in
this particular case.
However, the check is implemented so that `len(item)` is taken only if the
item is an instance of tuple or list, while custom objects may also
implement `def __len__(self):`. Because the custom object does not
subclass from tuple or list, it's length is set to 1, leading to
ValueErrors.
I have a working patch at https://github.com/django/django/compare/master
...sergei-maertens:duck-type-forloop-unpack-len?expand=1, tests are
passing. The check no longer checks if the item is an instance of
tuple/list, but just tries to get the `len(item)` and handles
`TypeError`s.
The only case I could imagine was where the items would be an iterable of
strings, but even then it turns out the check is correct (as it is
technically possible to unpack strings).
--
Ticket URL: <https://code.djangoproject.com/ticket/27058>
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 post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/058.2ebe109e464c26828f2db60185047143%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.