message.py 2.4 KB

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