misc.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  2. # For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
  3. # Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
  4. """Check source code is ascii only or has an encoding declaration (PEP 263)."""
  5. from __future__ import annotations
  6. import re
  7. import tokenize
  8. from typing import TYPE_CHECKING
  9. from astroid import nodes
  10. from pylint.checkers import BaseRawFileChecker, BaseTokenChecker
  11. from pylint.typing import ManagedMessage
  12. if TYPE_CHECKING:
  13. from pylint.lint import PyLinter
  14. class ByIdManagedMessagesChecker(BaseRawFileChecker):
  15. """Checks for messages that are enabled or disabled by id instead of symbol."""
  16. name = "miscellaneous"
  17. msgs = {
  18. "I0023": (
  19. "%s",
  20. "use-symbolic-message-instead",
  21. "Used when a message is enabled or disabled by id.",
  22. )
  23. }
  24. options = ()
  25. def _clear_by_id_managed_msgs(self) -> None:
  26. self.linter._by_id_managed_msgs.clear()
  27. def _get_by_id_managed_msgs(self) -> list[ManagedMessage]:
  28. return self.linter._by_id_managed_msgs
  29. def process_module(self, node: nodes.Module) -> None:
  30. """Inspect the source file to find messages activated or deactivated by id."""
  31. managed_msgs = self._get_by_id_managed_msgs()
  32. for mod_name, msgid, symbol, lineno, is_disabled in managed_msgs:
  33. if mod_name == node.name:
  34. verb = "disable" if is_disabled else "enable"
  35. txt = f"'{msgid}' is cryptic: use '# pylint: {verb}={symbol}' instead"
  36. self.add_message("use-symbolic-message-instead", line=lineno, args=txt)
  37. self._clear_by_id_managed_msgs()
  38. class EncodingChecker(BaseTokenChecker, BaseRawFileChecker):
  39. """BaseChecker for encoding issues.
  40. Checks for:
  41. * warning notes in the code like FIXME, XXX
  42. * encoding issues.
  43. """
  44. # configuration section name
  45. name = "miscellaneous"
  46. msgs = {
  47. "W0511": (
  48. "%s",
  49. "fixme",
  50. "Used when a warning note as FIXME or XXX is detected.",
  51. )
  52. }
  53. options = (
  54. (
  55. "notes",
  56. {
  57. "type": "csv",
  58. "metavar": "<comma separated values>",
  59. "default": ("FIXME", "XXX", "TODO"),
  60. "help": (
  61. "List of note tags to take in consideration, "
  62. "separated by a comma."
  63. ),
  64. },
  65. ),
  66. (
  67. "notes-rgx",
  68. {
  69. "type": "string",
  70. "metavar": "<regexp>",
  71. "help": "Regular expression of note tags to take in consideration.",
  72. "default": "",
  73. },
  74. ),
  75. )
  76. def open(self) -> None:
  77. super().open()
  78. notes = "|".join(re.escape(note) for note in self.linter.config.notes)
  79. if self.linter.config.notes_rgx:
  80. regex_string = rf"#\s*({notes}|{self.linter.config.notes_rgx})(?=(:|\s|\Z))"
  81. else:
  82. regex_string = rf"#\s*({notes})(?=(:|\s|\Z))"
  83. self._fixme_pattern = re.compile(regex_string, re.I)
  84. def _check_encoding(
  85. self, lineno: int, line: bytes, file_encoding: str
  86. ) -> str | None:
  87. try:
  88. return line.decode(file_encoding)
  89. except UnicodeDecodeError:
  90. pass
  91. except LookupError:
  92. if (
  93. line.startswith(b"#")
  94. and "coding" in str(line)
  95. and file_encoding in str(line)
  96. ):
  97. msg = f"Cannot decode using encoding '{file_encoding}', bad encoding"
  98. self.add_message("syntax-error", line=lineno, args=msg)
  99. return None
  100. def process_module(self, node: nodes.Module) -> None:
  101. """Inspect the source file to find encoding problem."""
  102. encoding = node.file_encoding if node.file_encoding else "ascii"
  103. with node.stream() as stream:
  104. for lineno, line in enumerate(stream):
  105. self._check_encoding(lineno + 1, line, encoding)
  106. def process_tokens(self, tokens: list[tokenize.TokenInfo]) -> None:
  107. """Inspect the source to find fixme problems."""
  108. if not self.linter.config.notes:
  109. return
  110. for token_info in tokens:
  111. if token_info.type != tokenize.COMMENT:
  112. continue
  113. comment_text = token_info.string[1:].lstrip() # trim '#' and white-spaces
  114. if self._fixme_pattern.search("#" + comment_text.lower()):
  115. self.add_message(
  116. "fixme",
  117. col_offset=token_info.start[1] + 1,
  118. args=comment_text,
  119. line=token_info.start[0],
  120. )
  121. def register(linter: PyLinter) -> None:
  122. linter.register_checker(EncodingChecker(linter))
  123. linter.register_checker(ByIdManagedMessagesChecker(linter))