Skip to content

Commit 33519cb

Browse files
committed
Added black and isort and fixed pytest dev branch
1 parent e27a021 commit 33519cb

File tree

9 files changed

+260
-320
lines changed

9 files changed

+260
-320
lines changed

pylintrc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1-
[TYPECHECK]
1+
[MESSAGES CONTROL]
2+
disable = C0330, C0326
3+
4+
[FORMAT]
5+
max-line-length = 88
26

7+
[TYPECHECK]
38
ignored-classes = pytest

pytest_pylint/plugin.py

Lines changed: 61 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -6,68 +6,63 @@
66

77

88
from collections import defaultdict
9-
from configparser import ConfigParser, NoSectionError, NoOptionError
9+
from configparser import ConfigParser, NoOptionError, NoSectionError
1010
from os import makedirs
11-
from os.path import getmtime, exists, join, dirname
11+
from os.path import dirname, exists, getmtime, join
1212

13-
from pylint import lint
14-
from pylint.config import PYLINTRC
1513
import pytest
1614
import toml
15+
from pylint import lint
16+
from pylint.config import PYLINTRC
1717

1818
from .pylint_util import ProgrammaticReporter
19-
from .util import get_rel_path, PyLintException, should_include_file
19+
from .util import PyLintException, get_rel_path, should_include_file
2020

21-
HISTKEY = 'pylint/mtimes'
22-
PYLINT_CONFIG_CACHE_KEY = 'pylintrc'
21+
HISTKEY = "pylint/mtimes"
22+
PYLINT_CONFIG_CACHE_KEY = "pylintrc"
2323
FILL_CHARS = 80
24-
MARKER = 'pylint'
24+
MARKER = "pylint"
2525

2626

2727
def pytest_addoption(parser):
2828
"""Add all our command line options"""
2929
group = parser.getgroup("pylint")
3030
group.addoption(
31-
"--pylint",
32-
action="store_true", default=False,
33-
help="run pylint on all"
31+
"--pylint", action="store_true", default=False, help="run pylint on all"
3432
)
3533
group.addoption(
3634
"--no-pylint",
37-
action="store_true", default=False,
38-
help="disable running pylint "
35+
action="store_true",
36+
default=False,
37+
help="disable running pylint ",
3938
)
4039

4140
group.addoption(
42-
'--pylint-rcfile',
43-
default=None,
44-
help='Location of RC file if not pylintrc'
41+
"--pylint-rcfile", default=None, help="Location of RC file if not pylintrc"
4542
)
4643
group.addoption(
47-
'--pylint-error-types',
48-
default='CRWEF',
49-
help='The types of pylint errors to consider failures by letter'
50-
', default is all of them (CRWEF).'
44+
"--pylint-error-types",
45+
default="CRWEF",
46+
help="The types of pylint errors to consider failures by letter"
47+
", default is all of them (CRWEF).",
5148
)
5249
group.addoption(
53-
'--pylint-jobs',
50+
"--pylint-jobs",
5451
default=None,
55-
help='Specify number of processes to use for pylint'
52+
help="Specify number of processes to use for pylint",
5653
)
5754
group.addoption(
58-
'--pylint-output-file',
55+
"--pylint-output-file",
5956
default=None,
60-
help='Path to a file where Pylint report will be printed to.'
57+
help="Path to a file where Pylint report will be printed to.",
6158
)
6259
group.addoption(
63-
'--pylint-ignore',
64-
default=None,
65-
help='Files/directories that will be ignored'
60+
"--pylint-ignore", default=None, help="Files/directories that will be ignored"
6661
)
6762
group.addoption(
68-
'--pylint-ignore-patterns',
63+
"--pylint-ignore-patterns",
6964
default=None,
70-
help='Files/directories patterns that will be ignored'
65+
help="Files/directories patterns that will be ignored",
7166
)
7267

7368

@@ -77,10 +72,7 @@ def pytest_configure(config):
7772
7873
:param _pytest.config.Config config: pytest config object
7974
"""
80-
config.addinivalue_line(
81-
'markers',
82-
"{0}: Tests which run pylint.".format(MARKER)
83-
)
75+
config.addinivalue_line("markers", "{0}: Tests which run pylint.".format(MARKER))
8476
if config.option.pylint and not config.option.no_pylint:
8577
pylint_plugin = PylintPlugin(config)
8678
config.pluginmanager.register(pylint_plugin)
@@ -90,9 +82,10 @@ class PylintPlugin:
9082
"""
9183
The core plugin for pylint
9284
"""
85+
9386
# pylint: disable=too-many-instance-attributes
9487
def __init__(self, config):
95-
if hasattr(config, 'cache'):
88+
if hasattr(config, "cache"):
9689
self.mtimes = config.cache.get(HISTKEY, {})
9790
else:
9891
self.mtimes = {}
@@ -136,36 +129,32 @@ def pytest_configure(self, config):
136129

137130
# Command line arguments take presedence over rcfile ones if set
138131
if config.option.pylint_ignore is not None:
139-
self.pylint_ignore = config.option.pylint_ignore.split(',')
132+
self.pylint_ignore = config.option.pylint_ignore.split(",")
140133
if config.option.pylint_ignore_patterns is not None:
141-
self.pylint_ignore_patterns = (
142-
config.option.pylint_ignore_patterns.split(',')
134+
self.pylint_ignore_patterns = config.option.pylint_ignore_patterns.split(
135+
","
143136
)
144137

145138
def _load_rc_file(self, pylintrc_file):
146139
self.pylint_config = ConfigParser()
147140
self.pylint_config.read(pylintrc_file)
148141

149142
try:
150-
ignore_string = self.pylint_config.get('MASTER', 'ignore')
143+
ignore_string = self.pylint_config.get("MASTER", "ignore")
151144
if ignore_string:
152-
self.pylint_ignore = ignore_string.split(',')
145+
self.pylint_ignore = ignore_string.split(",")
153146
except (NoSectionError, NoOptionError):
154147
pass
155148

156149
try:
157-
ignore_patterns = self.pylint_config.get(
158-
'MASTER', 'ignore-patterns'
159-
)
150+
ignore_patterns = self.pylint_config.get("MASTER", "ignore-patterns")
160151
if ignore_patterns:
161-
self.pylint_ignore_patterns = ignore_patterns.split(',')
152+
self.pylint_ignore_patterns = ignore_patterns.split(",")
162153
except (NoSectionError, NoOptionError):
163154
pass
164155

165156
try:
166-
self.pylint_msg_template = self.pylint_config.get(
167-
'REPORTS', 'msg-template'
168-
)
157+
self.pylint_msg_template = self.pylint_config.get("REPORTS", "msg-template")
169158
except (NoSectionError, NoOptionError):
170159
pass
171160

@@ -192,13 +181,9 @@ def _load_pyproject_toml(self, pylintrc_file):
192181
ignore = master_section.get("ignore")
193182
if ignore:
194183
self.pylint_ignore = (
195-
ignore.split(",")
196-
if isinstance(ignore, str)
197-
else ignore
184+
ignore.split(",") if isinstance(ignore, str) else ignore
198185
)
199-
self.pylint_ignore_patterns = (
200-
master_section.get("ignore-patterns") or []
201-
)
186+
self.pylint_ignore_patterns = master_section.get("ignore-patterns") or []
202187
self.pylint_msg_template = reports_section.get("msg-template")
203188

204189
def pytest_sessionfinish(self, session):
@@ -207,7 +192,7 @@ def pytest_sessionfinish(self, session):
207192
208193
:param _pytest.main.Session session: the pytest session object
209194
"""
210-
if hasattr(session.config, 'cache'):
195+
if hasattr(session.config, "cache"):
211196
session.config.cache.set(HISTKEY, self.mtimes)
212197

213198
def pytest_collect_file(self, path, parent):
@@ -217,11 +202,9 @@ def pytest_collect_file(self, path, parent):
217202

218203
rel_path = get_rel_path(path.strpath, parent.session.fspath.strpath)
219204
if should_include_file(
220-
rel_path, self.pylint_ignore, self.pylint_ignore_patterns
205+
rel_path, self.pylint_ignore, self.pylint_ignore_patterns
221206
):
222-
item = PylintFile.from_parent(
223-
parent, fspath=path, plugin=self
224-
)
207+
item = PylintFile.from_parent(parent, fspath=path, plugin=self)
225208
else:
226209
return None
227210

@@ -240,26 +223,20 @@ def pytest_collection_finish(self, session):
240223
# Build argument list for pylint
241224
args_list = list(self.pylint_files)
242225
if self.pylintrc_file:
243-
args_list.append('--rcfile={0}'.format(
244-
self.pylintrc_file
245-
))
226+
args_list.append("--rcfile={0}".format(self.pylintrc_file))
246227
if jobs is not None:
247-
args_list.append('-j')
228+
args_list.append("-j")
248229
args_list.append(jobs)
249230
# These allow the user to override the pylint configuration's
250231
# ignore list
251232
if self.pylint_ignore:
252-
args_list.append(
253-
'--ignore={0}'.format(','.join(self.pylint_ignore))
254-
)
233+
args_list.append("--ignore={0}".format(",".join(self.pylint_ignore)))
255234
if self.pylint_ignore_patterns:
256235
args_list.append(
257-
'--ignore-patterns={0}'.format(
258-
','.join(self.pylint_ignore_patterns)
259-
)
236+
"--ignore-patterns={0}".format(",".join(self.pylint_ignore_patterns))
260237
)
261-
print('-' * FILL_CHARS)
262-
print('Linting files')
238+
print("-" * FILL_CHARS)
239+
print("Linting files")
263240

264241
# Run pylint over the collected files.
265242

@@ -275,11 +252,12 @@ def pytest_collection_finish(self, session):
275252
# Stores the messages in a dictionary for lookup in tests.
276253
for message in messages:
277254
self.pylint_messages[message.path].append(message)
278-
print('-' * FILL_CHARS)
255+
print("-" * FILL_CHARS)
279256

280257

281258
class PylintFile(pytest.File):
282259
"""File that pylint will run on."""
260+
283261
rel_path = None # : str
284262
plugin = None # : PylintPlugin
285263
should_skip = False # : bool
@@ -290,25 +268,19 @@ def from_parent(cls, parent, *, fspath, plugin):
290268
# We add the ``plugin`` kwarg to get plugin level information so the
291269
# signature differs
292270
# pylint: disable=arguments-differ
293-
_self = getattr(super(), 'from_parent', cls)(parent, fspath=fspath)
271+
_self = getattr(super(), "from_parent", cls)(parent, fspath=fspath)
294272
_self.plugin = plugin
295273

296-
_self.rel_path = get_rel_path(
297-
fspath.strpath,
298-
parent.session.fspath.strpath
299-
)
274+
_self.rel_path = get_rel_path(fspath.strpath, parent.session.fspath.strpath)
300275
_self.mtime = fspath.mtime()
301276
prev_mtime = _self.plugin.mtimes.get(_self.rel_path, 0)
302-
_self.should_skip = (prev_mtime == _self.mtime)
277+
_self.should_skip = prev_mtime == _self.mtime
303278

304279
return _self
305280

306281
def collect(self):
307282
"""Create a PyLintItem for the File."""
308-
yield PyLintItem.from_parent(
309-
parent=self,
310-
name='PYLINT'
311-
)
283+
yield PyLintItem.from_parent(parent=self, name="PYLINT")
312284

313285

314286
class PyLintItem(pytest.Item):
@@ -324,13 +296,13 @@ def __init__(self, *args, **kwargs):
324296

325297
msg_format = self.plugin.pylint_msg_template
326298
if msg_format is None:
327-
self._msg_format = '{C}:{line:3d},{column:2d}: {msg} ({symbol})'
299+
self._msg_format = "{C}:{line:3d},{column:2d}: {msg} ({symbol})"
328300
else:
329301
self._msg_format = msg_format
330302

331303
@classmethod
332304
def from_parent(cls, parent, **kw):
333-
return getattr(super(), 'from_parent', cls)(parent, **kw)
305+
return getattr(super(), "from_parent", cls)(parent, **kw)
334306

335307
def setup(self):
336308
"""Mark unchanged files as SKIPPED."""
@@ -343,18 +315,14 @@ def runtest(self):
343315

344316
def _loop_errors(writer):
345317
reported_errors = []
346-
for error in self.plugin.pylint_messages.get(
347-
self.parent.rel_path, []
348-
):
318+
for error in self.plugin.pylint_messages.get(self.parent.rel_path, []):
349319
if error.C in self.config.option.pylint_error_types:
350-
reported_errors.append(
351-
error.format(self._msg_format)
352-
)
320+
reported_errors.append(error.format(self._msg_format))
353321

354322
writer(
355-
'{error_path}:{error_line}: [{error_msg_id}'
356-
'({error_symbol}), {error_obj}] '
357-
'{error_msg}\n'.format(
323+
"{error_path}:{error_line}: [{error_msg_id}"
324+
"({error_symbol}), {error_obj}] "
325+
"{error_msg}\n".format(
358326
error_path=error.path,
359327
error_line=error.line,
360328
error_msg_id=error.msg_id,
@@ -370,13 +338,13 @@ def _loop_errors(writer):
370338
output_dir = dirname(pylint_output_file)
371339
if output_dir:
372340
makedirs(output_dir, exist_ok=True)
373-
with open(pylint_output_file, 'a') as _file:
341+
with open(pylint_output_file, "a") as _file:
374342
reported_errors = _loop_errors(writer=_file.write)
375343
else:
376344
reported_errors = _loop_errors(writer=lambda *args, **kwargs: None)
377345

378346
if reported_errors:
379-
raise PyLintException('\n'.join(reported_errors))
347+
raise PyLintException("\n".join(reported_errors))
380348

381349
# Update the cache if the item passed pylint.
382350
self.plugin.mtimes[self.parent.rel_path] = self.parent.mtime

pytest_pylint/pylint_util.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
class ProgrammaticReporter(BaseReporter):
1010
"""Reporter that replaces output with storage in list of dictionaries"""
11+
1112
__implements__ = IReporter
12-
extension = 'prog'
13+
extension = "prog"
1314

1415
def __init__(self, output=None):
1516
BaseReporter.__init__(self, output)
@@ -29,10 +30,10 @@ def _display(self, layout):
2930

3031
def on_set_current_module(self, module, filepath):
3132
"""Hook called when a module starts to be analysed."""
32-
print('.', end='')
33+
print(".", end="")
3334
sys.stdout.flush()
3435

3536
def on_close(self, stats, previous_stats):
3637
"""Hook called when all modules finished analyzing."""
3738
# print a new line when pylint is finished
38-
print('')
39+
print("")

0 commit comments

Comments
 (0)