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 any code unlike the previously available IPCompleter.greedy option.

Be sure to update jedi to the latest stable version or to try the current development version to get better completions.

6 Classes

class IPython.core.completer.Sentinel

Bases: object

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, _origin='', signature='')

Bases: object

Completion object used and return 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 the jedi.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…).

__init__(start: int, end: int, text: str, *, type: str = None, _origin='', signature='') None
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.

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.

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 PENDING DEPRECTION. this is now mostly taken care of with Jedi.

This will enable completion on elements of lists, results of function calls, etc., but can be unsafe because the code is actually evaluated on TAB.

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, use_readline=<deprecated sentinel>, 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.

  • use_readline (bool, optional) – DEPRECATED, ignored since IPython 6.0, will have no effects

all_completions(text) List[str]

Wrapper around the completion methods for the benefit of emacs.

complete(text=None, line_buffer=None, cursor_pos=None)

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:

  • text (str) – Text that was actually used in the completion.

  • matches (list) – A list of completion matches.

Note

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:
  • text (str) – Full text of the current input, multi line string.

  • offset (int) – Integer representing the position of the cursor in text. Offset is 0-based indexed.

Yields:

Completion object

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 is True 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.

dict_key_matches(text)

Match string keys in a dictionary, after e.g. ‘foo[’

dict_keys_only

Whether to show dict key matches only

file_matches(text)

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.

latex_matches(text)

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_matches(text: str) List[str]

Match color schemes for %colors magic

magic_config_matches(text: str) List[str]

Match class names and attributes for %config magic

magic_matches(text)

Match magics

property matchers

All active matcher routines for completion

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.

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.

python_func_kw_matches(text)

Match named parameters (kwargs) of the last open function

python_matches(text)

Match attributes or global python names

unicode_name_matches(text)

Match Latex-like syntax for unicode characters base on the name of the character.

This does \GREEK SMALL LETTER ETA -> η

Works only on valid python 3 identifier, or on combining characters that will combine to form a valid identifier.

Used on Python 3 only.

13 Functions

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.rectify_completions(text: str, completions: Iterable[Completion], *, _debug=False) Iterable[Completion]

Rectify a set of completions to all have the same start and end

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

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 same start and end 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], prefix: str, delims: str)

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.

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 list of replacement/completion

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:
  • text (str) – The text in which to calculate the cursor offset

  • line (int) – Line of the cursor; 0-indexed

  • column (int) – Column of the cursor 0-indexed

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:
  • text (str) – The text in which to calculate the cursor offset

  • offset (int) – Position of the cursor in text, 0-indexed.

Returns:

(line, column) – Line of the cursor; 0-indexed, column of the cursor 0-indexed

Return type:

(int, int)

See also

cursor_to_position

reciprocal of this function

IPython.core.completer.back_unicode_name_matches(text)

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 …

Used on Python 3 only.

IPython.core.completer.back_latex_name_matches(text: str)

Match latex characters back to unicode name

This does \ℵ -> \aleph

Used on Python 3 only.