|
4 | 4 | import os |
5 | 5 |
|
6 | 6 | import jedi |
| 7 | +import pytest |
7 | 8 |
|
8 | 9 | from pylsp import uris |
9 | 10 | from pylsp.plugins.definition import pylsp_definitions |
@@ -199,3 +200,44 @@ def is_definition(self) -> bool: |
199 | 200 | # follow_builtin_definitions defaults to True, so the None-guard in the |
200 | 201 | # return comprehension is the only thing preventing the crash. |
201 | 202 | assert pylsp_definitions(config, doc, {"line": 3, "character": 6}) == [] |
| 203 | + |
| 204 | + |
| 205 | +@pytest.mark.parametrize("follow_builtin_definitions", [True, False]) |
| 206 | +@pytest.mark.parametrize( |
| 207 | + "character, expected_range", |
| 208 | + [ |
| 209 | + pytest.param(3, None, id="generated-attribute"), |
| 210 | + pytest.param( |
| 211 | + 0, |
| 212 | + { |
| 213 | + "start": {"line": 4, "character": 0}, |
| 214 | + "end": {"line": 4, "character": 1}, |
| 215 | + }, |
| 216 | + id="source-backed-instance", |
| 217 | + ), |
| 218 | + ], |
| 219 | +) |
| 220 | +def test_namedtuple_definitions( |
| 221 | + config, workspace, follow_builtin_definitions, character, expected_range |
| 222 | +) -> None: |
| 223 | + source = """from collections import namedtuple |
| 224 | +
|
| 225 | +Access = namedtuple("Access", ["address", "type"]) |
| 226 | +
|
| 227 | +a = Access(100, "w") |
| 228 | +a.address |
| 229 | +""" |
| 230 | + doc = Document(DOC_URI, workspace, source) |
| 231 | + config.update( |
| 232 | + { |
| 233 | + "plugins": { |
| 234 | + "jedi_definition": { |
| 235 | + "follow_builtin_definitions": follow_builtin_definitions |
| 236 | + } |
| 237 | + } |
| 238 | + } |
| 239 | + ) |
| 240 | + expected = [{"uri": DOC_URI, "range": expected_range}] if expected_range else [] |
| 241 | + assert ( |
| 242 | + pylsp_definitions(config, doc, {"line": 5, "character": character}) == expected |
| 243 | + ) |
0 commit comments