unittest_linter.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  2. # For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE
  3. # Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt
  4. # pylint: disable=duplicate-code
  5. from __future__ import annotations
  6. from typing import Any, Literal
  7. from astroid import nodes
  8. from pylint.interfaces import UNDEFINED, Confidence
  9. from pylint.lint import PyLinter
  10. from pylint.testutils.output_line import MessageTest
  11. class UnittestLinter(PyLinter):
  12. """A fake linter class to capture checker messages."""
  13. def __init__(self) -> None:
  14. self._messages: list[MessageTest] = []
  15. super().__init__()
  16. def release_messages(self) -> list[MessageTest]:
  17. try:
  18. return self._messages
  19. finally:
  20. self._messages = []
  21. def add_message(
  22. self,
  23. msgid: str,
  24. line: int | None = None,
  25. # TODO: Make node non optional
  26. node: nodes.NodeNG | None = None,
  27. args: Any = None,
  28. confidence: Confidence | None = None,
  29. col_offset: int | None = None,
  30. end_lineno: int | None = None,
  31. end_col_offset: int | None = None,
  32. ) -> None:
  33. """Add a MessageTest to the _messages attribute of the linter class."""
  34. # If confidence is None we set it to UNDEFINED as well in PyLinter
  35. if confidence is None:
  36. confidence = UNDEFINED
  37. # Look up "location" data of node if not yet supplied
  38. if node:
  39. if node.position:
  40. if not line:
  41. line = node.position.lineno
  42. if not col_offset:
  43. col_offset = node.position.col_offset
  44. if not end_lineno:
  45. end_lineno = node.position.end_lineno
  46. if not end_col_offset:
  47. end_col_offset = node.position.end_col_offset
  48. else:
  49. if not line:
  50. line = node.fromlineno
  51. if not col_offset:
  52. col_offset = node.col_offset
  53. if not end_lineno:
  54. end_lineno = node.end_lineno
  55. if not end_col_offset:
  56. end_col_offset = node.end_col_offset
  57. self._messages.append(
  58. MessageTest(
  59. msgid,
  60. line,
  61. node,
  62. args,
  63. confidence,
  64. col_offset,
  65. end_lineno,
  66. end_col_offset,
  67. )
  68. )
  69. @staticmethod
  70. def is_message_enabled(*unused_args: Any, **unused_kwargs: Any) -> Literal[True]:
  71. return True