diff --git a/humanfriendly/case.py b/humanfriendly/case.py index 4c71857..010a5a0 100644 --- a/humanfriendly/case.py +++ b/humanfriendly/case.py @@ -33,6 +33,8 @@ # Public identifiers that require documentation. __all__ = ("CaseInsensitiveDict", "CaseInsensitiveKey") +_MISSING = object() + class CaseInsensitiveDict(collections.OrderedDict): @@ -74,8 +76,10 @@ def get(self, key, default=None): """Get the value of an existing item.""" return super(CaseInsensitiveDict, self).get(self.coerce_key(key), default) - def pop(self, key, default=None): + def pop(self, key, default=_MISSING): """Remove an item from a case insensitive dictionary.""" + if default is _MISSING: + return super(CaseInsensitiveDict, self).pop(self.coerce_key(key)) return super(CaseInsensitiveDict, self).pop(self.coerce_key(key), default) def setdefault(self, key, default=None): @@ -95,7 +99,7 @@ def update(self, other=None, **kw): elif other is not None: # Complain about unsupported values. msg = "'%s' object is not iterable" - type_name = type(value).__name__ + type_name = type(other).__name__ raise TypeError(msg % type_name) # Copy the keyword arguments (if any). for key, value in kw.items(): diff --git a/humanfriendly/tests.py b/humanfriendly/tests.py index 72dad99..a7a8375 100644 --- a/humanfriendly/tests.py +++ b/humanfriendly/tests.py @@ -172,6 +172,21 @@ def test_case_insensitive_dict(self): obj['Existing_Key'] = 11 assert obj['existing_key'] == 11 + def test_case_insensitive_pop_missing(self): + """Preserve the distinction between a missing and an explicit default.""" + obj = CaseInsensitiveDict() + self.assertRaises(KeyError, obj.pop, 'Missing') + assert obj.pop('Missing', None) is None + assert obj.pop('Missing', default=None) is None + sentinel = object() + assert obj.pop('Missing', sentinel) is sentinel + + def test_case_insensitive_update_invalid(self): + """Reject unsupported update sources with TypeError.""" + obj = CaseInsensitiveDict() + self.assertRaises(TypeError, obj.update, 42) + assert not obj + def test_case_insensitive_key(self): """Test the CaseInsensitiveKey class.""" # Test the __eq__() special method.