message.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 warnings import warn
  7. from pylint.constants import MSG_TYPES
  8. from pylint.interfaces import UNDEFINED, Confidence
  9. from pylint.typing import MessageLocationTuple
  10. @dataclass(unsafe_hash=True)
  11. class Message: # pylint: disable=too-many-instance-attributes
  12. """This class represent a message to be issued by the reporters."""
  13. msg_id: str
  14. symbol: str
  15. msg: str
  16. C: str
  17. category: str
  18. confidence: Confidence
  19. abspath: str
  20. path: str
  21. module: str
  22. obj: str
  23. line: int
  24. column: int
  25. end_line: int | None
  26. end_column: int | None
  27. def __init__(
  28. self,
  29. msg_id: str,
  30. symbol: str,
  31. location: tuple[str, str, str, str, int, int] | MessageLocationTuple,
  32. msg: str,
  33. confidence: Confidence | None,
  34. ) -> None:
  35. if not isinstance(location, MessageLocationTuple):
  36. warn(
  37. "In pylint 3.0, Messages will only accept a MessageLocationTuple as location parameter",
  38. DeprecationWarning,
  39. stacklevel=2,
  40. )
  41. location = MessageLocationTuple(
  42. location[0],
  43. location[1],
  44. location[2],
  45. location[3],
  46. location[4],
  47. location[5],
  48. None,
  49. None,
  50. )
  51. self.msg_id = msg_id
  52. self.symbol = symbol
  53. self.msg = msg
  54. self.C = msg_id[0]
  55. self.category = MSG_TYPES[msg_id[0]]
  56. self.confidence = confidence or UNDEFINED
  57. self.abspath = location.abspath
  58. self.path = location.path
  59. self.module = location.module
  60. self.obj = location.obj
  61. self.line = location.line
  62. self.column = location.column
  63. self.end_line = location.end_line
  64. self.end_column = location.end_column
  65. def format(self, template: str) -> str:
  66. """Format the message according to the given template.
  67. The template format is the one of the format method :
  68. cf. https://docs.python.org/2/library/string.html#formatstrings
  69. """
  70. return template.format(**asdict(self))
  71. @property
  72. def location(self) -> MessageLocationTuple:
  73. return MessageLocationTuple(
  74. self.abspath,
  75. self.path,
  76. self.module,
  77. self.obj,
  78. self.line,
  79. self.column,
  80. self.end_line,
  81. self.end_column,
  82. )