5
5
6
6
7
7
def get_class (module_name , kls ):
8
- parts = kls .split ('.' )
8
+ parts = kls .split ("." )
9
9
m = __import__ (module_name )
10
- for mp in module_name .split ('.' )[1 :]:
10
+ for mp in module_name .split ("." )[1 :]:
11
11
m = getattr (m , mp )
12
12
klass = getattr (m , parts [0 ])
13
13
return klass
14
14
15
15
16
16
class NoSuchChecker (Exception ):
17
-
18
17
def __init__ (self , checker_class ):
19
18
self .message = "Checker class %s was not found" % checker_class
20
19
@@ -32,18 +31,21 @@ def get_checker(linter: PyLinter, checker_class):
32
31
def augment_visit (linter : PyLinter , checker_method , augmentation ):
33
32
"""
34
33
Augmenting a visit enables additional errors to be raised (although that case is
35
- better served using a new checker) or to suppress all warnings in certain circumstances.
34
+ better served using a new checker) or to suppress all warnings in certain
35
+ circumstances.
36
36
37
- Augmenting functions should accept a 'chain' function, which runs the checker method
38
- and possibly any other augmentations, and secondly an Astroid node. "chain()" can be
39
- called at any point to trigger the continuation of other checks, or not at all to
40
- prevent any further checking.
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
41
"""
42
42
43
43
try :
44
44
checker = get_checker (linter , checker_method .__self__ .__class__ )
45
45
except AttributeError :
46
- checker = get_checker (linter , get_class (checker_method .__module__ , checker_method .__qualname__ ))
46
+ checker = get_checker (
47
+ linter , get_class (checker_method .__module__ , checker_method .__qualname__ )
48
+ )
47
49
48
50
old_method = getattr (checker , checker_method .__name__ )
49
51
setattr (checker , checker_method .__name__ , AugmentFunc (old_method , augmentation ))
@@ -68,7 +70,6 @@ def __call__(self):
68
70
69
71
70
72
class Suppress :
71
-
72
73
def __init__ (self , linter ):
73
74
self ._linter = linter
74
75
self ._suppress = []
@@ -89,13 +90,6 @@ def suppress(self, *symbols):
89
90
def __exit__ (self , exc_type , exc_val , exc_tb ):
90
91
self ._linter .add_message = self ._orig_add_message
91
92
for to_append_args , to_append_kwargs in self ._messages_to_append :
92
- # Depending on the Pylint version, the add_message API is different.
93
- # Either a single object called 'message' is passed, or the first argument
94
- # is a message symbol.
95
- if hasattr ('symbol' , to_append_args [0 ]):
96
- code = to_append_args [0 ].symbol
97
- else :
98
- code = to_append_args [0 ]
99
93
if to_append_args [0 ] in self ._suppress :
100
94
continue
101
95
self ._linter .add_message (* to_append_args , ** to_append_kwargs )
@@ -107,7 +101,9 @@ def suppress_message(linter: PyLinter, checker_method, message_id_or_symbol, tes
107
101
returns True. It is useful to prevent one particular message from being raised
108
102
in one particular case, while leaving the rest of the messages intact.
109
103
"""
110
- augment_visit (linter , checker_method , DoSuppress (linter , message_id_or_symbol , test_func ))
104
+ augment_visit (
105
+ linter , checker_method , DoSuppress (linter , message_id_or_symbol , test_func )
106
+ )
111
107
112
108
113
109
class DoSuppress :
@@ -124,40 +120,49 @@ def __call__(self, chain, node):
124
120
125
121
@property
126
122
def symbols (self ) -> List :
127
- # At some point, pylint started preferring message symbols to message IDs. However this is not done
128
- # consistently or uniformly - occasionally there are some message IDs with no matching symbols.
129
- # We try to work around this here by suppressing both the ID and the symbol, if we can find it.
123
+ # At some point, pylint started preferring message symbols to message IDs.
124
+ # However, this is not done consistently or uniformly
125
+ # - occasionally there are some message IDs with no matching symbols.
126
+ # We try to work around this here by suppressing both the ID and the symbol.
130
127
# This also gives us compatability with a broader range of pylint versions.
131
128
132
- # Similarly, a commit between version 1.2 and 1.3 changed where the messages are stored - see:
133
- # https://bitbucket.org/logilab/pylint/commits/0b67f42799bed08aebb47babdc9fb0e761efc4ff#chg-reporters/__init__.py
134
- # Therefore here, we try the new attribute name, and fall back to the old version for
135
- # compatability with <=1.2 and >=1.3
129
+ # Similarly, between version 1.2 and 1.3 changed where the messages are stored
130
+ # - see:
131
+ # https://bitbucket.org/logilab/pylint/commits/0b67f42799bed08aebb47babdc9fb0e761efc4ff#chg-reporters/__init__.py
132
+ # Therefore here, we try the new attribute name, and fall back to the old
133
+ # version for compatability with <=1.2 and >=1.3
136
134
137
135
try :
138
136
pylint_messages = self .get_message_definitions (self .message_id_or_symbol )
139
- the_symbols = [symbol
140
- for pylint_message in pylint_messages
141
- for symbol in (pylint_message .msgid , pylint_message .symbol )
142
- if symbol is not None ]
137
+ the_symbols = [
138
+ symbol
139
+ for pylint_message in pylint_messages
140
+ for symbol in (pylint_message .msgid , pylint_message .symbol )
141
+ if symbol is not None
142
+ ]
143
143
except UnknownMessageError :
144
- # This can happen due to mismatches of pylint versions and plugin expectations of available messages
144
+ # This can happen due to mismatches of pylint versions and plugin
145
+ # expectations of available messages
145
146
the_symbols = [self .message_id_or_symbol ]
146
147
147
148
return the_symbols
148
149
149
150
def get_message_definitions (self , message_id_or_symbol ):
150
- msgs_store = getattr (self .linter , ' msgs_store' , self .linter )
151
+ msgs_store = getattr (self .linter , " msgs_store" , self .linter )
151
152
152
- if hasattr (msgs_store , ' check_message_id' ):
153
+ if hasattr (msgs_store , " check_message_id" ):
153
154
return [msgs_store .check_message_id (message_id_or_symbol )]
154
155
# pylint 2.0 renamed check_message_id to get_message_definition in:
155
156
# https://github.com/PyCQA/pylint/commit/5ccbf9eaa54c0c302c9180bdfb745566c16e416d
156
- elif hasattr (msgs_store , ' get_message_definition' ):
157
+ elif hasattr (msgs_store , " get_message_definition" ):
157
158
return [msgs_store .get_message_definition (message_id_or_symbol )]
158
159
# pylint 2.3.0 renamed get_message_definition to get_message_definitions in:
159
160
# https://github.com/PyCQA/pylint/commit/da67a9da682e51844fbc674229ff6619eb9c816a
160
- elif hasattr (msgs_store , ' get_message_definitions' ):
161
+ elif hasattr (msgs_store , " get_message_definitions" ):
161
162
return msgs_store .get_message_definitions (message_id_or_symbol )
162
163
else :
163
- raise ValueError ('pylint.utils.MessagesStore does not have a get_message_definition(s) method' )
164
+ msg = (
165
+ "pylint.utils.MessagesStore does not have a "
166
+ "get_message_definition(s) method"
167
+ )
168
+ raise ValueError (msg )
0 commit comments