Skip to content

Commit b303906

Browse files
committed
Also find the elif keyword
1 parent eb1ba3b commit b303906

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

astroid/nodes/node_classes.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3035,10 +3035,10 @@ def __init__(
30353035
"""Whether the if-statement is the orelse-block of another if statement."""
30363036

30373037
self.orelse_lineno: Optional[int] = None
3038-
"""The line number of the ``else`` keyword."""
3038+
"""The line number of the ``else`` or ``elif`` keyword."""
30393039

30403040
self.orelse_col_offset: Optional[int] = None
3041-
"""The column offset of the ``else`` keyword."""
3041+
"""The column offset of the ``else`` or ``elif`` keyword."""
30423042

30433043
super().__init__(
30443044
lineno=lineno,
@@ -3065,9 +3065,9 @@ def postinit(
30653065
30663066
:param orelse: The contents of the ``else`` block.
30673067
3068-
:param orelse_lineno: The line number of the ``else`` keyword.
3068+
:param orelse_lineno: The line number of the ``else`` or ``elif`` keyword.
30693069
3070-
:param orelse_lineno: The column offset of the ``else`` keyword.
3070+
:param orelse_lineno: The column offset of the ``else`` or ``elif`` keyword.
30713071
"""
30723072
self.test = test
30733073
if body is not None:

astroid/rebuilder.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
order to get a single Astroid representation
77
"""
88

9+
import ast
910
import sys
1011
import token
1112
import tokenize
1213
from io import StringIO
1314
from tokenize import TokenInfo, generate_tokens
1415
from typing import (
15-
TYPE_CHECKING,
1616
Callable,
1717
Dict,
1818
Generator,
@@ -39,9 +39,6 @@
3939
else:
4040
from typing_extensions import Final
4141

42-
if TYPE_CHECKING:
43-
import ast
44-
4542

4643
REDIRECT: Final[Dict[str, str]] = {
4744
"arguments": "Arguments",
@@ -1382,11 +1379,17 @@ def visit_global(self, node: "ast.Global", parent: NodeNG) -> nodes.Global:
13821379
self._global_names[-1].setdefault(name, []).append(newnode)
13831380
return newnode
13841381

1385-
def _find_else_keyword(self, node: "ast.If") -> Tuple[Optional[int], Optional[int]]:
1386-
"""Get the line number and column offset of the `else` keyword."""
1382+
def _find_orelse_keyword(
1383+
self, node: "ast.If"
1384+
) -> Tuple[Optional[int], Optional[int]]:
1385+
"""Get the line number and column offset of the `else` or `elif` keyword."""
13871386
if not self._data or not node.orelse:
13881387
return None, None
13891388

1389+
# If the first child in orelse is an If node the orelse is an elif block
1390+
if isinstance(node.orelse[0], ast.If):
1391+
return node.orelse[0].lineno, node.orelse[0].col_offset
1392+
13901393
end_lineno = node.orelse[0].lineno - 1
13911394

13921395
# pylint: disable-next=unsubscriptable-object
@@ -1407,7 +1410,7 @@ def visit_if(self, node: "ast.If", parent: NodeNG) -> nodes.If:
14071410
parent=parent,
14081411
)
14091412

1410-
orelse_lineno, orelse_col_offset = self._find_else_keyword(node)
1413+
orelse_lineno, orelse_col_offset = self._find_orelse_keyword(node)
14111414

14121415
newnode.postinit(
14131416
self.visit(node.test, newnode),

tests/unittest_nodes.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,12 @@ def test_orelse_line_numbering(self) -> None:
349349
assert self.astroid.body[0].orelse_col_offset is None
350350
assert self.astroid.body[1].orelse_lineno == 7
351351
assert self.astroid.body[1].orelse_col_offset == 0
352-
assert self.astroid.body[2].orelse_lineno is None
353-
assert self.astroid.body[2].orelse_col_offset is None
352+
assert self.astroid.body[2].orelse_lineno == 12
353+
assert self.astroid.body[2].orelse_col_offset == 0
354+
assert self.astroid.body[3].orelse_lineno == 17
355+
assert self.astroid.body[3].orelse_col_offset == 0
356+
assert self.astroid.body[3].orelse[0].orelse_lineno == 19
357+
assert self.astroid.body[3].orelse[0].orelse_col_offset == 0
354358
assert self.astroid.body[3].orelse[0].orelse[0].orelse_lineno == 21
355359
assert self.astroid.body[3].orelse[0].orelse[0].orelse_col_offset == 0
356360

0 commit comments

Comments
 (0)