src.hamcrest.core.core.IsInstanceOf   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 13
Duplicated Lines 0 %
Metric Value
dl 0
loc 13
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 4 2
A describe_to() 0 3 1
A _matches() 0 2 1
1
from hamcrest.core.base_matcher import BaseMatcher
2
from hamcrest.core.helpers.wrap_matcher import is_matchable_type
3
4
__author__ = "Jon Reid"
5
__copyright__ = "Copyright 2011 hamcrest.org"
6
__license__ = "BSD, see License.txt"
7
8
import types
9
10
class IsInstanceOf(BaseMatcher):
11
12
    def __init__(self, expected_type):
13
        if not is_matchable_type(expected_type):
14
            raise TypeError('IsInstanceOf requires type')
15
        self.expected_type = expected_type
16
17
    def _matches(self, item):
18
        return isinstance(item, self.expected_type)
19
20
    def describe_to(self, description):
21
        description.append_text('an instance of ')              \
22
                    .append_text(self.expected_type.__name__)
23
24
25
def instance_of(atype):
26
    """Matches if object is an instance of, or inherits from, a given type.
27
28
    :param atype: The type to compare against as the expected type.
29
30
    This matcher checks whether the evaluated object is an instance of
31
    ``atype`` or an instance of any class that inherits from ``atype``.
32
33
    Example::
34
35
        instance_of(str)
36
37
    """
38
    return IsInstanceOf(atype)
39