1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE
# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt
from __future__ import annotations
import textwrap
from astroid import builder, nodes
class TestNodePosition:
"""Test node ``position`` attribute."""
@staticmethod
def test_position_class() -> None:
"""Position should only include keyword and name.
>>> class A(Parent):
>>> ^^^^^^^
"""
code = textwrap.dedent(
"""
class A: #@
...
class B(A): #@
pass
class C: #@
'''Docstring'''
class D: #@
...
class E: #@
def f():
...
@decorator
class F: #@
...
"""
).strip()
ast_nodes: list[nodes.NodeNG] = builder.extract_node(code) # type: ignore[assignment]
a = ast_nodes[0]
assert isinstance(a, nodes.ClassDef)
assert a.position == (1, 0, 1, 7)
b = ast_nodes[1]
assert isinstance(b, nodes.ClassDef)
assert b.position == (4, 0, 4, 7)
c = ast_nodes[2]
assert isinstance(c, nodes.ClassDef)
assert c.position == (7, 0, 7, 7)
d = ast_nodes[3]
assert isinstance(d, nodes.ClassDef)
assert d.position == (10, 4, 10, 11)
e = ast_nodes[4]
assert isinstance(e, nodes.ClassDef)
assert e.position == (13, 0, 13, 7)
f = ast_nodes[5]
assert isinstance(f, nodes.ClassDef)
assert f.position == (18, 0, 18, 7)
@staticmethod
def test_position_function() -> None:
"""Position should only include keyword and name.
>>> def func(var: int = 42):
>>> ^^^^^^^^
"""
code = textwrap.dedent(
"""
def a(): #@
...
def b(): #@
'''Docstring'''
def c( #@
var: int = 42
):
def d(): #@
...
@decorator
def e(): #@
...
"""
).strip()
ast_nodes: list[nodes.NodeNG] = builder.extract_node(code) # type: ignore[assignment]
a = ast_nodes[0]
assert isinstance(a, nodes.FunctionDef)
assert a.position == (1, 0, 1, 5)
b = ast_nodes[1]
assert isinstance(b, nodes.FunctionDef)
assert b.position == (4, 0, 4, 5)
c = ast_nodes[2]
assert isinstance(c, nodes.FunctionDef)
assert c.position == (7, 0, 7, 5)
d = ast_nodes[3]
assert isinstance(d, nodes.FunctionDef)
assert d.position == (10, 4, 10, 9)
e = ast_nodes[4]
assert isinstance(e, nodes.FunctionDef)
assert e.position == (14, 0, 14, 5)
@staticmethod
def test_position_async_function() -> None:
"""Position should only include keyword and name.
>>> async def func(var: int = 42):
>>> ^^^^^^^^^^^^^^
"""
code = textwrap.dedent(
"""
async def a(): #@
...
async def b(): #@
'''Docstring'''
async def c( #@
var: int = 42
):
async def d(): #@
...
@decorator
async def e(): #@
...
"""
).strip()
ast_nodes: list[nodes.NodeNG] = builder.extract_node(code) # type: ignore[assignment]
a = ast_nodes[0]
assert isinstance(a, nodes.FunctionDef)
assert a.position == (1, 0, 1, 11)
b = ast_nodes[1]
assert isinstance(b, nodes.FunctionDef)
assert b.position == (4, 0, 4, 11)
c = ast_nodes[2]
assert isinstance(c, nodes.FunctionDef)
assert c.position == (7, 0, 7, 11)
d = ast_nodes[3]
assert isinstance(d, nodes.FunctionDef)
assert d.position == (10, 4, 10, 15)
e = ast_nodes[4]
assert isinstance(e, nodes.FunctionDef)
assert e.position == (14, 0, 14, 11)
|