#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 Changed 13 years ago by
Cc: | peter@… added |
---|
comment:2 Changed 13 years ago by
Triage Stage: | Unreviewed → Accepted |
---|
comment:3 Changed 13 years ago by
comment:4 Changed 13 years ago by
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 12 years ago by
Triage Stage: | Accepted → Design decision needed |
---|
comment:6 Changed 12 years ago by
Severity: | → Normal |
---|---|
Type: | → Bug |
comment:7 Changed 11 years ago by
Easy pickings: | unset |
---|---|
Triage Stage: | Design decision needed → 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 9 years ago by
Owner: | changed from nobody to Ivan Kolodyazhny |
---|---|
Status: | new → 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 9 years ago by
Has patch: | set |
---|---|
Needs documentation: | set |
Patch needs improvement: | set |
comment:10 Changed 9 years ago by
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
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.