Opened 7 years ago

Closed 3 years ago

Last modified 4 months 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: master
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 Changed 7 years ago by Peter Bengtsson

Cc: peter@… added

comment:2 Changed 7 years ago by Russell Keith-Magee

Triage Stage: UnreviewedAccepted

comment:3 Changed 7 years ago by Chris Beaven

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 Changed 7 years ago by Peter Bengtsson

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 Changed 6 years ago by Sam Thompson

Triage Stage: AcceptedDesign decision needed

comment:6 Changed 6 years ago by Julien Phalip

Severity: Normal
Type: Bug

comment:7 Changed 5 years ago by Aymeric Augustin

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 Changed 3 years ago by Ivan Kolodyazhny

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 Changed 3 years ago by Tim Graham

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

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

Resolution: fixed
Status: assignedclosed

In 2aaa045c61a3d4b325f964aa01554107b47b9774:

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

Thanks peterbe for the suggestion.

comment:11 Changed 15 months ago by Tim Graham <timograham@…>

In 3bbebd0:

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

comment:12 Changed 4 months ago by Tim Graham <timograham@…>

In ba749f8:

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

comment:13 Changed 4 months ago by Tim Graham <timograham@…>

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