227: check_type() and check_isinstance() now take arguments in (value, type_, name) order so the first two match the built-in isinstance() call signature

This commit is contained in:
Jason Gerard DeRose 2008-09-02 16:42:39 +00:00
parent 5af91df9a5
commit 6697b955ee
3 changed files with 65 additions and 62 deletions

View File

@ -23,17 +23,17 @@ All custom errors raised by `ipalib` package.
TYPE_FORMAT = '%s: need a %r; got %r'
def raise_TypeError(name, type_, value):
def raise_TypeError(value, type_, name):
"""
Raises a TypeError with a nicely formatted message and helpful attributes.
The TypeError raised will have three custom attributes:
``name`` - The name (identifier) of the argument in question.
``value`` - The value (of incorrect type) passed as argument.
``type`` - The type expected for the argument.
``value`` - The value (of incorrect type) passed as argument.
``name`` - The name (identifier) of the argument in question.
There is no edict that all TypeError should be raised with raise_TypeError,
but when it fits, use it... it makes the unit tests faster to write and
@ -48,40 +48,40 @@ def raise_TypeError(name, type_, value):
raise e
TypeError: message: need a <type 'str'>; got u'Hello.'
:param name: The name (identifier) of the argument in question.
:param value: The value (of incorrect type) passed as argument.
:param type_: The type expected for the argument.
:param value: The value (of incorrect type) passed argument.
:param name: The name (identifier) of the argument in question.
"""
assert type(name) is str, TYPE_FORMAT % ('name', str, name)
assert type(type_) is type, TYPE_FORMAT % ('type_', type, type_)
assert type(value) is not type_, 'value: %r is a %r' % (value, type_)
assert type(name) is str, TYPE_FORMAT % ('name', str, name)
e = TypeError(TYPE_FORMAT % (name, type_, value))
setattr(e, 'name', name)
setattr(e, 'type', type_)
setattr(e, 'value', value)
setattr(e, 'type', type_)
setattr(e, 'name', name)
raise e
def check_type(name, type_, value, allow_None=False):
def check_type(value, type_, name, allow_None=False):
assert type(name) is str, TYPE_FORMAT % ('name', str, name)
assert type(type_) is type, TYPE_FORMAT % ('type_', type, type_)
assert type(allow_None) is bool, TYPE_FORMAT % ('allow_None', bool, allow_None)
if value is None and allow_None:
return
if type(value) is not type_:
raise_TypeError(name, type_, value)
raise_TypeError(value, type_, name)
return value
def check_isinstance(name, type_, value, allow_None=False):
assert type(name) is str, TYPE_FORMAT % ('name', str, name)
def check_isinstance(value, type_, name, allow_None=False):
assert type(type_) is type, TYPE_FORMAT % ('type_', type, type_)
assert type(name) is str, TYPE_FORMAT % ('name', str, name)
assert type(allow_None) is bool, TYPE_FORMAT % ('allow_None', bool, allow_None)
if value is None and allow_None:
return
if not isinstance(value, type_):
raise_TypeError(name, type_, value)
raise_TypeError(value, type_, name)
return value

View File

@ -27,11 +27,12 @@ from ipalib import errors
type_format = '%s: need a %r; got %r'
def check_TypeError(f, name, type_, value, **kw):
e = raises(TypeError, f, name, type_, value, **kw)
assert e.name is name
assert e.type is type_
def check_TypeError(f, value, type_, name, **kw):
e = raises(TypeError, f, value, type_, name, **kw)
assert e.value is value
assert e.type is type_
assert e.name is name
assert str(e) == type_format % (name, type_, value)
@ -40,26 +41,26 @@ def test_raise_TypeError():
Tests the `errors.raise_TypeError` function.
"""
f = errors.raise_TypeError
name = 'message'
type_ = unicode
value = 'Hello.'
type_ = unicode
name = 'message'
check_TypeError(f, name, type_, value)
check_TypeError(f, value, type_, name)
# name not an str:
fail = 42
e = raises(AssertionError, f, fail, type_, value)
assert str(e) == type_format % ('name', str, fail)
# name not an str
fail_name = 42
e = raises(AssertionError, f, value, type_, fail_name)
assert str(e) == type_format % ('name', str, fail_name), str(e)
# type_ not a type:
fail = unicode()
e = raises(AssertionError, f, name, fail, value)
assert str(e) == type_format % ('type_', type, fail)
fail_type = unicode()
e = raises(AssertionError, f, value, fail_type, name)
assert str(e) == type_format % ('type_', type, fail_type)
# type(value) is type_:
fail = u'How are you?'
e = raises(AssertionError, f, name, type_, fail)
assert str(e) == 'value: %r is a %r' % (fail, type_)
fail_value = u'How are you?'
e = raises(AssertionError, f, fail_value, type_, name)
assert str(e) == 'value: %r is a %r' % (fail_value, type_)
def test_check_type():
@ -67,32 +68,33 @@ def test_check_type():
Tests the `errors.check_type` function.
"""
f = errors.check_type
name = 'greeting'
value = 'How are you?'
type_ = str
name = 'greeting'
# Should pass:
assert value is f(name, str, value)
assert None is f(name, str, None, allow_None=True)
assert value is f(value, type_, name)
assert None is f(None, type_, name, allow_None=True)
# Should raise TypeError
check_TypeError(f, name, str, None)
check_TypeError(f, name, basestring, value)
check_TypeError(f, name, unicode, value)
check_TypeError(f, None, type_, name)
check_TypeError(f, value, basestring, name)
check_TypeError(f, value, unicode, name)
# name not an str
fail = unicode(name)
e = raises(AssertionError, f, fail, str, value)
assert str(e) == type_format % ('name', str, fail)
fail_name = unicode(name)
e = raises(AssertionError, f, value, type_, fail_name)
assert str(e) == type_format % ('name', str, fail_name)
# type_ not a type:
fail = 42
e = raises(AssertionError, f, name, fail, value)
assert str(e) == type_format % ('type_', type, fail)
fail_type = 42
e = raises(AssertionError, f, value, fail_type, name)
assert str(e) == type_format % ('type_', type, fail_type)
# allow_None not a bool:
fail = 0
e = raises(AssertionError, f, name, str, value, allow_None=fail)
assert str(e) == type_format % ('allow_None', bool, fail)
fail_bool = 0
e = raises(AssertionError, f, value, type_, name, allow_None=fail_bool)
assert str(e) == type_format % ('allow_None', bool, fail_bool)
def test_check_isinstance():
@ -100,29 +102,30 @@ def test_check_isinstance():
Tests the `errors.check_isinstance` function.
"""
f = errors.check_isinstance
name = 'greeting'
value = 'How are you?'
type_ = str
name = 'greeting'
# Should pass:
assert value is f(name, str, value)
assert value is f(name, basestring, value)
assert None is f(name, str, None, allow_None=True)
assert value is f(value, type_, name)
assert value is f(value, basestring, name)
assert None is f(None, type_, name, allow_None=True)
# Should raise TypeError
check_TypeError(f, name, str, None)
check_TypeError(f, name, unicode, value)
check_TypeError(f, None, type_, name)
check_TypeError(f, value, unicode, name)
# name not an str
fail = unicode(name)
e = raises(AssertionError, f, fail, str, value)
assert str(e) == type_format % ('name', str, fail)
fail_name = unicode(name)
e = raises(AssertionError, f, value, type_, fail_name)
assert str(e) == type_format % ('name', str, fail_name)
# type_ not a type:
fail = 42
e = raises(AssertionError, f, name, fail, value)
assert str(e) == type_format % ('type_', type, fail)
fail_type = 42
e = raises(AssertionError, f, value, fail_type, name)
assert str(e) == type_format % ('type_', type, fail_type)
# allow_None not a bool:
fail = 0
e = raises(AssertionError, f, name, str, value, allow_None=fail)
assert str(e) == type_format % ('allow_None', bool, fail)
fail_bool = 0
e = raises(AssertionError, f, value, type_, name, allow_None=fail_bool)
assert str(e) == type_format % ('allow_None', bool, fail_bool)

View File

@ -117,8 +117,8 @@ class test_Option2(ClassChecker):
"""
Tests the `public.Option2.__init__` method.
"""
name = 'sn',
doc = 'Last Name',
name = 'sn'
doc = 'Last Name'
type_ = ipa_types.Unicode()
o = self.cls(name, doc, type_)
assert o.__islocked__() is True