Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable usage of custom pylintrc file for message documentation tests #6131

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.")
DanielNoord marked this conversation as resolved.
Show resolved Hide resolved


# 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]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DanielNoord I think we talked about this already before but what do you think about using FunctionalTestFile instead here ? At some point we will encounter the same issue than for functional tests (i.e. handling min_version, max_version, some options...). We could do test_file.option_file instead of recreating the logic a little differently.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was actually my initial approach, but the problem is that FunctionalTestFile assumes the config file to have the same name as the .py file.
There is quite a good portion of code duplication also in the LintModuleTest class here.

Besides the different folder structure, the tight coupling between the "original" LintModuleTest and test_functional.py is another problem.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I advise against trying to use LintModuleTest here. The idea is similar but because of the different folder structures it doesn't really work.

It's similar to your Duck vs. PlasticDuck example @Pierre-Sassoulas πŸ˜„

DanielNoord marked this conversation as resolved.
Show resolved Hide resolved
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