Skip to content

Commit 87117cf

Browse files
committed
Deal with 'is not' but exclude 'is not None'
1 parent 83a6e22 commit 87117cf

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

pylint/checkers/variables.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,9 +1861,9 @@ def _check_consumer(
18611861
if (
18621862
is_recursive_klass
18631863
and utils.get_node_first_ancestor_of_type(node, nodes.Lambda)
1864-
and (
1865-
not utils.is_default_argument(node)
1866-
or node.scope().parent.scope() is not defframe
1864+
and not (
1865+
utils.is_default_argument(node)
1866+
and node.scope().parent.scope() is defframe
18671867
)
18681868
):
18691869
# Self-referential class references are fine in lambda's --

pylint/extensions/code_style.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,10 @@ def _can_be_inverted(values: list[nodes.NodeNG]) -> InvertibleValues:
379379
invertible = InvertibleValues.NO
380380
for node in values:
381381
match node:
382-
case nodes.UnaryOp(op="not") | nodes.Compare(
383-
ops=[("!=" | "not in", _)]
382+
case nodes.UnaryOp(op="not"):
383+
invertible |= InvertibleValues.EXPLICIT_NEGATION
384+
case nodes.Compare(ops=[("!=" | "not in" | "is not" as op, n)]) if not (
385+
op == "is not" and isinstance(n, nodes.Const) and n.value is None
384386
):
385387
invertible |= InvertibleValues.EXPLICIT_NEGATION
386388
case nodes.Compare(
@@ -405,6 +407,8 @@ def _invert_node(node: nodes.NodeNG) -> nodes.NodeNG:
405407
new_op = "=="
406408
case "not in":
407409
new_op = "in"
410+
case "is not":
411+
new_op = "is"
408412
case "<":
409413
new_op = ">="
410414
case "<=":

tests/functional/ext/code_style/cs_improve_conditionals.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# pylint: disable=missing-docstring
1+
# pylint: disable=missing-docstring,too-many-branches
22

33
def f1(expr, node_cls, x, y, z):
44
if isinstance(expr, node_cls) and expr.attrname == "__init__":
@@ -19,10 +19,17 @@ def f1(expr, node_cls, x, y, z):
1919
elif x < 0 or y <= 1:
2020
...
2121

22+
if x is not None or y is not None:
23+
...
24+
elif not isinstance(expr, node_cls) or x is not None:
25+
...
26+
2227
if not isinstance(expr, node_cls) or expr.attrname != "__init__": # [improve-conditionals]
2328
...
2429
elif not x or y not in z: # [improve-conditionals]
2530
...
31+
elif not x or y is not z: # [improve-conditionals]
32+
...
2633
elif not (not x or not y): # [improve-conditionals]
2734
...
2835
elif x and (not y or not z): # [improve-conditionals]
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
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
1+
improve-conditionals:27:7:27:68:f1:Rewrite conditional expression to 'not (isinstance(expr, node_cls) and expr.attrname == '__init__')':HIGH
2+
improve-conditionals:29:9:29:28:f1:Rewrite conditional expression to 'not (x and y in z)':HIGH
3+
improve-conditionals:31:9:31:28:f1:Rewrite conditional expression to 'not (x and y is z)':HIGH
4+
improve-conditionals:33:9:33:29:f1:Rewrite conditional expression to 'x and y':HIGH
5+
improve-conditionals:35:16:35:30:f1:Rewrite conditional expression to 'not (y and z)':HIGH

0 commit comments

Comments
 (0)