Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

docstyle.py 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2016-2017 Claudiu Popa <pcmanticore@gmail.com>
  3. # Copyright (c) 2016 Łukasz Rogalski <rogalski.91@gmail.com>
  4. # Copyright (c) 2016 Luis Escobar <lescobar@vauxoo.com>
  5. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  6. # For details: https://github.com/PyCQA/pylint/blob/master/COPYING
  7. import linecache
  8. from pylint import checkers
  9. from pylint.interfaces import IAstroidChecker, HIGH
  10. from pylint.checkers.utils import check_messages
  11. class DocStringStyleChecker(checkers.BaseChecker):
  12. """Checks format of docstrings based on PEP 0257"""
  13. __implements__ = IAstroidChecker
  14. name = 'docstyle'
  15. msgs = {
  16. 'C0198': ('Bad docstring quotes in %s, expected """, given %s',
  17. 'bad-docstring-quotes',
  18. 'Used when a docstring does not have triple double quotes.'),
  19. 'C0199': ('First line empty in %s docstring',
  20. 'docstring-first-line-empty',
  21. 'Used when a blank line is found at the beginning of a docstring.'),
  22. }
  23. @check_messages('docstring-first-line-empty', 'bad-docstring-quotes')
  24. def visit_module(self, node):
  25. self._check_docstring('module', node)
  26. def visit_classdef(self, node):
  27. self._check_docstring('class', node)
  28. def visit_functiondef(self, node):
  29. ftype = 'method' if node.is_method() else 'function'
  30. self._check_docstring(ftype, node)
  31. visit_asyncfunctiondef = visit_functiondef
  32. def _check_docstring(self, node_type, node):
  33. docstring = node.doc
  34. if docstring and docstring[0] == '\n':
  35. self.add_message('docstring-first-line-empty', node=node,
  36. args=(node_type,), confidence=HIGH)
  37. # Use "linecache", instead of node.as_string(), because the latter
  38. # looses the original form of the docstrings.
  39. if docstring:
  40. lineno = node.fromlineno + 1
  41. line = linecache.getline(node.root().file, lineno).lstrip()
  42. if line and line.find('"""') == 0:
  43. return
  44. if line and '\'\'\'' in line:
  45. quotes = '\'\'\''
  46. elif line and line[0] == '"':
  47. quotes = '"'
  48. elif line and line[0] == '\'':
  49. quotes = '\''
  50. else:
  51. quotes = False
  52. if quotes:
  53. self.add_message('bad-docstring-quotes', node=node,
  54. args=(node_type, quotes), confidence=HIGH)
  55. def register(linter):
  56. """Required method to auto register this checker.
  57. :param linter: Main interface object for Pylint plugins
  58. :type linter: Pylint object
  59. """
  60. linter.register_checker(DocStringStyleChecker(linter))