Skip to content

Commit 6584761

Browse files
[comparison-of-constants] Use as_string() from astroid during message formatting
1 parent 0148ff1 commit 6584761

File tree

9 files changed

+23
-18
lines changed

9 files changed

+23
-18
lines changed

doc/whatsnew/fragments/8736.feature

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
``comparison-of-constants`` now use the unicode from the ast instead of reformatting from
2+
the node's values preventing some bad formatting due to ``utf-8`` limitation. The message now use
3+
``"`` instead of ``'`` to better work with what the python ast returns.
4+
5+
Refs #8736

pylint/checkers/base/comparison_checker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class ComparisonChecker(_BasicChecker):
6060
"Used when something is compared against itself.",
6161
),
6262
"R0133": (
63-
"Comparison between constants: '%s %s %s' has a constant value",
63+
'Comparison between constants: "%s %s %s" has a constant value',
6464
"comparison-of-constants",
6565
"When two literals are compared with each other the result is a constant. "
6666
"Using the constant directly is both easier to read and more performant. "
@@ -257,7 +257,7 @@ def _check_constants_comparison(self, node: nodes.Compare) -> None:
257257
self.add_message(
258258
"comparison-of-constants",
259259
node=node,
260-
args=(left_operand.value, operator, right_operand.value),
260+
args=(left_operand.as_string(), operator, right_operand.as_string()),
261261
confidence=HIGH,
262262
)
263263

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
comparison-of-constants:3:6:3:12::"Comparison between constants: '2 == 2' has a constant value":HIGH
2-
comparison-of-constants:6:6:6:11::"Comparison between constants: '2 > 2' has a constant value":HIGH
3-
comparison-of-constants:16:3:16:15::"Comparison between constants: 'True == True' has a constant value":HIGH
1+
comparison-of-constants:3:6:3:12::"Comparison between constants: ""2 == 2"" has a constant value":HIGH
2+
comparison-of-constants:6:6:6:11::"Comparison between constants: ""2 > 2"" has a constant value":HIGH
3+
comparison-of-constants:16:3:16:15::"Comparison between constants: ""True == True"" has a constant value":HIGH
44
singleton-comparison:16:3:16:15::Comparison 'True == True' should be 'True is True' if checking for the singleton value True, or 'True' if testing for truthiness:UNDEFINED

tests/functional/ext/magic_value_comparison/magic_value_comparison.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
magic-value-comparison:16:3:16:10::Consider using a named constant or an enum instead of '5'.:HIGH
22
magic-value-comparison:19:3:19:17::Consider using a named constant or an enum instead of '10'.:HIGH
33
magic-value-comparison:22:9:22:18::Consider using a named constant or an enum instead of '100'.:HIGH
4-
comparison-of-constants:24:17:24:22::"Comparison between constants: '5 > 7' has a constant value":HIGH
4+
comparison-of-constants:24:17:24:22::"Comparison between constants: ""5 > 7"" has a constant value":HIGH
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

tests/functional/l/literal_comparison.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
comparison-of-constants:4:3:4:9::"Comparison between constants: '2 is 2' has a constant value":HIGH
1+
comparison-of-constants:4:3:4:9::"Comparison between constants: ""2 is 2"" has a constant value":HIGH
22
literal-comparison:4:3:4:9::In '2 is 2', use '==' when comparing constant literals not 'is' ('2 == 2'):HIGH
3-
comparison-of-constants:7:3:7:14::"Comparison between constants: 'a is b'a'' has a constant value":HIGH
3+
comparison-of-constants:7:3:7:14::"Comparison between constants: ""'a' is b'a'"" has a constant value":HIGH
44
literal-comparison:7:3:7:14::In ''a' is b'a'', use '==' when comparing constant literals not 'is' (''a' == b'a''):HIGH
5-
comparison-of-constants:10:3:10:13::"Comparison between constants: '2.0 is 3.0' has a constant value":HIGH
5+
comparison-of-constants:10:3:10:13::"Comparison between constants: ""2.0 is 3.0"" has a constant value":HIGH
66
literal-comparison:10:3:10:13::In '2.0 is 3.0', use '==' when comparing constant literals not 'is' ('2.0 == 3.0'):HIGH
77
literal-comparison:16:3:16:19::"In '() is {1: 2, 2: 3}', use '==' when comparing constant literals not 'is' ('() == {1: 2, 2: 3}')":HIGH
88
literal-comparison:19:3:19:18::In '[] is [4, 5, 6]', use '==' when comparing constant literals not 'is' ('[] == [4, 5, 6]'):HIGH

tests/functional/l/logical_tautology.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ comparison-with-itself:6:7:6:17:foo:Redundant comparison - arg == arg:UNDEFINED
22
comparison-with-itself:8:9:8:19:foo:Redundant comparison - arg != arg:UNDEFINED
33
comparison-with-itself:10:9:10:18:foo:Redundant comparison - arg > arg:UNDEFINED
44
comparison-with-itself:12:9:12:19:foo:Redundant comparison - arg <= arg:UNDEFINED
5-
comparison-of-constants:14:9:14:21:foo:"Comparison between constants: 'None == None' has a constant value":HIGH
5+
comparison-of-constants:14:9:14:21:foo:"Comparison between constants: ""None == None"" has a constant value":HIGH
66
comparison-with-itself:14:9:14:21:foo:Redundant comparison - None == None:UNDEFINED
7-
comparison-of-constants:16:9:16:19:foo:"Comparison between constants: '786 == 786' has a constant value":HIGH
7+
comparison-of-constants:16:9:16:19:foo:"Comparison between constants: ""786 == 786"" has a constant value":HIGH
88
comparison-with-itself:16:9:16:19:foo:Redundant comparison - 786 == 786:UNDEFINED
9-
comparison-of-constants:18:9:18:19:foo:"Comparison between constants: '786 is 786' has a constant value":HIGH
9+
comparison-of-constants:18:9:18:19:foo:"Comparison between constants: ""786 is 786"" has a constant value":HIGH
1010
comparison-with-itself:18:9:18:19:foo:Redundant comparison - 786 is 786:UNDEFINED
11-
comparison-of-constants:20:9:20:23:foo:"Comparison between constants: '786 is not 786' has a constant value":HIGH
11+
comparison-of-constants:20:9:20:23:foo:"Comparison between constants: ""786 is not 786"" has a constant value":HIGH
1212
comparison-with-itself:20:9:20:23:foo:Redundant comparison - 786 is not 786:UNDEFINED
1313
comparison-with-itself:22:9:22:19:foo:Redundant comparison - arg is arg:UNDEFINED
1414
comparison-with-itself:24:9:24:23:foo:Redundant comparison - arg is not arg:UNDEFINED
15-
comparison-of-constants:26:9:26:21:foo:"Comparison between constants: 'True is True' has a constant value":HIGH
15+
comparison-of-constants:26:9:26:21:foo:"Comparison between constants: ""True is True"" has a constant value":HIGH
1616
comparison-with-itself:26:9:26:21:foo:Redundant comparison - True is True:UNDEFINED
17-
comparison-of-constants:28:9:28:19:foo:"Comparison between constants: '666 == 786' has a constant value":HIGH
17+
comparison-of-constants:28:9:28:19:foo:"Comparison between constants: ""666 == 786"" has a constant value":HIGH
1818
comparison-with-itself:36:18:36:28:bar:Redundant comparison - arg != arg:UNDEFINED
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
comparison-of-constants:3:7:3:37::"Comparison between constants: '𐀀 == ??' has a constant value":HIGH
1+
comparison-of-constants:3:7:3:37::"Comparison between constants: ""'𐀀' == '\ud800\udc00'"" has a constant value":HIGH

tests/functional/u/use/use_implicit_booleaness_not_len.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use-implicit-booleaness-not-len:4:3:4:14::Do not use `len(SEQUENCE)` without com
22
use-implicit-booleaness-not-len:7:3:7:18::Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty:HIGH
33
use-implicit-booleaness-not-len:11:9:11:34::Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty:INFERENCE
44
use-implicit-booleaness-not-len:14:11:14:22::Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty:INFERENCE
5-
comparison-of-constants:39:3:39:28::"Comparison between constants: '0 < 1' has a constant value":HIGH
5+
comparison-of-constants:39:3:39:28::"Comparison between constants: ""0 < 1"" has a constant value":HIGH
66
use-implicit-booleaness-not-len:56:5:56:16::Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty:INFERENCE
77
use-implicit-booleaness-not-len:61:5:61:20::Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty:HIGH
88
use-implicit-booleaness-not-len:64:6:64:17::Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty:INFERENCE

tests/functional/u/using_constant_test.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ using-constant-test:84:36:84:39:test_comprehensions:Using a conditional statemen
2424
using-constant-test:85:39:85:42:test_comprehensions:Using a conditional statement with a constant value:INFERENCE
2525
using-constant-test:89:3:89:15::Using a conditional statement with a constant value:INFERENCE
2626
using-constant-test:93:3:93:18::Using a conditional statement with a constant value:INFERENCE
27-
comparison-of-constants:117:3:117:8::"Comparison between constants: '2 < 3' has a constant value":HIGH
27+
comparison-of-constants:117:3:117:8::"Comparison between constants: ""2 < 3"" has a constant value":HIGH
2828
using-constant-test:156:0:157:8::Using a conditional statement with a constant value:INFERENCE
2929
using-constant-test:168:3:168:4::Using a conditional statement with a constant value:INFERENCE
3030
using-constant-test:177:0:178:8::Using a conditional statement with a constant value:INFERENCE

0 commit comments

Comments
 (0)