#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 , 16 years ago
| Cc: | added |
|---|
comment:2 by , 16 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:3 by , 16 years ago
comment:4 by , 16 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 , 15 years ago
| Triage Stage: | Accepted → Design decision needed |
|---|
comment:6 by , 15 years ago
| Severity: | → Normal |
|---|---|
| Type: | → Bug |
comment:7 by , 14 years ago
| 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 by , 12 years ago
| Owner: | changed from to |
|---|---|
| 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 by , 12 years ago
| Has patch: | set |
|---|---|
| Needs documentation: | set |
| Patch needs improvement: | set |
comment:10 by , 12 years ago
| 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.