Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No suggestions from autoimport #431

Open
WhyNotHugo opened this issue Sep 6, 2023 · 14 comments
Open

No suggestions from autoimport #431

WhyNotHugo opened this issue Sep 6, 2023 · 14 comments

Comments

@WhyNotHugo
Copy link

WhyNotHugo commented Sep 6, 2023

I can't get autoimport to work. I've followed the instructions in the documentation and a added rope_autoimport in the relevant section of my LSP's configuration:

   pylsp = {
     cmd = vim.lsp.rpc.connect("127.0.0.1", 9977),
     settings = {
       pylsp = {
         plugins = {
           -- These functionalities are handled by ruff:
           pyflakes = { enabled = false },
           pycodestyle = { enabled = false },
           -- I use black for formatting:
           yapf = { enabled = false },
           autopep8 = { enabled = false },
           black = { enabled = true }, -- NOTE: Requires `py3-lsp-black`.
           -- Others:
           mypy = { enabled = true },
+          rope_autoimport = { enabled = true },
         },
       },
     },
   },

I'm opening a file in a project with the following content:

from todoman import version  # type: ignore[attr-defined]

__version__ = version.version
__documentation__ = "https://todoman.rtfd.org/en/latest/"

Decimal(0)

The LSP logs:

2023-09-06 17:23:58,272 CEST - INFO - pylsp.python_lsp - Serving PythonLSPServer on (127.0.0.1, 9977)
2023-09-06 17:26:15,792 CEST - INFO - pylsp.config.config - Failed to load pylsp entry point 'autopep8': No module named 'pycodestyle'
2023-09-06 17:26:15,796 CEST - INFO - pylsp.config.config - Failed to load pylsp entry point 'mccabe': No module named 'mccabe'
2023-09-06 17:26:15,797 CEST - INFO - pylsp.config.config - Failed to load pylsp entry point 'pycodestyle': No module named 'pycodestyle'
2023-09-06 17:26:15,797 CEST - INFO - pylsp.config.config - Failed to load pylsp entry point 'pydocstyle': No module named 'pydocstyle'
2023-09-06 17:26:15,798 CEST - INFO - pylsp.config.config - Failed to load pylsp entry point 'pyflakes': No module named 'pyflakes'
2023-09-06 17:26:15,817 CEST - INFO - pylsp.config.config - Failed to load pylsp entry point 'yapf': No module named 'yapf'
2023-09-06 17:26:15,823 CEST - INFO - pylsp.config.config - Loaded pylsp plugin pylsp_mypy from <module 'pylsp_mypy.plugin' from '/usr/lib/python3.11/site-packages/pylsp_mypy/plugin.py'>
2023-09-06 17:26:15,823 CEST - INFO - pylsp.config.config - Loaded pylsp plugin pylsp_black from <module 'pylsp_black.plugin' from '/usr/lib/python3.11/site-packages/pylsp_black/plugin.py'>
2023-09-06 17:26:15,823 CEST - INFO - pylsp.config.config - Loaded pylsp plugin flake8 from <module 'pylsp.plugins.flake8_lint' from '/usr/lib/python3.11/site-packages/pylsp/plugins/flake8_lint.py'>
2023-09-06 17:26:15,823 CEST - INFO - pylsp.config.config - Loaded pylsp plugin folding from <module 'pylsp.plugins.folding' from '/usr/lib/python3.11/site-packages/pylsp/plugins/folding.py'>
2023-09-06 17:26:15,823 CEST - INFO - pylsp.config.config - Loaded pylsp plugin jedi_completion from <module 'pylsp.plugins.jedi_completion' from '/usr/lib/python3.11/site-packages/pylsp/plugins/jedi_completion.py'>
2023-09-06 17:26:15,823 CEST - INFO - pylsp.config.config - Loaded pylsp plugin jedi_definition from <module 'pylsp.plugins.definition' from '/usr/lib/python3.11/site-packages/pylsp/plugins/definition.py'>
2023-09-06 17:26:15,823 CEST - INFO - pylsp.config.config - Loaded pylsp plugin jedi_highlight from <module 'pylsp.plugins.highlight' from '/usr/lib/python3.11/site-packages/pylsp/plugins/highlight.py'>
2023-09-06 17:26:15,823 CEST - INFO - pylsp.config.config - Loaded pylsp plugin jedi_hover from <module 'pylsp.plugins.hover' from '/usr/lib/python3.11/site-packages/pylsp/plugins/hover.py'>
2023-09-06 17:26:15,823 CEST - INFO - pylsp.config.config - Loaded pylsp plugin jedi_references from <module 'pylsp.plugins.references' from '/usr/lib/python3.11/site-packages/pylsp/plugins/references.py'>
2023-09-06 17:26:15,823 CEST - INFO - pylsp.config.config - Loaded pylsp plugin jedi_rename from <module 'pylsp.plugins.jedi_rename' from '/usr/lib/python3.11/site-packages/pylsp/plugins/jedi_rename.py'>
2023-09-06 17:26:15,823 CEST - INFO - pylsp.config.config - Loaded pylsp plugin jedi_signature_help from <module 'pylsp.plugins.signature' from '/usr/lib/python3.11/site-packages/pylsp/plugins/signature.py'>
2023-09-06 17:26:15,823 CEST - INFO - pylsp.config.config - Loaded pylsp plugin jedi_symbols from <module 'pylsp.plugins.symbols' from '/usr/lib/python3.11/site-packages/pylsp/plugins/symbols.py'>
2023-09-06 17:26:15,823 CEST - INFO - pylsp.config.config - Loaded pylsp plugin preload from <module 'pylsp.plugins.preload_imports' from '/usr/lib/python3.11/site-packages/pylsp/plugins/preload_imports.py'>
2023-09-06 17:26:15,823 CEST - INFO - pylsp.config.config - Loaded pylsp plugin pylint from <module 'pylsp.plugins.pylint_lint' from '/usr/lib/python3.11/site-packages/pylsp/plugins/pylint_lint.py'>
2023-09-06 17:26:15,823 CEST - INFO - pylsp.config.config - Loaded pylsp plugin rope_autoimport from <module 'pylsp.plugins.rope_autoimport' from '/usr/lib/python3.11/site-packages/pylsp/plugins/rope_autoimport.py'>
2023-09-06 17:26:15,823 CEST - INFO - pylsp.config.config - Loaded pylsp plugin rope_completion from <module 'pylsp.plugins.rope_completion' from '/usr/lib/python3.11/site-packages/pylsp/plugins/rope_completion.py'>
2023-09-06 17:26:15,823 CEST - INFO - pylsp.config.config - Loaded pylsp plugin rope_rename from <module 'pylsp.plugins.rope_rename' from '/usr/lib/python3.11/site-packages/pylsp/plugins/rope_rename.py'>
2023-09-06 17:26:15,823 CEST - INFO - pylsp_mypy.plugin - init workspace = /home/hugo/src/github.com/pimutils/todoman
2023-09-06 17:26:15,824 CEST - INFO - pylsp_mypy.plugin - mypyConfigFile = /home/hugo/src/github.com/pimutils/todoman/pyproject.toml configuration = {}
2023-09-06 17:26:15,824 CEST - INFO - pylsp.config.config - Disabled plugins: [None, None, <module 'pylsp.plugins.flake8_lint' from '/usr/lib/python3.11/site-packages/pylsp/plugins/flake8_lint.py'>, <module 'pylsp.plugins.pylint_lint' from '/usr/lib/python3.11/site-packages/pylsp/plugins/pylint_lint.py'>, <module 'pylsp.plugins.rope_autoimport' from '/usr/lib/python3.11/site-packages/pylsp/plugins/rope_autoimport.py'>, <module 'pylsp.plugins.rope_completion' from '/usr/lib/python3.11/site-packages/pylsp/plugins/rope_completion.py'>, <module 'pylsp.plugins.rope_rename' from '/usr/lib/python3.11/site-packages/pylsp/plugins/rope_rename.py'>]
2023-09-06 17:26:15,864 CEST - INFO - pylsp.python_lsp - Server capabilities: {'codeActionProvider': True, 'codeLensProvider': {'resolveProvider': False}, 'completionProvider': {'resolveProvider': True, 'triggerCharacters': ['.']}, 'documentFormattingProvider': True, 'documentHighlightProvider': True, 'documentRangeFormattingProvider': True, 'documentSymbolProvider': True, 'definitionProvider': True, 'executeCommandProvider': {'commands': []}, 'hoverProvider': True, 'referencesProvider': True, 'renameProvider': True, 'foldingRangeProvider': True, 'signatureHelpProvider': {'triggerCharacters': ['(', ',', '=']}, 'textDocumentSync': {'change': 2, 'save': {'includeText': True}, 'openClose': True}, 'workspace': {'workspaceFolders': {'supported': True, 'changeNotifications': True}}, 'experimental': {}}
2023-09-06 17:26:15,864 CEST - INFO - pylsp.config.config - Updated settings to {'plugins': {'pycodestyle': {'enabled': False}, 'yapf': {'enabled': False}, 'autopep8': {'enabled': False}, 'black': {'enabled': True}, 'mypy': {'enabled': True}, 'rope_autoimport': {'enabled': True}, 'pyflakes': {'enabled': False}}}
2023-09-06 17:26:15,864 CEST - INFO - pylsp.config.config - Disabled plugins: [None, None, None, None, <module 'pylsp.plugins.flake8_lint' from '/usr/lib/python3.11/site-packages/pylsp/plugins/flake8_lint.py'>, <module 'pylsp.plugins.pylint_lint' from '/usr/lib/python3.11/site-packages/pylsp/plugins/pylint_lint.py'>, <module 'pylsp.plugins.rope_completion' from '/usr/lib/python3.11/site-packages/pylsp/plugins/rope_completion.py'>, <module 'pylsp.plugins.rope_rename' from '/usr/lib/python3.11/site-packages/pylsp/plugins/rope_rename.py'>]
2023-09-06 17:26:15,864 CEST - INFO - pylsp.config.config - Updated settings to {'plugins': {'pycodestyle': {'enabled': False}, 'yapf': {'enabled': False}, 'autopep8': {'enabled': False}, 'black': {'enabled': True}, 'mypy': {'enabled': True}, 'rope_autoimport': {'enabled': True}, 'pyflakes': {'enabled': False}}}
2023-09-06 17:26:15,864 CEST - INFO - pylsp.config.config - Disabled plugins: [None, None, None, None, <module 'pylsp.plugins.flake8_lint' from '/usr/lib/python3.11/site-packages/pylsp/plugins/flake8_lint.py'>, <module 'pylsp.plugins.pylint_lint' from '/usr/lib/python3.11/site-packages/pylsp/plugins/pylint_lint.py'>, <module 'pylsp.plugins.rope_completion' from '/usr/lib/python3.11/site-packages/pylsp/plugins/rope_completion.py'>, <module 'pylsp.plugins.rope_rename' from '/usr/lib/python3.11/site-packages/pylsp/plugins/rope_rename.py'>]
2023-09-06 17:26:15,864 CEST - INFO - pylsp.config.config - Updated settings to {'plugins': {'pycodestyle': {'enabled': False}, 'yapf': {'enabled': False}, 'autopep8': {'enabled': False}, 'black': {'enabled': True}, 'mypy': {'enabled': True}, 'rope_autoimport': {'enabled': True}, 'pyflakes': {'enabled': False}}}
2023-09-06 17:26:15,865 CEST - INFO - pylsp.config.config - Disabled plugins: [None, None, None, None, <module 'pylsp.plugins.flake8_lint' from '/usr/lib/python3.11/site-packages/pylsp/plugins/flake8_lint.py'>, <module 'pylsp.plugins.pylint_lint' from '/usr/lib/python3.11/site-packages/pylsp/plugins/pylint_lint.py'>, <module 'pylsp.plugins.rope_completion' from '/usr/lib/python3.11/site-packages/pylsp/plugins/rope_completion.py'>, <module 'pylsp.plugins.rope_rename' from '/usr/lib/python3.11/site-packages/pylsp/plugins/rope_rename.py'>]
2023-09-06 17:26:15,865 CEST - INFO - pylsp.config.config - Updated settings to {'plugins': {'pycodestyle': {'enabled': False}, 'yapf': {'enabled': False}, 'autopep8': {'enabled': False}, 'black': {'enabled': True}, 'mypy': {'enabled': True}, 'rope_autoimport': {'enabled': True}, 'pyflakes': {'enabled': False}}}
2023-09-06 17:26:15,865 CEST - INFO - pylsp.config.config - Disabled plugins: [None, None, None, None, <module 'pylsp.plugins.flake8_lint' from '/usr/lib/python3.11/site-packages/pylsp/plugins/flake8_lint.py'>, <module 'pylsp.plugins.pylint_lint' from '/usr/lib/python3.11/site-packages/pylsp/plugins/pylint_lint.py'>, <module 'pylsp.plugins.rope_completion' from '/usr/lib/python3.11/site-packages/pylsp/plugins/rope_completion.py'>, <module 'pylsp.plugins.rope_rename' from '/usr/lib/python3.11/site-packages/pylsp/plugins/rope_rename.py'>]
2023-09-06 17:26:16,438 CEST - INFO - pylsp_mypy.plugin - lint settings = {} document.path = /home/hugo/src/github.com/pimutils/todoman/todoman/__init__.py is_saved = True
2023-09-06 17:26:16,438 CEST - INFO - pylsp_mypy.plugin - executing mypy args = ['--show-error-end', '--no-error-summary', '--config-file', '/home/hugo/src/github.com/pimutils/todoman/pyproject.toml', '/home/hugo/src/github.com/pimutils/todoman/todoman/__init__.py', '--incremental', '--follow-imports', 'silent'] on path
2023-09-06 17:26:16,621 CEST - INFO - pylsp_mypy.plugin - pylsp-mypy len(diagnostics) = 1

After putting the mouse cursor over Decimal (note that its import is missing), I request any available actions from the LSP. Neovim reports that no actions are available (I don't see any additional logs while this happens, which is odd).

Any hints as to what might be wrong? I'm not sure what other information can be of use here.

@magnuslarsen
Copy link

magnuslarsen commented Oct 2, 2023

I have the same problem, though I get a couple of errors from rope itself:

> rg rope.plugin ~/.local/state/nvim/
/home/mlar/.local/state/nvim/lsp.log
5515:[ERROR][2023-09-05 08:11:16] .../vim/lsp/rpc.lua:675       "rpc"   "/home/mlar/.local/share/nvim/mason/bin/pylsp"  "stderr"        '2023-09-05 08:11:16,428 CEST - ERROR - pylsp_rope.plugin - Exception when doing workspace/executeCommand: Syntax error in file <string> line <1>: expected an indented block after function definition on line 1\nTraceback (most recent call last):\n  File "/home/mlar/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/rope/base/pyobjectsdef.py", line 200, in _init_source\n    ast_node = ast.parse(source_bytes, filename=filename)\n  File "/home/mlar/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/rope/base/ast.py", line 33, in parse\n    return ast.parse(source, filename=filename, *args, **kwargs)\n  File "/usr/lib/python3.10/ast.py", line 50, in parse\n    return compile(source, filename, mode, flags,\n  File "string", line 1\n    def f():\n            ^\nIndentationError: expected an indented block after function definition on line 1\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n  File "/home/mlar/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/pylsp_rope/plugin.py", line 143, in pylsp_execute_command\n    return commands[command](workspace, **arguments[0])()\n  File "/home/mlar/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/pylsp_rope/refactoring.py", line 36, in __call__\n    rope_changeset = self.get_changes()\n  File "/home/mlar/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/pylsp_rope/refactoring.py", line 100, in get_changes\n    rope_changeset = refactoring.get_changes(\n  File "/home/mlar/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/rope/refactor/extract.py", line 82, in get_changes\n    new_contents = _ExtractPerformer(info).extract()\n  File "/home/mlar/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/rope/refactor/extract.py", line 287, in extract\n    extract_info = self._collect_info()\n  File "/home/mlar/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/rope/refactor/extract.py", line 313, in _collect_info\n    self._find_definition(extract_collector)\n  File "/home/mlar/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/rope/refactor/extract.py", line 387, in _find_definition\n    collector.body_pattern = parts.get_body_pattern()\n  File "/home/mlar/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/rope/refactor/extract.py", line 562, in get_body_pattern\n    variables.extend(self._find_temps())\n  File "/home/mlar/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/rope/refactor/extract.py", line 572, in _find_temps\n    return usefunction.find_temps(self.info.project, self._get_body())\n  File "/home/mlar/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/rope/refactor/usefunction.py", line 143, in find_temps\n    pymodule = libutils.get_string_module(project, code)\n  File "/home/mlar/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/rope/base/libutils.py", line 91, in get_string_module\n    return pyobjectsdef.PyModule(\n  File "/home/mlar/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/rope/base/pyobjectsdef.py", line 173, in __init__\n    source, node = self._init_source(pycore, source, resource)\n  File "/home/mlar/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/rope/base/pyobjectsdef.py", line 202, in _init_source\n    raise exceptions.ModuleSyntaxError(filename, e.lineno, e.msg)\nrope.base.exceptions.ModuleSyntaxError: Syntax error in file <string> line <1>: expected an indented block after function definition on line 1\n'
5519:[ERROR][2023-09-05 08:11:26] .../vim/lsp/rpc.lua:675       "rpc"   "/home/mlar/.local/share/nvim/mason/bin/pylsp"  "stderr"        '2023-09-05 08:11:26,424 CEST - ERROR - pylsp_rope.plugin - Exception when doing workspace/executeCommand: Convert local variable to field should be performed on a local variable of a method.\nTraceback (most recent call last):\n  File "/home/mlar/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/pylsp_rope/plugin.py", line 143, in pylsp_execute_command\n    return commands[command](workspace, **arguments[0])()\n  File "/home/mlar/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/pylsp_rope/refactoring.py", line 36, in __call__\n    rope_changeset = self.get_changes()\n  File "/home/mlar/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/pylsp_rope/refactoring.py", line 306, in get_changes\n    rope_changeset = refactoring.get_changes()\n  File "/home/mlar/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/rope/refactor/localtofield.py", line 16, in get_changes\n    raise exceptions.RefactoringError(\nrope.base.exceptions.RefactoringError: Convert local variable to field should be performed on a local variable of a method.\n'

I am on Ubuntu 22.04, using Mason to manage the venv (I installed rope with :PylspInstall rope, to bring it into the same venv)

@lieryan
Copy link
Contributor

lieryan commented Oct 2, 2023

I think this ticket is confounding a number of things. The autoimport mechanism as currently implemented in pylsp is triggered by doing code completion, not by code action. When you finish autocompleting a symbol that wasn't in the current scope, pylsp would insert the import statement at the top of the file. rope does support adding import statement for unrecognized symbol under the cursor, but AFAIK this is not yet implemented in either pylsp's builtin rope support nor in pylsp-rope.

Also, if you're using Neovim's builtin LSP client, please try again using a different LSP client. Neovim's builtin LSP is currently unusably broken when it comes to handling CodeAction. Neovim's builtin LSP sometimes just randomly and inexplicably didn't pass in the line numbers that the user has selected to the language server. If you're interested in getting this fixed, please reach out to Neovim's authors to fix their builtin LSP's CodeAction handling. Or use any of the other supported LSP clients. vim-lsp, coc, and ALE all works perfectly with pylsp and pylsp-rope on neovim.

@WhyNotHugo
Copy link
Author

The autoimport mechanism as currently implemented in pylsp is triggered by doing code completion, not by code action.When you finish autocompleting a symbol that wasn't in the current scope, pylsp would insert the import statement at the top of the file.

This doesn't fully make sense to me. In order to get autoimport to import a symbol, I need to select that symbol from the autocompletion list. However, the autocompletion only suggests symbols which are already imported.

So in order to get an autocompletion, I need to first import the symbol, but to import the symbol, I need to pick it from the autocompletion list.

Doesn't this mean that the feature is impossible to use?

rope does support adding import statement for unrecognized symbol under the cursor, but AFAIK this is not yet implemented in either pylsp's builtin rope support nor in pylsp-rope.

I think this is the feature that I'm really looking for. Would it be best for this to be implemented in pylsp's builtin rope support or in pylsp-rope?

Also, if you're using Neovim's builtin LSP client, please try again using a different LSP client. Neovim's builtin LSP is currently unusably broken when it comes to handling CodeAction.

Didn't you just say that autoimport doesn't operate via code-actions? How would using a different LSP client change the result here?

@lieryan
Copy link
Contributor

lieryan commented Oct 10, 2023

@WhyNotHugo Hi, sorry for the late reply, I was in the shower thinking about other things when I suddenly remembered that I never responded to your question here.

Basically, the way this works is that if you want to use rope_autoimport, you need to change the pylsp completion provider to rope. IOW, you need set pylsp.plugins.rope_completion.enabled = true, pylsp.plugins.rope_autoimport.enabled = true, and pylsp.plugins.jedi_completion.enabled = false. When autoimport is enabled, the completion behavior changes to allow completing from every global names in your python path (i.e. from everything in standard library and virtualenv) and any name visible in the projects. This is how autocomplete looks like when rope_autoimport is enabled (in Vim using vim-lsp):

image

When you accept the completion, for example, by typing ( or ., then autoimport will insert the import line at the top of the file, after all the existing imports.

image

You'd normally want to run rope organise import (this is available in pylsp-rope as codeaction) and/or isort after that.

Also, if you're using Neovim's builtin LSP client, please try again using a different LSP client. Neovim's builtin LSP is currently unusably broken when it comes to handling CodeAction.

Didn't you just say that autoimport doesn't operate via code-actions? How would using a different LSP client change the result here?

Ah, sorry for the confusion, that wasn't about the autoimport feature itself as currently implemented. I was responding to because you and @magnuslarsen trying to use LSP code action. python-lsp-server have plugin hooks that allows external plugins like pylsp-rope to implement code actions but AFAIK none of its built-in plugins actually implements any code actions.

Implementing autoimport into pylsp-rope is definitely on the list of what I want to have too, it just wasn't clear how actions like this is best implemented in terms of what LSP standard provides. In the trivial case, when there's only one unambiguous choice autoimport as code action is just inserting a single line to the file, which is fine, but in more complex case, there could be multiple aliases with the same name to import from (e.g. if you try to autoimport loads, from the standard library itself there are at least 2 versions of it from json.loads and marshall.loads, and there may be half a dozen more if you have other popular third party libraries installed.

Ideally, there would be an additional interactive menu that pops up after selecting the code action to request for additional prompts for the user to pass in additional choices/arguments to the actions, but the code action protocol doesn't seem to make it obvious how or whether something like that is even possible.

@tkrabel-db
Copy link
Contributor

@WhyNotHugo not sure this actually addresses your issue, but I found there was a bug that we don't set LSP preferences when the server realizes an initialize message. My #459 changes that.

Happy to also address your configuration file use case if you tell me what exactly you wanted to to.

@tkrabel
Copy link
Contributor

tkrabel commented Oct 28, 2023

Update: I added a way to get autoimport suggestions as part of code actions #471
@WhyNotHugo does this address your issue?

@Shane-XB-Qian
Copy link

#459 should be not released yet, but i tried to set it via ws config.
and i try and verify a bit, TBH to me i felt a bit bad experience and perf,
maybe 'rope' is not a right approach, how about thinking to persuade 'ruff'? 😄

@bagel897
Copy link
Contributor

bagel897 commented Oct 30, 2023

Just a note, when I wrote pylsp autoimport, I tested it against jedi-completion only. I never ran it with rope-completion, though they are from the same project. In fact, pylsp-autoimport shares components from Jedi (it uses parsers from there). These components are likely causing issues as they are AST parsers I wrote myself.
The plugin uses 3 AST components that could break:

  1. _should_insert in the pylsp plugin, using parso. This is the most fragile
  2. There's another one that calls Jedi
  3. The core importer logic in rope (though this could be swapped for just importing modules, perhaps as a configuration option)

Note: this may not reflect the current status, just the initial development from a year ago.

@tkrabel
Copy link
Contributor

tkrabel commented Nov 1, 2023

#459 should be not released yet, but i tried to set it via ws config. and i try and verify a bit, TBH to me i felt a bit bad experience and perf, maybe 'rope' is not a right approach, how about thinking to persuade 'ruff'? 😄

Well, I was diving a bit into the python rope code base and found a quick improvement that should speed up the autoimporting process by ~2x (python-rope/rope#722)

@bagel897
Copy link
Contributor

bagel897 commented Nov 2, 2023

There could be two performance bottlenecks:

Parsing/Discovery:

The vast majority of this time (when I profiled it) was AST parsing. The above solution will fix a discovery bug but it can't surpass this limitation. The options to meaningfully improve indexing time are limited:

  • Switch ASTs to something else. The code should use stdlib ast, not rope's patched AST. Not sure if any other one is better. Maybe the Rust AST?
  • Use the import-based parser instead of the AST parser - this will have severe security issues but is very easy to implement
  • Use multithreading instead of multi processing. This won't work due to the GIL

Completion

This does 3 things

  • Parse the file to determine if it should insert at the current spot
  • Get the current word under the cursor
  • Query the sqllite db
  • Sort all the statements

We have a lot of room to improve here, especially on the first two. I'd look into the Jedi Environments or using async or something. The sqlite db can also implement a lot of the sorting/indexing/deduplicating - I just didn't know that when I wrote the original queries

@magnuslarsen
Copy link

I just want to report back (a little late...), with the newest version of pylsp autoimport works, even using jedi_completion - thanks all!

@tkrabel
Copy link
Contributor

tkrabel commented Dec 22, 2023

@WhyNotHugo pls let us know if the issue has been resolved for you.

@eyalk11
Copy link

eyalk11 commented Dec 25, 2023

I seem to have the same issue with newer version.

@bagel897
Copy link
Contributor

It may be a bug in should_insert

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants