From 173c5d331b3c1a2f5f43c44e8d0df93f9174434a Mon Sep 17 00:00:00 2001 From: DudeNr33 <3929834+DudeNr33@users.noreply.github.com> Date: Sat, 2 Apr 2022 13:23:23 +0200 Subject: [PATCH 1/3] Enable usage of custom pylintrc file for message documentation tests, and demonstrate it with optional checker message as example. --- doc/data/messages/c/confusing-consecutive-elif/bad.py | 6 ++++++ .../messages/c/confusing-consecutive-elif/details.rst | 2 ++ doc/data/messages/c/confusing-consecutive-elif/good.py | 8 ++++++++ .../messages/c/confusing-consecutive-elif/pylintrc | 2 ++ doc/test_messages_documentation.py | 10 ++++++++-- 5 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 doc/data/messages/c/confusing-consecutive-elif/bad.py create mode 100644 doc/data/messages/c/confusing-consecutive-elif/details.rst create mode 100644 doc/data/messages/c/confusing-consecutive-elif/good.py create mode 100644 doc/data/messages/c/confusing-consecutive-elif/pylintrc diff --git a/doc/data/messages/c/confusing-consecutive-elif/bad.py b/doc/data/messages/c/confusing-consecutive-elif/bad.py new file mode 100644 index 0000000000..93e1e2dee8 --- /dev/null +++ b/doc/data/messages/c/confusing-consecutive-elif/bad.py @@ -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.") diff --git a/doc/data/messages/c/confusing-consecutive-elif/details.rst b/doc/data/messages/c/confusing-consecutive-elif/details.rst new file mode 100644 index 0000000000..400f0c0f2c --- /dev/null +++ b/doc/data/messages/c/confusing-consecutive-elif/details.rst @@ -0,0 +1,2 @@ +Following an indentend ``if`` block directly with an ``elif`` of lower indentation can lead to confusion whether the unindent is deliberate or accidental. +Adding an explicit ``else`` in the indented ``if`` statement, even if it only contains a ``pass`` statement, can help clarify the code. diff --git a/doc/data/messages/c/confusing-consecutive-elif/good.py b/doc/data/messages/c/confusing-consecutive-elif/good.py new file mode 100644 index 0000000000..abd4c49f21 --- /dev/null +++ b/doc/data/messages/c/confusing-consecutive-elif/good.py @@ -0,0 +1,8 @@ +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.") diff --git a/doc/data/messages/c/confusing-consecutive-elif/pylintrc b/doc/data/messages/c/confusing-consecutive-elif/pylintrc new file mode 100644 index 0000000000..6a11b2c099 --- /dev/null +++ b/doc/data/messages/c/confusing-consecutive-elif/pylintrc @@ -0,0 +1,2 @@ +[MASTER] +load-plugins=pylint.extensions.confusing_elif diff --git a/doc/test_messages_documentation.py b/doc/test_messages_documentation.py index e131d318ba..f80542f07d 100644 --- a/doc/test_messages_documentation.py +++ b/doc/test_messages_documentation.py @@ -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 @@ -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, From 92fea5c660fdb56f52ea7b3934db1be3f0447b07 Mon Sep 17 00:00:00 2001 From: DudeNr33 <3929834+DudeNr33@users.noreply.github.com> Date: Sat, 2 Apr 2022 13:32:51 +0200 Subject: [PATCH 2/3] Fix typo --- doc/data/messages/c/confusing-consecutive-elif/details.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/data/messages/c/confusing-consecutive-elif/details.rst b/doc/data/messages/c/confusing-consecutive-elif/details.rst index 400f0c0f2c..3f508bcd49 100644 --- a/doc/data/messages/c/confusing-consecutive-elif/details.rst +++ b/doc/data/messages/c/confusing-consecutive-elif/details.rst @@ -1,2 +1,2 @@ -Following an indentend ``if`` block directly with an ``elif`` of lower indentation can lead to confusion whether the unindent is deliberate or accidental. +Following an indented ``if`` block directly with an ``elif`` of lower indentation can lead to confusion whether the unindent is deliberate or accidental. Adding an explicit ``else`` in the indented ``if`` statement, even if it only contains a ``pass`` statement, can help clarify the code. From e6023e224dfe720dd52a336ff6d6449fc2c44751 Mon Sep 17 00:00:00 2001 From: DudeNr33 <3929834+DudeNr33@users.noreply.github.com> Date: Sat, 2 Apr 2022 15:13:35 +0200 Subject: [PATCH 3/3] Add alternative for good code --- .../c/confusing-consecutive-elif/details.rst | 3 +-- .../messages/c/confusing-consecutive-elif/good.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/doc/data/messages/c/confusing-consecutive-elif/details.rst b/doc/data/messages/c/confusing-consecutive-elif/details.rst index 3f508bcd49..bd2ecc4ee5 100644 --- a/doc/data/messages/c/confusing-consecutive-elif/details.rst +++ b/doc/data/messages/c/confusing-consecutive-elif/details.rst @@ -1,2 +1 @@ -Following an indented ``if`` block directly with an ``elif`` of lower indentation can lead to confusion whether the unindent is deliberate or accidental. -Adding an explicit ``else`` in the indented ``if`` statement, even if it only contains a ``pass`` statement, can help clarify the code. +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. diff --git a/doc/data/messages/c/confusing-consecutive-elif/good.py b/doc/data/messages/c/confusing-consecutive-elif/good.py index abd4c49f21..1722a6bfa6 100644 --- a/doc/data/messages/c/confusing-consecutive-elif/good.py +++ b/doc/data/messages/c/confusing-consecutive-elif/good.py @@ -1,3 +1,4 @@ +# Option 1: add explicit 'else' def myfunc(shall_continue: bool, shall_exit: bool): if shall_continue: if input("Are you sure?") == "y": @@ -6,3 +7,16 @@ def myfunc(shall_continue: bool, shall_exit: bool): 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.")