API Return Classes¶
The jedi.api.classes
module contains the return classes of the API.
These classes are the much bigger part of the whole API, because they contain
the interesting information about completion and goto operations.
-
jedi.api.classes.
defined_names
(evaluator, scope)[source]¶ List sub-definitions (e.g., methods in class).
Return type: list of Definition
-
class
jedi.api.classes.
BaseDefinition
(evaluator, name)[source]¶ -
module_path
= None¶ Shows the file path of a module. e.g.
/usr/lib/python2.7/os.py
-
name
¶ Name of variable/function/class/module.
For example, for
x = None
it returns'x'
.Return type: str or None
-
type
¶ The type of the definition.
Here is an example of the value of this attribute. Let’s consider the following source. As what is in
variable
is unambiguous to Jedi,jedi.Script.goto_definitions()
should return a list of definition forsys
,f
,C
andx
.>>> from jedi import Script >>> source = ''' ... import keyword ... ... class C: ... pass ... ... class D: ... pass ... ... x = D() ... ... def f(): ... pass ... ... for variable in [keyword, f, C, x]: ... variable'''
>>> script = Script(source) >>> defs = script.goto_definitions()
Before showing what is in
defs
, let’s sort it byline
so that it is easy to relate the result to the source code.>>> defs = sorted(defs, key=lambda d: d.line) >>> defs [<Definition module keyword>, <Definition class C>, <Definition class D>, <Definition def f>]
Finally, here is what you can get from
type
:>>> defs[0].type 'module' >>> defs[1].type 'class' >>> defs[2].type 'instance' >>> defs[3].type 'function'
-
module_name
¶ The module name.
>>> from jedi import Script >>> source = 'import json' >>> script = Script(source, path='example.py') >>> d = script.goto_definitions()[0] >>> print(d.module_name) json
-
line
¶ The line where the definition occurs (starting with 1).
-
column
¶ The column where the definition occurs (starting with 0).
-
docstring
(raw=False)[source]¶ Return a document string for this completion object.
Example:
>>> from jedi import Script >>> source = '''\ ... def f(a, b=1): ... "Document for function f." ... ''' >>> script = Script(source, 1, len('def f'), 'example.py') >>> doc = script.goto_definitions()[0].docstring() >>> print(doc) f(a, b=1) Document for function f.
Notice that useful extra information is added to the actual docstring. For function, it is call signature. If you need actual docstring, use
raw=True
instead.>>> print(script.goto_definitions()[0].docstring(raw=True)) Document for function f.
-
description
¶ A textual description of the object.
-
full_name
¶ Dot-separated path of this object.
It is in the form of
<module>[.<submodule>[...]][.<object>]
. It is useful when you want to look up Python manual of the object at hand.Example:
>>> from jedi import Script >>> source = ''' ... import os ... os.path.join''' >>> script = Script(source, 3, len('os.path.join'), 'example.py') >>> print(script.goto_definitions()[0].full_name) os.path.join
Notice that it correctly returns
'os.path.join'
instead of (for example)'posixpath.join'
.
-
params
¶
-
-
class
jedi.api.classes.
Completion
(evaluator, name, needs_dot, like_name_length)[source]¶ Completion objects are returned from
api.Script.completions()
. They provide additional information about a completion.-
complete
¶ Return the rest of the word, e.g. completing
isinstance
:isinstan# <-- Cursor is here
would return the string ‘ce’. It also adds additional stuff, depending on your settings.py.
-
name_with_symbols
¶ Similar to
name
, but likename
returns also the symbols, for example:list()
would return
.append
and others (which means it adds a dot).
-
description
¶ Provide a description of the completion object.
-
docstring
(raw=False, fast=True)[source]¶ Parameters: fast – Don’t follow imports that are only one level deep like import foo
, but followfrom foo import bar
. This makes sense for speed reasons. Completing import a is slow if you use thefoo.docstring(fast=False)
on every object, because it parses all libraries starting witha
.
-
type
¶ The type of the completion objects. Follows imports. For a further description, look at
jedi.api.classes.BaseDefinition.type
.
-
follow_definition
(obj, *args, **kwargs)¶
-
-
class
jedi.api.classes.
Definition
(evaluator, definition)[source]¶ Definition objects are returned from
api.Script.goto_assignments()
orapi.Script.goto_definitions()
.-
description
¶ A description of the
Definition
object, which is heavily used in testing. e.g. forisinstance
it returnsdef isinstance
.Example:
>>> from jedi import Script >>> source = ''' ... def f(): ... pass ... ... class C: ... pass ... ... variable = f if random.choice([0,1]) else C''' >>> script = Script(source, column=3) # line is maximum by default >>> defs = script.goto_definitions() >>> defs = sorted(defs, key=lambda d: d.line) >>> defs [<Definition def f>, <Definition class C>] >>> str(defs[0].description) # strip literals in python2 'def f' >>> str(defs[1].description) 'class C'
-
desc_with_module
¶ In addition to the definition, also return the module.
Warning
Don’t use this function yet, its behaviour may change. If you really need it, talk to me.
-
defined_names
(obj, *args, **kwargs)¶
-
-
class
jedi.api.classes.
CallSignature
(evaluator, executable_name, call_stmt, index, key_name)[source]¶ CallSignature objects is the return value of Script.function_definition. It knows what functions you are currently in. e.g. isinstance( would return the isinstance function. without ( it would return nothing.
-
index
¶ The Param index of the current call. Returns None if the index cannot be found in the curent call.
-
bracket_start
¶ The indent of the bracket that is responsible for the last function call.
-