Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions humanfriendly/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
# Public identifiers that require documentation.
__all__ = ("CaseInsensitiveDict", "CaseInsensitiveKey")

_MISSING = object()


class CaseInsensitiveDict(collections.OrderedDict):

Expand Down Expand Up @@ -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):
Expand All @@ -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():
Expand Down
15 changes: 15 additions & 0 deletions humanfriendly/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down