Opened 5 years ago

Closed 14 months ago

#13408 closed Bug (fixed)

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

Reported by: peterbe Owned by: e0ne
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 (10)

comment:1 Changed 5 years ago by peterbe

  • Cc peter@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 5 years ago by russellm

  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 5 years ago by SmileyChris

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 5 years ago by peterbe

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 4 years ago by GDorn

  • Triage Stage changed from Accepted to Design decision needed

comment:6 Changed 4 years ago by julien

  • Severity set to Normal
  • Type set to Bug

comment:7 Changed 3 years ago by aaugustin

  • Easy pickings unset
  • Triage Stage changed from Design decision needed to Accepted
  • 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 21 months ago by e0ne

  • Owner changed from nobody to e0ne
  • Status changed from new to assigned

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

comment:9 Changed 21 months ago by timo

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

comment:10 Changed 14 months ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

In 2aaa045c61a3d4b325f964aa01554107b47b9774:

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

Thanks peterbe for the suggestion.

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