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

This commit is contained in:
Tim Graham 2015-08-18 12:25:38 -04:00
parent 1392aff440
commit 3bbebd06ad
2 changed files with 20 additions and 38 deletions

View File

@ -9,7 +9,6 @@ from itertools import cycle as itertools_cycle, groupby
from django.conf import settings from django.conf import settings
from django.utils import six, timezone from django.utils import six, timezone
from django.utils.deprecation import RemovedInDjango110Warning
from django.utils.encoding import force_text, smart_text from django.utils.encoding import force_text, smart_text
from django.utils.html import conditional_escape, format_html from django.utils.html import conditional_escape, format_html
from django.utils.lorem_ipsum import paragraphs, words from django.utils.lorem_ipsum import paragraphs, words
@ -200,11 +199,10 @@ class ForNode(Node):
len_item = len(item) len_item = len(item)
# Check loop variable count before unpacking # Check loop variable count before unpacking
if num_loopvars != len_item: if num_loopvars != len_item:
warnings.warn( raise ValueError(
"Need {} values to unpack in for loop; got {}. " "Need {} values to unpack in for loop; got {}. "
"This will raise an exception in Django 1.10."
.format(num_loopvars, len_item), .format(num_loopvars, len_item),
RemovedInDjango110Warning) )
try: try:
unpacked_vars = dict(zip(self.loopvars, item)) unpacked_vars = dict(zip(self.loopvars, item))
except TypeError: except TypeError:

View File

@ -1,6 +1,5 @@
from django.template import TemplateSyntaxError from django.template import TemplateSyntaxError
from django.test import SimpleTestCase, ignore_warnings from django.test import SimpleTestCase
from django.utils.deprecation import RemovedInDjango110Warning
from ..utils import setup from ..utils import setup
@ -130,46 +129,31 @@ class ForTagTests(SimpleTestCase):
# These tests raise deprecation warnings and will raise an exception # These tests raise deprecation warnings and will raise an exception
# in Django 1.10. The existing behavior is silent truncation if the # in Django 1.10. The existing behavior is silent truncation if the
# length of loopvars differs to the length of each set of items. # length of loopvars differs to the length of each set of items.
@ignore_warnings(category=RemovedInDjango110Warning)
@setup({'for-tag-unpack10': '{% for x,y in items %}{{ x }}:{{ y }}/{% endfor %}'}) @setup({'for-tag-unpack10': '{% for x,y in items %}{{ x }}:{{ y }}/{% endfor %}'})
def test_for_tag_unpack10(self): def test_for_tag_unpack10(self):
output = self.engine.render_to_string( with self.assertRaisesMessage(ValueError, 'Need 2 values to unpack in for loop; got 3.'):
self.engine.render_to_string(
'for-tag-unpack10', 'for-tag-unpack10',
{'items': (('one', 1, 'carrot'), ('two', 2, 'orange'))}, {'items': (('one', 1, 'carrot'), ('two', 2, 'orange'))},
) )
self.assertEqual(output, 'one:1/two:2/')
@ignore_warnings(category=RemovedInDjango110Warning)
@setup({'for-tag-unpack11': '{% for x,y,z in items %}{{ x }}:{{ y }},{{ z }}/{% endfor %}'}) @setup({'for-tag-unpack11': '{% for x,y,z in items %}{{ x }}:{{ y }},{{ z }}/{% endfor %}'})
def test_for_tag_unpack11(self): def test_for_tag_unpack11(self):
output = self.engine.render_to_string( with self.assertRaisesMessage(ValueError, 'Need 3 values to unpack in for loop; got 2.'):
self.engine.render_to_string(
'for-tag-unpack11', 'for-tag-unpack11',
{'items': (('one', 1), ('two', 2))}, {'items': (('one', 1), ('two', 2))},
) )
if self.engine.string_if_invalid:
self.assertEqual(output, 'one:1,INVALID/two:2,INVALID/')
else:
self.assertEqual(output, 'one:1,/two:2,/')
@ignore_warnings(category=RemovedInDjango110Warning)
@setup({'for-tag-unpack12': '{% for x,y,z in items %}{{ x }}:{{ y }},{{ z }}/{% endfor %}'}) @setup({'for-tag-unpack12': '{% for x,y,z in items %}{{ x }}:{{ y }},{{ z }}/{% endfor %}'})
def test_for_tag_unpack12(self): def test_for_tag_unpack12(self):
output = self.engine.render_to_string( with self.assertRaisesMessage(ValueError, 'Need 3 values to unpack in for loop; got 2.'):
self.engine.render_to_string(
'for-tag-unpack12', 'for-tag-unpack12',
{'items': (('one', 1, 'carrot'), ('two', 2))} {'items': (('one', 1, 'carrot'), ('two', 2))}
) )
if self.engine.string_if_invalid:
self.assertEqual(output, 'one:1,carrot/two:2,INVALID/')
else:
self.assertEqual(output, 'one:1,carrot/two:2,/')
@ignore_warnings(category=RemovedInDjango110Warning)
@setup({'for-tag-unpack14': '{% for x,y in items %}{{ x }}:{{ y }}/{% endfor %}'}) @setup({'for-tag-unpack14': '{% for x,y in items %}{{ x }}:{{ y }}/{% endfor %}'})
def test_for_tag_unpack14(self): def test_for_tag_unpack14(self):
output = self.engine.render_to_string('for-tag-unpack14', {'items': (1, 2)}) with self.assertRaisesMessage(ValueError, 'Need 2 values to unpack in for loop; got 1.'):
self.engine.render_to_string('for-tag-unpack14', {'items': (1, 2)})
if self.engine.string_if_invalid:
self.assertEqual(output, 'INVALID:INVALID/INVALID:INVALID/')
else:
self.assertEqual(output, ':/:/')