Skip to content

Latest commit

 

History

History
255 lines (174 loc) · 5.88 KB

usage.rst

File metadata and controls

255 lines (174 loc) · 5.88 KB

Using Jedi

is can be used with a variety of plugins and software. It is also possible to use in the Python shell or with IPython <repl-completion>.

Below you can also find a list of recipes for type hinting <recipes>.

Editor Plugins

Vim

Visual Studio Code

Emacs

Sublime Text 2/3

SynWrite

TextMate

  • Textmate (Not sure if it's actually working)

Kate

Atom

GNOME Builder

Gedit

Eric IDE

Web Debugger

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

jedi.api.replstartup

Using a Custom $HOME/.pythonrc.py

jedi.utils.setup_readline

Recipes

Here are some tips on how to use efficiently.

Type Hinting

If 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.

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