Skip to content

Commit b08beb4

Browse files
committed
Don't emit message for 'x < 0 or x > 100'
1 parent 3e0fda5 commit b08beb4

File tree

3 files changed

+37
-20
lines changed

3 files changed

+37
-20
lines changed

pylint/extensions/code_style.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from __future__ import annotations
66

77
from copy import copy
8+
from enum import IntFlag, auto
89
from typing import TYPE_CHECKING, TypeGuard, cast
910

1011
from astroid import nodes
@@ -17,6 +18,12 @@
1718
from pylint.lint import PyLinter
1819

1920

21+
class InvertibleValues(IntFlag):
22+
NO = 0
23+
YES = auto()
24+
EXPLICIT_NEGATION = auto()
25+
26+
2027
class CodeStyleChecker(BaseChecker):
2128
"""Checkers that can improve code consistency.
2229
@@ -330,16 +337,21 @@ def visit_assign(self, node: nodes.Assign) -> None:
330337
)
331338

332339
@staticmethod
333-
def _can_be_inverted(node: nodes.NodeNG) -> bool:
334-
match node:
335-
case nodes.UnaryOp(op="not"):
336-
return True
337-
case nodes.Compare(
338-
ops=[("!=" | "not in", _)]
339-
| [("<" | "<=" | ">" | ">=", nodes.Const(value=int()))]
340-
):
341-
return True
342-
return False
340+
def _can_be_inverted(values: list[nodes.NodeNG]) -> InvertibleValues:
341+
invertible = InvertibleValues.NO
342+
for node in values:
343+
match node:
344+
case nodes.UnaryOp(op="not") | nodes.Compare(
345+
ops=[("!=" | "not in", _)]
346+
):
347+
invertible |= InvertibleValues.EXPLICIT_NEGATION
348+
case nodes.Compare(
349+
ops=[("<" | "<=" | ">" | ">=", nodes.Const(value=int()))]
350+
):
351+
invertible |= InvertibleValues.YES
352+
case _:
353+
return InvertibleValues.NO
354+
return invertible
343355

344356
@staticmethod
345357
def _invert_node(node: nodes.NodeNG) -> nodes.NodeNG:
@@ -372,7 +384,11 @@ def _invert_node(node: nodes.NodeNG) -> nodes.NodeNG:
372384

373385
@only_required_for_messages("improve-conditionals")
374386
def visit_boolop(self, node: nodes.BoolOp) -> None:
375-
if node.op == "or" and all(self._can_be_inverted(val) for val in node.values):
387+
if (
388+
node.op == "or"
389+
and (invertible := self._can_be_inverted(node.values))
390+
and invertible & InvertibleValues.EXPLICIT_NEGATION
391+
):
376392
new_boolop = copy(node)
377393
new_boolop.op = "and"
378394
new_boolop.postinit([self._invert_node(val) for val in node.values])

tests/functional/ext/code_style/cs_improve_conditionals.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ def f1(expr, node_cls, x, y, z):
1212
elif not isinstance(expr, node_cls) or expr.attrname == "__init__":
1313
...
1414

15-
if not isinstance(expr, node_cls) or expr.attrname != "__init__": # [improve-conditionals]
15+
if x < 0 or x > 100:
16+
...
17+
elif x > 0 or y >= 1:
1618
...
17-
elif x > 0 or y >= 1: # [improve-conditionals]
19+
elif x < 0 or y <= 1:
1820
...
19-
elif x < 0 or y <= 1: # [improve-conditionals]
21+
22+
if not isinstance(expr, node_cls) or expr.attrname != "__init__": # [improve-conditionals]
2023
...
2124
elif not x or y not in z: # [improve-conditionals]
2225
...
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
improve-conditionals:15:7:15:68:f1:Rewrite conditional expression to 'not (isinstance(expr, node_cls) and expr.attrname == '__init__')':HIGH
2-
improve-conditionals:17:9:17:24:f1:Rewrite conditional expression to 'not (x <= 0 and y < 1)':HIGH
3-
improve-conditionals:19:9:19:24:f1:Rewrite conditional expression to 'not (x >= 0 and y > 1)':HIGH
4-
improve-conditionals:21:9:21:28:f1:Rewrite conditional expression to 'not (x and y in z)':HIGH
5-
improve-conditionals:23:9:23:29:f1:Rewrite conditional expression to 'x and y':HIGH
6-
improve-conditionals:25:16:25:30:f1:Rewrite conditional expression to 'not (y and z)':HIGH
1+
improve-conditionals:22:7:22:68:f1:Rewrite conditional expression to 'not (isinstance(expr, node_cls) and expr.attrname == '__init__')':HIGH
2+
improve-conditionals:24:9:24:28:f1:Rewrite conditional expression to 'not (x and y in z)':HIGH
3+
improve-conditionals:26:9:26:29:f1:Rewrite conditional expression to 'x and y':HIGH
4+
improve-conditionals:28:16:28:30:f1:Rewrite conditional expression to 'not (y and z)':HIGH

0 commit comments

Comments
 (0)