• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

textbook / joythief / 16634898969

30 Jul 2025 10:11PM UTC coverage: 99.315%. First build
16634898969

push

github

textbook
Add Anything matcher

4 of 5 new or added lines in 1 file covered. (80.0%)

145 of 146 relevant lines covered (99.32%)

0.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.65
/src/joythief/objects.py
1
"""Matchers for general object types."""
2

3
import typing as tp
1✔
4

5
from joythief.core import Matcher
1✔
6

7
T = tp.TypeVar("T")
1✔
8

9
Type = tp.Union[type[T], tuple[type[T], ...]]
1✔
10

11

12
class Anything(Matcher[T]):
1✔
13
    """Matches anything at all.
14

15
    .. versionadded:: 0.5.0
16

17
    The JoyThief version of :py:data:`unittest.mock.ANY`.
18

19
    """
20

21
    def compare(self, _: tp.Any) -> bool:
1✔
22
        return True
1✔
23

24
    def represent(self) -> str:
1✔
NEW
25
        return super().represent()
×
26

27

28
class InstanceOf(Matcher[T]):
1✔
29
    """Matches any instance of the specified type(s).
30

31
    This matcher compares the received value using
32
    :py:func:`isinstance`, so accepts either a single type or a tuple
33
    of types. With :code:`nullable` set to :py:const:`True`, the
34
    received value can also be :py:const:`None`.
35

36
    Originally formulated for `this answer`_.
37

38
    .. _this answer: https://stackoverflow.com/a/64973325/3001761
39

40
    """
41

42
    _nullable: bool
1✔
43
    _type: Type[T]
1✔
44

45
    def __init__(self, type_: Type[T], *, nullable: bool = False):
1✔
46
        super().__init__()
1✔
47
        self._nullable = nullable
1✔
48
        self._type = type_
1✔
49

50
    def compare(self, other: tp.Any) -> bool:
1✔
51
        type_: tuple[type[tp.Any], ...] = (
1✔
52
            self._type if isinstance(self._type, tuple) else (self._type,)
53
        )
54
        if self._nullable:
1✔
55
            type_ = type_ + (type(None),)
1✔
56
        return isinstance(other, type_)
1✔
57

58
    def represent(self) -> str:
1✔
59
        return (
1✔
60
            f"InstanceOf({self._type!r}"
61
            f"{', nullable=True' if self._nullable else ''})"
62
        )
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc