Skip to content

Commit df0800e

Browse files
authored
Escape special symbols and newlines in messages. (#9164)
1 parent f9e118f commit df0800e

File tree

4 files changed

+8
-2
lines changed

4 files changed

+8
-2
lines changed

doc/whatsnew/fragments/7874.bugfix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Escape special symbols and newlines in messages.
2+
3+
Closes #7874

pylint/extensions/magic_value.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ def _check_constants_comparison(self, node: nodes.Compare) -> None:
8484

8585
operand_value = None
8686
if const_operands[LEFT_OPERAND] and self._is_magic_value(left_operand):
87-
operand_value = left_operand.value
87+
operand_value = left_operand.as_string()
8888
elif const_operands[RIGHT_OPERAND] and self._is_magic_value(right_operand):
89-
operand_value = right_operand.value
89+
operand_value = right_operand.as_string()
9090
if operand_value is not None:
9191
self.add_message(
9292
"magic-value-comparison",

tests/functional/ext/magic_value_comparison/magic_value_comparison.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ class Christmas(Enum):
3434

3535
shouldnt_raise = var == '\n'
3636
shouldnt_raise = var == '\\b'
37+
38+
should_escape = var == "foo\nbar" # [magic-value-comparison]

tests/functional/ext/magic_value_comparison/magic_value_comparison.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ comparison-of-constants:24:17:24:22::"Comparison between constants: '5 > 7' has
55
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
66
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
77
singleton-comparison:31:17:31:28::Comparison 'var == None' should be 'var is None':UNDEFINED
8+
magic-value-comparison:38:16:38:33::Consider using a named constant or an enum instead of ''foo\nbar''.:HIGH

0 commit comments

Comments
 (0)