Skip to content

Commit 4769db6

Browse files
committed
Fixed #20321 -- Added missing key name in MergeDict KeyError message
Thanks mark.harviston et gmail.com for the report.
1 parent 6bccbc0 commit 4769db6

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

django/utils/datastructures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def __getitem__(self, key):
2626
return dict_[key]
2727
except KeyError:
2828
pass
29-
raise KeyError
29+
raise KeyError(key)
3030

3131
def __copy__(self):
3232
return self.__class__(*self.dicts)

tests/utils_tests/test_datastructures.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,14 @@ def test_bool_casting(self):
209209
self.assertFalse(empty)
210210
self.assertTrue(not_empty)
211211

212+
def test_key_error(self):
213+
"""
214+
Test that the message of KeyError contains the missing key name.
215+
"""
216+
d1 = MergeDict({'key1': 42})
217+
with six.assertRaisesRegex(self, KeyError, 'key2'):
218+
d1['key2']
219+
212220

213221
class MultiValueDictTests(SimpleTestCase):
214222

0 commit comments

Comments
 (0)