diff --git a/doc/whatsnew/fragments/7874.bugfix b/doc/whatsnew/fragments/7874.bugfix new file mode 100644 index 0000000000..112fbb534c --- /dev/null +++ b/doc/whatsnew/fragments/7874.bugfix @@ -0,0 +1,3 @@ +Escape special symbols and newlines in messages. + +Closes #7874 diff --git a/pylint/extensions/magic_value.py b/pylint/extensions/magic_value.py index c69711f85b..fd18476aea 100644 --- a/pylint/extensions/magic_value.py +++ b/pylint/extensions/magic_value.py @@ -84,9 +84,9 @@ def _check_constants_comparison(self, node: nodes.Compare) -> None: operand_value = None if const_operands[LEFT_OPERAND] and self._is_magic_value(left_operand): - operand_value = left_operand.value + operand_value = left_operand.as_string() elif const_operands[RIGHT_OPERAND] and self._is_magic_value(right_operand): - operand_value = right_operand.value + operand_value = right_operand.as_string() if operand_value is not None: self.add_message( "magic-value-comparison", diff --git a/tests/functional/ext/magic_value_comparison/magic_value_comparison.py b/tests/functional/ext/magic_value_comparison/magic_value_comparison.py index f6fb9369be..b7e7cd38a8 100644 --- a/tests/functional/ext/magic_value_comparison/magic_value_comparison.py +++ b/tests/functional/ext/magic_value_comparison/magic_value_comparison.py @@ -34,3 +34,5 @@ class Christmas(Enum): shouldnt_raise = var == '\n' shouldnt_raise = var == '\\b' + +should_escape = var == "foo\nbar" # [magic-value-comparison] diff --git a/tests/functional/ext/magic_value_comparison/magic_value_comparison.txt b/tests/functional/ext/magic_value_comparison/magic_value_comparison.txt index 63976ff68b..256cd4a0c7 100644 --- a/tests/functional/ext/magic_value_comparison/magic_value_comparison.txt +++ b/tests/functional/ext/magic_value_comparison/magic_value_comparison.txt @@ -5,3 +5,4 @@ comparison-of-constants:24:17:24:22::"Comparison between constants: '5 > 7' has singleton-comparison:29:17:29:28::Comparison 'var == True' should be 'var is True' if checking for the singleton value True, or 'bool(var)' if testing for truthiness:UNDEFINED singleton-comparison:30:17:30:29::Comparison 'var == False' should be 'var is False' if checking for the singleton value False, or 'not var' if testing for falsiness:UNDEFINED singleton-comparison:31:17:31:28::Comparison 'var == None' should be 'var is None':UNDEFINED +magic-value-comparison:38:16:38:33::Consider using a named constant or an enum instead of ''foo\nbar''.:HIGH