Skip to content

Commit e94d54e

Browse files
Change name to 'consider-rewriting-conditional'
Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent 094f055 commit e94d54e

File tree

13 files changed

+30
-24
lines changed

13 files changed

+30
-24
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
def is_penguin(animal):
22
# Penguins are the only flightless, kneeless sea birds
33
return animal.is_seabird() and (
4-
not animal.can_fly() or not animal.has_visible_knee() # [improve-conditionals]
4+
# +1: [consider-rewriting-conditional]
5+
not animal.can_fly()
6+
or not animal.has_visible_knee()
57
)

doc/user_guide/checkers/extensions.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Code Style checker Messages
9292
Using math.inf or math.nan permits to benefit from typing and it is up to 4
9393
times faster than a float call (after the initial import of math). This check
9494
also catches typos in float calls as a side effect.
95-
:improve-conditionals (R6107): *Rewrite conditional expression to '%s'*
95+
:consider-rewriting-conditional (R6107): *Rewrite conditional expression to '%s'*
9696
Rewrite negated if expressions to improve readability. This style is simpler
9797
and also permits converting long if/elif chains to match case with more ease.
9898
Disabled by default!

doc/user_guide/configuration/all-options.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ Standard Checkers
233233
234234
confidence = ["HIGH", "CONTROL_FLOW", "INFERENCE", "INFERENCE_FAILURE", "UNDEFINED"]
235235
236-
disable = ["bad-inline-option", "consider-using-augmented-assign", "deprecated-pragma", "file-ignored", "improve-conditionals", "locally-disabled", "prefer-typing-namedtuple", "raw-checker-failed", "suppressed-message", "use-implicit-booleaness-not-comparison-to-string", "use-implicit-booleaness-not-comparison-to-zero", "use-symbolic-message-instead", "useless-suppression"]
236+
disable = ["bad-inline-option", "consider-rewriting-conditional", "consider-using-augmented-assign", "deprecated-pragma", "file-ignored", "locally-disabled", "prefer-typing-namedtuple", "raw-checker-failed", "suppressed-message", "use-implicit-booleaness-not-comparison-to-string", "use-implicit-booleaness-not-comparison-to-zero", "use-symbolic-message-instead", "useless-suppression"]
237237
238238
enable = []
239239

doc/user_guide/messages/messages_overview.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ All messages in the refactor category:
494494
refactor/consider-math-not-float
495495
refactor/consider-merging-isinstance
496496
refactor/consider-refactoring-into-while-condition
497+
refactor/consider-rewriting-conditional
497498
refactor/consider-swap-variables
498499
refactor/consider-using-alias
499500
refactor/consider-using-assignment-expr
@@ -516,7 +517,6 @@ All messages in the refactor category:
516517
refactor/duplicate-code
517518
refactor/else-if-used
518519
refactor/empty-comment
519-
refactor/improve-conditionals
520520
refactor/inconsistent-return-statements
521521
refactor/literal-comparison
522522
refactor/magic-value-comparison
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
Add :ref:`improve-conditionals` check to the Code Style extension.
1+
Add ``consider-rewriting-conditional`` check to the Code Style extension to suggest
2+
``not (x and y)`` instead of ``not x or not y`` in order to facilitate the detection
3+
of match case refactors. The code style extension must be enabled and
4+
``consider-rewriting-conditional`` itself needs to be explicitly enabled.
25

36
Refs #10600

pylint/extensions/code_style.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class CodeStyleChecker(BaseChecker):
9292
),
9393
"R6107": (
9494
"Rewrite conditional expression to '%s'",
95-
"improve-conditionals",
95+
"consider-rewriting-conditional",
9696
"Rewrite negated if expressions to improve readability. This style is simpler "
9797
"and also permits converting long if/elif chains to match case with more ease.\n"
9898
"Disabled by default!",
@@ -424,7 +424,7 @@ def _invert_node(node: nodes.NodeNG) -> nodes.NodeNG:
424424
case _: # pragma: no cover
425425
raise AssertionError
426426

427-
@only_required_for_messages("improve-conditionals")
427+
@only_required_for_messages("consider-rewriting-conditional")
428428
def visit_boolop(self, node: nodes.BoolOp) -> None:
429429
if (
430430
node.op == "or"
@@ -451,7 +451,7 @@ def visit_boolop(self, node: nodes.BoolOp) -> None:
451451
new_node.postinit(operand=new_boolop)
452452

453453
self.add_message(
454-
"improve-conditionals",
454+
"consider-rewriting-conditional",
455455
node=target_node,
456456
args=(new_node.as_string(),),
457457
confidence=HIGH,

pylintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ clear-cache-post-run=no
8181
# multiple time (only on the command line, not in the configuration file where
8282
# it should appear only once). See also the "--disable" option for examples.
8383
enable=
84-
improve-conditionals,
84+
consider-rewriting-conditional,
8585
use-symbolic-message-instead,
8686
useless-suppression,
8787

tests/functional/ext/code_style/cs_improve_conditionals.py renamed to tests/functional/ext/code_style/cs_consider_rewriting_conditional.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,18 @@ def f1(expr, node_cls, x, y, z):
2424
elif not isinstance(expr, node_cls) or x is not None:
2525
...
2626

27-
if not isinstance(expr, node_cls) or expr.attrname != "__init__": # [improve-conditionals]
27+
# +1: [consider-rewriting-conditional]
28+
if not isinstance(expr, node_cls) or expr.attrname != "__init__":
2829
...
29-
elif not x or y not in z: # [improve-conditionals]
30+
elif not x or y not in z: # [consider-rewriting-conditional]
3031
...
31-
elif not x or y is not z: # [improve-conditionals]
32+
elif not x or y is not z: # [consider-rewriting-conditional]
3233
...
33-
elif not (not x or not y): # [improve-conditionals]
34+
elif not (not x or not y): # [consider-rewriting-conditional]
3435
...
35-
elif x and (not y or not z): # [improve-conditionals]
36+
elif x and (not y or not z): # [consider-rewriting-conditional]
3637
...
37-
elif not x or y < 0 or z <= 0: # [improve-conditionals]
38+
elif not x or y < 0 or z <= 0: # [consider-rewriting-conditional]
3839
...
39-
elif not x or y > 0 or z >= 0: # [improve-conditionals]
40+
elif not x or y > 0 or z >= 0: # [consider-rewriting-conditional]
4041
...

0 commit comments

Comments
 (0)