-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcode_pydocstyle.py
More file actions
72 lines (57 loc) · 2.59 KB
/
code_pydocstyle.py
File metadata and controls
72 lines (57 loc) · 2.59 KB
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
import pydocstyle
import os
from glob import glob
import random
from datetime import date
print(f"Pydocstyle version: {pydocstyle.__version__}")
file_list = filter(lambda z: not z.endswith("__init__.py"),
[y for x in os.walk("./dessia_common") for y in glob(os.path.join(x[0], "*.py"))])
UNWATCHED_ERRORS = [
# Do not watch these errors
"D100", "D104", "D105", "D107",
"D203", "D206", "D210", "D212",
"D301", "D302",
"D401", "D402", "D407", "D408", "D409",
"D412", "D415", "D418"
]
MAX_ERROR_BY_TYPE = {
# If the error code is not in this dict, then there is no tolerance on the error.
# http://www.pydocstyle.org/en/stable/error_codes.html
"D200": 50,
"D204": 48
}
error_detected = False
error_over_ratchet_limit = False
ratchet_limit = 9
effective_date = date(2022, 11, 28)
today = date.today()
weekly_decrease = 5
time_decrease = int((today - effective_date).days / 7. * weekly_decrease)
code_to_errors = {}
for error in pydocstyle.check(file_list, ignore=UNWATCHED_ERRORS):
code_to_errors.setdefault(error.code, [])
code_to_errors[error.code].append(error)
code_to_number = {code: len(errors) for code, errors in code_to_errors.items()}
for error_code, number_errors in code_to_number.items():
if error_code not in UNWATCHED_ERRORS:
max_errors = max(MAX_ERROR_BY_TYPE.get(error_code, 0) - time_decrease, 0)
if number_errors > max_errors:
error_detected = True
print(f"\nFix some {error_code} errors: {number_errors}/{max_errors}")
errors = code_to_errors[error_code]
errors_to_show = sorted(random.sample(errors, min(50, len(errors))),
key=lambda m: (m.filename, m.line))
for error in errors_to_show:
print(f"{error.filename} line {error.line}: {error.message}")
elif max_errors - ratchet_limit <= number_errors < max_errors:
print((f"\nYou can lower number of {error_code} to {number_errors + time_decrease}"
+ f" (actual {max_errors + time_decrease})"))
elif number_errors < max_errors - ratchet_limit:
error_over_ratchet_limit = True
print((f"\nYou MUST lower number of {error_code} to {number_errors + time_decrease}"
+ f"(actual {max_errors + time_decrease})"))
if error_detected:
raise RuntimeError("Too many errors\nRun pydocstyle dessia_common to get the errors")
if error_over_ratchet_limit:
msg = "Please lower the error limits in code_pydocstyle.py MAX_ERROR_BY_TYPE according to warnings above"
raise RuntimeError(msg)