Skip to content

Commit d5dec5c

Browse files
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.
1 parent a80d00b commit d5dec5c

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

pylint/extensions/check_elif.py

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def visit_ifexp(self, node: nodes.IfExp) -> None:
5959

6060
def visit_comprehension(self, node: nodes.Comprehension) -> None:
6161
self._if_counter += len(node.ifs)
62+
self._elifs.insert(self._if_counter, True)
6263

6364
@check_messages("else-if-used")
6465
def visit_if(self, node: nodes.If) -> None:
@@ -67,6 +68,8 @@ def visit_if(self, node: nodes.If) -> None:
6768
# current if node must directly follow an "else"
6869
if orelse and orelse == [node]:
6970
if (
71+
# Safeguard if we missed an if in self._elifs
72+
# See https://github.com/PyCQA/pylint/pull/5369
7073
self._if_counter < len(self._elifs)
7174
and not self._elifs[self._if_counter]
7275
):

tests/functional/ext/check_elif/check_elif.py

+15
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,18 @@ def _if_in_fstring_comprehension():
3737
)
3838
elif "i" in "true":
3939
raise TypeError("d")
40+
41+
42+
def _if_in_fstring_comprehension_with_elif():
43+
order = {}
44+
if "z" not in "false":
45+
raise TypeError(
46+
f" {', '.join(sorted(i for i in order or () if i not in vars))}"
47+
)
48+
elif "z" not in "true":
49+
pass
50+
else:
51+
if "t" not in "false": # [else-if-used]
52+
raise TypeError("d")
53+
else:
54+
print("e")
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
else-if-used:12:8:my_function:"Consider using ""elif"" instead of ""else if""":HIGH
22
else-if-used:24:20:my_function:"Consider using ""elif"" instead of ""else if""":HIGH
3+
else-if-used:50:8:_if_in_fstring_comprehension_with_elif:"Consider using ""elif"" instead of ""else if""":HIGH

0 commit comments

Comments
 (0)