diff options
author | orSolocate <38433858+orSolocate@users.noreply.github.com> | 2023-01-12 18:46:59 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-12 17:46:59 +0100 |
commit | e487a4659dcaeb7a48b084b2acbe5274583b1e3a (patch) | |
tree | a38e87bb1b2d440097167015724c93a1ce228f77 /pylint | |
parent | 51ac867157c1c710b8697f492bcc1f959d0acec9 (diff) | |
download | pylint-git-e487a4659dcaeb7a48b084b2acbe5274583b1e3a.tar.gz |
Bugfix - extend ``magic-value-comparison`` support for parsing numerical types and `\\` in rcfile configuration (#7880)
Diffstat (limited to 'pylint')
-rw-r--r-- | pylint/extensions/magic_value.py | 37 |
1 files changed, 34 insertions, 3 deletions
diff --git a/pylint/extensions/magic_value.py b/pylint/extensions/magic_value.py index 161bb2c95..7cfb410ae 100644 --- a/pylint/extensions/magic_value.py +++ b/pylint/extensions/magic_value.py @@ -6,6 +6,7 @@ from __future__ import annotations +from re import match as regex_match from typing import TYPE_CHECKING from astroid import nodes @@ -37,11 +38,31 @@ class MagicValueChecker(BaseChecker): "default": (0, -1, 1, "", "__main__"), "type": "csv", "metavar": "<argument names>", - "help": " List of valid magic values that `magic-value-compare` will not detect.", + "help": "List of valid magic values that `magic-value-compare` will not detect. " + "Supports integers, floats, negative numbers, for empty string enter ``''``," + " for backslash values just use one backslash e.g \\n.", }, ), ) + def __init__(self, linter: PyLinter) -> None: + """Initialize checker instance.""" + super().__init__(linter=linter) + self.valid_magic_vals: tuple[float | str, ...] = () + + def open(self) -> None: + # Extra manipulation is needed in case of using external configuration like an rcfile + if self._magic_vals_ext_configured(): + self.valid_magic_vals = tuple( + self._parse_rcfile_magic_numbers(value) + for value in self.linter.config.valid_magic_values + ) + else: + self.valid_magic_vals = self.linter.config.valid_magic_values + + def _magic_vals_ext_configured(self) -> bool: + return not isinstance(self.linter.config.valid_magic_values, tuple) + def _check_constants_comparison(self, node: nodes.Compare) -> None: """ Magic values in any side of the comparison should be avoided, @@ -74,11 +95,21 @@ class MagicValueChecker(BaseChecker): confidence=HIGH, ) - def _is_magic_value(self, node: nodes.NodeNG) -> bool: + def _is_magic_value(self, node: nodes.Const) -> bool: return (not utils.is_singleton_const(node)) and ( - node.value not in self.linter.config.valid_magic_values + node.value not in (self.valid_magic_vals) ) + @staticmethod + def _parse_rcfile_magic_numbers(parsed_val: str) -> float | str: + parsed_val = parsed_val.encode().decode("unicode_escape") + + if parsed_val.startswith("'") and parsed_val.endswith("'"): + return parsed_val[1:-1] + + is_number = regex_match(r"[-+]?\d+(\.0*)?$", parsed_val) + return float(parsed_val) if is_number else parsed_val + @utils.only_required_for_messages("magic-comparison") def visit_compare(self, node: nodes.Compare) -> None: self._check_constants_comparison(node) |