From 046657853308d279e654ae1035a7c9c7e84b27e9 2020-06-17 15:02:39 From: Corentin Cadiou Date: 2020-06-17 15:02:39 Subject: [PATCH] Add docstring --- diff --git a/IPython/core/completer.py b/IPython/core/completer.py index fdbfc8b..804ae8f 100644 --- a/IPython/core/completer.py +++ b/IPython/core/completer.py @@ -753,7 +753,8 @@ def get__all__entries(obj): return [w for w in words if isinstance(w, str)] -def match_dict_keys(keys: List[Union[str, bytes]], prefix: str, delims: str) -> Tuple[str, int, List[str]]: +def match_dict_keys(keys: List[Union[str, bytes, Tuple[Union[str, bytes]]]], prefix: str, delims: str, + prefix_tuple: Tuple[str, bytes]=()) -> Tuple[str, int, List[str]]: """Used by dict_key_matches, matching the prefix to a list of keys Parameters @@ -761,9 +762,12 @@ def match_dict_keys(keys: List[Union[str, bytes]], prefix: str, delims: str) -> keys: list of keys in dictionary currently being completed. prefix: - Part of the text already typed by the user. e.g. `mydict[b'fo` + 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. + prefix_tuple: 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 ======= @@ -774,7 +778,23 @@ def match_dict_keys(keys: List[Union[str, bytes]], prefix: str, delims: str) -> ``matches`` a list of replacement/completion """ - keys = [k for k in keys if isinstance(k, (str, bytes))] + Nprefix = len(prefix_tuple) + def filter_by_prefix_tuple(key): + if len(key) < Nprefix: + return False + for k, pt in zip(key, prefix_tuple): + if k != pt: + return False + return True + + new_keys = [] + for k in keys: + if isinstance(k, (str, bytes)): + new_keys.append(k) + elif isinstance(k, tuple) and filter_by_prefix_tuple(k): + new_keys.append(k[Nprefix]) + + keys = new_keys if not prefix: return '', 0, [repr(k) for k in keys if isinstance(k, (str, bytes))] @@ -1641,6 +1661,7 @@ class IPCompleter(Completer): ) \[ # open bracket \s* # and optional whitespace + # Capture any number of str-like objects (e.g. "a", "b", 'c') ((?:[uUbB]? # string prefix (r not handled) (?: '(?:[^']|(?