File: _warnings.py

package info (click to toggle)
python-testtools 2.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,244 kB
  • sloc: python: 15,086; makefile: 127; sh: 3
file content (94 lines) | stat: -rw-r--r-- 3,472 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Copyright (c) 2009-2016 testtools developers. See LICENSE for details.

__all__ = ["IsDeprecated", "WarningMessage", "Warnings"]

import warnings

from ._basic import Is
from ._const import Always
from ._datastructures import MatchesListwise, MatchesStructure
from ._higherorder import (
    AfterPreprocessing,
    Annotate,
)
from ._impl import Mismatch


def WarningMessage(category_type, message=None, filename=None, lineno=None, line=None):
    r"""Create a matcher that will match `warnings.WarningMessage`\s.

    For example, to match captured `DeprecationWarning`s with a message about
    some ``foo`` being replaced with ``bar``:

    .. code-block:: python

       WarningMessage(DeprecationWarning,
                      message=MatchesAll(
                          Contains('foo is deprecated'),
                          Contains('use bar instead')))

    :param type category_type: A warning type, for example `DeprecationWarning`.
    :param message_matcher: A matcher object that will be evaluated against
        warning's message.
    :param filename_matcher: A matcher object that will be evaluated against
        the warning's filename.
    :param lineno_matcher: A matcher object that will be evaluated against the
        warning's line number.
    :param line_matcher: A matcher object that will be evaluated against the
        warning's line of source code.
    """
    category_matcher = Is(category_type)
    message_matcher = message or Always()
    filename_matcher = filename or Always()
    lineno_matcher = lineno or Always()
    line_matcher = line or Always()
    return MatchesStructure(
        category=Annotate("Warning's category type does not match", category_matcher),
        message=Annotate(
            "Warning's message does not match", AfterPreprocessing(str, message_matcher)
        ),
        filename=Annotate("Warning's filname does not match", filename_matcher),
        lineno=Annotate("Warning's line number does not match", lineno_matcher),
        line=Annotate("Warning's source line does not match", line_matcher),
    )


class Warnings:
    """Match if the matchee produces warnings."""

    def __init__(self, warnings_matcher=None):
        """Create a Warnings matcher.

        :param warnings_matcher: Optional validator for the warnings emitted by
        matchee. If no warnings_matcher is supplied then the simple fact that
        at least one warning is emitted is considered enough to match on.
        """
        self.warnings_matcher = warnings_matcher

    def match(self, matchee):
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            # Handle staticmethod objects by extracting the underlying function
            if isinstance(matchee, staticmethod):
                matchee = matchee.__func__
            matchee()
            if self.warnings_matcher is not None:
                return self.warnings_matcher.match(w)
            elif not w:
                return Mismatch("Expected at least one warning, got none")

    def __str__(self):
        return f"Warnings({self.warnings_matcher!s})"


def IsDeprecated(message):
    """Make a matcher that checks that a callable produces exactly one
    `DeprecationWarning`.

    :param message: Matcher for the warning message.
    """
    return Warnings(
        MatchesListwise(
            [WarningMessage(category_type=DeprecationWarning, message=message)]
        )
    )