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

PyCQA / pylint-plugin-utils / 4972582973

pending completion
4972582973

push

github

Carl Crowder
Updating CI config with poetry packaging

27 of 87 relevant lines covered (31.03%)

0.31 hits per line

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

31.03
/pylint_plugin_utils/__init__.py
1
from typing import List
1✔
2

3
from pylint.exceptions import UnknownMessageError
1✔
4
from pylint.lint import PyLinter
1✔
5

6

7
def get_class(module_name, kls):
1✔
8
    parts = kls.split(".")
×
9
    m = __import__(module_name)
×
10
    for mp in module_name.split(".")[1:]:
×
11
        m = getattr(m, mp)
×
12
    klass = getattr(m, parts[0])
×
13
    return klass
×
14

15

16
class NoSuchChecker(Exception):
1✔
17
    def __init__(self, checker_class):
1✔
18
        self.message = "Checker class %s was not found" % checker_class
×
19

20
    def __repr__(self):
21
        return self.message
22

23

24
def get_checker(linter: PyLinter, checker_class):
1✔
25
    for checker in linter.get_checkers():
×
26
        if isinstance(checker, checker_class):
×
27
            return checker
×
28
    raise NoSuchChecker(checker_class)
×
29

30

31
def augment_visit(linter: PyLinter, checker_method, augmentation):
1✔
32
    """
33
    Augmenting a visit enables additional errors to be raised (although that case is
34
    better served using a new checker) or to suppress all warnings in certain
35
    circumstances.
36

37
    Augmenting functions should accept a 'chain' function, which runs the checker
38
    method and possibly any other augmentations, and secondly an Astroid node.
39
    "chain()" can be called at any point to trigger the continuation of other
40
    checks, or not at all to prevent any further checking.
41
    """
42

43
    try:
×
44
        checker = get_checker(linter, checker_method.__self__.__class__)
×
45
    except AttributeError:
×
46
        checker = get_checker(linter, get_class(checker_method.__module__, checker_method.__qualname__))
×
47

48
    old_method = getattr(checker, checker_method.__name__)
×
49
    setattr(checker, checker_method.__name__, AugmentFunc(old_method, augmentation))
×
50

51

52
class AugmentFunc:
1✔
53
    def __init__(self, old_method, augmentation_func):
1✔
54
        self.old_method = old_method
×
55
        self.augmentation_func = augmentation_func
×
56

57
    def __call__(self, node):
1✔
58
        self.augmentation_func(Chain(self.old_method, node), node)
×
59

60

61
class Chain:
1✔
62
    def __init__(self, old_method, node):
1✔
63
        self.old_method = old_method
×
64
        self.node = node
×
65

66
    def __call__(self):
1✔
67
        self.old_method(self.node)
×
68

69

70
class Suppress:
1✔
71
    def __init__(self, linter):
1✔
72
        self._linter = linter
×
73
        self._suppress = []
×
74
        self._messages_to_append = []
×
75

76
    def __enter__(self):
1✔
77
        self._orig_add_message = self._linter.add_message
×
78
        self._linter.add_message = self.add_message
×
79
        return self
×
80

81
    def add_message(self, *args, **kwargs):
1✔
82
        self._messages_to_append.append((args, kwargs))
×
83

84
    def suppress(self, *symbols):
1✔
85
        for symbol in symbols:
×
86
            self._suppress.append(symbol)
×
87

88
    def __exit__(self, exc_type, exc_val, exc_tb):
1✔
89
        self._linter.add_message = self._orig_add_message
×
90
        for to_append_args, to_append_kwargs in self._messages_to_append:
×
91
            if to_append_args[0] in self._suppress:
×
92
                continue
×
93
            self._linter.add_message(*to_append_args, **to_append_kwargs)
×
94

95

96
def suppress_message(linter: PyLinter, checker_method, message_id_or_symbol, test_func):
1✔
97
    """
98
    This wrapper allows the suppression of a message if the supplied test function
99
    returns True. It is useful to prevent one particular message from being raised
100
    in one particular case, while leaving the rest of the messages intact.
101
    """
102
    augment_visit(linter, checker_method, DoSuppress(linter, message_id_or_symbol, test_func))
×
103

104

105
class DoSuppress:
1✔
106
    def __init__(self, linter: PyLinter, message_id_or_symbol, test_func):
1✔
107
        self.linter = linter
×
108
        self.message_id_or_symbol = message_id_or_symbol
×
109
        self.test_func = test_func
×
110

111
    def __call__(self, chain, node):
1✔
112
        with Suppress(self.linter) as s:
×
113
            if self.test_func(node):
×
114
                s.suppress(*self.symbols)
×
115
            chain()
×
116

117
    @property
1✔
118
    def symbols(self) -> List:
1✔
119
        # At some point, pylint started preferring message symbols to message IDs.
120
        # However, this is not done consistently or uniformly
121
        #   - occasionally there are some message IDs with no matching symbols.
122
        # We try to work around this here by suppressing both the ID and the symbol.
123
        # This also gives us compatability with a broader range of pylint versions.
124

125
        # Similarly, between version 1.2 and 1.3 changed where the messages are stored
126
        # - see:
127
        #   https://bitbucket.org/logilab/pylint/commits/0b67f42799bed08aebb47babdc9fb0e761efc4ff#chg-reporters/__init__.py
128
        # Therefore here, we try the new attribute name, and fall back to the old
129
        # version for compatability with <=1.2 and >=1.3
130

131
        try:
×
132
            pylint_messages = self.get_message_definitions(self.message_id_or_symbol)
×
133
            the_symbols = [
×
134
                symbol
135
                for pylint_message in pylint_messages
136
                for symbol in (pylint_message.msgid, pylint_message.symbol)
137
                if symbol is not None
138
            ]
139
        except UnknownMessageError:
×
140
            # This can happen due to mismatches of pylint versions and plugin
141
            # expectations of available messages
142
            the_symbols = [self.message_id_or_symbol]
×
143

144
        return the_symbols
×
145

146
    def get_message_definitions(self, message_id_or_symbol):
1✔
147
        msgs_store = getattr(self.linter, "msgs_store", self.linter)
×
148

149
        if hasattr(msgs_store, "check_message_id"):
×
150
            return [msgs_store.check_message_id(message_id_or_symbol)]
×
151
        # pylint 2.0 renamed check_message_id to get_message_definition in:
152
        # https://github.com/PyCQA/pylint/commit/5ccbf9eaa54c0c302c9180bdfb745566c16e416d
153
        elif hasattr(msgs_store, "get_message_definition"):
×
154
            return [msgs_store.get_message_definition(message_id_or_symbol)]
×
155
        # pylint 2.3.0 renamed get_message_definition to get_message_definitions in:
156
        # https://github.com/PyCQA/pylint/commit/da67a9da682e51844fbc674229ff6619eb9c816a
157
        elif hasattr(msgs_store, "get_message_definitions"):
×
158
            return msgs_store.get_message_definitions(message_id_or_symbol)
×
159
        else:
160
            msg = "pylint.utils.MessagesStore does not have a " "get_message_definition(s) method"
×
161
            raise ValueError(msg)
×
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

© 2025 Coveralls, Inc