magic_value.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. """Checks for magic values instead of literals."""
  5. from __future__ import annotations
  6. from re import match as regex_match
  7. from typing import TYPE_CHECKING
  8. from astroid import nodes
  9. from pylint.checkers import BaseChecker, utils
  10. from pylint.interfaces import HIGH
  11. if TYPE_CHECKING:
  12. from pylint.lint import PyLinter
  13. class MagicValueChecker(BaseChecker):
  14. """Checks for constants in comparisons."""
  15. name = "magic-value"
  16. msgs = {
  17. "R2004": (
  18. "Consider using a named constant or an enum instead of '%s'.",
  19. "magic-value-comparison",
  20. "Using named constants instead of magic values helps improve readability and maintainability of your"
  21. " code, try to avoid them in comparisons.",
  22. )
  23. }
  24. options = (
  25. (
  26. "valid-magic-values",
  27. {
  28. "default": (0, -1, 1, "", "__main__"),
  29. "type": "csv",
  30. "metavar": "<argument names>",
  31. "help": "List of valid magic values that `magic-value-compare` will not detect. "
  32. "Supports integers, floats, negative numbers, for empty string enter ``''``,"
  33. " for backslash values just use one backslash e.g \\n.",
  34. },
  35. ),
  36. )
  37. def __init__(self, linter: PyLinter) -> None:
  38. """Initialize checker instance."""
  39. super().__init__(linter=linter)
  40. self.valid_magic_vals: tuple[float | str, ...] = ()
  41. def open(self) -> None:
  42. # Extra manipulation is needed in case of using external configuration like an rcfile
  43. if self._magic_vals_ext_configured():
  44. self.valid_magic_vals = tuple(
  45. self._parse_rcfile_magic_numbers(value)
  46. for value in self.linter.config.valid_magic_values
  47. )
  48. else:
  49. self.valid_magic_vals = self.linter.config.valid_magic_values
  50. def _magic_vals_ext_configured(self) -> bool:
  51. return not isinstance(self.linter.config.valid_magic_values, tuple)
  52. def _check_constants_comparison(self, node: nodes.Compare) -> None:
  53. """
  54. Magic values in any side of the comparison should be avoided,
  55. Detects comparisons that `comparison-of-constants` core checker cannot detect.
  56. """
  57. const_operands = []
  58. LEFT_OPERAND = 0
  59. RIGHT_OPERAND = 1
  60. left_operand = node.left
  61. const_operands.append(isinstance(left_operand, nodes.Const))
  62. right_operand = node.ops[0][1]
  63. const_operands.append(isinstance(right_operand, nodes.Const))
  64. if all(const_operands):
  65. # `comparison-of-constants` avoided
  66. return
  67. operand_value = None
  68. if const_operands[LEFT_OPERAND] and self._is_magic_value(left_operand):
  69. operand_value = left_operand.value
  70. elif const_operands[RIGHT_OPERAND] and self._is_magic_value(right_operand):
  71. operand_value = right_operand.value
  72. if operand_value is not None:
  73. self.add_message(
  74. "magic-value-comparison",
  75. node=node,
  76. args=(operand_value),
  77. confidence=HIGH,
  78. )
  79. def _is_magic_value(self, node: nodes.Const) -> bool:
  80. return (not utils.is_singleton_const(node)) and (
  81. node.value not in (self.valid_magic_vals)
  82. )
  83. @staticmethod
  84. def _parse_rcfile_magic_numbers(parsed_val: str) -> float | str:
  85. parsed_val = parsed_val.encode().decode("unicode_escape")
  86. if parsed_val.startswith("'") and parsed_val.endswith("'"):
  87. return parsed_val[1:-1]
  88. is_number = regex_match(r"[-+]?\d+(\.0*)?$", parsed_val)
  89. return float(parsed_val) if is_number else parsed_val
  90. @utils.only_required_for_messages("magic-comparison")
  91. def visit_compare(self, node: nodes.Compare) -> None:
  92. self._check_constants_comparison(node)
  93. def register(linter: PyLinter) -> None:
  94. linter.register_checker(MagicValueChecker(linter))