Skip to content

Commit

Permalink
refactor type name getter into func
Browse files Browse the repository at this point in the history
  • Loading branch information
saturnboy committed Oct 22, 2019
1 parent d18a31e commit d06a238
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions assertpy/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,31 +87,27 @@ def is_not_none(self):
self._err('Expected not <None>, but was.')
return self

def _type(self, val):
if hasattr(val, '__name__'):
return val.__name__
elif hasattr(val, '__class__'):
return val.__class__.__name__
return 'unknown'

def is_type_of(self, some_type):
"""Asserts that val is of the given type."""
if type(some_type) is not type and\
not issubclass(type(some_type), type):
if type(some_type) is not type and not issubclass(type(some_type), type):
raise TypeError('given arg must be a type')
if type(self.val) is not some_type:
if hasattr(self.val, '__name__'):
t = self.val.__name__
elif hasattr(self.val, '__class__'):
t = self.val.__class__.__name__
else:
t = 'unknown'
t = self._type(self.val)
self._err('Expected <%s:%s> to be of type <%s>, but was not.' % (self.val, t, some_type.__name__))
return self

def is_instance_of(self, some_class):
"""Asserts that val is an instance of the given class."""
try:
if not isinstance(self.val, some_class):
if hasattr(self.val, '__name__'):
t = self.val.__name__
elif hasattr(self.val, '__class__'):
t = self.val.__class__.__name__
else:
t = 'unknown'
t = self._type(self.val)
self._err('Expected <%s:%s> to be instance of class <%s>, but was not.' % (self.val, t, some_class.__name__))
except TypeError:
raise TypeError('given arg must be a class')
Expand Down

0 comments on commit d06a238

Please sign in to comment.