bpo-35224: Reverse evaluation order of key: value in dict comprehensions (GH-14139)
… as proposed in PEP 572; key is now evaluated before value.
https://bugs.python.org/issue35224
diff --git a/Lib/test/test_dictcomps.py b/Lib/test/test_dictcomps.py
index afe68a8..927e310 100644
--- a/Lib/test/test_dictcomps.py
+++ b/Lib/test/test_dictcomps.py
@@ -81,6 +81,35 @@
compile("{x: y for y, x in ((1, 2), (3, 4))} += 5", "<test>",
"exec")
+ def test_evaluation_order(self):
+ expected = {
+ 'H': 'W',
+ 'e': 'o',
+ 'l': 'l',
+ 'o': 'd',
+ }
+
+ expected_calls = [
+ ('key', 'H'), ('value', 'W'),
+ ('key', 'e'), ('value', 'o'),
+ ('key', 'l'), ('value', 'r'),
+ ('key', 'l'), ('value', 'l'),
+ ('key', 'o'), ('value', 'd'),
+ ]
+
+ actual_calls = []
+
+ def add_call(pos, value):
+ actual_calls.append((pos, value))
+ return value
+
+ actual = {
+ add_call('key', k): add_call('value', v)
+ for k, v in zip('Hello', 'World')
+ }
+
+ self.assertEqual(actual, expected)
+ self.assertEqual(actual_calls, expected_calls)
if __name__ == "__main__":
unittest.main()