violations.py 12 KB

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