src.hamcrest.core.core.IsInstanceOf._matches()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 2
rs 10
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