Skip to content

Commit

Permalink
implement type checking list.contains()
Browse files Browse the repository at this point in the history
closes #3.
  • Loading branch information
wbolster committed Jul 2, 2017
1 parent e844ea3 commit 4dce5b4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,7 @@ API

.. py:method:: path_like in l
.. automethod:: __contains__
.. automethod:: contains

.. py:method:: len(l)
.. automethod:: __len__
Expand Down
13 changes: 13 additions & 0 deletions src/sanest/sanest.py
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,19 @@ def __contains__(self, value):
"""
return clean_value(value) in self._data

def contains(self, value, *, type=None):
"""
Check whether ``value`` is contained in this list.
This is the same as ``value in l`` but allows for a type check.
:param type: expected type
"""
try:
return clean_value(value, type=type) in self._data
except InvalidValueError:
return False

def index(self, value, start=0, stop=None, *, type=None):
"""
Get the index of ``value``; like ``list.index()``.
Expand Down
11 changes: 11 additions & 0 deletions test_sanest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,17 @@ def test_list_contains():
assert str(excinfo.value) == "invalid value of type MyClass: <MyClass>"


def test_list_contains_with_type():
l = sanest.list([1, 'a', {'c': 'd'}])
assert l.contains(1, type=int)
assert l.contains('a', type=str)
assert l.contains({'c': 'd'}, type=dict)
assert not l.contains(1, type=str)
assert not l.contains(2, type=str)
assert not l.contains({'x': 'y'}, type=dict)
assert not l.contains({'x': 'y'}, type=int)


def test_list_iteration():
l = sanest.list([
{'a': 1},
Expand Down

0 comments on commit 4dce5b4

Please sign in to comment.