Skip to content

Commit

Permalink
Enable usage of custom pylintrc file for message documentation tests (#…
Browse files Browse the repository at this point in the history
…6131)

Demonstrate it with optional checker message as example.

Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
  • Loading branch information
DudeNr33 and Pierre-Sassoulas committed Apr 2, 2022
1 parent be43fff commit 236313a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
6 changes: 6 additions & 0 deletions doc/data/messages/c/confusing-consecutive-elif/bad.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def myfunc(shall_continue: bool, shall_exit: bool):
if shall_continue:
if input("Are you sure?") == "y":
print("Moving on.")
elif shall_exit: # [confusing-consecutive-elif]
print("Exiting.")
1 change: 1 addition & 0 deletions doc/data/messages/c/confusing-consecutive-elif/details.rst
Original file line number Diff line number Diff line change
@@ -0,0 +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.
22 changes: 22 additions & 0 deletions doc/data/messages/c/confusing-consecutive-elif/good.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Option 1: add explicit 'else'
def myfunc(shall_continue: bool, shall_exit: bool):
if shall_continue:
if input("Are you sure?") == "y":
print("Moving on.")
else:
pass
elif shall_exit:
print("Exiting.")


# Option 2: extract function
def user_confirmation():
if input("Are you sure?") == "y":
print("Moving on.")


def myfunc2(shall_continue: bool, shall_exit: bool):
if shall_continue:
user_confirmation()
elif shall_exit:
print("Exiting.")
2 changes: 2 additions & 0 deletions doc/data/messages/c/confusing-consecutive-elif/pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[MASTER]
load-plugins=pylint.extensions.confusing_elif
10 changes: 8 additions & 2 deletions doc/test_messages_documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from collections import Counter
from pathlib import Path
from typing import Counter as CounterType
from typing import List, TextIO, Tuple
from typing import List, Optional, TextIO, Tuple

import pytest

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

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

_config_initialization(
self._linter,
Expand Down

0 comments on commit 236313a

Please sign in to comment.