Skip to content

Commit 50f2269

Browse files
gracejiang16Jiang
andauthored
Rename unneeded-not to unnecessary-negation for clarity (#8846)
Co-authored-by: Jiang <[email protected]>
1 parent fe15b7e commit 50f2269

File tree

9 files changed

+54
-39
lines changed

9 files changed

+54
-39
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
if not not input(): # [unnecessary-negation]
2+
pass
3+
4+
a = 3
5+
b = 10
6+
if not a > b: # [unnecessary-negation]
7+
pass
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
if input():
2+
pass
3+
4+
a = 3
5+
b = 10
6+
if a <= b:
7+
pass

doc/data/messages/u/unneeded-not/bad.py

Lines changed: 0 additions & 2 deletions
This file was deleted.

doc/data/messages/u/unneeded-not/good.py

Lines changed: 0 additions & 2 deletions
This file was deleted.

doc/whatsnew/fragments/8789.other

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Renamed the "unneeded-not" error into "unnecessary_negation" to be clearer.
2+
3+
Closes #8789

pylint/checkers/refactoring/not_checker.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ class NotChecker(checkers.BaseChecker):
1717
"""
1818

1919
msgs = {
20-
"C0113": (
20+
"C0117": (
2121
'Consider changing "%s" to "%s"',
22-
"unneeded-not",
23-
"Used when a boolean expression contains an unneeded negation.",
22+
"unnecessary-negation",
23+
"Used when a boolean expression contains an unneeded negation, "
24+
"e.g. when two negation operators cancel each other out.",
25+
{"old_names": [("C0113", "unneeded-not")]},
2426
)
2527
}
2628
name = "refactoring"
@@ -40,15 +42,15 @@ class NotChecker(checkers.BaseChecker):
4042
# 'builtins' py3, '__builtin__' py2
4143
skipped_classnames = [f"builtins.{qname}" for qname in ("set", "frozenset")]
4244

43-
@utils.only_required_for_messages("unneeded-not")
45+
@utils.only_required_for_messages("unnecessary-negation")
4446
def visit_unaryop(self, node: nodes.UnaryOp) -> None:
4547
if node.op != "not":
4648
return
4749
operand = node.operand
4850

4951
if isinstance(operand, nodes.UnaryOp) and operand.op == "not":
5052
self.add_message(
51-
"unneeded-not",
53+
"unnecessary-negation",
5254
node=node,
5355
args=(node.as_string(), operand.operand.as_string()),
5456
)
@@ -78,5 +80,5 @@ def visit_unaryop(self, node: nodes.UnaryOp) -> None:
7880
f"{left.as_string()} {self.reverse_op[operator]} {right.as_string()}"
7981
)
8082
self.add_message(
81-
"unneeded-not", node=node, args=(node.as_string(), suggestion)
83+
"unnecessary-negation", node=node, args=(node.as_string(), suggestion)
8284
)

tests/functional/u/unnecessary/unnecessary_not.py renamed to tests/functional/u/unnecessary/unnecessary_negation.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,38 @@
22

33
# pylint: disable=singleton-comparison,too-many-branches,too-few-public-methods,undefined-variable
44
# pylint: disable=literal-comparison, comparison-with-itself, comparison-of-constants
5-
def unneeded_not():
5+
def unnecessary_negation():
66
"""This is not ok
77
"""
88
bool_var = True
99
someint = 2
10-
if not not bool_var: # [unneeded-not]
10+
if not not bool_var: # [unnecessary-negation]
1111
pass
12-
if not someint == 1: # [unneeded-not]
12+
if not someint == 1: # [unnecessary-negation]
1313
pass
14-
if not someint != 1: # [unneeded-not]
14+
if not someint != 1: # [unnecessary-negation]
1515
pass
16-
if not someint < 1: # [unneeded-not]
16+
if not someint < 1: # [unnecessary-negation]
1717
pass
18-
if not someint > 1: # [unneeded-not]
18+
if not someint > 1: # [unnecessary-negation]
1919
pass
20-
if not someint <= 1: # [unneeded-not]
20+
if not someint <= 1: # [unnecessary-negation]
2121
pass
22-
if not someint >= 1: # [unneeded-not]
22+
if not someint >= 1: # [unnecessary-negation]
2323
pass
24-
if not not someint: # [unneeded-not]
24+
if not not someint: # [unnecessary-negation]
2525
pass
26-
if not bool_var == True: # [unneeded-not]
26+
if not bool_var == True: # [unnecessary-negation]
2727
pass
28-
if not bool_var == False: # [unneeded-not]
28+
if not bool_var == False: # [unnecessary-negation]
2929
pass
30-
if not bool_var != True: # [unneeded-not]
30+
if not bool_var != True: # [unnecessary-negation]
3131
pass
32-
if not True == True: # [unneeded-not]
32+
if not True == True: # [unnecessary-negation]
3333
pass
34-
if not 2 in [3, 4]: # [unneeded-not]
34+
if not 2 in [3, 4]: # [unnecessary-negation]
3535
pass
36-
if not someint is 'test': # [unneeded-not]
36+
if not someint == 'test': # [unnecessary-negation]
3737
pass
3838

3939

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
unnecessary-negation:10:7:10:23:unnecessary_negation:"Consider changing ""not not bool_var"" to ""bool_var""":UNDEFINED
2+
unnecessary-negation:12:7:12:23:unnecessary_negation:"Consider changing ""not someint == 1"" to ""someint != 1""":UNDEFINED
3+
unnecessary-negation:14:7:14:23:unnecessary_negation:"Consider changing ""not someint != 1"" to ""someint == 1""":UNDEFINED
4+
unnecessary-negation:16:7:16:22:unnecessary_negation:"Consider changing ""not someint < 1"" to ""someint >= 1""":UNDEFINED
5+
unnecessary-negation:18:7:18:22:unnecessary_negation:"Consider changing ""not someint > 1"" to ""someint <= 1""":UNDEFINED
6+
unnecessary-negation:20:7:20:23:unnecessary_negation:"Consider changing ""not someint <= 1"" to ""someint > 1""":UNDEFINED
7+
unnecessary-negation:22:7:22:23:unnecessary_negation:"Consider changing ""not someint >= 1"" to ""someint < 1""":UNDEFINED
8+
unnecessary-negation:24:7:24:22:unnecessary_negation:"Consider changing ""not not someint"" to ""someint""":UNDEFINED
9+
unnecessary-negation:26:7:26:27:unnecessary_negation:"Consider changing ""not bool_var == True"" to ""bool_var != True""":UNDEFINED
10+
unnecessary-negation:28:7:28:28:unnecessary_negation:"Consider changing ""not bool_var == False"" to ""bool_var != False""":UNDEFINED
11+
unnecessary-negation:30:7:30:27:unnecessary_negation:"Consider changing ""not bool_var != True"" to ""bool_var == True""":UNDEFINED
12+
unnecessary-negation:32:7:32:23:unnecessary_negation:"Consider changing ""not True == True"" to ""True != True""":UNDEFINED
13+
unnecessary-negation:34:7:34:22:unnecessary_negation:"Consider changing ""not 2 in [3, 4]"" to ""2 not in [3, 4]""":UNDEFINED
14+
unnecessary-negation:36:7:36:28:unnecessary_negation:"Consider changing ""not someint == 'test'"" to ""someint != 'test'""":UNDEFINED

tests/functional/u/unnecessary/unnecessary_not.txt

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)