diff options
author | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2021-11-22 17:59:33 +0100 |
---|---|---|
committer | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2021-11-23 09:55:06 +0100 |
commit | d5dec5c7053c855452091f94b555c7d942c0d77e (patch) | |
tree | aff907f0497c0753a6477dd83737c9c90de067f2 | |
parent | a80d00be4c8310ade70413dabddfda50822e81ba (diff) | |
download | pylint-git-d5dec5c7053c855452091f94b555c7d942c0d77e.tar.gz |
Add a test case that make the hot fix fail
We do not remove the hotfix in order to have a false positive
instead of a crash is something else is still buggy.
-rw-r--r-- | pylint/extensions/check_elif.py | 3 | ||||
-rw-r--r-- | tests/functional/ext/check_elif/check_elif.py | 15 | ||||
-rw-r--r-- | tests/functional/ext/check_elif/check_elif.txt | 1 |
3 files changed, 19 insertions, 0 deletions
diff --git a/pylint/extensions/check_elif.py b/pylint/extensions/check_elif.py index 76837a8ef..3b80a82a7 100644 --- a/pylint/extensions/check_elif.py +++ b/pylint/extensions/check_elif.py @@ -59,6 +59,7 @@ class ElseifUsedChecker(BaseTokenChecker): def visit_comprehension(self, node: nodes.Comprehension) -> None: self._if_counter += len(node.ifs) + self._elifs.insert(self._if_counter, True) @check_messages("else-if-used") def visit_if(self, node: nodes.If) -> None: @@ -67,6 +68,8 @@ class ElseifUsedChecker(BaseTokenChecker): # current if node must directly follow an "else" if orelse and orelse == [node]: if ( + # Safeguard if we missed an if in self._elifs + # See https://github.com/PyCQA/pylint/pull/5369 self._if_counter < len(self._elifs) and not self._elifs[self._if_counter] ): diff --git a/tests/functional/ext/check_elif/check_elif.py b/tests/functional/ext/check_elif/check_elif.py index 23207ae8a..f14df1c75 100644 --- a/tests/functional/ext/check_elif/check_elif.py +++ b/tests/functional/ext/check_elif/check_elif.py @@ -37,3 +37,18 @@ def _if_in_fstring_comprehension(): ) elif "i" in "true": raise TypeError("d") + + +def _if_in_fstring_comprehension_with_elif(): + order = {} + if "z" not in "false": + raise TypeError( + f" {', '.join(sorted(i for i in order or () if i not in vars))}" + ) + elif "z" not in "true": + pass + else: + if "t" not in "false": # [else-if-used] + raise TypeError("d") + else: + print("e") diff --git a/tests/functional/ext/check_elif/check_elif.txt b/tests/functional/ext/check_elif/check_elif.txt index 6c72f56f0..5aa2eafa5 100644 --- a/tests/functional/ext/check_elif/check_elif.txt +++ b/tests/functional/ext/check_elif/check_elif.txt @@ -1,2 +1,3 @@ else-if-used:12:8:my_function:"Consider using ""elif"" instead of ""else if""":HIGH else-if-used:24:20:my_function:"Consider using ""elif"" instead of ""else if""":HIGH +else-if-used:50:8:_if_in_fstring_comprehension_with_elif:"Consider using ""elif"" instead of ""else if""":HIGH |