-
-
Notifications
You must be signed in to change notification settings - Fork 536
/
pylint_useless_suppression.py
76 lines (58 loc) · 2.3 KB
/
pylint_useless_suppression.py
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
"""
This script runs pylint on the rotki codebase with the useless-suppression check on.
Then reads all the output, finds the files and lines and removes the useless suppressions.
There is a few problems with this and it's all due to pylint.
1. This does not find all useless suppressions.
2. It has false positives. Which means that it may flag as useless suppressions that are needed.
In general when the codebase has too many useless suppressions like the first time
I ran this, it's usefull. Removed ~500 occurences. 20 of them were false positives,
which I manually entered again later.
We should probably open bug reports to pylint for all of these at some point.
"""
import io
import re
import subprocess
from collections import defaultdict
from pathlib import Path
from typing import NamedTuple
OCCURENCE_RE = re.compile(r'(.*\.py):(\d+):(\d+): I0021: Useless suppression of \'(.*)\'.*')
root_path = Path(__file__).resolve().parent.parent.parent
pylint_call = subprocess.Popen(
['pylint', '--rcfile', '.pylint.rc', 'rotkehlchen/', 'setup.py', 'package.py', 'tools/', '--enable', 'useless-suppression'], # noqa: E501
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=False,
)
pylint_call.wait()
class Suppression(NamedTuple):
filename: str
line: int
error: str
files = defaultdict(dict)
for line in io.TextIOWrapper(pylint_call.stdout, encoding='utf-8'):
print(line)
match = OCCURENCE_RE.search(line)
if match is None:
continue
matched_file = match.group(1)
matched_line = int(match.group(2))
files[matched_file][matched_line] = Suppression(
filename=matched_file,
line=matched_line - 1, # 0 based indexing for file reading but 1 based for pylint
error=match.group(4),
)
for filename, occurences in files.items():
with open(filename, encoding='utf8') as file:
line_data = file.readlines()
count = 0
for idx, line in enumerate(line_data):
suppression = occurences.get(idx)
if suppression is None:
continue
line_data[idx] = line.replace(f' # pylint: disable={suppression.error}', '')
count += 1
with open(filename, 'w', encoding='utf8') as file:
file.writelines(line_data)
print(f'Replaced {count} lines in {filename}')
print('DONE!')