python-lsp / python-lsp-server

Fork of the python-language-server project, maintained by the Spyder IDE team and the community
MIT License
1.76k stars 186 forks source link

why pslsp is not returning correct definition? #424

Closed code2graph closed 10 months ago

code2graph commented 10 months ago

I am sending a go to definition request to pylsp using websockets. However, correct deifinition is not returned.

Here is the definition_request:

definition_request = {
    "jsonrpc": "2.0",
    "id": 2,
    "method": "textDocument/definition",
    "params": {
        "textDocument": {
            "uri": "file://" + os.path.join(root_path, file_name)
        },
        "position": {
            "line": line,
            "character": character
        }
    }
}

For the following code, I want to get the definition for check_files_in_directory which is defined in the GPT4Readability.utils file. But somehow pylsp is not able to provide me with the correct definition location.

GPT4Readability/readme_gen.py file:

import os
from getpass import getpass
from GPT4Readability.utils import *
import importlib.resources as pkg_resources  

def generate_readme(root_dir, output_name, model):
    """Generates a README.md file based on the python files in the provided directory

    Args:
        root_dir (str): The root directory of the python package to parse and generate a readme for
    """

    # prompt_folder_name = os.path.join(os.path.dirname(__file__), "prompts")
    # prompt_path = os.path.join(prompt_folder_name, "readme_prompt.txt")

    with pkg_resources.open_text('GPT4Readability.prompts','readme_prompt.txt') as f:         
        inb_msg = f.read()

    # with open(prompt_path) as f:
    #     lines = f.readlines()
    # inb_msg = "".join(lines)

    file_check_result = check_files_in_directory(root_dir)
....

I get the following response:

finish pylsp_definitions --> [[{'uri': 'file:///path/to/the/repo/GPT4Readability/GPT4Readability/readme_gen.py', 'range': {'start': {'line': 25, 'character': 4}, 'end': {'line': 25, 'character': 29}}}]]

Very strange as it thinks that check_files_in_directory is defined in the same GPT4Readability/readme_gen.py file. Why is that?

Am I missing something in this case?

rchl commented 10 months ago

Maybe you've specified wrong line. Remember that lines are 0-based in LSP.

code2graph commented 10 months ago

Does python-lsp-server support wild card imports? I think that's the issue.

from GPT4Readability.utils import *
ccordoba12 commented 10 months ago

Pylsp is only a wrapper over several Python code completion and linting libraries. In this case Jedi is used to get definitions.

So, please ask in its repo if Jedi supports star imports (I guess not and that's why you don't get the definition you want).