| 1 |
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html |
| 2 |
# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE |
| 3 |
# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt |
| 4 |
|
| 5 |
import textwrap |
| 6 |
|
| 7 |
import astroid |
| 8 |
from astroid import builder, nodes |
| 9 |
from astroid.const import PY312_PLUS |
| 10 |
|
| 11 |
|
| 12 |
class TestLinenoColOffset: |
| 13 |
"""Test 'lineno', 'col_offset', 'end_lineno', and 'end_col_offset' for all |
| 14 |
nodes. |
| 15 |
""" |
| 16 |
|
| 17 |
@staticmethod |
| 18 |
def test_end_lineno_container() -> None: |
| 19 |
"""Container nodes: List, Tuple, Set.""" |
| 20 |
code = textwrap.dedent(""" |
| 21 |
[1, 2, 3] #@ |
| 22 |
[ #@ |
| 23 |
1, 2, 3 |
| 24 |
] |
| 25 |
(1, 2, 3) #@ |
| 26 |
{1, 2, 3} #@ |
| 27 |
""").strip() |
| 28 |
ast_nodes = builder.extract_node(code) |
| 29 |
assert isinstance(ast_nodes, list) and len(ast_nodes) == 4 |
| 30 |
|
| 31 |
c1 = ast_nodes[0] |
| 32 |
assert isinstance(c1, nodes.List) |
| 33 |
assert (c1.lineno, c1.col_offset) == (1, 0) |
| 34 |
assert (c1.end_lineno, c1.end_col_offset) == (1, 9) |
| 35 |
|
| 36 |
c2 = ast_nodes[1] |
| 37 |
assert isinstance(c2, nodes.List) |
| 38 |
assert (c2.lineno, c2.col_offset) == (2, 0) |
| 39 |
assert (c2.end_lineno, c2.end_col_offset) == (4, 1) |
| 40 |
|
| 41 |
c3 = ast_nodes[2] |
| 42 |
assert isinstance(c3, nodes.Tuple) |
| 43 |
assert (c3.lineno, c3.col_offset) == (5, 0) |
| 44 |
assert (c3.end_lineno, c3.end_col_offset) == (5, 9) |
| 45 |
|
| 46 |
c4 = ast_nodes[3] |
| 47 |
assert isinstance(c4, nodes.Set) |
| 48 |
assert (c4.lineno, c4.col_offset) == (6, 0) |
| 49 |
assert (c4.end_lineno, c4.end_col_offset) == (6, 9) |
| 50 |
|
| 51 |
@staticmethod |
| 52 |
def test_end_lineno_name() -> None: |
| 53 |
"""Name, Assign, AssignName, Delete, DelName.""" |
| 54 |
code = textwrap.dedent(""" |
| 55 |
var = 42 #@ |
| 56 |
var #@ |
| 57 |
del var #@ |
| 58 |
|
| 59 |
var2 = ( #@ |
| 60 |
1, 2, 3 |
| 61 |
) |
| 62 |
""").strip() |
| 63 |
ast_nodes = builder.extract_node(code) |
| 64 |
assert isinstance(ast_nodes, list) and len(ast_nodes) == 4 |
| 65 |
|
| 66 |
n1 = ast_nodes[0] |
| 67 |
assert isinstance(n1, nodes.Assign) |
| 68 |
assert isinstance(n1.targets[0], nodes.AssignName) |
| 69 |
assert isinstance(n1.value, nodes.Const) |
| 70 |
assert (n1.lineno, n1.col_offset) == (1, 0) |
| 71 |
assert (n1.end_lineno, n1.end_col_offset) == (1, 8) |
| 72 |
assert (n1.targets[0].lineno, n1.targets[0].col_offset) == (1, 0) |
| 73 |
assert (n1.targets[0].end_lineno, n1.targets[0].end_col_offset) == (1, 3) |
| 74 |
assert (n1.value.lineno, n1.value.col_offset) == (1, 6) |
| 75 |
assert (n1.value.end_lineno, n1.value.end_col_offset) == (1, 8) |
| 76 |
|
| 77 |
n2 = ast_nodes[1] |
| 78 |
assert isinstance(n2, nodes.Name) |
| 79 |
assert (n2.lineno, n2.col_offset) == (2, 0) |
| 80 |
assert (n2.end_lineno, n2.end_col_offset) == (2, 3) |
| 81 |
|
| 82 |
n3 = ast_nodes[2] |
| 83 |
assert isinstance(n3, nodes.Delete) and isinstance(n3.targets[0], nodes.DelName) |
| 84 |
assert (n3.lineno, n3.col_offset) == (3, 0) |
| 85 |
assert (n3.end_lineno, n3.end_col_offset) == (3, 7) |
| 86 |
assert (n3.targets[0].lineno, n3.targets[0].col_offset) == (3, 4) |
| 87 |
assert (n3.targets[0].end_lineno, n3.targets[0].end_col_offset) == (3, 7) |
| 88 |
|
| 89 |
n4 = ast_nodes[3] |
| 90 |
assert isinstance(n4, nodes.Assign) |
| 91 |
assert isinstance(n4.targets[0], nodes.AssignName) |
| 92 |
assert (n4.lineno, n4.col_offset) == (5, 0) |
| 93 |
assert (n4.end_lineno, n4.end_col_offset) == (7, 1) |
| 94 |
assert (n4.targets[0].lineno, n4.targets[0].col_offset) == (5, 0) |
| 95 |
assert (n4.targets[0].end_lineno, n4.targets[0].end_col_offset) == (5, 4) |
| 96 |
|
| 97 |
@staticmethod |
| 98 |
def test_end_lineno_attribute() -> None: |
| 99 |
"""Attribute, AssignAttr, DelAttr.""" |
| 100 |
code = textwrap.dedent(""" |
| 101 |
class X: |
| 102 |
var = 42 |
| 103 |
|
| 104 |
X.var2 = 2 #@ |
| 105 |
X.var2 #@ |
| 106 |
del X.var2 #@ |
| 107 |
""").strip() |
| 108 |
ast_nodes = builder.extract_node(code) |
| 109 |
assert isinstance(ast_nodes, list) and len(ast_nodes) == 3 |
| 110 |
|
| 111 |
a1 = ast_nodes[0] |
| 112 |
assert isinstance(a1, nodes.Assign) |
| 113 |
assert isinstance(a1.targets[0], nodes.AssignAttr) |
| 114 |
assert isinstance(a1.value, nodes.Const) |
| 115 |
assert (a1.lineno, a1.col_offset) == (4, 0) |
| 116 |
assert (a1.end_lineno, a1.end_col_offset) == (4, 10) |
| 117 |
assert (a1.targets[0].lineno, a1.targets[0].col_offset) == (4, 0) |
| 118 |
assert (a1.targets[0].end_lineno, a1.targets[0].end_col_offset) == (4, 6) |
| 119 |
assert (a1.value.lineno, a1.value.col_offset) == (4, 9) |
| 120 |
assert (a1.value.end_lineno, a1.value.end_col_offset) == (4, 10) |
| 121 |
|
| 122 |
a2 = ast_nodes[1] |
| 123 |
assert isinstance(a2, nodes.Attribute) and isinstance(a2.expr, nodes.Name) |
| 124 |
assert (a2.lineno, a2.col_offset) == (5, 0) |
| 125 |
assert (a2.end_lineno, a2.end_col_offset) == (5, 6) |
| 126 |
assert (a2.expr.lineno, a2.expr.col_offset) == (5, 0) |
| 127 |
assert (a2.expr.end_lineno, a2.expr.end_col_offset) == (5, 1) |
| 128 |
|
| 129 |
a3 = ast_nodes[2] |
| 130 |
assert isinstance(a3, nodes.Delete) and isinstance(a3.targets[0], nodes.DelAttr) |
| 131 |
assert (a3.lineno, a3.col_offset) == (6, 0) |
| 132 |
assert (a3.end_lineno, a3.end_col_offset) == (6, 10) |
| 133 |
assert (a3.targets[0].lineno, a3.targets[0].col_offset) == (6, 4) |
| 134 |
assert (a3.targets[0].end_lineno, a3.targets[0].end_col_offset) == (6, 10) |
| 135 |
|
| 136 |
@staticmethod |
| 137 |
def test_end_lineno_call() -> None: |
| 138 |
"""Call, Keyword.""" |
| 139 |
code = textwrap.dedent(""" |
| 140 |
func(arg1, arg2=value) #@ |
| 141 |
""").strip() |
| 142 |
c1 = builder.extract_node(code) |
| 143 |
assert isinstance(c1, nodes.Call) |
| 144 |
assert isinstance(c1.func, nodes.Name) |
| 145 |
assert isinstance(c1.args[0], nodes.Name) |
| 146 |
assert isinstance(c1.keywords[0], nodes.Keyword) |
| 147 |
assert isinstance(c1.keywords[0].value, nodes.Name) |
| 148 |
|
| 149 |
assert (c1.lineno, c1.col_offset) == (1, 0) |
| 150 |
assert (c1.end_lineno, c1.end_col_offset) == (1, 22) |
| 151 |
assert (c1.func.lineno, c1.func.col_offset) == (1, 0) |
| 152 |
assert (c1.func.end_lineno, c1.func.end_col_offset) == (1, 4) |
| 153 |
|
| 154 |
assert (c1.args[0].lineno, c1.args[0].col_offset) == (1, 5) |
| 155 |
assert (c1.args[0].end_lineno, c1.args[0].end_col_offset) == (1, 9) |
| 156 |
|
| 157 |
# fmt: off |
| 158 |
assert (c1.keywords[0].lineno, c1.keywords[0].col_offset) == (1, 11) |
| 159 |
assert (c1.keywords[0].end_lineno, c1.keywords[0].end_col_offset) == (1, 21) |
| 160 |
assert (c1.keywords[0].value.lineno, c1.keywords[0].value.col_offset) == (1, 16) |
| 161 |
assert (c1.keywords[0].value.end_lineno, c1.keywords[0].value.end_col_offset) == (1, 21) |
| 162 |
# fmt: on |
| 163 |
|
| 164 |
@staticmethod |
| 165 |
def test_end_lineno_assignment() -> None: |
| 166 |
"""Assign, AnnAssign, AugAssign.""" |
| 167 |
code = textwrap.dedent(""" |
| 168 |
var = 2 #@ |
| 169 |
var2: int = 2 #@ |
| 170 |
var3 += 2 #@ |
| 171 |
""").strip() |
| 172 |
ast_nodes = builder.extract_node(code) |
| 173 |
assert isinstance(ast_nodes, list) and len(ast_nodes) == 3 |
| 174 |
|
| 175 |
a1 = ast_nodes[0] |
| 176 |
assert isinstance(a1, nodes.Assign) |
| 177 |
assert isinstance(a1.targets[0], nodes.AssignName) |
| 178 |
assert isinstance(a1.value, nodes.Const) |
| 179 |
assert (a1.lineno, a1.col_offset) == (1, 0) |
| 180 |
assert (a1.end_lineno, a1.end_col_offset) == (1, 7) |
| 181 |
assert (a1.targets[0].lineno, a1.targets[0].col_offset) == (1, 0) |
| 182 |
assert (a1.targets[0].end_lineno, a1.targets[0].end_col_offset) == (1, 3) |
| 183 |
assert (a1.value.lineno, a1.value.col_offset) == (1, 6) |
| 184 |
assert (a1.value.end_lineno, a1.value.end_col_offset) == (1, 7) |
| 185 |
|
| 186 |
a2 = ast_nodes[1] |
| 187 |
assert isinstance(a2, nodes.AnnAssign) |
| 188 |
assert isinstance(a2.target, nodes.AssignName) |
| 189 |
assert isinstance(a2.annotation, nodes.Name) |
| 190 |
assert isinstance(a2.value, nodes.Const) |
| 191 |
assert (a2.lineno, a2.col_offset) == (2, 0) |
| 192 |
assert (a2.end_lineno, a2.end_col_offset) == (2, 13) |
| 193 |
assert (a2.target.lineno, a2.target.col_offset) == (2, 0) |
| 194 |
assert (a2.target.end_lineno, a2.target.end_col_offset) == (2, 4) |
| 195 |
assert (a2.annotation.lineno, a2.annotation.col_offset) == (2, 6) |
| 196 |
assert (a2.annotation.end_lineno, a2.annotation.end_col_offset) == (2, 9) |
| 197 |
assert (a2.value.lineno, a2.value.col_offset) == (2, 12) |
| 198 |
assert (a2.value.end_lineno, a2.value.end_col_offset) == (2, 13) |
| 199 |
|
| 200 |
a3 = ast_nodes[2] |
| 201 |
assert isinstance(a3, nodes.AugAssign) |
| 202 |
assert isinstance(a3.target, nodes.AssignName) |
| 203 |
assert isinstance(a3.value, nodes.Const) |
| 204 |
assert (a3.lineno, a3.col_offset) == (3, 0) |
| 205 |
assert (a3.end_lineno, a3.end_col_offset) == (3, 9) |
| 206 |
assert (a3.target.lineno, a3.target.col_offset) == (3, 0) |
| 207 |
assert (a3.target.end_lineno, a3.target.end_col_offset) == (3, 4) |
| 208 |
assert (a3.value.lineno, a3.value.col_offset) == (3, 8) |
| 209 |
assert (a3.value.end_lineno, a3.value.end_col_offset) == (3, 9) |
| 210 |
|
| 211 |
@staticmethod |
| 212 |
def test_end_lineno_mix_stmts() -> None: |
| 213 |
"""Assert, Break, Continue, Global, Nonlocal, Pass, Raise, Return, Expr.""" |
| 214 |
code = textwrap.dedent(""" |
| 215 |
assert True, "Some message" #@ |
| 216 |
break #@ |
| 217 |
continue #@ |
| 218 |
global var #@ |
| 219 |
nonlocal var #@ |
| 220 |
pass #@ |
| 221 |
raise Exception from ex #@ |
| 222 |
return 42 #@ |
| 223 |
var #@ |
| 224 |
""").strip() |
| 225 |
ast_nodes = builder.extract_node(code) |
| 226 |
assert isinstance(ast_nodes, list) and len(ast_nodes) == 9 |
| 227 |
|
| 228 |
s1 = ast_nodes[0] |
| 229 |
assert isinstance(s1, nodes.Assert) |
| 230 |
assert isinstance(s1.test, nodes.Const) |
| 231 |
assert isinstance(s1.fail, nodes.Const) |
| 232 |
assert (s1.lineno, s1.col_offset) == (1, 0) |
| 233 |
assert (s1.end_lineno, s1.end_col_offset) == (1, 27) |
| 234 |
assert (s1.test.lineno, s1.test.col_offset) == (1, 7) |
| 235 |
assert (s1.test.end_lineno, s1.test.end_col_offset) == (1, 11) |
| 236 |
assert (s1.fail.lineno, s1.fail.col_offset) == (1, 13) |
| 237 |
assert (s1.fail.end_lineno, s1.fail.end_col_offset) == (1, 27) |
| 238 |
|
| 239 |
s2 = ast_nodes[1] |
| 240 |
assert isinstance(s2, nodes.Break) |
| 241 |
assert (s2.lineno, s2.col_offset) == (2, 0) |
| 242 |
assert (s2.end_lineno, s2.end_col_offset) == (2, 5) |
| 243 |
|
| 244 |
s3 = ast_nodes[2] |
| 245 |
assert isinstance(s3, nodes.Continue) |
| 246 |
assert (s3.lineno, s3.col_offset) == (3, 0) |
| 247 |
assert (s3.end_lineno, s3.end_col_offset) == (3, 8) |
| 248 |
|
| 249 |
s4 = ast_nodes[3] |
| 250 |
assert isinstance(s4, nodes.Global) |
| 251 |
assert (s4.lineno, s4.col_offset) == (4, 0) |
| 252 |
assert (s4.end_lineno, s4.end_col_offset) == (4, 10) |
| 253 |
|
| 254 |
s5 = ast_nodes[4] |
| 255 |
assert isinstance(s5, nodes.Nonlocal) |
| 256 |
assert (s5.lineno, s5.col_offset) == (5, 0) |
| 257 |
assert (s5.end_lineno, s5.end_col_offset) == (5, 12) |
| 258 |
|
| 259 |
s6 = ast_nodes[5] |
| 260 |
assert isinstance(s6, nodes.Pass) |
| 261 |
assert (s6.lineno, s6.col_offset) == (6, 0) |
| 262 |
assert (s6.end_lineno, s6.end_col_offset) == (6, 4) |
| 263 |
|
| 264 |
s7 = ast_nodes[6] |
| 265 |
assert isinstance(s7, nodes.Raise) |
| 266 |
assert isinstance(s7.exc, nodes.Name) |
| 267 |
assert isinstance(s7.cause, nodes.Name) |
| 268 |
assert (s7.lineno, s7.col_offset) == (7, 0) |
| 269 |
assert (s7.end_lineno, s7.end_col_offset) == (7, 23) |
| 270 |
assert (s7.exc.lineno, s7.exc.col_offset) == (7, 6) |
| 271 |
assert (s7.exc.end_lineno, s7.exc.end_col_offset) == (7, 15) |
| 272 |
assert (s7.cause.lineno, s7.cause.col_offset) == (7, 21) |
| 273 |
assert (s7.cause.end_lineno, s7.cause.end_col_offset) == (7, 23) |
| 274 |
|
| 275 |
s8 = ast_nodes[7] |
| 276 |
assert isinstance(s8, nodes.Return) |
| 277 |
assert isinstance(s8.value, nodes.Const) |
| 278 |
assert (s8.lineno, s8.col_offset) == (8, 0) |
| 279 |
assert (s8.end_lineno, s8.end_col_offset) == (8, 9) |
| 280 |
assert (s8.value.lineno, s8.value.col_offset) == (8, 7) |
| 281 |
assert (s8.value.end_lineno, s8.value.end_col_offset) == (8, 9) |
| 282 |
|
| 283 |
s9 = ast_nodes[8].parent |
| 284 |
assert isinstance(s9, nodes.Expr) |
| 285 |
assert isinstance(s9.value, nodes.Name) |
| 286 |
assert (s9.lineno, s9.col_offset) == (9, 0) |
| 287 |
assert (s9.end_lineno, s9.end_col_offset) == (9, 3) |
| 288 |
assert (s9.value.lineno, s9.value.col_offset) == (9, 0) |
| 289 |
assert (s9.value.end_lineno, s9.value.end_col_offset) == (9, 3) |
| 290 |
|
| 291 |
@staticmethod |
| 292 |
def test_end_lineno_mix_nodes() -> None: |
| 293 |
"""Await, Starred, Yield, YieldFrom.""" |
| 294 |
code = textwrap.dedent(""" |
| 295 |
await func #@ |
| 296 |
*args #@ |
| 297 |
yield 42 #@ |
| 298 |
yield from (1, 2) #@ |
| 299 |
""").strip() |
| 300 |
ast_nodes = builder.extract_node(code) |
| 301 |
assert isinstance(ast_nodes, list) and len(ast_nodes) == 4 |
| 302 |
|
| 303 |
n1 = ast_nodes[0] |
| 304 |
assert isinstance(n1, nodes.Await) |
| 305 |
assert isinstance(n1.value, nodes.Name) |
| 306 |
assert (n1.lineno, n1.col_offset) == (1, 0) |
| 307 |
assert (n1.end_lineno, n1.end_col_offset) == (1, 10) |
| 308 |
assert (n1.value.lineno, n1.value.col_offset) == (1, 6) |
| 309 |
assert (n1.value.end_lineno, n1.value.end_col_offset) == (1, 10) |
| 310 |
|
| 311 |
n2 = ast_nodes[1] |
| 312 |
assert isinstance(n2, nodes.Starred) |
| 313 |
assert isinstance(n2.value, nodes.Name) |
| 314 |
assert (n2.lineno, n2.col_offset) == (2, 0) |
| 315 |
assert (n2.end_lineno, n2.end_col_offset) == (2, 5) |
| 316 |
assert (n2.value.lineno, n2.value.col_offset) == (2, 1) |
| 317 |
assert (n2.value.end_lineno, n2.value.end_col_offset) == (2, 5) |
| 318 |
|
| 319 |
n3 = ast_nodes[2] |
| 320 |
assert isinstance(n3, nodes.Yield) |
| 321 |
assert isinstance(n3.value, nodes.Const) |
| 322 |
assert (n3.lineno, n3.col_offset) == (3, 0) |
| 323 |
assert (n3.end_lineno, n3.end_col_offset) == (3, 8) |
| 324 |
assert (n3.value.lineno, n3.value.col_offset) == (3, 6) |
| 325 |
assert (n3.value.end_lineno, n3.value.end_col_offset) == (3, 8) |
| 326 |
|
| 327 |
n4 = ast_nodes[3] |
| 328 |
assert isinstance(n4, nodes.YieldFrom) |
| 329 |
assert isinstance(n4.value, nodes.Tuple) |
| 330 |
assert (n4.lineno, n4.col_offset) == (4, 0) |
| 331 |
assert (n4.end_lineno, n4.end_col_offset) == (4, 17) |
| 332 |
assert (n4.value.lineno, n4.value.col_offset) == (4, 11) |
| 333 |
assert (n4.value.end_lineno, n4.value.end_col_offset) == (4, 17) |
| 334 |
|
| 335 |
@staticmethod |
| 336 |
def test_end_lineno_ops() -> None: |
| 337 |
"""BinOp, BoolOp, UnaryOp, Compare.""" |
| 338 |
code = textwrap.dedent(""" |
| 339 |
x + y #@ |
| 340 |
a and b #@ |
| 341 |
-var #@ |
| 342 |
a < b #@ |
| 343 |
""").strip() |
| 344 |
ast_nodes = builder.extract_node(code) |
| 345 |
assert isinstance(ast_nodes, list) and len(ast_nodes) == 4 |
| 346 |
|
| 347 |
o1 = ast_nodes[0] |
| 348 |
assert isinstance(o1, nodes.BinOp) |
| 349 |
assert isinstance(o1.left, nodes.Name) |
| 350 |
assert isinstance(o1.right, nodes.Name) |
| 351 |
assert (o1.lineno, o1.col_offset) == (1, 0) |
| 352 |
assert (o1.end_lineno, o1.end_col_offset) == (1, 5) |
| 353 |
assert (o1.left.lineno, o1.left.col_offset) == (1, 0) |
| 354 |
assert (o1.left.end_lineno, o1.left.end_col_offset) == (1, 1) |
| 355 |
assert (o1.right.lineno, o1.right.col_offset) == (1, 4) |
| 356 |
assert (o1.right.end_lineno, o1.right.end_col_offset) == (1, 5) |
| 357 |
|
| 358 |
o2 = ast_nodes[1] |
| 359 |
assert isinstance(o2, nodes.BoolOp) |
| 360 |
assert isinstance(o2.values[0], nodes.Name) |
| 361 |
assert isinstance(o2.values[1], nodes.Name) |
| 362 |
assert (o2.lineno, o2.col_offset) == (2, 0) |
| 363 |
assert (o2.end_lineno, o2.end_col_offset) == (2, 7) |
| 364 |
assert (o2.values[0].lineno, o2.values[0].col_offset) == (2, 0) |
| 365 |
assert (o2.values[0].end_lineno, o2.values[0].end_col_offset) == (2, 1) |
| 366 |
assert (o2.values[1].lineno, o2.values[1].col_offset) == (2, 6) |
| 367 |
assert (o2.values[1].end_lineno, o2.values[1].end_col_offset) == (2, 7) |
| 368 |
|
| 369 |
o3 = ast_nodes[2] |
| 370 |
assert isinstance(o3, nodes.UnaryOp) |
| 371 |
assert isinstance(o3.operand, nodes.Name) |
| 372 |
assert (o3.lineno, o3.col_offset) == (3, 0) |
| 373 |
assert (o3.end_lineno, o3.end_col_offset) == (3, 4) |
| 374 |
assert (o3.operand.lineno, o3.operand.col_offset) == (3, 1) |
| 375 |
assert (o3.operand.end_lineno, o3.operand.end_col_offset) == (3, 4) |
| 376 |
|
| 377 |
o4 = ast_nodes[3] |
| 378 |
assert isinstance(o4, nodes.Compare) |
| 379 |
assert isinstance(o4.left, nodes.Name) |
| 380 |
assert isinstance(o4.ops[0][1], nodes.Name) |
| 381 |
assert (o4.lineno, o4.col_offset) == (4, 0) |
| 382 |
assert (o4.end_lineno, o4.end_col_offset) == (4, 5) |
| 383 |
assert (o4.left.lineno, o4.left.col_offset) == (4, 0) |
| 384 |
assert (o4.left.end_lineno, o4.left.end_col_offset) == (4, 1) |
| 385 |
assert (o4.ops[0][1].lineno, o4.ops[0][1].col_offset) == (4, 4) |
| 386 |
assert (o4.ops[0][1].end_lineno, o4.ops[0][1].end_col_offset) == (4, 5) |
| 387 |
|
| 388 |
@staticmethod |
| 389 |
def test_end_lineno_if() -> None: |
| 390 |
"""If, IfExp, NamedExpr.""" |
| 391 |
code = textwrap.dedent(""" |
| 392 |
if ( #@ |
| 393 |
var := 2 #@ |
| 394 |
): |
| 395 |
pass |
| 396 |
else: |
| 397 |
pass |
| 398 |
|
| 399 |
2 if True else 1 #@ |
| 400 |
""").strip() |
| 401 |
ast_nodes = builder.extract_node(code) |
| 402 |
assert isinstance(ast_nodes, list) and len(ast_nodes) == 3 |
| 403 |
|
| 404 |
i1 = ast_nodes[0] |
| 405 |
assert isinstance(i1, nodes.If) |
| 406 |
assert isinstance(i1.test, nodes.NamedExpr) |
| 407 |
assert isinstance(i1.body[0], nodes.Pass) |
| 408 |
assert isinstance(i1.orelse[0], nodes.Pass) |
| 409 |
assert (i1.lineno, i1.col_offset) == (1, 0) |
| 410 |
assert (i1.end_lineno, i1.end_col_offset) == (6, 8) |
| 411 |
assert (i1.test.lineno, i1.test.col_offset) == (2, 4) |
| 412 |
assert (i1.test.end_lineno, i1.test.end_col_offset) == (2, 12) |
| 413 |
assert (i1.body[0].lineno, i1.body[0].col_offset) == (4, 4) |
| 414 |
assert (i1.body[0].end_lineno, i1.body[0].end_col_offset) == (4, 8) |
| 415 |
assert (i1.orelse[0].lineno, i1.orelse[0].col_offset) == (6, 4) |
| 416 |
assert (i1.orelse[0].end_lineno, i1.orelse[0].end_col_offset) == (6, 8) |
| 417 |
|
| 418 |
i2 = ast_nodes[1] |
| 419 |
assert isinstance(i2, nodes.NamedExpr) |
| 420 |
assert isinstance(i2.target, nodes.AssignName) |
| 421 |
assert isinstance(i2.value, nodes.Const) |
| 422 |
assert (i2.lineno, i2.col_offset) == (2, 4) |
| 423 |
assert (i2.end_lineno, i2.end_col_offset) == (2, 12) |
| 424 |
assert (i2.target.lineno, i2.target.col_offset) == (2, 4) |
| 425 |
assert (i2.target.end_lineno, i2.target.end_col_offset) == (2, 7) |
| 426 |
assert (i2.value.lineno, i2.value.col_offset) == (2, 11) |
| 427 |
assert (i2.value.end_lineno, i2.value.end_col_offset) == (2, 12) |
| 428 |
|
| 429 |
i3 = ast_nodes[2] |
| 430 |
assert isinstance(i3, nodes.IfExp) |
| 431 |
assert isinstance(i3.test, nodes.Const) |
| 432 |
assert isinstance(i3.body, nodes.Const) |
| 433 |
assert isinstance(i3.orelse, nodes.Const) |
| 434 |
assert (i3.lineno, i3.col_offset) == (8, 0) |
| 435 |
assert (i3.end_lineno, i3.end_col_offset) == (8, 16) |
| 436 |
assert (i3.test.lineno, i3.test.col_offset) == (8, 5) |
| 437 |
assert (i3.test.end_lineno, i3.test.end_col_offset) == (8, 9) |
| 438 |
assert (i3.body.lineno, i3.body.col_offset) == (8, 0) |
| 439 |
assert (i3.body.end_lineno, i3.body.end_col_offset) == (8, 1) |
| 440 |
assert (i3.orelse.lineno, i3.orelse.col_offset) == (8, 15) |
| 441 |
assert (i3.orelse.end_lineno, i3.orelse.end_col_offset) == (8, 16) |
| 442 |
|
| 443 |
@staticmethod |
| 444 |
def test_end_lineno_for() -> None: |
| 445 |
"""For, AsyncFor.""" |
| 446 |
code = textwrap.dedent(""" |
| 447 |
for i in lst: #@ |
| 448 |
pass |
| 449 |
else: |
| 450 |
pass |
| 451 |
|
| 452 |
async for i in lst: #@ |
| 453 |
pass |
| 454 |
""").strip() |
| 455 |
ast_nodes = builder.extract_node(code) |
| 456 |
assert isinstance(ast_nodes, list) and len(ast_nodes) == 2 |
| 457 |
|
| 458 |
f1 = ast_nodes[0] |
| 459 |
assert isinstance(f1, nodes.For) |
| 460 |
assert isinstance(f1.target, nodes.AssignName) |
| 461 |
assert isinstance(f1.iter, nodes.Name) |
| 462 |
assert isinstance(f1.body[0], nodes.Pass) |
| 463 |
assert isinstance(f1.orelse[0], nodes.Pass) |
| 464 |
assert (f1.lineno, f1.col_offset) == (1, 0) |
| 465 |
assert (f1.end_lineno, f1.end_col_offset) == (4, 8) |
| 466 |
assert (f1.target.lineno, f1.target.col_offset) == (1, 4) |
| 467 |
assert (f1.target.end_lineno, f1.target.end_col_offset) == (1, 5) |
| 468 |
assert (f1.iter.lineno, f1.iter.col_offset) == (1, 9) |
| 469 |
assert (f1.iter.end_lineno, f1.iter.end_col_offset) == (1, 12) |
| 470 |
assert (f1.body[0].lineno, f1.body[0].col_offset) == (2, 4) |
| 471 |
assert (f1.body[0].end_lineno, f1.body[0].end_col_offset) == (2, 8) |
| 472 |
assert (f1.orelse[0].lineno, f1.orelse[0].col_offset) == (4, 4) |
| 473 |
assert (f1.orelse[0].end_lineno, f1.orelse[0].end_col_offset) == (4, 8) |
| 474 |
|
| 475 |
f2 = ast_nodes[1] |
| 476 |
assert isinstance(f2, nodes.AsyncFor) |
| 477 |
assert isinstance(f2.target, nodes.AssignName) |
| 478 |
assert isinstance(f2.iter, nodes.Name) |
| 479 |
assert isinstance(f2.body[0], nodes.Pass) |
| 480 |
assert (f2.lineno, f2.col_offset) == (6, 0) |
| 481 |
assert (f2.end_lineno, f2.end_col_offset) == (7, 8) |
| 482 |
assert (f2.target.lineno, f2.target.col_offset) == (6, 10) |
| 483 |
assert (f2.target.end_lineno, f2.target.end_col_offset) == (6, 11) |
| 484 |
assert (f2.iter.lineno, f2.iter.col_offset) == (6, 15) |
| 485 |
assert (f2.iter.end_lineno, f2.iter.end_col_offset) == (6, 18) |
| 486 |
assert (f2.body[0].lineno, f2.body[0].col_offset) == (7, 4) |
| 487 |
assert (f2.body[0].end_lineno, f2.body[0].end_col_offset) == (7, 8) |
| 488 |
|
| 489 |
@staticmethod |
| 490 |
def test_end_lineno_const() -> None: |
| 491 |
"""Const (int, str, bool, None, bytes, ellipsis).""" |
| 492 |
code = textwrap.dedent(""" |
| 493 |
2 #@ |
| 494 |
"Hello" #@ |
| 495 |
True #@ |
| 496 |
None #@ |
| 497 |
b"01" #@ |
| 498 |
... #@ |
| 499 |
""").strip() |
| 500 |
ast_nodes = builder.extract_node(code) |
| 501 |
assert isinstance(ast_nodes, list) and len(ast_nodes) == 6 |
| 502 |
|
| 503 |
c1 = ast_nodes[0] |
| 504 |
assert isinstance(c1, nodes.Const) |
| 505 |
assert (c1.lineno, c1.col_offset) == (1, 0) |
| 506 |
assert (c1.end_lineno, c1.end_col_offset) == (1, 1) |
| 507 |
|
| 508 |
c2 = ast_nodes[1] |
| 509 |
assert isinstance(c2, nodes.Const) |
| 510 |
assert (c2.lineno, c2.col_offset) == (2, 0) |
| 511 |
assert (c2.end_lineno, c2.end_col_offset) == (2, 7) |
| 512 |
|
| 513 |
c3 = ast_nodes[2] |
| 514 |
assert isinstance(c3, nodes.Const) |
| 515 |
assert (c3.lineno, c3.col_offset) == (3, 0) |
| 516 |
assert (c3.end_lineno, c3.end_col_offset) == (3, 4) |
| 517 |
|
| 518 |
c4 = ast_nodes[3] |
| 519 |
assert isinstance(c4, nodes.Const) |
| 520 |
assert (c4.lineno, c4.col_offset) == (4, 0) |
| 521 |
assert (c4.end_lineno, c4.end_col_offset) == (4, 4) |
| 522 |
|
| 523 |
c5 = ast_nodes[4] |
| 524 |
assert isinstance(c5, nodes.Const) |
| 525 |
assert (c5.lineno, c5.col_offset) == (5, 0) |
| 526 |
assert (c5.end_lineno, c5.end_col_offset) == (5, 5) |
| 527 |
|
| 528 |
c6 = ast_nodes[5] |
| 529 |
assert isinstance(c6, nodes.Const) |
| 530 |
assert (c6.lineno, c6.col_offset) == (6, 0) |
| 531 |
assert (c6.end_lineno, c6.end_col_offset) == (6, 3) |
| 532 |
|
| 533 |
@staticmethod |
| 534 |
def test_end_lineno_function() -> None: |
| 535 |
"""FunctionDef, AsyncFunctionDef, Decorators, Lambda, Arguments.""" |
| 536 |
# pylint: disable = too-many-statements |
| 537 |
code = textwrap.dedent(""" |
| 538 |
def func( #@ |
| 539 |
a: int = 0, /, |
| 540 |
var: int = 1, *args: Any, |
| 541 |
keyword: int = 2, **kwargs: Any |
| 542 |
) -> None: |
| 543 |
pass |
| 544 |
|
| 545 |
@decorator1 |
| 546 |
@decorator2 |
| 547 |
async def func(): #@ |
| 548 |
pass |
| 549 |
|
| 550 |
lambda x: 2 #@ |
| 551 |
""").strip() |
| 552 |
ast_nodes = builder.extract_node(code) |
| 553 |
assert isinstance(ast_nodes, list) and len(ast_nodes) == 3 |
| 554 |
|
| 555 |
# fmt: off |
| 556 |
f1 = ast_nodes[0] |
| 557 |
assert isinstance(f1, nodes.FunctionDef) |
| 558 |
assert isinstance(f1.args, nodes.Arguments) |
| 559 |
assert isinstance(f1.returns, nodes.Const) |
| 560 |
assert isinstance(f1.body[0], nodes.Pass) |
| 561 |
assert (f1.lineno, f1.col_offset) == (1, 0) |
| 562 |
assert (f1.end_lineno, f1.end_col_offset) == (6, 8) |
| 563 |
assert (f1.returns.lineno, f1.returns.col_offset) == (5, 5) |
| 564 |
assert (f1.returns.end_lineno, f1.returns.end_col_offset) == (5, 9) |
| 565 |
assert (f1.body[0].lineno, f1.body[0].col_offset) == (6, 4) |
| 566 |
assert (f1.body[0].end_lineno, f1.body[0].end_col_offset) == (6, 8) |
| 567 |
|
| 568 |
assert (f1.args.lineno, f1.args.end_lineno) == (2, 4) |
| 569 |
|
| 570 |
# pos only arguments |
| 571 |
# TODO fix column offset: arg -> arg (AssignName) |
| 572 |
assert isinstance(f1.args.posonlyargs[0], nodes.AssignName) |
| 573 |
assert (f1.args.posonlyargs[0].lineno, f1.args.posonlyargs[0].col_offset) == (2, 4) |
| 574 |
assert (f1.args.posonlyargs[0].end_lineno, f1.args.posonlyargs[0].end_col_offset) == (2, 10) |
| 575 |
assert isinstance(f1.args.posonlyargs_annotations[0], nodes.Name) |
| 576 |
assert ( |
| 577 |
f1.args.posonlyargs_annotations[0].lineno, f1.args.posonlyargs_annotations[0].col_offset |
| 578 |
) == (2, 7) |
| 579 |
assert ( |
| 580 |
f1.args.posonlyargs_annotations[0].end_lineno, f1.args.posonlyargs_annotations[0].end_col_offset |
| 581 |
) == (2, 10) |
| 582 |
assert (f1.args.defaults[0].lineno, f1.args.defaults[0].col_offset) == (2, 13) |
| 583 |
assert (f1.args.defaults[0].end_lineno, f1.args.defaults[0].end_col_offset) == (2, 14) |
| 584 |
|
| 585 |
# pos or kw arguments |
| 586 |
assert isinstance(f1.args.args[0], nodes.AssignName) |
| 587 |
assert (f1.args.args[0].lineno, f1.args.args[0].col_offset) == (3, 4) |
| 588 |
assert (f1.args.args[0].end_lineno, f1.args.args[0].end_col_offset) == (3, 12) |
| 589 |
assert isinstance(f1.args.annotations[0], nodes.Name) |
| 590 |
assert (f1.args.annotations[0].lineno, f1.args.annotations[0].col_offset) == (3, 9) |
| 591 |
assert (f1.args.annotations[0].end_lineno, f1.args.annotations[0].end_col_offset) == (3, 12) |
| 592 |
assert isinstance(f1.args.defaults[1], nodes.Const) |
| 593 |
assert (f1.args.defaults[1].lineno, f1.args.defaults[1].col_offset) == (3, 15) |
| 594 |
assert (f1.args.defaults[1].end_lineno, f1.args.defaults[1].end_col_offset) == (3, 16) |
| 595 |
|
| 596 |
# *args |
| 597 |
assert isinstance(f1.args.varargannotation, nodes.Name) |
| 598 |
assert (f1.args.varargannotation.lineno, f1.args.varargannotation.col_offset) == (3, 25) |
| 599 |
assert (f1.args.varargannotation.end_lineno, f1.args.varargannotation.end_col_offset) == (3, 28) |
| 600 |
|
| 601 |
# kw_only arguments |
| 602 |
assert isinstance(f1.args.kwonlyargs[0], nodes.AssignName) |
| 603 |
assert (f1.args.kwonlyargs[0].lineno, f1.args.kwonlyargs[0].col_offset) == (4, 4) |
| 604 |
assert (f1.args.kwonlyargs[0].end_lineno, f1.args.kwonlyargs[0].end_col_offset) == (4, 16) |
| 605 |
annotations = f1.args.kwonlyargs_annotations |
| 606 |
assert isinstance(annotations[0], nodes.Name) |
| 607 |
assert (annotations[0].lineno, annotations[0].col_offset) == (4, 13) |
| 608 |
assert (annotations[0].end_lineno, annotations[0].end_col_offset) == (4, 16) |
| 609 |
assert isinstance(f1.args.kw_defaults[0], nodes.Const) |
| 610 |
assert (f1.args.kw_defaults[0].lineno, f1.args.kw_defaults[0].col_offset) == (4, 19) |
| 611 |
assert (f1.args.kw_defaults[0].end_lineno, f1.args.kw_defaults[0].end_col_offset) == (4, 20) |
| 612 |
|
| 613 |
# **kwargs |
| 614 |
assert isinstance(f1.args.kwargannotation, nodes.Name) |
| 615 |
assert (f1.args.kwargannotation.lineno, f1.args.kwargannotation.col_offset) == (4, 32) |
| 616 |
assert (f1.args.kwargannotation.end_lineno, f1.args.kwargannotation.end_col_offset) == (4, 35) |
| 617 |
|
| 618 |
f2 = ast_nodes[1] |
| 619 |
assert isinstance(f2, nodes.AsyncFunctionDef) |
| 620 |
assert isinstance(f2.decorators, nodes.Decorators) |
| 621 |
assert isinstance(f2.decorators.nodes[0], nodes.Name) |
| 622 |
assert isinstance(f2.decorators.nodes[1], nodes.Name) |
| 623 |
assert (f2.lineno, f2.col_offset) == (8, 0) |
| 624 |
assert (f2.end_lineno, f2.end_col_offset) == (11, 8) |
| 625 |
assert (f2.decorators.lineno, f2.decorators.col_offset) == (8, 0) |
| 626 |
assert (f2.decorators.end_lineno, f2.decorators.end_col_offset) == (9, 11) |
| 627 |
assert (f2.decorators.nodes[0].lineno, f2.decorators.nodes[0].col_offset) == (8, 1) |
| 628 |
assert (f2.decorators.nodes[0].end_lineno, f2.decorators.nodes[0].end_col_offset) == (8, 11) |
| 629 |
assert (f2.decorators.nodes[1].lineno, f2.decorators.nodes[1].col_offset) == (9, 1) |
| 630 |
assert (f2.decorators.nodes[1].end_lineno, f2.decorators.nodes[1].end_col_offset) == (9, 11) |
| 631 |
|
| 632 |
f3 = ast_nodes[2] |
| 633 |
assert isinstance(f3, nodes.Lambda) |
| 634 |
assert isinstance(f3.args, nodes.Arguments) |
| 635 |
assert isinstance(f3.args.args[0], nodes.AssignName) |
| 636 |
assert isinstance(f3.body, nodes.Const) |
| 637 |
assert (f3.lineno, f3.col_offset) == (13, 0) |
| 638 |
assert (f3.end_lineno, f3.end_col_offset) == (13, 11) |
| 639 |
assert (f3.args.args[0].lineno, f3.args.args[0].col_offset) == (13, 7) |
| 640 |
assert (f3.args.args[0].end_lineno, f3.args.args[0].end_col_offset) == (13, 8) |
| 641 |
assert (f3.body.lineno, f3.body.col_offset) == (13, 10) |
| 642 |
assert (f3.body.end_lineno, f3.body.end_col_offset) == (13, 11) |
| 643 |
# fmt: on |
| 644 |
|
| 645 |
@staticmethod |
| 646 |
def test_end_lineno_dict() -> None: |
| 647 |
"""Dict, DictUnpack.""" |
| 648 |
code = textwrap.dedent(""" |
| 649 |
{ #@ |
| 650 |
1: "Hello", |
| 651 |
**{2: "World"} #@ |
| 652 |
} |
| 653 |
""").strip() |
| 654 |
ast_nodes = builder.extract_node(code) |
| 655 |
assert isinstance(ast_nodes, list) and len(ast_nodes) == 2 |
| 656 |
|
| 657 |
d1 = ast_nodes[0] |
| 658 |
assert isinstance(d1, nodes.Dict) |
| 659 |
assert isinstance(d1.items[0][0], nodes.Const) |
| 660 |
assert isinstance(d1.items[0][1], nodes.Const) |
| 661 |
assert (d1.lineno, d1.col_offset) == (1, 0) |
| 662 |
assert (d1.end_lineno, d1.end_col_offset) == (4, 1) |
| 663 |
assert (d1.items[0][0].lineno, d1.items[0][0].col_offset) == (2, 4) |
| 664 |
assert (d1.items[0][0].end_lineno, d1.items[0][0].end_col_offset) == (2, 5) |
| 665 |
assert (d1.items[0][1].lineno, d1.items[0][1].col_offset) == (2, 7) |
| 666 |
assert (d1.items[0][1].end_lineno, d1.items[0][1].end_col_offset) == (2, 14) |
| 667 |
|
| 668 |
d2 = ast_nodes[1] |
| 669 |
assert isinstance(d2, nodes.DictUnpack) |
| 670 |
assert (d2.lineno, d2.col_offset) == (3, 6) |
| 671 |
assert (d2.end_lineno, d2.end_col_offset) == (3, 18) |
| 672 |
|
| 673 |
@staticmethod |
| 674 |
def test_end_lineno_try() -> None: |
| 675 |
"""Try, ExceptHandler.""" |
| 676 |
code = textwrap.dedent(""" |
| 677 |
try: #@ |
| 678 |
pass |
| 679 |
except KeyError as ex: |
| 680 |
pass |
| 681 |
except AttributeError as ex: |
| 682 |
pass |
| 683 |
else: |
| 684 |
pass |
| 685 |
|
| 686 |
try: #@ |
| 687 |
pass |
| 688 |
except KeyError as ex: |
| 689 |
pass |
| 690 |
else: |
| 691 |
pass |
| 692 |
finally: |
| 693 |
pass |
| 694 |
""").strip() |
| 695 |
ast_nodes = builder.extract_node(code) |
| 696 |
assert isinstance(ast_nodes, list) and len(ast_nodes) == 2 |
| 697 |
|
| 698 |
t1 = ast_nodes[0] |
| 699 |
assert isinstance(t1, nodes.Try) |
| 700 |
assert isinstance(t1.body[0], nodes.Pass) |
| 701 |
assert isinstance(t1.orelse[0], nodes.Pass) |
| 702 |
assert (t1.lineno, t1.col_offset) == (1, 0) |
| 703 |
assert (t1.end_lineno, t1.end_col_offset) == (8, 8) |
| 704 |
assert (t1.body[0].lineno, t1.body[0].col_offset) == (2, 4) |
| 705 |
assert (t1.body[0].end_lineno, t1.body[0].end_col_offset) == (2, 8) |
| 706 |
assert (t1.orelse[0].lineno, t1.orelse[0].col_offset) == (8, 4) |
| 707 |
assert (t1.orelse[0].end_lineno, t1.orelse[0].end_col_offset) == (8, 8) |
| 708 |
|
| 709 |
t2 = t1.handlers[0] |
| 710 |
assert isinstance(t2, nodes.ExceptHandler) |
| 711 |
assert isinstance(t2.type, nodes.Name) |
| 712 |
assert isinstance(t2.name, nodes.AssignName) |
| 713 |
assert isinstance(t2.body[0], nodes.Pass) |
| 714 |
assert (t2.lineno, t2.col_offset) == (3, 0) |
| 715 |
assert (t2.end_lineno, t2.end_col_offset) == (4, 8) |
| 716 |
assert (t2.type.lineno, t2.type.col_offset) == (3, 7) |
| 717 |
assert (t2.type.end_lineno, t2.type.end_col_offset) == (3, 15) |
| 718 |
# TODO fix column offset: ExceptHandler -> name (AssignName) |
| 719 |
assert (t2.name.lineno, t2.name.col_offset) == (3, 0) |
| 720 |
assert (t2.name.end_lineno, t2.name.end_col_offset) == (4, 8) |
| 721 |
assert (t2.body[0].lineno, t2.body[0].col_offset) == (4, 4) |
| 722 |
assert (t2.body[0].end_lineno, t2.body[0].end_col_offset) == (4, 8) |
| 723 |
|
| 724 |
t3 = ast_nodes[1] |
| 725 |
assert isinstance(t3, nodes.Try) |
| 726 |
assert isinstance(t3.finalbody[0], nodes.Pass) |
| 727 |
assert (t3.lineno, t3.col_offset) == (10, 0) |
| 728 |
assert (t3.end_lineno, t3.end_col_offset) == (17, 8) |
| 729 |
assert (t3.body[0].lineno, t3.body[0].col_offset) == (11, 4) |
| 730 |
assert (t3.body[0].end_lineno, t3.body[0].end_col_offset) == (11, 8) |
| 731 |
assert (t3.finalbody[0].lineno, t3.finalbody[0].col_offset) == (17, 4) |
| 732 |
assert (t3.finalbody[0].end_lineno, t3.finalbody[0].end_col_offset) == (17, 8) |
| 733 |
|
| 734 |
@staticmethod |
| 735 |
def test_end_lineno_subscript() -> None: |
| 736 |
"""Subscript, Slice, (ExtSlice, Index).""" |
| 737 |
code = textwrap.dedent(""" |
| 738 |
var[0] #@ |
| 739 |
var[1:2:1] #@ |
| 740 |
var[1:2, 2] #@ |
| 741 |
""").strip() |
| 742 |
ast_nodes = builder.extract_node(code) |
| 743 |
assert isinstance(ast_nodes, list) and len(ast_nodes) == 3 |
| 744 |
|
| 745 |
s1 = ast_nodes[0] |
| 746 |
assert isinstance(s1, nodes.Subscript) |
| 747 |
assert isinstance(s1.value, nodes.Name) |
| 748 |
assert isinstance(s1.slice, nodes.Const) |
| 749 |
assert (s1.lineno, s1.col_offset) == (1, 0) |
| 750 |
assert (s1.end_lineno, s1.end_col_offset) == (1, 6) |
| 751 |
assert (s1.value.lineno, s1.value.col_offset) == (1, 0) |
| 752 |
assert (s1.value.end_lineno, s1.value.end_col_offset) == (1, 3) |
| 753 |
assert (s1.slice.lineno, s1.slice.col_offset) == (1, 4) |
| 754 |
assert (s1.slice.end_lineno, s1.slice.end_col_offset) == (1, 5) |
| 755 |
|
| 756 |
s2 = ast_nodes[1] |
| 757 |
assert isinstance(s2, nodes.Subscript) |
| 758 |
assert isinstance(s2.slice, nodes.Slice) |
| 759 |
assert isinstance(s2.slice.lower, nodes.Const) |
| 760 |
assert isinstance(s2.slice.upper, nodes.Const) |
| 761 |
assert isinstance(s2.slice.step, nodes.Const) |
| 762 |
assert (s2.lineno, s2.col_offset) == (2, 0) |
| 763 |
assert (s2.end_lineno, s2.end_col_offset) == (2, 10) |
| 764 |
assert (s2.slice.lower.lineno, s2.slice.lower.col_offset) == (2, 4) |
| 765 |
assert (s2.slice.lower.end_lineno, s2.slice.lower.end_col_offset) == (2, 5) |
| 766 |
assert (s2.slice.upper.lineno, s2.slice.upper.col_offset) == (2, 6) |
| 767 |
assert (s2.slice.upper.end_lineno, s2.slice.upper.end_col_offset) == (2, 7) |
| 768 |
assert (s2.slice.step.lineno, s2.slice.step.col_offset) == (2, 8) |
| 769 |
assert (s2.slice.step.end_lineno, s2.slice.step.end_col_offset) == (2, 9) |
| 770 |
|
| 771 |
s3 = ast_nodes[2] |
| 772 |
assert isinstance(s3, nodes.Subscript) |
| 773 |
assert isinstance(s3.slice, nodes.Tuple) |
| 774 |
assert (s3.lineno, s3.col_offset) == (3, 0) |
| 775 |
assert (s3.end_lineno, s3.end_col_offset) == (3, 11) |
| 776 |
assert (s3.slice.lineno, s3.slice.col_offset) == (3, 4) |
| 777 |
assert (s3.slice.end_lineno, s3.slice.end_col_offset) == (3, 10) |
| 778 |
|
| 779 |
@staticmethod |
| 780 |
def test_end_lineno_import() -> None: |
| 781 |
"""Import, ImportFrom.""" |
| 782 |
code = textwrap.dedent(""" |
| 783 |
import a.b #@ |
| 784 |
import a as x #@ |
| 785 |
from . import x #@ |
| 786 |
from .a import y as y #@ |
| 787 |
""").strip() |
| 788 |
ast_nodes = builder.extract_node(code) |
| 789 |
assert isinstance(ast_nodes, list) and len(ast_nodes) == 4 |
| 790 |
|
| 791 |
i1 = ast_nodes[0] |
| 792 |
assert isinstance(i1, nodes.Import) |
| 793 |
assert (i1.lineno, i1.col_offset) == (1, 0) |
| 794 |
assert (i1.end_lineno, i1.end_col_offset) == (1, 10) |
| 795 |
|
| 796 |
i2 = ast_nodes[1] |
| 797 |
assert isinstance(i2, nodes.Import) |
| 798 |
assert (i2.lineno, i2.col_offset) == (2, 0) |
| 799 |
assert (i2.end_lineno, i2.end_col_offset) == (2, 13) |
| 800 |
|
| 801 |
i3 = ast_nodes[2] |
| 802 |
assert isinstance(i3, nodes.ImportFrom) |
| 803 |
assert (i3.lineno, i3.col_offset) == (3, 0) |
| 804 |
assert (i3.end_lineno, i3.end_col_offset) == (3, 15) |
| 805 |
|
| 806 |
i4 = ast_nodes[3] |
| 807 |
assert isinstance(i4, nodes.ImportFrom) |
| 808 |
assert (i4.lineno, i4.col_offset) == (4, 0) |
| 809 |
assert (i4.end_lineno, i4.end_col_offset) == (4, 21) |
| 810 |
|
| 811 |
@staticmethod |
| 812 |
def test_end_lineno_with() -> None: |
| 813 |
"""With, AsyncWith.""" |
| 814 |
code = textwrap.dedent(""" |
| 815 |
with open(file) as fp, \\ |
| 816 |
open(file2) as fp2: #@ |
| 817 |
pass |
| 818 |
|
| 819 |
async with open(file) as fp: #@ |
| 820 |
pass |
| 821 |
""").strip() |
| 822 |
ast_nodes = builder.extract_node(code) |
| 823 |
assert isinstance(ast_nodes, list) and len(ast_nodes) == 2 |
| 824 |
|
| 825 |
w1 = ast_nodes[0].parent |
| 826 |
assert isinstance(w1, nodes.With) |
| 827 |
assert isinstance(w1.items[0][0], nodes.Call) |
| 828 |
assert isinstance(w1.items[0][1], nodes.AssignName) |
| 829 |
assert isinstance(w1.items[1][0], nodes.Call) |
| 830 |
assert isinstance(w1.items[1][1], nodes.AssignName) |
| 831 |
assert isinstance(w1.body[0], nodes.Pass) |
| 832 |
assert (w1.lineno, w1.col_offset) == (1, 0) |
| 833 |
assert (w1.end_lineno, w1.end_col_offset) == (3, 8) |
| 834 |
assert (w1.items[0][0].lineno, w1.items[0][0].col_offset) == (1, 5) |
| 835 |
assert (w1.items[0][0].end_lineno, w1.items[0][0].end_col_offset) == (1, 15) |
| 836 |
assert (w1.items[0][1].lineno, w1.items[0][1].col_offset) == (1, 19) |
| 837 |
assert (w1.items[0][1].end_lineno, w1.items[0][1].end_col_offset) == (1, 21) |
| 838 |
assert (w1.items[1][0].lineno, w1.items[1][0].col_offset) == (2, 8) |
| 839 |
assert (w1.items[1][0].end_lineno, w1.items[1][0].end_col_offset) == (2, 19) |
| 840 |
assert (w1.items[1][1].lineno, w1.items[1][1].col_offset) == (2, 23) |
| 841 |
assert (w1.items[1][1].end_lineno, w1.items[1][1].end_col_offset) == (2, 26) |
| 842 |
assert (w1.body[0].lineno, w1.body[0].col_offset) == (3, 4) |
| 843 |
assert (w1.body[0].end_lineno, w1.body[0].end_col_offset) == (3, 8) |
| 844 |
|
| 845 |
w2 = ast_nodes[1] |
| 846 |
assert isinstance(w2, nodes.AsyncWith) |
| 847 |
assert isinstance(w2.items[0][0], nodes.Call) |
| 848 |
assert isinstance(w2.items[0][1], nodes.AssignName) |
| 849 |
assert isinstance(w2.body[0], nodes.Pass) |
| 850 |
assert (w2.lineno, w2.col_offset) == (5, 0) |
| 851 |
assert (w2.end_lineno, w2.end_col_offset) == (6, 8) |
| 852 |
assert (w2.items[0][0].lineno, w2.items[0][0].col_offset) == (5, 11) |
| 853 |
assert (w2.items[0][0].end_lineno, w2.items[0][0].end_col_offset) == (5, 21) |
| 854 |
assert (w2.items[0][1].lineno, w2.items[0][1].col_offset) == (5, 25) |
| 855 |
assert (w2.items[0][1].end_lineno, w2.items[0][1].end_col_offset) == (5, 27) |
| 856 |
assert (w2.body[0].lineno, w2.body[0].col_offset) == (6, 4) |
| 857 |
assert (w2.body[0].end_lineno, w2.body[0].end_col_offset) == (6, 8) |
| 858 |
|
| 859 |
@staticmethod |
| 860 |
def test_end_lineno_while() -> None: |
| 861 |
"""While.""" |
| 862 |
code = textwrap.dedent(""" |
| 863 |
while 2: |
| 864 |
pass |
| 865 |
else: |
| 866 |
pass |
| 867 |
""").strip() |
| 868 |
w1 = builder.extract_node(code) |
| 869 |
assert isinstance(w1, nodes.While) |
| 870 |
assert isinstance(w1.test, nodes.Const) |
| 871 |
assert isinstance(w1.body[0], nodes.Pass) |
| 872 |
assert isinstance(w1.orelse[0], nodes.Pass) |
| 873 |
assert (w1.lineno, w1.col_offset) == (1, 0) |
| 874 |
assert (w1.end_lineno, w1.end_col_offset) == (4, 8) |
| 875 |
assert (w1.test.lineno, w1.test.col_offset) == (1, 6) |
| 876 |
assert (w1.test.end_lineno, w1.test.end_col_offset) == (1, 7) |
| 877 |
assert (w1.body[0].lineno, w1.body[0].col_offset) == (2, 4) |
| 878 |
assert (w1.body[0].end_lineno, w1.body[0].end_col_offset) == (2, 8) |
| 879 |
assert (w1.orelse[0].lineno, w1.orelse[0].col_offset) == (4, 4) |
| 880 |
assert (w1.orelse[0].end_lineno, w1.orelse[0].end_col_offset) == (4, 8) |
| 881 |
assert w1.block_range(1) == (1, 2) |
| 882 |
assert w1.block_range(2) == (2, 2) |
| 883 |
assert w1.block_range(3) == (3, 4) |
| 884 |
assert w1.block_range(4) == (4, 4) |
| 885 |
|
| 886 |
@staticmethod |
| 887 |
def test_end_lineno_string() -> None: |
| 888 |
"""FormattedValue, JoinedStr.""" |
| 889 |
code = textwrap.dedent(""" |
| 890 |
f"Hello World: {42.1234:02d}" #@ |
| 891 |
f"Hello: {name=}" #@ |
| 892 |
""").strip() |
| 893 |
ast_nodes = builder.extract_node(code) |
| 894 |
assert isinstance(ast_nodes, list) and len(ast_nodes) == 2 |
| 895 |
|
| 896 |
s1 = ast_nodes[0] |
| 897 |
assert isinstance(s1, nodes.JoinedStr) |
| 898 |
assert isinstance(s1.values[0], nodes.Const) |
| 899 |
assert (s1.lineno, s1.col_offset) == (1, 0) |
| 900 |
assert (s1.end_lineno, s1.end_col_offset) == (1, 29) |
| 901 |
if PY312_PLUS: |
| 902 |
assert (s1.values[0].lineno, s1.values[0].col_offset) == (1, 2) |
| 903 |
assert (s1.values[0].end_lineno, s1.values[0].end_col_offset) == (1, 15) |
| 904 |
else: |
| 905 |
# Bug in Python 3.11 |
| 906 |
# https://github.com/python/cpython/issues/81639 |
| 907 |
assert (s1.values[0].lineno, s1.values[0].col_offset) == (1, 0) |
| 908 |
assert (s1.values[0].end_lineno, s1.values[0].end_col_offset) == (1, 29) |
| 909 |
|
| 910 |
s2 = s1.values[1] |
| 911 |
assert isinstance(s2, nodes.FormattedValue) |
| 912 |
if PY312_PLUS: |
| 913 |
assert (s2.lineno, s2.col_offset) == (1, 15) |
| 914 |
assert (s2.end_lineno, s2.end_col_offset) == (1, 28) |
| 915 |
else: |
| 916 |
assert (s2.lineno, s2.col_offset) == (1, 0) |
| 917 |
assert (s2.end_lineno, s2.end_col_offset) == (1, 29) |
| 918 |
|
| 919 |
assert isinstance(s2.value, nodes.Const) # 42.1234 |
| 920 |
assert (s2.value.lineno, s2.value.col_offset) == (1, 16) |
| 921 |
assert (s2.value.end_lineno, s2.value.end_col_offset) == (1, 23) |
| 922 |
assert isinstance(s2.format_spec, nodes.JoinedStr) # ':02d' |
| 923 |
if PY312_PLUS: |
| 924 |
assert (s2.format_spec.lineno, s2.format_spec.col_offset) == (1, 23) |
| 925 |
assert (s2.format_spec.end_lineno, s2.format_spec.end_col_offset) == (1, 27) |
| 926 |
else: |
| 927 |
assert (s2.format_spec.lineno, s2.format_spec.col_offset) == (1, 0) |
| 928 |
assert (s2.format_spec.end_lineno, s2.format_spec.end_col_offset) == (1, 29) |
| 929 |
|
| 930 |
s3 = ast_nodes[1] |
| 931 |
assert isinstance(s3, nodes.JoinedStr) |
| 932 |
assert isinstance(s3.values[0], nodes.Const) |
| 933 |
assert (s3.lineno, s3.col_offset) == (2, 0) |
| 934 |
assert (s3.end_lineno, s3.end_col_offset) == (2, 17) |
| 935 |
if PY312_PLUS: |
| 936 |
assert (s3.values[0].lineno, s3.values[0].col_offset) == (2, 2) |
| 937 |
assert (s3.values[0].end_lineno, s3.values[0].end_col_offset) == (2, 15) |
| 938 |
else: |
| 939 |
assert (s3.values[0].lineno, s3.values[0].col_offset) == (2, 0) |
| 940 |
assert (s3.values[0].end_lineno, s3.values[0].end_col_offset) == (2, 17) |
| 941 |
|
| 942 |
s4 = s3.values[1] |
| 943 |
assert isinstance(s4, nodes.FormattedValue) |
| 944 |
if PY312_PLUS: |
| 945 |
assert (s4.lineno, s4.col_offset) == (2, 9) |
| 946 |
assert (s4.end_lineno, s4.end_col_offset) == (2, 16) |
| 947 |
else: |
| 948 |
assert (s4.lineno, s4.col_offset) == (2, 0) |
| 949 |
assert (s4.end_lineno, s4.end_col_offset) == (2, 17) |
| 950 |
|
| 951 |
assert isinstance(s4.value, nodes.Name) # 'name' |
| 952 |
assert (s4.value.lineno, s4.value.col_offset) == (2, 10) |
| 953 |
assert (s4.value.end_lineno, s4.value.end_col_offset) == (2, 14) |
| 954 |
|
| 955 |
@staticmethod |
| 956 |
def test_end_lineno_match() -> None: |
| 957 |
"""Match, MatchValue, MatchSingleton, MatchSequence, MatchMapping, |
| 958 |
MatchClass, MatchStar, MatchOr, MatchAs. |
| 959 |
""" |
| 960 |
# pylint: disable = too-many-statements |
| 961 |
code = textwrap.dedent(""" |
| 962 |
match x: #@ |
| 963 |
case 200 if True: #@ |
| 964 |
pass |
| 965 |
case True: #@ |
| 966 |
pass |
| 967 |
case (1, 2, *args): #@ |
| 968 |
pass |
| 969 |
case {1: "Hello", **rest}: #@ |
| 970 |
pass |
| 971 |
case Point2d(0, y=0): #@ |
| 972 |
pass |
| 973 |
case 200 | 300: #@ |
| 974 |
pass |
| 975 |
case 200 as c: #@ |
| 976 |
pass |
| 977 |
""").strip() |
| 978 |
ast_nodes = builder.extract_node(code) |
| 979 |
assert isinstance(ast_nodes, list) and len(ast_nodes) == 8 |
| 980 |
|
| 981 |
# fmt: off |
| 982 |
m1 = ast_nodes[0] |
| 983 |
assert isinstance(m1, nodes.Match) |
| 984 |
assert (m1.lineno, m1.col_offset) == (1, 0) |
| 985 |
assert (m1.end_lineno, m1.end_col_offset) == (15, 12) |
| 986 |
assert (m1.subject.lineno, m1.subject.col_offset) == (1, 6) |
| 987 |
assert (m1.subject.end_lineno, m1.subject.end_col_offset) == (1, 7) |
| 988 |
|
| 989 |
m2 = ast_nodes[1] |
| 990 |
assert isinstance(m2, nodes.MatchCase) |
| 991 |
assert isinstance(m2.pattern, nodes.MatchValue) |
| 992 |
assert isinstance(m2.guard, nodes.Const) |
| 993 |
assert isinstance(m2.body[0], nodes.Pass) |
| 994 |
assert (m2.pattern.lineno, m2.pattern.col_offset) == (2, 9) |
| 995 |
assert (m2.pattern.end_lineno, m2.pattern.end_col_offset) == (2, 12) |
| 996 |
assert (m2.guard.lineno, m2.guard.col_offset) == (2, 16) |
| 997 |
assert (m2.guard.end_lineno, m2.guard.end_col_offset) == (2, 20) |
| 998 |
assert (m2.body[0].lineno, m2.body[0].col_offset) == (3, 8) |
| 999 |
assert (m2.body[0].end_lineno, m2.body[0].end_col_offset) == (3, 12) |
| 1000 |
|
| 1001 |
m3 = ast_nodes[2] |
| 1002 |
assert isinstance(m3, nodes.MatchCase) |
| 1003 |
assert isinstance(m3.pattern, nodes.MatchSingleton) |
| 1004 |
assert (m3.pattern.lineno, m3.pattern.col_offset) == (4, 9) |
| 1005 |
assert (m3.pattern.end_lineno, m3.pattern.end_col_offset) == (4, 13) |
| 1006 |
|
| 1007 |
m4 = ast_nodes[3] |
| 1008 |
assert isinstance(m4, nodes.MatchCase) |
| 1009 |
assert isinstance(m4.pattern, nodes.MatchSequence) |
| 1010 |
assert isinstance(m4.pattern.patterns[0], nodes.MatchValue) |
| 1011 |
assert (m4.pattern.lineno, m4.pattern.col_offset) == (6, 9) |
| 1012 |
assert (m4.pattern.end_lineno, m4.pattern.end_col_offset) == (6, 22) |
| 1013 |
assert (m4.pattern.patterns[0].lineno, m4.pattern.patterns[0].col_offset) == (6, 10) |
| 1014 |
assert (m4.pattern.patterns[0].end_lineno, m4.pattern.patterns[0].end_col_offset) == (6, 11) |
| 1015 |
|
| 1016 |
m5 = m4.pattern.patterns[2] |
| 1017 |
assert isinstance(m5, nodes.MatchStar) |
| 1018 |
assert isinstance(m5.name, nodes.AssignName) |
| 1019 |
assert (m5.lineno, m5.col_offset) == (6, 16) |
| 1020 |
assert (m5.end_lineno, m5.end_col_offset) == (6, 21) |
| 1021 |
# TODO fix column offset: MatchStar -> name (AssignName) |
| 1022 |
assert (m5.name.lineno, m5.name.col_offset) == (6, 16) |
| 1023 |
assert (m5.name.end_lineno, m5.name.end_col_offset) == (6, 21) |
| 1024 |
|
| 1025 |
m6 = ast_nodes[4] |
| 1026 |
assert isinstance(m6, nodes.MatchCase) |
| 1027 |
assert isinstance(m6.pattern, nodes.MatchMapping) |
| 1028 |
assert isinstance(m6.pattern.keys[0], nodes.Const) |
| 1029 |
assert isinstance(m6.pattern.patterns[0], nodes.MatchValue) |
| 1030 |
assert isinstance(m6.pattern.rest, nodes.AssignName) |
| 1031 |
assert (m6.pattern.lineno, m6.pattern.col_offset) == (8, 9) |
| 1032 |
assert (m6.pattern.end_lineno, m6.pattern.end_col_offset) == (8, 29) |
| 1033 |
assert (m6.pattern.keys[0].lineno, m6.pattern.keys[0].col_offset) == (8, 10) |
| 1034 |
assert (m6.pattern.keys[0].end_lineno, m6.pattern.keys[0].end_col_offset) == (8, 11) |
| 1035 |
assert (m6.pattern.patterns[0].lineno, m6.pattern.patterns[0].col_offset) == (8, 13) |
| 1036 |
assert (m6.pattern.patterns[0].end_lineno, m6.pattern.patterns[0].end_col_offset) == (8, 20) |
| 1037 |
# TODO fix column offset: MatchMapping -> rest (AssignName) |
| 1038 |
assert (m6.pattern.rest.lineno, m6.pattern.rest.col_offset) == (8, 9) |
| 1039 |
assert (m6.pattern.rest.end_lineno, m6.pattern.rest.end_col_offset) == (8, 29) |
| 1040 |
|
| 1041 |
m7 = ast_nodes[5] |
| 1042 |
assert isinstance(m7, nodes.MatchCase) |
| 1043 |
assert isinstance(m7.pattern, nodes.MatchClass) |
| 1044 |
assert isinstance(m7.pattern.cls, nodes.Name) |
| 1045 |
assert isinstance(m7.pattern.patterns[0], nodes.MatchValue) |
| 1046 |
assert isinstance(m7.pattern.kwd_patterns[0], nodes.MatchValue) |
| 1047 |
assert (m7.pattern.lineno, m7.pattern.col_offset) == (10, 9) |
| 1048 |
assert (m7.pattern.end_lineno, m7.pattern.end_col_offset) == (10, 24) |
| 1049 |
assert (m7.pattern.cls.lineno, m7.pattern.cls.col_offset) == (10, 9) |
| 1050 |
assert (m7.pattern.cls.end_lineno, m7.pattern.cls.end_col_offset) == (10, 16) |
| 1051 |
assert (m7.pattern.patterns[0].lineno, m7.pattern.patterns[0].col_offset) == (10, 17) |
| 1052 |
assert (m7.pattern.patterns[0].end_lineno, m7.pattern.patterns[0].end_col_offset) == (10, 18) |
| 1053 |
assert (m7.pattern.kwd_patterns[0].lineno, m7.pattern.kwd_patterns[0].col_offset) == (10, 22) |
| 1054 |
assert (m7.pattern.kwd_patterns[0].end_lineno, m7.pattern.kwd_patterns[0].end_col_offset) == (10, 23) |
| 1055 |
|
| 1056 |
m8 = ast_nodes[6] |
| 1057 |
assert isinstance(m8, nodes.MatchCase) |
| 1058 |
assert isinstance(m8.pattern, nodes.MatchOr) |
| 1059 |
assert isinstance(m8.pattern.patterns[0], nodes.MatchValue) |
| 1060 |
assert (m8.pattern.lineno, m8.pattern.col_offset) == (12, 9) |
| 1061 |
assert (m8.pattern.end_lineno, m8.pattern.end_col_offset) == (12, 18) |
| 1062 |
assert (m8.pattern.patterns[0].lineno, m8.pattern.patterns[0].col_offset) == (12, 9) |
| 1063 |
assert (m8.pattern.patterns[0].end_lineno, m8.pattern.patterns[0].end_col_offset) == (12, 12) |
| 1064 |
|
| 1065 |
m9 = ast_nodes[7] |
| 1066 |
assert isinstance(m9, nodes.MatchCase) |
| 1067 |
assert isinstance(m9.pattern, nodes.MatchAs) |
| 1068 |
assert isinstance(m9.pattern.pattern, nodes.MatchValue) |
| 1069 |
assert isinstance(m9.pattern.name, nodes.AssignName) |
| 1070 |
assert (m9.pattern.lineno, m9.pattern.col_offset) == (14, 9) |
| 1071 |
assert (m9.pattern.end_lineno, m9.pattern.end_col_offset) == (14, 17) |
| 1072 |
assert (m9.pattern.pattern.lineno, m9.pattern.pattern.col_offset) == (14, 9) |
| 1073 |
assert (m9.pattern.pattern.end_lineno, m9.pattern.pattern.end_col_offset) == (14, 12) |
| 1074 |
# TODO fix column offset: MatchAs -> name (AssignName) |
| 1075 |
assert (m9.pattern.name.lineno, m9.pattern.name.col_offset) == (14, 9) |
| 1076 |
assert (m9.pattern.name.end_lineno, m9.pattern.name.end_col_offset) == (14, 17) |
| 1077 |
# fmt: on |
| 1078 |
|
| 1079 |
@staticmethod |
| 1080 |
def test_end_lineno_comprehension() -> None: |
| 1081 |
"""ListComp, SetComp, DictComp, GeneratorExpr.""" |
| 1082 |
code = textwrap.dedent(""" |
| 1083 |
[x for x in var] #@ |
| 1084 |
{x for x in var} #@ |
| 1085 |
{x: y for x, y in var} #@ |
| 1086 |
(x for x in var) #@ |
| 1087 |
""").strip() |
| 1088 |
ast_nodes = builder.extract_node(code) |
| 1089 |
assert isinstance(ast_nodes, list) and len(ast_nodes) == 4 |
| 1090 |
|
| 1091 |
c1 = ast_nodes[0] |
| 1092 |
assert isinstance(c1, nodes.ListComp) |
| 1093 |
assert isinstance(c1.elt, nodes.Name) |
| 1094 |
assert isinstance(c1.generators[0], nodes.Comprehension) # type: ignore[index] |
| 1095 |
assert (c1.lineno, c1.col_offset) == (1, 0) |
| 1096 |
assert (c1.end_lineno, c1.end_col_offset) == (1, 16) |
| 1097 |
assert (c1.elt.lineno, c1.elt.col_offset) == (1, 1) |
| 1098 |
assert (c1.elt.end_lineno, c1.elt.end_col_offset) == (1, 2) |
| 1099 |
|
| 1100 |
c2 = ast_nodes[1] |
| 1101 |
assert isinstance(c2, nodes.SetComp) |
| 1102 |
assert isinstance(c2.elt, nodes.Name) |
| 1103 |
assert isinstance(c2.generators[0], nodes.Comprehension) # type: ignore[index] |
| 1104 |
assert (c2.lineno, c2.col_offset) == (2, 0) |
| 1105 |
assert (c2.end_lineno, c2.end_col_offset) == (2, 16) |
| 1106 |
assert (c2.elt.lineno, c2.elt.col_offset) == (2, 1) |
| 1107 |
assert (c2.elt.end_lineno, c2.elt.end_col_offset) == (2, 2) |
| 1108 |
|
| 1109 |
c3 = ast_nodes[2] |
| 1110 |
assert isinstance(c3, nodes.DictComp) |
| 1111 |
assert isinstance(c3.key, nodes.Name) |
| 1112 |
assert isinstance(c3.value, nodes.Name) |
| 1113 |
assert isinstance(c3.generators[0], nodes.Comprehension) # type: ignore[index] |
| 1114 |
assert (c3.lineno, c3.col_offset) == (3, 0) |
| 1115 |
assert (c3.end_lineno, c3.end_col_offset) == (3, 22) |
| 1116 |
assert (c3.key.lineno, c3.key.col_offset) == (3, 1) |
| 1117 |
assert (c3.key.end_lineno, c3.key.end_col_offset) == (3, 2) |
| 1118 |
assert (c3.value.lineno, c3.value.col_offset) == (3, 4) |
| 1119 |
assert (c3.value.end_lineno, c3.value.end_col_offset) == (3, 5) |
| 1120 |
|
| 1121 |
c4 = ast_nodes[3] |
| 1122 |
assert isinstance(c4, nodes.GeneratorExp) |
| 1123 |
assert isinstance(c4.elt, nodes.Name) |
| 1124 |
assert isinstance(c4.generators[0], nodes.Comprehension) # type: ignore[index] |
| 1125 |
assert (c4.lineno, c4.col_offset) == (4, 0) |
| 1126 |
assert (c4.end_lineno, c4.end_col_offset) == (4, 16) |
| 1127 |
assert (c4.elt.lineno, c4.elt.col_offset) == (4, 1) |
| 1128 |
assert (c4.elt.end_lineno, c4.elt.end_col_offset) == (4, 2) |
| 1129 |
|
| 1130 |
@staticmethod |
| 1131 |
def test_end_lineno_class() -> None: |
| 1132 |
"""ClassDef, Keyword.""" |
| 1133 |
code = textwrap.dedent(""" |
| 1134 |
@decorator1 |
| 1135 |
@decorator2 |
| 1136 |
class X(Parent, var=42): |
| 1137 |
pass |
| 1138 |
""").strip() |
| 1139 |
c1 = builder.extract_node(code) |
| 1140 |
assert isinstance(c1, nodes.ClassDef) |
| 1141 |
assert isinstance(c1.decorators, nodes.Decorators) |
| 1142 |
assert isinstance(c1.bases[0], nodes.Name) |
| 1143 |
assert isinstance(c1.keywords[0], nodes.Keyword) |
| 1144 |
assert isinstance(c1.body[0], nodes.Pass) |
| 1145 |
|
| 1146 |
# fmt: off |
| 1147 |
assert (c1.lineno, c1.col_offset) == (3, 0) |
| 1148 |
assert (c1.end_lineno, c1.end_col_offset) == (4, 8) |
| 1149 |
assert (c1.decorators.lineno, c1.decorators.col_offset) == (1, 0) |
| 1150 |
assert (c1.decorators.end_lineno, c1.decorators.end_col_offset) == (2, 11) |
| 1151 |
assert (c1.bases[0].lineno, c1.bases[0].col_offset) == (3, 8) |
| 1152 |
assert (c1.bases[0].end_lineno, c1.bases[0].end_col_offset) == (3, 14) |
| 1153 |
assert (c1.keywords[0].lineno, c1.keywords[0].col_offset) == (3, 16) |
| 1154 |
assert (c1.keywords[0].end_lineno, c1.keywords[0].end_col_offset) == (3, 22) |
| 1155 |
assert (c1.body[0].lineno, c1.body[0].col_offset) == (4, 4) |
| 1156 |
assert (c1.body[0].end_lineno, c1.body[0].end_col_offset) == (4, 8) |
| 1157 |
# fmt: on |
| 1158 |
|
| 1159 |
@staticmethod |
| 1160 |
def test_end_lineno_module() -> None: |
| 1161 |
"""Tests for Module.""" |
| 1162 |
code = """print()""" |
| 1163 |
module = astroid.parse(code) |
| 1164 |
assert isinstance(module, nodes.Module) |
| 1165 |
assert module.lineno == 0 |
| 1166 |
assert module.col_offset == 0 |
| 1167 |
assert module.end_lineno is None |
| 1168 |
assert module.end_col_offset is None |