unittest_linter.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. from typing import Any, Optional
  4. from astroid import nodes
  5. from pylint.interfaces import UNDEFINED, Confidence
  6. from pylint.testutils.global_test_linter import linter
  7. from pylint.testutils.output_line import MessageTest
  8. from pylint.utils import LinterStats
  9. class UnittestLinter:
  10. """A fake linter class to capture checker messages."""
  11. # pylint: disable=unused-argument
  12. def __init__(self):
  13. self._messages = []
  14. self.stats = LinterStats()
  15. def release_messages(self):
  16. try:
  17. return self._messages
  18. finally:
  19. self._messages = []
  20. def add_message(
  21. self,
  22. msg_id: str,
  23. line: Optional[int] = None,
  24. node: Optional[nodes.NodeNG] = None,
  25. args: Any = None,
  26. confidence: Optional[Confidence] = None,
  27. col_offset: Optional[int] = None,
  28. end_lineno: Optional[int] = None,
  29. end_col_offset: Optional[int] = None,
  30. ) -> None:
  31. """Add a MessageTest to the _messages attribute of the linter class."""
  32. # If confidence is None we set it to UNDEFINED as well in PyLinter
  33. if confidence is None:
  34. confidence = UNDEFINED
  35. # pylint: disable=fixme
  36. # TODO: Test col_offset
  37. # pylint: disable=fixme
  38. # TODO: Initialize col_offset on every node (can be None) -> astroid
  39. # if col_offset is None and hasattr(node, "col_offset"):
  40. # col_offset = node.col_offset
  41. # pylint: disable=fixme
  42. # TODO: Test end_lineno and end_col_offset :)
  43. self._messages.append(
  44. MessageTest(msg_id, line, node, args, confidence, col_offset)
  45. )
  46. @staticmethod
  47. def is_message_enabled(*unused_args, **unused_kwargs):
  48. return True
  49. @property
  50. def options_providers(self):
  51. return linter.options_providers