From d5dec5c7053c855452091f94b555c7d942c0d77e Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Mon, 22 Nov 2021 17:59:33 +0100 Subject: 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. --- pylint/extensions/check_elif.py | 3 +++ tests/functional/ext/check_elif/check_elif.py | 15 +++++++++++++++ tests/functional/ext/check_elif/check_elif.txt | 1 + 3 files changed, 19 insertions(+) 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 -- cgit v1.2.1