|
| 1 | +# Copyright 2017-2020 Palantir Technologies, Inc. |
| 2 | +# Copyright 2021- Python Language Server Contributors. |
| 3 | + |
| 4 | +import os |
| 5 | + |
| 6 | +from pylsp import uris |
| 7 | +from pylsp.plugins.type_definition import pylsp_type_definition |
| 8 | +from pylsp.workspace import Document |
| 9 | + |
| 10 | +DOC_URI = uris.from_fs_path(__file__) |
| 11 | +DOC = """\ |
| 12 | +from dataclasses import dataclass |
| 13 | +
|
| 14 | +@dataclass |
| 15 | +class IntPair: |
| 16 | + a: int |
| 17 | + b: int |
| 18 | +
|
| 19 | +def main() -> None: |
| 20 | + l0 = list(1, 2) |
| 21 | +
|
| 22 | + my_pair = IntPair(a=10, b=20) |
| 23 | + print(f"Original pair: {my_pair}") |
| 24 | +""" |
| 25 | + |
| 26 | + |
| 27 | +def test_type_definitions(config, workspace) -> None: |
| 28 | + # Over 'IntPair' in 'main' |
| 29 | + cursor_pos = {"line": 10, "character": 14} |
| 30 | + |
| 31 | + # The definition of 'IntPair' |
| 32 | + def_range = { |
| 33 | + "start": {"line": 3, "character": 6}, |
| 34 | + "end": {"line": 3, "character": 13}, |
| 35 | + } |
| 36 | + |
| 37 | + doc = Document(DOC_URI, workspace, DOC) |
| 38 | + assert [{"uri": DOC_URI, "range": def_range}] == pylsp_type_definition( |
| 39 | + config, doc, cursor_pos |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +def test_builtin_definition(config, workspace) -> None: |
| 44 | + # Over 'list' in main |
| 45 | + cursor_pos = {"line": 8, "character": 9} |
| 46 | + |
| 47 | + doc = Document(DOC_URI, workspace, DOC) |
| 48 | + orig_settings = config.settings() |
| 49 | + |
| 50 | + defns = pylsp_type_definition(config, doc, cursor_pos) |
| 51 | + assert len(defns) == 1 |
| 52 | + assert defns[0]["uri"].endswith("builtins.pyi") |
| 53 | + |
| 54 | + |
| 55 | +def test_mutli_file_type_definitions(config, workspace, tmpdir) -> None: |
| 56 | + # Create a dummy module out of the workspace's root_path and try to get |
| 57 | + # a definition on it in another file placed next to it. |
| 58 | + module_content = """\ |
| 59 | +from dataclasses import dataclass |
| 60 | +
|
| 61 | +@dataclass |
| 62 | +class IntPair: |
| 63 | + a: int |
| 64 | + b: int |
| 65 | +""" |
| 66 | + p1 = tmpdir.join("intpair.py") |
| 67 | + p1.write(module_content) |
| 68 | + # The uri for intpair.py |
| 69 | + module_path = str(p1) |
| 70 | + module_uri = uris.from_fs_path(module_path) |
| 71 | + |
| 72 | + # Content of doc to test type definition |
| 73 | + doc_content = """\ |
| 74 | +from intpair import IntPair |
| 75 | +
|
| 76 | +def main() -> None: |
| 77 | + l0 = list(1, 2) |
| 78 | +
|
| 79 | + my_pair = IntPair(a=10, b=20) |
| 80 | + print(f"Original pair: {my_pair}") |
| 81 | +""" |
| 82 | + p2 = tmpdir.join("main.py") |
| 83 | + p2.write(doc_content) |
| 84 | + doc_path = str(p2) |
| 85 | + doc_uri = uris.from_fs_path(doc_path) |
| 86 | + |
| 87 | + doc = Document(doc_uri, workspace, doc_content) |
| 88 | + |
| 89 | + # The range where IntPair is defined in intpair.py |
| 90 | + def_range = { |
| 91 | + "start": {"line": 3, "character": 6}, |
| 92 | + "end": {"line": 3, "character": 13}, |
| 93 | + } |
| 94 | + |
| 95 | + # The position where IntPair is called in main.py |
| 96 | + cursor_pos = {"line": 5, "character": 14} |
| 97 | + |
| 98 | + print("!URI", module_uri) |
| 99 | + print("!URI", doc_uri) |
| 100 | + |
| 101 | + assert [{"uri": module_uri, "range": def_range}] == pylsp_type_definition( |
| 102 | + config, doc, cursor_pos |
| 103 | + ) |
0 commit comments