Refs #13408 -- Made unpacking mismatch an exception in {% for %} tag per deprecation timeline.
This commit is contained in:
parent
1392aff440
commit
3bbebd06ad
|
@ -9,7 +9,6 @@ from itertools import cycle as itertools_cycle, groupby
|
|||
|
||||
from django.conf import settings
|
||||
from django.utils import six, timezone
|
||||
from django.utils.deprecation import RemovedInDjango110Warning
|
||||
from django.utils.encoding import force_text, smart_text
|
||||
from django.utils.html import conditional_escape, format_html
|
||||
from django.utils.lorem_ipsum import paragraphs, words
|
||||
|
@ -200,11 +199,10 @@ class ForNode(Node):
|
|||
len_item = len(item)
|
||||
# Check loop variable count before unpacking
|
||||
if num_loopvars != len_item:
|
||||
warnings.warn(
|
||||
raise ValueError(
|
||||
"Need {} values to unpack in for loop; got {}. "
|
||||
"This will raise an exception in Django 1.10."
|
||||
.format(num_loopvars, len_item),
|
||||
RemovedInDjango110Warning)
|
||||
)
|
||||
try:
|
||||
unpacked_vars = dict(zip(self.loopvars, item))
|
||||
except TypeError:
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
from django.template import TemplateSyntaxError
|
||||
from django.test import SimpleTestCase, ignore_warnings
|
||||
from django.utils.deprecation import RemovedInDjango110Warning
|
||||
from django.test import SimpleTestCase
|
||||
|
||||
from ..utils import setup
|
||||
|
||||
|
@ -130,46 +129,31 @@ class ForTagTests(SimpleTestCase):
|
|||
# These tests raise deprecation warnings and will raise an exception
|
||||
# in Django 1.10. The existing behavior is silent truncation if the
|
||||
# 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 %}'})
|
||||
def test_for_tag_unpack10(self):
|
||||
output = self.engine.render_to_string(
|
||||
'for-tag-unpack10',
|
||||
{'items': (('one', 1, 'carrot'), ('two', 2, 'orange'))},
|
||||
)
|
||||
self.assertEqual(output, 'one:1/two:2/')
|
||||
with self.assertRaisesMessage(ValueError, 'Need 2 values to unpack in for loop; got 3.'):
|
||||
self.engine.render_to_string(
|
||||
'for-tag-unpack10',
|
||||
{'items': (('one', 1, 'carrot'), ('two', 2, 'orange'))},
|
||||
)
|
||||
|
||||
@ignore_warnings(category=RemovedInDjango110Warning)
|
||||
@setup({'for-tag-unpack11': '{% for x,y,z in items %}{{ x }}:{{ y }},{{ z }}/{% endfor %}'})
|
||||
def test_for_tag_unpack11(self):
|
||||
output = self.engine.render_to_string(
|
||||
'for-tag-unpack11',
|
||||
{'items': (('one', 1), ('two', 2))},
|
||||
)
|
||||
with self.assertRaisesMessage(ValueError, 'Need 3 values to unpack in for loop; got 2.'):
|
||||
self.engine.render_to_string(
|
||||
'for-tag-unpack11',
|
||||
{'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 %}'})
|
||||
def test_for_tag_unpack12(self):
|
||||
output = self.engine.render_to_string(
|
||||
'for-tag-unpack12',
|
||||
{'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,/')
|
||||
with self.assertRaisesMessage(ValueError, 'Need 3 values to unpack in for loop; got 2.'):
|
||||
self.engine.render_to_string(
|
||||
'for-tag-unpack12',
|
||||
{'items': (('one', 1, 'carrot'), ('two', 2))}
|
||||
)
|
||||
|
||||
@ignore_warnings(category=RemovedInDjango110Warning)
|
||||
@setup({'for-tag-unpack14': '{% for x,y in items %}{{ x }}:{{ y }}/{% endfor %}'})
|
||||
def test_for_tag_unpack14(self):
|
||||
output = 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, ':/:/')
|
||||
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)})
|
||||
|
|
Loading…
Reference in New Issue