Improve the handling of the pylsp-auto-import-modules patch

This commit is contained in:
Gerard Vermeulen 2022-09-18 11:13:02 +02:00
parent 77b3a503be
commit 02cfb36efc
2 changed files with 9 additions and 67 deletions

View File

@ -3984,17 +3984,17 @@ agree with [[https://black.readthedocs.io/en/stable/index.html][black's uncompro
#+end_src
[[https://jedi.readthedocs.io/en/latest/][Jedi]] provides grammar checking and completion candidates to [[https://github.com/python-lsp/python-lsp-server][python-lsp-server]].
Only [[https://jedi.readthedocs.io/en/latest/docs/changelog.html][jedi-0.18.1]] works with for instance [[https://numpy.org/][numpy-1.23.0]] in the sense that it does
not choke on universal functions provided that [[https://jedi.readthedocs.io/en/latest/][jedi]] does not parse but imports
[[https://numpy.org/][numpy-1.23.1]] (see [[https://github.com/davidhalter/jedi/issues/1744][jedi issue #1744]], [[https://github.com/davidhalter/jedi/issues/1745][#1745]], and [[https://github.com/davidhalter/jedi/issues/1746][#1746]]). Since the universal
Only [[https://jedi.readthedocs.io/en/latest/docs/changelog.html][jedi-0.18.1]] works with for instance [[https://numpy.org/][numpy-1.23.3]] in the sense that it does
not choke on universal functions provided that [[https://jedi.readthedocs.io/en/latest/][jedi]] does not parse but import
[[https://numpy.org/][numpy-1.23.3]] (see [[https://github.com/davidhalter/jedi/issues/1744][jedi issue #1744]], [[https://github.com/davidhalter/jedi/issues/1745][#1745]], and [[https://github.com/davidhalter/jedi/issues/1746][#1746]]). Since the universal
functions are neither builtin methods nor data instances but a kind of "callable
instances", the [[https://docs.python.org/3/library/inspect.html][Python inspect]] module also fails to handle the universal
functions properly.
Listing [[lst:make-pylsp-server-patch]] generates and listing
[[lst:echo-pylsp-server-patch]] echos the patch in listing
[[lst:show-pylsp-server-patch]] to make [[https://jedi.readthedocs.io/en/latest/][jedi]] import [[https://numpy.org/][numpy-1.22.2]] in order to serve
the information to [[https://github.com/python-lsp/python-lsp-server][python-lsp-server]] allowing it to handle universal functions.
[[lst:show-pylsp-server-patch]] shows the patch to make [[https://jedi.readthedocs.io/en/latest/][jedi]] import [[https://numpy.org/][numpy-1.23.3]] in
order to obtain the necessary information to make [[https://github.com/python-lsp/python-lsp-server][python-lsp-server]] handle
universal functions.
#+caption[Make =pylsp-auto-import-modules.patch= listing]:
#+caption: Make =pylsp-auto-import-modules.patch= listing.
@ -4003,69 +4003,11 @@ the information to [[https://github.com/python-lsp/python-lsp-server][python-lsp
git -C $HOME/VCS/python-lsp-server diff >pylsp-auto-import-modules.patch
#+end_src
#+caption[Echo =pylsp-auto-import-modules.patch= listing]:
#+caption: Echo =pylsp-auto-import-modules.patch= listing.
#+name: lst:echo-pylsp-server-patch
#+begin_src shell :exports both :results drawer
echo "#+attr_latex: :options breaklines"
echo "#+caption[Show =pylsp-auto-import-modules.patch=]:"
echo "#+caption: Show =pylsp-auto-import-modules.patch=."
echo "#+name: lst:show-pylsp-server-patch"
echo "#+begin_src diff :tangle no"
cat pylsp-auto-import-modules.patch
echo -n "#+end_src"
#+end_src
#+RESULTS: lst:echo-pylsp-server-patch
:results:
#+attr_latex: :options breaklines
#+caption[Show =pylsp-auto-import-modules.patch=]:
#+caption: Show =pylsp-auto-import-modules.patch=.
#+name: lst:show-pylsp-server-patch
#+begin_src diff :tangle no
diff --git a/pylsp/config/schema.json b/pylsp/config/schema.json
index 9e744ac..d369432 100644
--- a/pylsp/config/schema.json
+++ b/pylsp/config/schema.json
@@ -87,6 +87,14 @@
"uniqueItems": true,
"description": "List of errors and warnings to enable."
},
+ "pylsp.plugins.jedi.auto_import_modules": {
+ "type": "array",
+ "default": ["numpy", "gi"],
+ "items": {
+ "type": "string"
+ },
+ "description": "List of module names for jedi to import (jedi.settings.auto_import_modules)."
+ },
"pylsp.plugins.jedi.extra_paths": {
"type": "array",
"default": [],
diff --git a/pylsp/workspace.py b/pylsp/workspace.py
index bf312f6..4758b53 100644
--- a/pylsp/workspace.py
+++ b/pylsp/workspace.py
@@ -14,6 +14,8 @@ from . import lsp, uris, _utils
log = logging.getLogger(__name__)
+DEFAULT_AUTO_IMPORT_MODULES = ["numpy", "gi"]
+
# TODO: this is not the best e.g. we capture numbers
RE_START_WORD = re.compile('[A-Za-z_0-9]*$')
RE_END_WORD = re.compile('^[A-Za-z_0-9]*')
@@ -252,6 +254,8 @@ class Document:
if self._config:
jedi_settings = self._config.plugin_settings('jedi', document_path=self.path)
+ jedi.settings.auto_import_modules = jedi_settings.get('auto_import_modules',
+ DEFAULT_AUTO_IMPORT_MODULES)
environment_path = jedi_settings.get('environment')
extra_paths = jedi_settings.get('extra_paths') or []
env_vars = jedi_settings.get('env_vars')
#+end_src
:end:
#+include: "pylsp-auto-import-modules.patch" src diff
*** [[https://jedi.readthedocs.io/en/latest/][Jedi]]
:PROPERTIES:

View File

@ -1,5 +1,5 @@
diff --git a/pylsp/config/schema.json b/pylsp/config/schema.json
index 9e744ac..d369432 100644
index 9e744ac..9b59d1c 100644
--- a/pylsp/config/schema.json
+++ b/pylsp/config/schema.json
@@ -87,6 +87,14 @@
@ -12,7 +12,7 @@ index 9e744ac..d369432 100644
+ "items": {
+ "type": "string"
+ },
+ "description": "List of module names for jedi to import (jedi.settings.auto_import_modules)."
+ "description": "List of module names for jedi.settings.auto_import_modules."
+ },
"pylsp.plugins.jedi.extra_paths": {
"type": "array",