Skip to content

Commit 236313a

Browse files
Enable usage of custom pylintrc file for message documentation tests (#6131)
Demonstrate it with optional checker message as example. Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
1 parent be43fff commit 236313a

5 files changed

Lines changed: 39 additions & 2 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def myfunc(shall_continue: bool, shall_exit: bool):
2+
if shall_continue:
3+
if input("Are you sure?") == "y":
4+
print("Moving on.")
5+
elif shall_exit: # [confusing-consecutive-elif]
6+
print("Exiting.")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Creating a function for the nested conditional, or adding an explicit ``else`` in the indented ``if`` statement, even if it only contains a ``pass`` statement, can help clarify the code.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Option 1: add explicit 'else'
2+
def myfunc(shall_continue: bool, shall_exit: bool):
3+
if shall_continue:
4+
if input("Are you sure?") == "y":
5+
print("Moving on.")
6+
else:
7+
pass
8+
elif shall_exit:
9+
print("Exiting.")
10+
11+
12+
# Option 2: extract function
13+
def user_confirmation():
14+
if input("Are you sure?") == "y":
15+
print("Moving on.")
16+
17+
18+
def myfunc2(shall_continue: bool, shall_exit: bool):
19+
if shall_continue:
20+
user_confirmation()
21+
elif shall_exit:
22+
print("Exiting.")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[MASTER]
2+
load-plugins=pylint.extensions.confusing_elif

doc/test_messages_documentation.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from collections import Counter
88
from pathlib import Path
99
from typing import Counter as CounterType
10-
from typing import List, TextIO, Tuple
10+
from typing import List, Optional, TextIO, Tuple
1111

1212
import pytest
1313

@@ -53,7 +53,13 @@ def __init__(self, test_file: Tuple[str, Path]) -> None:
5353
self._linter.config.persistent = 0
5454
checkers.initialize(self._linter)
5555

56-
config_file = next(config.find_default_config_files(), None)
56+
# Check if this message has a custom configuration file (e.g. for enabling optional checkers).
57+
# If not, use the default configuration.
58+
config_file: Optional[Path]
59+
if (test_file[1].parent / "pylintrc").exists():
60+
config_file = test_file[1].parent / "pylintrc"
61+
else:
62+
config_file = next(config.find_default_config_files(), None)
5763

5864
_config_initialization(
5965
self._linter,

0 commit comments

Comments
 (0)