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
(inference_state, context)[source]¶ List sub-definitions (e.g., methods in class).
Return type: list of Definition
-
class
jedi.api.classes.
BaseDefinition
(inference_state, name)[source]¶ -
module_path
¶ 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.infer()
should return a list of definition forsys
,f
,C
andx
.>>> from jedi._compatibility import no_unicode_pprint >>> 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.infer()
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) >>> no_unicode_pprint(defs) # doctest: +NORMALIZE_WHITESPACE [<Definition full_name='keyword', description='module keyword'>, <Definition full_name='__main__.C', description='class C'>, <Definition full_name='__main__.D', description='instance D'>, <Definition full_name='__main__.f', description='def f'>]
Finally, here is what you can get from
type
:>>> defs = [str(d.type) for d in defs] # It's unicode and in Py2 has u before it. >>> defs[0] 'module' >>> defs[1] 'class' >>> defs[2] 'instance' >>> defs[3] 'function'
Valid values for are
module
,class
,instance
,function
,param
,path
andkeyword
.
-
module_name
¶ The module name.
>>> from jedi import Script >>> source = 'import json' >>> script = Script(source, path='example.py') >>> d = script.infer()[0] >>> print(d.module_name) # doctest: +ELLIPSIS json
-
line
¶ The line where the definition occurs (starting with 1).
-
column
¶ The column where the definition occurs (starting with 0).
-
docstring
(raw=False, fast=True)[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, path='example.py') >>> doc = script.infer(1, len('def f'))[0].docstring() >>> print(doc) f(a, b=1) <BLANKLINE> Document for function f.
Notice that useful extra information is added to the actual docstring. For function, it is signature. If you need actual docstring, use
raw=True
instead.>>> print(script.infer(1, len('def f'))[0].docstring(raw=True)) Document for function f.
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
.
-
description
¶ A description of the
Definition
object, which is heavily used in testing. e.g. forisinstance
it returnsdef isinstance
.Example:
>>> from jedi._compatibility import no_unicode_pprint >>> from jedi import Script >>> source = ''' ... def f(): ... pass ... ... class C: ... pass ... ... variable = f if random.choice([0,1]) else C''' >>> script = Script(source) # line is maximum by default >>> defs = script.infer(column=3) >>> defs = sorted(defs, key=lambda d: d.line) >>> no_unicode_pprint(defs) # doctest: +NORMALIZE_WHITESPACE [<Definition full_name='__main__.f', description='def f'>, <Definition full_name='__main__.C', description='class C'>] >>> str(defs[0].description) # strip literals in python2 'def f' >>> str(defs[1].description) 'class C'
-
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, path='example.py') >>> print(script.infer(3, len('os.path.join'))[0].full_name) os.path.join
Notice that it returns
'os.path.join'
instead of (for example)'posixpath.join'
. This is not correct, since the modules name would be<module 'posixpath' ...>`
. However most users find the latter more practical.
-
params
¶ Deprecated! Will raise a warning soon. Use get_signatures()[…].params.
Raises an
AttributeError
if the definition is not callable. Otherwise returns a list of Definition that represents the params.
-
get_line_code
(before=0, after=0)[source]¶ Returns the line of code where this object was defined.
Parameters: - before – Add n lines before the current line to the output.
- after – Add n lines after the current line to the output.
Return str: Returns the line(s) of code or an empty string if it’s a builtin.
-
-
class
jedi.api.classes.
Completion
(inference_state, name, stack, like_name_length, is_fuzzy, cached_name=None)[source]¶ Completion objects are returned from
api.Script.complete()
. They provide additional information about a completion.-
complete
¶ Only works with non-fuzzy completions. Returns None if fuzzy completions are used.
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.
Assuming the following function definition:
def foo(param=0): pass
completing
foo(par
would give aCompletion
which complete would be am=
-
name_with_symbols
¶ Similar to
name
, but likename
returns also the symbols, for example assuming the following function definition:def foo(param=0): pass
completing
foo(
would give aCompletion
whichname_with_symbols
would be “param=”.
-
docstring
(raw=False, fast=True)[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, path='example.py') >>> doc = script.infer(1, len('def f'))[0].docstring() >>> print(doc) f(a, b=1) <BLANKLINE> Document for function f.
Notice that useful extra information is added to the actual docstring. For function, it is signature. If you need actual docstring, use
raw=True
instead.>>> print(script.infer(1, len('def f'))[0].docstring(raw=True)) Document for function f.
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
¶
-
follow_definition
(*args, **kwargs)[source]¶ Deprecated!
Return the original definitions. I strongly recommend not using it for your completions, because it might slow down Jedi. If you want to read only a few objects (<=20), it might be useful, especially to get the original docstrings. The basic problem of this function is that it follows all results. This means with 1000 completions (e.g. numpy), it’s just PITA-slow.
-
-
class
jedi.api.classes.
Definition
(inference_state, definition)[source]¶ Definition objects are returned from
api.Script.goto()
orapi.Script.infer()
.-
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.
-
-
class
jedi.api.classes.
BaseSignature
(inference_state, signature)[source]¶ BaseSignature 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.
-
params
¶ Return list of ParamDefinition:
-
-
class
jedi.api.classes.
Signature
(inference_state, signature, call_details)[source]¶ Signature objects is the return value of Script.get_signatures. It knows what functions you are currently in. e.g. isinstance( would return the isinstance function with its params. 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 line/column of the bracket that is responsible for the last function call.
-
-
class
jedi.api.classes.
ParamDefinition
(inference_state, definition)[source]¶ -
-
infer_annotation
(**kwargs)[source]¶ Return list of Definition: Parameters: execute_annotation – If False, the values are not executed and you get classes instead of instances.
-
kind
¶ Returns an enum instance. Returns the same values as the builtin
inspect.Parameter.kind
.No support for Python < 3.4 anymore.
-