Skip to content

Commit a502ba5

Browse files
committed
add assert_not_is_instance()
1 parent 920916d commit a502ba5

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

NEWS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Version 0.6
33

44
* add assert_less(), assert_less_equal(), assert_greater(), and
55
assert_greater_equal()
6+
* add assert_not_is_instance()
67
* assert_datetime_about_now()/assert_datetime_about_now_utc(): Handle
78
comparison with None more gracefully.
89

asserts.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,28 @@ def assert_is_instance(obj, cls, msg=None):
346346
>>> assert_is_instance(5, str)
347347
Traceback (most recent call last):
348348
...
349-
AssertionError: 5 is of <class 'int'>, expected <class 'str'>
349+
AssertionError: 5 is an instance of <class 'int'>, expected <class 'str'>
350350
351351
"""
352352
if not isinstance(obj, cls):
353-
msg = (msg if msg is not None else
354-
repr(obj) + " is of " + repr(obj.__class__) +
355-
", expected " + repr(cls))
353+
msg = (msg or "{!r} is an instance of {!r}, expected {!r}".format(
354+
obj, obj.__class__, cls))
355+
fail(msg)
356+
357+
358+
def assert_not_is_instance(obj, cls, msg=None):
359+
"""Fail if an object is an instance of a class or tuple of classes.
360+
361+
>>> assert_not_is_instance(5, str)
362+
>>> assert_not_is_instance(5, (str, bytes))
363+
>>> assert_not_is_instance('foo', str)
364+
Traceback (most recent call last):
365+
...
366+
AssertionError: 'foo' is an instance of <class 'str'>
367+
368+
"""
369+
if isinstance(obj, cls):
370+
msg = msg or "{!r} is an instance of {!r}".format(obj, obj.__class__)
356371
fail(msg)
357372

358373

test_asserts.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
assert_not_in,
2626
assert_regex,
2727
assert_is_instance,
28+
assert_not_is_instance,
2829
assert_has_attr,
2930
assert_datetime_about_now,
3031
assert_datetime_about_now_utc,
@@ -341,26 +342,36 @@ def test_assert_between__too_high__custom_message(self):
341342
with _assert_raises_assertion("my message"):
342343
assert_between(0, 10, 11, msg="my message")
343344

344-
def test_assert_is_instance__is_instance(self):
345+
def test_assert_is_instance(self):
345346
assert_is_instance(4, int)
346-
347-
def test_assert_is_instance__is_instance__multiple_classes(self):
348347
assert_is_instance(4, (str, int))
349-
350-
def test_assert_is_instance__is_sub_class(self):
351348
assert_is_instance(OSError(), Exception)
349+
with _assert_raises_assertion("my message"):
350+
assert_is_instance("my string", int, msg="my message")
352351

353-
def test_assert_is_instance__not_instance__default_message(self):
354-
expected_message = ("'my string' is of <class 'str'>, "
355-
"expected <class 'int'>")
352+
def test_assert_is_instance__default_message(self):
353+
expected_message = (
354+
"'my string' is an instance of <class 'str'>, "
355+
"expected <class 'int'>")
356356
if sys.version_info[0] < 3:
357357
expected_message = expected_message.replace("class", "type")
358358
with _assert_raises_assertion(expected_message):
359359
assert_is_instance("my string", int)
360360

361-
def test_assert_is_instance__custom_message(self):
361+
def test_assert_not_is_instance(self):
362+
assert_not_is_instance(4, str)
363+
assert_not_is_instance(4, (str, bytes))
362364
with _assert_raises_assertion("my message"):
363-
assert_is_instance("my string", int, msg="my message")
365+
assert_not_is_instance(OSError(), Exception, msg="my message")
366+
367+
def test_assert_not_is_instance__default_message(self):
368+
expected_message = "OSError() is an instance of <class 'OSError'>"
369+
if sys.version_info[0] < 3:
370+
expected_message = expected_message.replace("class", "type")
371+
expected_message = expected_message.replace(
372+
"type 'OSError'", "type 'exceptions.OSError'")
373+
with _assert_raises_assertion(expected_message):
374+
assert_not_is_instance(OSError(), Exception)
364375

365376
def test_assert_has_attr__has_attribute(self):
366377
d = _DummyObject()

0 commit comments

Comments
 (0)