Important
This documentation covers IPython versions 6.0 and higher. Beginning with version 6.0, IPython stopped supporting compatibility with Python versions lower than 3.3 including all versions of Python 2.7.
If you are looking for an IPython version compatible with Python 2.7, please use the IPython 5.x LTS release and refer to its documentation (LTS is the long term support release).
Module: core.completer
Completion for IPython.
This module started as fork of the rlcompleter module in the Python standard library. The original enhancements made to rlcompleter have been sent upstream and were accepted as of Python 2.3,
This module now support a wide variety of completion mechanism both available for normal classic Python code, as well as completer for IPython specific Syntax like magics.
Latex and Unicode completion
IPython and compatible frontends not only can complete your code, but can help you to input a wide range of characters. In particular we allow you to insert a unicode character using the tab completion mechanism.
Forward latex/unicode completion
Forward completion allows you to easily type a unicode character using its latex name, or unicode long description. To do so type a backslash follow by the relevant name and press tab:
Using latex completion:
\alpha<tab>
α
or using unicode completion:
\GREEK SMALL LETTER ALPHA<tab>
α
Only valid Python identifiers will complete. Combining characters (like arrow or
dots) are also available, unlike latex they need to be put after the their
counterpart that is to say, F\\vec<tab>
is correct, not \\vec<tab>F
.
Some browsers are known to display combining characters incorrectly.
Backward latex completion
It is sometime challenging to know how to type a character, if you are using IPython, or any compatible frontend you can prepend backslash to the character and press Tab to expand it to its latex form.
\α<tab>
\alpha
Both forward and backward completions can be deactivated by setting the
Completer.backslash_combining_completions
option to
False
.
Experimental
Starting with IPython 6.0, this module can make use of the Jedi library to
generate completions both using static analysis of the code, and dynamically
inspecting multiple namespaces. Jedi is an autocompletion and static analysis
for Python. The APIs attached to this new mechanism is unstable and will
raise unless use in an provisionalcompleter
context manager.
You will find that the following are experimental:
Note
better name for rectify_completions
?
We welcome any feedback on these new API, and we also encourage you to try this
module in debug mode (start IPython with --Completer.debug=True
) in order
to have extra logging information if jedi
is crashing, or if current
IPython completer pending deprecations are returning results not yet handled
by jedi
Using Jedi for tab completion allow snippets like the following to work without having to execute any code:
>>> myvar = ['hello', 42]
... myvar[1].bi<tab>
Tab completion will be able to infer that myvar[1]
is a real number without
executing almost any code unlike the deprecated IPCompleter.greedy
option.
Be sure to update jedi
to the latest stable version or to try the
current development version to get better completions.
Matchers
All completions routines are implemented using unified Matchers API. The matchers API is provisional and subject to change without notice.
The built-in matchers include:
IPCompleter.dict_key_matcher
: dictionary key completions,IPCompleter.magic_matcher
: completions for magics,IPCompleter.unicode_name_matcher
,IPCompleter.fwd_unicode_matcher
andIPCompleter.latex_name_matcher
: see Forward latex/unicode completion,back_unicode_name_matcher
andback_latex_name_matcher
: see Backward latex completion,IPCompleter.file_matcher
: paths to files and directories,IPCompleter.python_func_kw_matcher
- function keywords,IPCompleter.python_matches
- globals and attributes (v1 API),IPCompleter.jedi_matcher
- static analysis with Jedi,IPCompleter.custom_completer_matcher
- pluggable completer with a default implementation inInteractiveShell
which uses IPython hooks system (complete_command
) with string dispatch (including regular expressions). Differently to other matchers,custom_completer_matcher
will not suppress Jedi results to match behaviour in earlier IPython versions.
Custom matchers can be added by appending to IPCompleter.custom_matchers
list.
Matcher API
Simplifying some details, the Matcher
interface can described as
MatcherAPIv1 = Callable[[str], list[str]]
MatcherAPIv2 = Callable[[CompletionContext], SimpleMatcherResult]
Matcher = MatcherAPIv1 | MatcherAPIv2
The MatcherAPIv1
reflects the matcher API as available prior to IPython 8.6.0
and remains supported as a simplest way for generating completions. This is also
currently the only API supported by the IPython hooks system complete_command
.
To distinguish between matcher versions matcher_api_version
attribute is used.
More precisely, the API allows to omit matcher_api_version
for v1 Matchers,
and requires a literal 2
for v2 Matchers.
Once the API stabilises future versions may relax the requirement for specifying
matcher_api_version
by switching to functools.singledispatch
, therefore
please do not rely on the presence of matcher_api_version
for any purposes.
Suppression of competing matchers
By default results from all matchers are combined, in the order determined by
their priority. Matchers can request to suppress results from subsequent
matchers by setting suppress
to True
in the MatcherResult
.
When multiple matchers simultaneously request suppression, the results from of the matcher with higher priority will be returned.
Sometimes it is desirable to suppress most but not all other matchers;
this can be achieved by adding a set of identifiers of matchers which
should not be suppressed to MatcherResult
under do_not_suppress
key.
The suppression behaviour can is user-configurable via
IPCompleter.suppress_competing_matchers
.
9 Classes
- class IPython.core.completer.ProvisionalCompleterWarning
Bases:
FutureWarning
Exception raise by an experimental feature in this module.
Wrap code in
provisionalcompleter
context manager if you are certain you want to use an unstable feature.
- class IPython.core.completer.Completion(start: int, end: int, text: str, *, type: str | None = None, _origin='', signature='')
Bases:
object
Completion object used and returned by IPython completers.
Warning
Unstable
This function is unstable, API may change without warning. It will also raise unless use in proper context manager.
This act as a middle ground
Completion
object between thejedi.api.classes.Completion
object and the Prompt Toolkit completion object. While Jedi need a lot of information about evaluator and how the code should be ran/inspected, PromptToolkit (and other frontend) mostly need user facing information.Which range should be replaced replaced by what.
Some metadata (like completion type), or meta information to displayed to the use user.
For debugging purpose we can also store the origin of the completion (
jedi
,IPython.python_matches
,IPython.magics_matches
…).
- class IPython.core.completer.SimpleCompletion(text: str, *, type: str | None = None)
Bases:
object
Completion item to be included in the dictionary returned by new-style Matcher (API v2).
Warning
Provisional
This class is used to describe the currently supported attributes of simple completion items, and any additional implementation details should not be relied on. Additional attributes may be included in future versions, and meaning of text disambiguated from the current dual meaning of “text to insert” and “text to used as a label”.
- class IPython.core.completer.SimpleMatcherResult
Bases:
_MatcherResultBase
,Dict
Result of new-style completion matcher.
- completions: Sequence[SimpleCompletion] | Iterator[SimpleCompletion]
List of candidate completions
- class IPython.core.completer.CompletionContext(token: str, full_text: str, cursor_position: int, cursor_line: int, limit: int | None)
Bases:
object
Completion context provided as an argument to matchers in the Matcher API v2.
- class IPython.core.completer.CompletionSplitter(delims=None)
Bases:
object
An object to split an input line in a manner similar to readline.
By having our own implementation, we can expose readline-like completion in a uniform manner to all frontends. This object only needs to be given the line of text to be split and the cursor position on said line, and it returns the ‘word’ to be completed on at the cursor after splitting the entire line.
What characters are used as splitting delimiters can be controlled by setting the
delims
attribute (this is a property that internally automatically builds the necessary regular expression)- __init__(delims=None)
- property delims
Return the string of delimiter characters.
- split_line(line, cursor_pos=None)
Split a line of text with a cursor at the given position.
- class IPython.core.completer.Completer(**kwargs: Any)
Bases:
Configurable
- __init__(namespace=None, global_namespace=None, **kwargs)
Create a new completer for the command line.
Completer(namespace=ns, global_namespace=ns2) -> completer instance.
If unspecified, the default namespace where completions are performed is __main__ (technically, __main__.__dict__). Namespaces should be given as dictionaries.
An optional second namespace can be given. This allows the completer to handle cases where both the local and global scopes need to be distinguished.
- attr_matches(text)
Compute matches when text contains a dot.
Assuming the text is of the form NAME.NAME….[NAME], and is evaluatable in self.namespace or self.global_namespace, it will be evaluated and its attributes (as revealed by dir()) are used as possible completions. (For class instances, class members are also considered.)
WARNING: this can still invoke arbitrary C code, if an object with a __getattr__ hook is evaluated.
- auto_close_dict_keys
Enable auto-closing dictionary keys.
When enabled string keys will be suffixed with a final quote (matching the opening quote), tuple keys will also receive a separating comma if needed, and keys which are final will receive a closing bracket (
]
).
- backslash_combining_completions
Enable unicode completions, e.g. alpha<tab> . Includes completion of latex commands, unicode names, and expanding unicode characters back to latex commands.
- complete(text, state)
Return the next possible completion for ‘text’.
This is called successively with state == 0, 1, 2, … until it returns None. The completion should begin with ‘text’.
- debug
Enable debug for the Completer. Mostly print extra information for experimental jedi integration.
- evaluation
Policy for code evaluation under completion.
Successive options allow to enable more eager evaluation for better completion suggestions, including for nested dictionaries, nested lists, or even results of function calls. Setting
unsafe
or higher can lead to evaluation of arbitrary user code on Tab with potentially unwanted or dangerous side effects.Allowed values are:
forbidden
: no evaluation of code is permitted,minimal
: evaluation of literals and access to built-in namespace; no item/attribute evaluationm no access to locals/globals, no evaluation of any operations or comparisons.limited
: access to all namespaces, evaluation of hard-coded methods (for example:dict.keys
,object.__getattr__
,object.__getitem__
) on allow-listed objects (for example:dict
,list
,tuple
,pandas.Series
),unsafe
: evaluation of all methods and function calls but not of syntax with side-effects likedel x
,dangerous
: completely arbitrary evaluation.
- global_matches(text)
Compute matches when text is a simple name.
Return a list of all keywords, built-in functions and names currently defined in self.namespace or self.global_namespace that match.
- greedy
Activate greedy completion.
Deprecated since version 8.8: Use
Completer.evaluation
andCompleter.auto_close_dict_keys
instead.When enabled in IPython 8.8 or newer, changes configuration as follows:
Completer.evaluation = 'unsafe'
Completer.auto_close_dict_keys = True
- jedi_compute_type_timeout
restrict time (in milliseconds) during which Jedi can compute types. Set to 0 to stop computing types. Non-zero value lower than 100ms may hurt performance by preventing jedi to build its cache.
- Type:
Experimental
- use_jedi
Use Jedi to generate autocompletions. Default to True if jedi is installed.
- Type:
Experimental
- class IPython.core.completer.IPCompleter(**kwargs: Any)
Bases:
Completer
Extension of the completer class with IPython-specific features
- __init__(shell=None, namespace=None, global_namespace=None, config=None, **kwargs)
IPCompleter() -> completer
Return a completer object.
- Parameters:
shell – a pointer to the ipython shell itself. This is needed because this completer knows about magic functions, and those can only be accessed via the ipython instance.
namespace (dict, optional) – an optional dict where completions are performed.
global_namespace (dict, optional) – secondary optional dict for completions, to handle cases (such as IPython embedded inside functions) where both Python scopes are visible.
config (Config) – traitlet’s config object
**kwargs – passed to super class unmodified.
- all_completions(text: str) List[str]
Wrapper around the completion methods for the benefit of emacs.
- complete(text=None, line_buffer=None, cursor_pos=None) Tuple[str, Sequence[str]]
Find completions for the given text and line context.
Note that both the text and the line_buffer are optional, but at least one of them must be given.
- Parameters:
text (string, optional) – Text to perform the completion on. If not given, the line buffer is split using the instance’s CompletionSplitter object.
line_buffer (string, optional) – If not given, the completer attempts to obtain the current line buffer via readline. This keyword allows clients which are requesting for text completions in non-readline contexts to inform the completer of the entire text.
cursor_pos (int, optional) – Index of the cursor in the full line buffer. Should be provided by remote frontends where kernel has no access to frontend state.
- Returns:
Tuple of two items
text (str) – Text that was actually used in the completion.
matches (list) – A list of completion matches.
Notes
This API is likely to be deprecated and replaced by
IPCompleter.completions
in the future.
- completions(text: str, offset: int) Iterator[Completion]
Returns an iterator over the possible completions
Warning
Unstable
This function is unstable, API may change without warning. It will also raise unless use in proper context manager.
- Parameters:
- Yields:
Completion
Notes
The cursor on a text can either be seen as being “in between” characters or “On” a character depending on the interface visible to the user. For consistency the cursor being on “in between” characters X and Y is equivalent to the cursor being “on” character Y, that is to say the character the cursor is on is considered as being after the cursor.
Combining characters may span more that one position in the text.
Note
If
IPCompleter.debug
isTrue
will yield a--jedi/ipython--
fake Completion token to distinguish completion returned by Jedi and usual IPython completion.Note
Completions are not completely deduplicated yet. If identical completions are coming from different sources this function does not ensure that each completion object will only be present once.
- custom_completer_matcher(context)
Dispatch custom completer.
If a match is found, suppresses all other matchers except for Jedi.
- dict_key_matcher(context: CompletionContext) SimpleMatcherResult
Match string keys in a dictionary, after e.g.
foo[
.
- dict_key_matches(text: str) List[str]
Match string keys in a dictionary, after e.g.
foo[
.Deprecated since version 8.6: You can use
dict_key_matcher()
instead.
- dict_keys_only
Whether to show dict key matches only.
(disables all matchers except for
IPCompleter.dict_key_matcher
).
- disable_matchers
List of matchers to disable.
The list should contain matcher identifiers (see
completion_matcher
).
- dispatch_custom_completer(text)
Deprecated since version 8.6: You can use
custom_completer_matcher()
instead.
- file_matcher(context: CompletionContext) SimpleMatcherResult
Same as
file_matches
, but adopted to new Matcher API.
- file_matches(text: str) List[str]
Match filenames, expanding ~USER type strings.
Most of the seemingly convoluted logic in this completer is an attempt to handle filenames with spaces in them. And yet it’s not quite perfect, because Python’s readline doesn’t expose all of the GNU readline details needed for this to be done correctly.
For a filename with a space in it, the printed completions will be only the parts after what’s already been typed (instead of the full completions, as is normally done). I don’t think with the current (as of Python 2.3) Python readline it’s possible to do better.
Deprecated since version 8.6: You can use
file_matcher()
instead.
- fwd_unicode_match(text: str) Tuple[str, Sequence[str]]
Forward match a string starting with a backslash with a list of potential Unicode completions.
Will compute list of Unicode character names on first call and cache it.
Deprecated since version 8.6: You can use
fwd_unicode_matcher()
instead.- Returns:
matched text (empty if no matches)
list of potential completions, empty tuple otherwise)
- Return type:
At tuple with
- fwd_unicode_matcher(context: CompletionContext)
Same as
fwd_unicode_match
, but adopted to new Matcher API.
- latex_matches(text: str) Tuple[str, Sequence[str]]
Match Latex syntax for unicode characters.
This does both
\alp
->\alpha
and\alpha
->α
Deprecated since version 8.6: You can use
latex_name_matcher()
instead.
- latex_name_matcher(context: CompletionContext)
Match Latex syntax for unicode characters.
This does both
\alp
->\alpha
and\alpha
->α
- limit_to__all__
DEPRECATED as of version 5.0.
Instruct the completer to use __all__ for the completion
Specifically, when completing on
object.<tab>
.When True: only those names in obj.__all__ will be included.
When False [default]: the __all__ attribute is ignored
- magic_color_matcher(context: CompletionContext) SimpleMatcherResult
Match color schemes for %colors magic.
- magic_color_matches(text: str) List[str]
Match color schemes for %colors magic.
Deprecated since version 8.6: You can use
magic_color_matcher()
instead.
- magic_config_matcher(context: CompletionContext) SimpleMatcherResult
Match class names and attributes for %config magic.
- magic_config_matches(text: str) List[str]
Match class names and attributes for %config magic.
Deprecated since version 8.6: You can use
magic_config_matcher()
instead.
- magic_matcher(context: CompletionContext) SimpleMatcherResult
Match magics.
- magic_matches(text: str)
Match magics.
Deprecated since version 8.6: You can use
magic_matcher()
instead.
- merge_completions
Whether to merge completion results into a single list
If False, only the completion results from the first non-empty completer will be returned.
As of version 8.6.0, setting the value to
False
is an alias for:IPCompleter.suppress_competing_matchers = True.
.
- omit__names
Instruct the completer to omit private method names
Specifically, when completing on
object.<tab>
.When 2 [default]: all names that start with ‘_’ will be excluded.
When 1: all ‘magic’ names (
__foo__
) will be excluded.When 0: nothing will be excluded.
- profile_completions
If True, emit profiling data for completion subsystem using cProfile.
- profiler_output_dir
Template for path at which to output profile data for completions.
- python_func_kw_matcher(context: CompletionContext) SimpleMatcherResult
Match named parameters (kwargs) of the last open function.
- python_func_kw_matches(text)
Match named parameters (kwargs) of the last open function.
Deprecated since version 8.6: You can use
python_func_kw_matcher()
instead.
- python_matcher(context: CompletionContext) SimpleMatcherResult
Match attributes or global python names
- python_matches(text: str) Iterable[str]
Match attributes or global python names.
Deprecated since version 8.27: You can use
python_matcher()
instead.
- suppress_competing_matchers
Whether to suppress completions from other Matchers.
When set to
None
(default) the matchers will attempt to auto-detect whether suppression of other matchers is desirable. For example, at the beginning of a line followed by%
we expect a magic completion to be the only applicable option, and aftermy_dict['
we usually expect a completion with an existing dictionary key.If you want to disable this heuristic and see completions from all matchers, set
IPCompleter.suppress_competing_matchers = False
. To disable the heuristic for specific matchers provide a dictionary mapping:IPCompleter.suppress_competing_matchers = {'IPCompleter.dict_key_matcher': False}
.Set
IPCompleter.suppress_competing_matchers = True
to limit completions to the set of matchers with the highest priority; this is equivalent toIPCompleter.merge_completions
and can be beneficial for performance, but will sometimes omit relevant candidates from matchers further down the priority list.
- unicode_name_matcher(context: CompletionContext)
Same as
unicode_name_matches
, but adopted to new Matcher API.
18 Functions
- IPython.core.completer.cast(type_, obj)
Workaround for
TypeError: MatcherAPIv2() takes no arguments
- IPython.core.completer.provisionalcompleter(action='ignore')
This context manager has to be used in any place where unstable completer behavior and API may be called.
>>> with provisionalcompleter(): ... completer.do_experimental_things() # works
>>> completer.do_experimental_things() # raises.
Note
Unstable
By using this context manager you agree that the API in use may change without warning, and that you won’t complain if they do so.
You also understand that, if the API is not to your liking, you should report a bug to explain your use case upstream.
We’ll be happy to get your feedback, feature requests, and improvements on any of the unstable APIs!
- IPython.core.completer.has_open_quotes(s)
Return whether a string has open quotes.
This simply counts whether the number of quote characters of either type in the string is odd.
- Returns:
If there is an open quote, the quote character is returned. Else, return
False.
- IPython.core.completer.protect_filename(s, protectables=' ()[]{}?=\\|;:\'#*"^&')
Escape a string to protect certain characters.
- IPython.core.completer.expand_user(path: str) Tuple[str, bool, str]
Expand
~
-style usernames in strings.This is similar to
os.path.expanduser()
, but it computes and returns extra information that will be useful if the input was being used in computing completions, and you wish to return the completions with the original ‘~’ instead of its expanded value.- Parameters:
path (str) – String to be expanded. If no ~ is present, the output is the same as the input.
- Returns:
newpath (str) – Result of ~ expansion in the input path.
tilde_expand (bool) – Whether any expansion was performed or not.
tilde_val (str) – The value that ~ was replaced with.
- IPython.core.completer.compress_user(path: str, tilde_expand: bool, tilde_val: str) str
Does the opposite of expand_user, with its outputs.
- IPython.core.completer.completions_sorting_key(word)
key for sorting completions
This does several things:
Demote any completions starting with underscores to the end
Insert any %magic and %%cellmagic completions in the alphabetical order by their name
- IPython.core.completer.has_any_completions(result: SimpleMatcherResult | _JediMatcherResult) bool
Check if any result includes any completions.
- IPython.core.completer.completion_matcher(*, priority: float | None = None, identifier: str | None = None, api_version: int = 1)
Adds attributes describing the matcher.
- Parameters:
priority (Optional[float]) – The priority of the matcher, determines the order of execution of matchers. Higher priority means that the matcher will be executed first. Defaults to 0.
identifier (Optional[str]) –
identifier of the matcher allowing users to modify the behaviour via traitlets, and also used to for debugging (will be passed as
origin
with the completions).Defaults to matcher function’s
__qualname__
(for example,IPCompleter.file_matcher
for the built-in matched defined as afile_matcher
method of theIPCompleter
class).api_version (Optional[int]) – version of the Matcher API used by this matcher. Currently supported values are 1 and 2. Defaults to 1.
- IPython.core.completer.rectify_completions(text: str, completions: Iterable[Completion], *, _debug: bool = False) Iterable[Completion]
Rectify a set of completions to all have the same
start
andend
Warning
Unstable
This function is unstable, API may change without warning. It will also raise unless use in proper context manager.
- Parameters:
text (str) – text that should be completed.
completions (Iterator[Completion]) – iterator over the completions to rectify
_debug (bool) – Log failed completion
Notes
jedi.api.classes.Completion
s returned by Jedi may not have the same start and end, though the Jupyter Protocol requires them to behave like so. This will readjust the completion to have the samestart
andend
by padding both extremities with surrounding text.During stabilisation should support a
_debug
option to log which completion are return by the IPython completer and not found in Jedi in order to make upstream bug report.
- IPython.core.completer.get__all__entries(obj)
returns the strings in the __all__ attribute
- IPython.core.completer.match_dict_keys(keys: List[str | bytes | Tuple[str | bytes, ...]], prefix: str, delims: str, extra_prefix: Tuple[str | bytes, ...] | None = None) Tuple[str, int, Dict[str, _DictKeyState]]
Used by dict_key_matches, matching the prefix to a list of keys
- Parameters:
keys – list of keys in dictionary currently being completed.
prefix – Part of the text already typed by the user. E.g.
mydict[b'fo
delims – String of delimiters to consider when finding the current key.
extra_prefix (optional) – Part of the text already typed in multi-key index cases. E.g. for
mydict['foo', "bar", 'b
, this would be('foo', 'bar')
.
- Returns:
A tuple of three elements (
quote
,token_start
,matched
, with)quote
being the quote that need to be used to close current string.token_start
the position where the replacement should start occurring,matches
a dictionary of replacement/completion keys on keys and values – indicating whether the state.
- IPython.core.completer.cursor_to_position(text: str, line: int, column: int) int
Convert the (line,column) position of the cursor in text to an offset in a string.
- Parameters:
- Return type:
Position of the cursor in
text
, 0-indexed.
See also
position_to_cursor
reciprocal of this function
- IPython.core.completer.position_to_cursor(text: str, offset: int) Tuple[int, int]
Convert the position of the cursor in text (0 indexed) to a line number(0-indexed) and a column number (0-indexed) pair
Position should be a valid position in
text
.- Parameters:
- Returns:
(line, column) – Line of the cursor; 0-indexed, column of the cursor 0-indexed
- Return type:
See also
cursor_to_position
reciprocal of this function
- IPython.core.completer.back_unicode_name_matcher(context: CompletionContext)
Match Unicode characters back to Unicode name
Same as
back_unicode_name_matches
, but adopted to new Matcher API.
- IPython.core.completer.back_unicode_name_matches(text: str) Tuple[str, Sequence[str]]
Match Unicode characters back to Unicode name
This does
☃
->\snowman
Note that snowman is not a valid python3 combining character but will be expanded. Though it will not recombine back to the snowman character by the completion machinery.
This will not either back-complete standard sequences like n, b …
Deprecated since version 8.6: You can use
back_unicode_name_matcher()
instead.- Returns:
Return a tuple with two elements
- The Unicode character that was matched (preceded with a backslash), or – empty string,
- a sequence (of 1), name for the match Unicode character, preceded by – backslash, or empty if no match.
- IPython.core.completer.back_latex_name_matcher(context: CompletionContext)
Match latex characters back to unicode name
Same as
back_latex_name_matches
, but adopted to new Matcher API.