Skip to content

Commit

Permalink
remove non-standard dict.contains() method
Browse files Browse the repository at this point in the history
the same functionality is available from the "in"
operator (__contains__ method).
  • Loading branch information
wbolster committed Jul 2, 2017
1 parent 26723a8 commit d5ceef4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 23 deletions.
23 changes: 9 additions & 14 deletions src/sanest/sanest.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,19 +617,6 @@ def get(self, path_like, default=None, *, type=None):
value = wrap(value, check=False)
return value

def contains(self, path_like, *, type=None):
# TODO: remove this method
key, path = parse_path_like(path_like)
try:
if type is None:
self[path]
else:
self[path:type]
except (LookupError, DataError):
return False
else:
return True

def __contains__(self, path_like):
"""
Check whether ``path_like`` (with optional type) points to an
Expand All @@ -640,7 +627,15 @@ def __contains__(self, path_like):
return path_like in self._data
# e.g. ['a', 'b'] and ['a', 'b', int] (slice syntax not possible)
_, path, type = parse_path_like_with_type(path_like, allow_slice=False)
return self.contains(path, type=type)
try:
if type is None:
self[path]
else:
self[path:type]
except (LookupError, DataError):
return False
else:
return True

def setdefault(self, path_like, default=None, *, type=None):
"""
Expand Down
9 changes: 0 additions & 9 deletions test_sanest.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,6 @@ def test_dict_getitem_with_path_and_type():
def test_dict_contains_with_type():
d = sanest.dict()
d['a'] = 123
assert d.contains('a', type=int)
assert not d.contains('a', type=str)
assert ['a', int] in d
assert ['a', str] not in d

Expand All @@ -430,20 +428,15 @@ def test_dict_contains_with_path():
assert ('a', 'b') in d # tuple
assert ['a', 'b'] in d # list
assert ['c', 'd'] not in d
assert d.contains(['a', 'b'])
assert not d.contains(['a', 'c'])
with pytest.raises(sanest.InvalidPathError):
['a', None] in d


def test_dict_contains_with_path_and_type():
d = sanest.dict()
d['a', 'b'] = 123
assert d.contains(['a', 'b'], type=int)
assert d.contains(('a', 'b'), type=int)
assert ['a', 'b', int] in d
assert ('a', 'b', int) in d
assert not d.contains(('a', 'b'), type=str)
assert ('a', 'b', str) not in d
assert ('a', 'b', 'c') not in d
assert ('a', 'b', 'c', int) not in d
Expand All @@ -459,8 +452,6 @@ def test_dict_slice_syntax_limited_use():
d.get(x)
with pytest.raises(sanest.InvalidPathError):
x in d
with pytest.raises(sanest.InvalidPathError):
d.contains(x)
with pytest.raises(sanest.InvalidPathError):
d.setdefault(x, 123)

Expand Down

0 comments on commit d5ceef4

Please sign in to comment.