message.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. from __future__ import annotations
  5. from dataclasses import asdict, dataclass
  6. from pylint.constants import MSG_TYPES
  7. from pylint.interfaces import UNDEFINED, Confidence
  8. from pylint.typing import MessageLocationTuple
  9. @dataclass(unsafe_hash=True)
  10. class Message: # pylint: disable=too-many-instance-attributes
  11. """This class represent a message to be issued by the reporters."""
  12. msg_id: str
  13. symbol: str
  14. msg: str
  15. C: str
  16. category: str
  17. confidence: Confidence
  18. abspath: str
  19. path: str
  20. module: str
  21. obj: str
  22. line: int
  23. column: int
  24. end_line: int | None
  25. end_column: int | None
  26. def __init__(
  27. self,
  28. msg_id: str,
  29. symbol: str,
  30. location: MessageLocationTuple,
  31. msg: str,
  32. confidence: Confidence | None,
  33. ) -> None:
  34. self.msg_id = msg_id
  35. self.symbol = symbol
  36. self.msg = msg
  37. self.C = msg_id[0]
  38. self.category = MSG_TYPES[msg_id[0]]
  39. self.confidence = confidence or UNDEFINED
  40. self.abspath = location.abspath
  41. self.path = location.path
  42. self.module = location.module
  43. self.obj = location.obj
  44. self.line = location.line
  45. self.column = location.column
  46. self.end_line = location.end_line
  47. self.end_column = location.end_column
  48. def format(self, template: str) -> str:
  49. """Format the message according to the given template.
  50. The template format is the one of the format method :
  51. cf. https://docs.python.org/2/library/string.html#formatstrings
  52. """
  53. return template.format(**asdict(self))
  54. @property
  55. def location(self) -> MessageLocationTuple:
  56. return MessageLocationTuple(
  57. self.abspath,
  58. self.path,
  59. self.module,
  60. self.obj,
  61. self.line,
  62. self.column,
  63. self.end_line,
  64. self.end_column,
  65. )