Skip to content

Commit 31113e9

Browse files
Add regression tests for if in fstring comprehension
1 parent 8c6eb9c commit 31113e9

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

pylint/extensions/check_elif.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ def visit_if(self, node: nodes.If) -> None:
6666
orelse = node.parent.orelse
6767
# current if node must directly follow an "else"
6868
if orelse and orelse == [node]:
69-
if not self._elifs[self._if_counter]:
69+
if (
70+
self._if_counter < len(self._elifs)
71+
and not self._elifs[self._if_counter]
72+
):
7073
self.add_message("else-if-used", node=node)
7174
self._if_counter += 1
7275

tests/functional/e/elif_checker.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# pylint: disable=no-else-raise,unsupported-membership-test,using-constant-test
2+
13
"""Checks use of "else if" triggers a refactor message"""
24

35

@@ -7,7 +9,7 @@ def my_function():
79
if myint > 5:
810
pass
911
else:
10-
if myint <= 5: # [else-if-used]
12+
if myint <= 5: # [else-if-used]
1113
pass
1214
else:
1315
myint = 3
@@ -25,3 +27,13 @@ def my_function():
2527
if myint:
2628
pass
2729
myint = 4
30+
31+
32+
def _if_in_fstring_comprehension():
33+
order = {}
34+
if "z" not in "false":
35+
raise TypeError(
36+
f" {', '.join(sorted(i for i in order or () if i not in vars))}"
37+
)
38+
elif "i" in "true":
39+
raise TypeError("d")

tests/functional/e/elif_checker.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
else-if-used:10:8:my_function:"Consider using ""elif"" instead of ""else if""":HIGH
2-
else-if-used:22:20:my_function:"Consider using ""elif"" instead of ""else if""":HIGH
1+
else-if-used:12:8:my_function:"Consider using ""elif"" instead of ""else if""":HIGH
2+
else-if-used:24:20:my_function:"Consider using ""elif"" instead of ""else if""":HIGH

0 commit comments

Comments
 (0)