Opened 14 years ago

Closed 10 years ago

Last modified 8 years ago

#13408 closed Bug (fixed)

Unpacking list/tuple in for loop should raise exception on unpacking if length is different

Reported by: Peter Bengtsson Owned by: Ivan Kolodyazhny
Component: Template system Version: dev
Severity: Normal Keywords:
Cc: peter@… Triage Stage: Accepted
Has patch: yes Needs documentation: yes
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

In Python and Django you can do this:

points_3d = [(1,1,1), (2,2,0), ]

# python
for x, y, z in points_3d:
    pass

# django template
{% for x, y, z in points_3d %}
{% endfor %}

However, in Python you can't do this

for x, y in points_3d:
ValueError: too many values to unpack

# or

for x, y, z, w in points_3d:
ValueError: need more than 3 values to unpack

BUT Django allows it and doesn't raise a ValueError and I think this is bad. It allows you to do this:

{% for x, y, z, w in points_3d %}
  no probs :(
{% endfor %}

It lends itself to invalid and "incorrect" template code which can potentially hide programming mistakes that come back and bite you later.

Change History (13)

comment:1 by Peter Bengtsson, 14 years ago

Cc: peter@… added

comment:2 by Russell Keith-Magee, 14 years ago

Triage Stage: UnreviewedAccepted

comment:3 by Chris Beaven, 14 years ago

I'm not sure if this falls into a backwards compatibility issue: The for tag was designed to not raise exceptions while rendering, and the current behaviour has specific tests.

comment:4 by Peter Bengtsson, 14 years ago

I honestly don't know about the core tenants of the templating language. It might not be a bug then but my opinion is that it's a bad design decision and I'm questioning it.

comment:5 by Sam Thompson, 13 years ago

Triage Stage: AcceptedDesign decision needed

comment:6 by Julien Phalip, 13 years ago

Severity: Normal
Type: Bug

comment:7 by Aymeric Augustin, 12 years ago

Easy pickings: unset
Triage Stage: Design decision neededAccepted
UI/UX: unset

This was initially marked as accepted by Russell, and I'm going to mark it as accepted again.

Idon't see much advantage in hiding this kind of errors.

We'll have to follow a deprecation path: start by raising warnings before we raise an actual exception.

comment:8 by Ivan Kolodyazhny, 11 years ago

Owner: changed from nobody to Ivan Kolodyazhny
Status: newassigned

Here is proposed patch https://github.com/django/django/pull/1628. I'm not sure that warning message is good enough.

comment:9 by Tim Graham, 11 years ago

Has patch: set
Needs documentation: set
Patch needs improvement: set

comment:10 by Tim Graham <timograham@…>, 10 years ago

Resolution: fixed
Status: assignedclosed

In 2aaa045c61a3d4b325f964aa01554107b47b9774:

Fixed #13408 -- Deprecated silent unpacking exception passing in for template tag.

Thanks peterbe for the suggestion.

comment:11 by Tim Graham <timograham@…>, 9 years ago

In 3bbebd0:

Refs #13408 -- Made unpacking mismatch an exception in {% for %} tag per deprecation timeline.

comment:12 by Tim Graham <timograham@…>, 8 years ago

In ba749f8:

Refs #13408 -- Removed obsolete code/comments from {% for %} unpacking deprecation.

comment:13 by Tim Graham <timograham@…>, 8 years ago

In 54771f66:

[1.10.x] Refs #13408 -- Removed obsolete code/comments from {% for %} unpacking deprecation.

Backport of ba749f8f87dd60b20aeaefb84ee182f192746ebf from master

Note: See TracTickets for help on using tickets.
Back to Top