Using Jedi¶
Jedi is can be used with a variety of plugins, language servers and other software. It is also possible to use Jedi in the Python shell or with IPython.
Below you can also find a list of recipes for type hinting.
Language Servers¶
- jedi-language-server
- python-language-server (currently unmaintained)
- python-lsp-server (fork from python-language-server)
- anakin-language-server
Editor Plugins¶
Visual Studio Code¶
Emacs¶
Sublime Text 2/3¶
- SublimeJEDI (ST2 & ST3)
- anaconda (only ST3)
Kate¶
- Kate version 4.13+ supports it natively, you have to enable it, though.
GNOME Builder¶
- GNOME Builder supports it natively, and is enabled by default.
xonsh shell¶
Jedi is a preinstalled extension in xonsh shell. Run the following command to enable:
xontrib load jedi
and many more!
Tab Completion in the Python Shell¶
Jedi is a dependency of IPython. Autocompletion in IPython is therefore possible without additional configuration.
Here is an example video how REPL completion can look like in a different shell.
There are two different options how you can use Jedi autocompletion in
your python
interpreter. One with your custom $HOME/.pythonrc.py
file
and one that uses PYTHONSTARTUP
.
Using PYTHONSTARTUP
¶
To use Jedi completion in Python interpreter, add the following in your shell
setup (e.g., .bashrc
). This works only on Linux/Mac, because readline is
not available on Windows. If you still want Jedi autocompletion in your REPL,
just use IPython instead:
export PYTHONSTARTUP="$(python -m jedi repl)"
Then you will be able to use Jedi completer in your Python interpreter:
$ python
Python 3.9.2+ (default, Jul 20 2020, 22:15:08)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.path.join('a', 'b').split().in<TAB> # doctest: +SKIP
..dex ..sert
Using a Custom $HOME/.pythonrc.py
¶
-
jedi.utils.
setup_readline
(namespace_module=<module 'sphinx.__main__' from '/home/docs/checkouts/readthedocs.org/user_builds/jedi/envs/latest/lib/python3.11/site-packages/sphinx/__main__.py'>, fuzzy=False)[source]¶ This function sets up
readline
to use Jedi in a Python interactive shell.If you want to use a custom
PYTHONSTARTUP
file (typically$HOME/.pythonrc.py
), you can add this piece of code:try: from jedi.utils import setup_readline except ImportError: # Fallback to the stdlib readline completer if it is installed. # Taken from http://docs.python.org/2/library/rlcompleter.html print("Jedi is not installed, falling back to readline") try: import readline import rlcompleter readline.parse_and_bind("tab: complete") except ImportError: print("Readline is not installed either. No tab completion is enabled.") else: setup_readline()
This will fallback to the readline completer if Jedi is not installed. The readline completer will only complete names in the global namespace, so for example:
ran<TAB>
will complete to
range
.With Jedi the following code:
range(10).cou<TAB>
will complete to
range(10).count
, this does not work with the default cPythonreadline
completer.You will also need to add
export PYTHONSTARTUP=$HOME/.pythonrc.py
to your shell profile (usually.bash_profile
or.profile
if you use bash).
Recipes¶
Here are some tips on how to use Jedi efficiently.
Type Hinting¶
If Jedi cannot detect the type of a function argument correctly (due to the dynamic nature of Python), you can help it by hinting the type using one of the docstring/annotation styles below. Only gradual typing will always work, all the docstring solutions are glorified hacks and more complicated cases will probably not work.
Official Gradual Typing (Recommended)¶
You can read a lot about Python’s gradual typing system in the corresponding PEPs like:
- PEP 484 as an introduction
- PEP 526 for variable annotations
- PEP 589 for
TypeDict
- There are probably more :)
Below you can find a few examples how you can use this feature.
Function annotations:
def myfunction(node: ProgramNode, foo: str) -> None:
"""Do something with a ``node``.
"""
node.| # complete here
Assignment, for-loop and with-statement type hints:
import typing
x: int = foo()
y: typing.Optional[int] = 3
key: str
value: Employee
for key, value in foo.items():
pass
f: Union[int, float]
with foo() as f:
print(f + 3)
PEP-0484 should be supported in its entirety. Feel free to open issues if that is not the case. You can also use stub files.
Sphinx style¶
http://www.sphinx-doc.org/en/stable/domains.html#info-field-lists
def myfunction(node, foo):
"""
Do something with a ``node``.
:type node: ProgramNode
:param str foo: foo parameter description
"""
node.| # complete here
Epydoc¶
http://epydoc.sourceforge.net/manual-fields.html
def myfunction(node):
"""
Do something with a ``node``.
@type node: ProgramNode
"""
node.| # complete here
Numpydoc¶
https://github.com/numpy/numpy/blob/master/doc/HOWTO_DOCUMENT.rst.txt
In order to support the numpydoc format, you need to install the numpydoc package.
def foo(var1, var2, long_var_name='hi'):
r"""
A one-line summary that does not use variable names or the
function name.
...
Parameters
----------
var1 : array_like
Array_like means all those objects -- lists, nested lists,
etc. -- that can be converted to an array. We can also
refer to variables like `var1`.
var2 : int
The type above can either refer to an actual Python type
(e.g. ``int``), or describe the type of the variable in more
detail, e.g. ``(N,) ndarray`` or ``array_like``.
long_variable_name : {'hi', 'ho'}, optional
Choices in brackets, default first when optional.
...
"""
var2.| # complete here