This repository was archived by the owner on Nov 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathviolations.py
491 lines (449 loc) · 12.3 KB
/
violations.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
"""Docstring violation definition."""
from collections import namedtuple
from functools import partial
from itertools import dropwhile
from typing import Any, Callable, Iterable, List, Optional
from .parser import Definition
from .utils import is_blank
__all__ = ('Error', 'ErrorRegistry', 'conventions')
ErrorParams = namedtuple('ErrorParams', ['code', 'short_desc', 'context'])
class Error:
"""Error in docstring style."""
# Options that define how errors are printed:
explain = False
source = False
def __init__(
self,
code: str,
short_desc: str,
context: str,
*parameters: Iterable[str],
) -> None:
"""Initialize the object.
`parameters` are specific to the created error.
"""
self.code = code
self.short_desc = short_desc
self.context = context
self.parameters = parameters
self.definition = None # type: Optional[Definition]
self.explanation = None # type: Optional[str]
def set_context(self, definition: Definition, explanation: str) -> None:
"""Set the source code context for this error."""
self.definition = definition
self.explanation = explanation
filename = property(lambda self: self.definition.module.name)
line = property(lambda self: self.definition.error_lineno)
@property
def message(self) -> str:
"""Return the message to print to the user."""
ret = f'{self.code}: {self.short_desc}'
if self.context is not None:
specific_error_msg = self.context.format(*self.parameters)
ret += f' ({specific_error_msg})'
return ret
@property
def lines(self) -> str:
"""Return the source code lines for this error."""
if self.definition is None:
return ''
source = ''
lines = self.definition.source.splitlines(keepends=True)
offset = self.definition.start # type: ignore
lines_stripped = list(
reversed(list(dropwhile(is_blank, reversed(lines))))
)
numbers_width = len(str(offset + len(lines_stripped)))
line_format = f'{{:{numbers_width}}}:{{}}'
for n, line in enumerate(lines_stripped):
if line:
line = ' ' + line
source += line_format.format(n + offset, line)
if n > 5:
source += ' ...\n'
break
return source
def __str__(self) -> str:
if self.explanation:
self.explanation = '\n'.join(
l for l in self.explanation.split('\n') if not is_blank(l)
)
template = '{filename}:{line} {definition}:\n {message}'
if self.source and self.explain:
template += '\n\n{explanation}\n\n{lines}\n'
elif self.source and not self.explain:
template += '\n\n{lines}\n'
elif self.explain and not self.source:
template += '\n\n{explanation}\n\n'
return template.format(
**{
name: getattr(self, name)
for name in [
'filename',
'line',
'definition',
'message',
'explanation',
'lines',
]
}
)
def __repr__(self) -> str:
return str(self)
def __lt__(self, other: 'Error') -> bool:
return (self.filename, self.line) < (other.filename, other.line)
class ErrorRegistry:
"""A registry of all error codes, divided to groups."""
groups = [] # type: ignore
class ErrorGroup:
"""A group of similarly themed errors."""
def __init__(self, prefix: str, name: str) -> None:
"""Initialize the object.
`Prefix` should be the common prefix for errors in this group,
e.g., "D1".
`name` is the name of the group (its subject).
"""
self.prefix = prefix
self.name = name
self.errors = [] # type: List[ErrorParams]
def create_error(
self,
error_code: str,
error_desc: str,
error_context: Optional[str] = None,
) -> Callable[[Iterable[str]], Error]:
"""Create an error, register it to this group and return it."""
# TODO: check prefix
error_params = ErrorParams(error_code, error_desc, error_context)
factory = partial(Error, *error_params)
self.errors.append(error_params)
return factory
@classmethod
def create_group(cls, prefix: str, name: str) -> ErrorGroup:
"""Create a new error group and return it."""
group = cls.ErrorGroup(prefix, name)
cls.groups.append(group)
return group
@classmethod
def get_error_codes(cls) -> Iterable[str]:
"""Yield all registered codes."""
for group in cls.groups:
for error in group.errors:
yield error.code
@classmethod
def to_rst(cls) -> str:
"""Output the registry as reStructuredText, for documentation."""
max_len = max(
len(error.short_desc)
for group in cls.groups
for error in group.errors
)
sep_line = '+' + 6 * '-' + '+' + '-' * (max_len + 2) + '+\n'
blank_line = '|' + (max_len + 9) * ' ' + '|\n'
table = ''
for group in cls.groups:
table += sep_line
table += blank_line
table += '|' + f'**{group.name}**'.center(max_len + 9) + '|\n'
table += blank_line
for error in group.errors:
table += sep_line
table += (
'|'
+ error.code.center(6)
+ '| '
+ error.short_desc.ljust(max_len + 1)
+ '|\n'
)
table += sep_line
return table
D1xx = ErrorRegistry.create_group('D1', 'Missing Docstrings')
D100 = D1xx.create_error(
'D100',
'Missing docstring in public module',
)
D101 = D1xx.create_error(
'D101',
'Missing docstring in public class',
)
D102 = D1xx.create_error(
'D102',
'Missing docstring in public method',
)
D103 = D1xx.create_error(
'D103',
'Missing docstring in public function',
)
D104 = D1xx.create_error(
'D104',
'Missing docstring in public package',
)
D105 = D1xx.create_error(
'D105',
'Missing docstring in magic method',
)
D106 = D1xx.create_error(
'D106',
'Missing docstring in public nested class',
)
D107 = D1xx.create_error(
'D107',
'Missing docstring in __init__',
)
D121 = D1xx.create_error(
'D121',
'Missing docstring in inaccessible public class',
)
D123 = D1xx.create_error(
'D123',
'Missing docstring in inaccessible public function',
)
D2xx = ErrorRegistry.create_group('D2', 'Whitespace Issues')
D200 = D2xx.create_error(
'D200',
'One-line docstring should fit on one line ' 'with quotes',
'found {0}',
)
D201 = D2xx.create_error(
'D201',
'No blank lines allowed before function docstring',
'found {0}',
)
D202 = D2xx.create_error(
'D202',
'No blank lines allowed after function docstring',
'found {0}',
)
D203 = D2xx.create_error(
'D203',
'1 blank line required before class docstring',
'found {0}',
)
D204 = D2xx.create_error(
'D204',
'1 blank line required after class docstring',
'found {0}',
)
D205 = D2xx.create_error(
'D205',
'1 blank line required between summary line and description',
'found {0}',
)
D206 = D2xx.create_error(
'D206',
'Docstring should be indented with spaces, not tabs',
)
D207 = D2xx.create_error(
'D207',
'Docstring is under-indented',
)
D208 = D2xx.create_error(
'D208',
'Docstring is over-indented',
)
D209 = D2xx.create_error(
'D209',
'Multi-line docstring closing quotes should be on a separate line',
)
D210 = D2xx.create_error(
'D210',
'No whitespaces allowed surrounding docstring text',
)
D211 = D2xx.create_error(
'D211',
'No blank lines allowed before class docstring',
'found {0}',
)
D212 = D2xx.create_error(
'D212',
'Multi-line docstring summary should start at the first line',
)
D213 = D2xx.create_error(
'D213',
'Multi-line docstring summary should start at the second line',
)
D214 = D2xx.create_error(
'D214',
'Section is over-indented',
'{0!r}',
)
D215 = D2xx.create_error(
'D215',
'Section underline is over-indented',
'in section {0!r}',
)
D3xx = ErrorRegistry.create_group('D3', 'Quotes Issues')
D300 = D3xx.create_error(
'D300',
'Use """triple double quotes"""',
'found {0}-quotes',
)
D301 = D3xx.create_error(
'D301',
'Use r""" if any backslashes in a docstring',
)
D302 = D3xx.create_error(
'D302',
'Deprecated: Use u""" for Unicode docstrings',
)
D4xx = ErrorRegistry.create_group('D4', 'Docstring Content Issues')
D400 = D4xx.create_error(
'D400',
'First line should end with a period',
'not {0!r}',
)
D401 = D4xx.create_error(
'D401',
'First line should be in imperative mood',
"perhaps '{0}', not '{1}'",
)
D401b = D4xx.create_error(
'D401',
'First line should be in imperative mood; try rephrasing',
"found '{0}'",
)
D402 = D4xx.create_error(
'D402',
'First line should not be the function\'s "signature"',
)
D403 = D4xx.create_error(
'D403',
'First word of the first line should be properly capitalized',
'{0!r}, not {1!r}',
)
D404 = D4xx.create_error(
'D404',
'First word of the docstring should not be `This`',
)
D405 = D4xx.create_error(
'D405',
'Section name should be properly capitalized',
'{0!r}, not {1!r}',
)
D406 = D4xx.create_error(
'D406',
'Section name should end with a newline',
'{0!r}, not {1!r}',
)
D407 = D4xx.create_error(
'D407',
'Missing dashed underline after section',
'{0!r}',
)
D408 = D4xx.create_error(
'D408',
'Section underline should be in the line following the section\'s name',
'{0!r}',
)
D409 = D4xx.create_error(
'D409',
'Section underline should match the length of its name',
'Expected {0!r} dashes in section {1!r}, got {2!r}',
)
D410 = D4xx.create_error(
'D410',
'Missing blank line after section',
'{0!r}',
)
D411 = D4xx.create_error(
'D411',
'Missing blank line before section',
'{0!r}',
)
D412 = D4xx.create_error(
'D412',
'No blank lines allowed between a section header and its content',
'{0!r}',
)
D413 = D4xx.create_error(
'D413',
'Missing blank line after last section',
'{0!r}',
)
D414 = D4xx.create_error(
'D414',
'Section has no content',
'{0!r}',
)
D415 = D4xx.create_error(
'D415',
(
'First line should end with a period, question '
'mark, or exclamation point'
),
'not {0!r}',
)
D416 = D4xx.create_error(
'D416',
'Section name should end with a colon',
'{0!r}, not {1!r}',
)
D417 = D4xx.create_error(
'D417',
'Missing argument descriptions in the docstring',
'argument(s) {0} are missing descriptions in {1!r} docstring',
)
D418 = D4xx.create_error(
'D418',
'Function/ Method decorated with @overload shouldn\'t contain a docstring',
)
D419 = D4xx.create_error(
'D419',
'Docstring is empty',
)
class AttrDict(dict):
def __getattr__(self, item: str) -> Any:
return self[item]
all_errors = set(ErrorRegistry.get_error_codes())
conventions = AttrDict(
{
'pep257': all_errors
- {
'D203',
'D212',
'D213',
'D214',
'D215',
'D404',
'D405',
'D406',
'D407',
'D408',
'D409',
'D410',
'D411',
'D413',
'D415',
'D416',
'D417',
'D418',
},
'numpy': all_errors
- {
'D107',
'D203',
'D212',
'D213',
'D402',
'D413',
'D415',
'D416',
'D417',
},
'google': all_errors
- {
'D203',
'D204',
'D213',
'D215',
'D400',
'D401',
'D404',
'D406',
'D407',
'D408',
'D409',
'D413',
},
}
)