Skip to content

Commit

Permalink
Merge pull request assertpy#90 from wuan/fix_collection_abc_deprecation
Browse files Browse the repository at this point in the history
fix collections ABC deprecation
  • Loading branch information
saturnboy authored Apr 1, 2019
2 parents 2892a69 + 49bfed1 commit 08d799c
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions assertpy/assertpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@
str_types = (str,)
xrange = range
unicode = str
Iterable = collections.abc.Iterable
else:
str_types = (basestring,)
xrange = xrange
unicode = unicode
Iterable = collections.Iterable


### soft assertions ###
Expand Down Expand Up @@ -584,7 +586,7 @@ def contains_ignoring_case(self, *items):
missing.append(i)
if missing:
self._err('Expected <%s> to case-insensitive contain items %s, but did not contain %s.' % (self.val, self._fmt_items(items), self._fmt_items(missing)))
elif isinstance(self.val, collections.Iterable):
elif isinstance(self.val, Iterable):
missing = []
for i in items:
if not isinstance(i, str_types):
Expand Down Expand Up @@ -615,7 +617,7 @@ def starts_with(self, prefix):
raise ValueError('given prefix arg must not be empty')
if not self.val.startswith(prefix):
self._err('Expected <%s> to start with <%s>, but did not.' % (self.val, prefix))
elif isinstance(self.val, collections.Iterable):
elif isinstance(self.val, Iterable):
if len(self.val) == 0:
raise ValueError('val must not be empty')
first = next(iter(self.val))
Expand All @@ -636,7 +638,7 @@ def ends_with(self, suffix):
raise ValueError('given suffix arg must not be empty')
if not self.val.endswith(suffix):
self._err('Expected <%s> to end with <%s>, but did not.' % (self.val, suffix))
elif isinstance(self.val, collections.Iterable):
elif isinstance(self.val, Iterable):
if len(self.val) == 0:
raise ValueError('val must not be empty')
last = None
Expand Down Expand Up @@ -721,19 +723,19 @@ def is_unicode(self):
### collection assertions ###
def is_iterable(self):
"""Asserts that val is iterable collection."""
if not isinstance(self.val, collections.Iterable):
if not isinstance(self.val, Iterable):
self._err('Expected iterable, but was not.')
return self

def is_not_iterable(self):
"""Asserts that val is not iterable collection."""
if isinstance(self.val, collections.Iterable):
if isinstance(self.val, Iterable):
self._err('Expected not iterable, but was.')
return self

def is_subset_of(self, *supersets):
"""Asserts that val is iterable and a subset of the given superset or flattened superset if multiple supersets are given."""
if not isinstance(self.val, collections.Iterable):
if not isinstance(self.val, Iterable):
raise TypeError('val is not iterable')
if len(supersets) == 0:
raise ValueError('one or more superset args must be given')
Expand Down Expand Up @@ -953,7 +955,7 @@ def is_child_of(self, parent):
### collection of objects assertions ###
def extracting(self, *names, **kwargs):
"""Asserts that val is collection, then extracts the named properties or named zero-arg methods into a list (or list of tuples if multiple names are given)."""
if not isinstance(self.val, collections.Iterable):
if not isinstance(self.val, Iterable):
raise TypeError('val is not iterable')
if isinstance(self.val, str_types):
raise TypeError('val must not be string')
Expand All @@ -966,7 +968,7 @@ def _extract(x, name):
return x[name]
else:
raise ValueError('item keys %s did not contain key <%s>' % (list(x.keys()), name))
elif isinstance(x, collections.Iterable):
elif isinstance(x, Iterable):
self._check_iterable(x, name='item')
return x[name]
elif hasattr(x, name):
Expand Down Expand Up @@ -1000,7 +1002,7 @@ def _sort(x):
if 'sort' in kwargs:
if isinstance(kwargs['sort'], str_types):
return _extract(x, kwargs['sort'])
elif isinstance(kwargs['sort'], collections.Iterable):
elif isinstance(kwargs['sort'], Iterable):
items = []
for k in kwargs['sort']:
if isinstance(k, str_types):
Expand All @@ -1025,7 +1027,7 @@ def __getattr__(self, attr):

attr_name = attr[4:]
err_msg = False
is_dict = isinstance(self.val, collections.Iterable) and hasattr(self.val, '__getitem__')
is_dict = isinstance(self.val, Iterable) and hasattr(self.val, '__getitem__')

if not hasattr(self.val, attr_name):
if is_dict:
Expand Down Expand Up @@ -1177,7 +1179,7 @@ def _validate_close_to_args(self, val, other, tolerance):
raise ValueError('given tolerance arg must be positive')

def _check_dict_like(self, d, check_keys=True, check_values=True, check_getitem=True, name='val', return_as_bool=False):
if not isinstance(d, collections.Iterable):
if not isinstance(d, Iterable):
if return_as_bool:
return False
else:
Expand All @@ -1204,7 +1206,7 @@ def _check_dict_like(self, d, check_keys=True, check_values=True, check_getitem=
return True

def _check_iterable(self, l, check_getitem=True, name='val'):
if not isinstance(l, collections.Iterable):
if not isinstance(l, Iterable):
raise TypeError('%s <%s> is not iterable' % (name, type(l).__name__))
if check_getitem:
if not hasattr(l, '__getitem__'):
Expand Down

0 comments on commit 08d799c

Please sign in to comment.