classification
Title: Add autocompletion for keys in dictionaries
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Trundle, Valery.Khamenya, eric.araujo, ezio.melotti, facundobatista, georg.brandl, martin.panter, rbp, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2010-11-08 09:22 by Valery.Khamenya, last changed 2015-12-07 10:05 by serhiy.storchaka.

Files
File name Uploaded Description Edit
rlcompleter-dict-keys-autocompletion.tar.gz Valery.Khamenya, 2010-11-08 09:22 patch, rlcomeplter.py -- full, test_rlcompleter -- full
patch.diff Valery.Khamenya, 2010-11-09 10:26
Messages (7)
msg120721 - (view) Author: Valery Khamenya (Valery.Khamenya) Date: 2010-11-08 09:22
1. The patch introduces autocompletion for keys in dictionaries (patch attached)

2. The patched rlcompleter as such works OK for unicode dictionary keys as well. All tests pass OK. HOWEVER, readline's completion mechanism seem to be confused with unicode strings -- see comments to Completer.dict_key_matches(). So, perhaps, one day some changes should be applied to readline code too.

3. rlcompleter.py has no tests in trunk -- I spawn a separate issue for it. Meanwhile I took test_rlcompleter.py from 2.7 and extended it.

4. The following usual lines in .pythonstartup:
import readline
import rlcompleter
readline.parse_and_bind('tab: complete')
readline.parse_and_bind('Control-Space: complete')

should be extended by this one:
readline.set_completer_delims(re.compile(r'[\'"\\[]').sub('', readline.get_completer_delims()))
msg120753 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-11-08 15:14
Thank you for the report and patch.  This is a new feature, thus targetting the py3k branch (future 3.2).  If your patch is not against this branch, can you refresh it?  Also, please attach it as text file(s), and generally follow guidelines outlined at http://www.python.org/dev/patches/  Thanks again!
msg120855 - (view) Author: Valery Khamenya (Valery.Khamenya) Date: 2010-11-09 10:26
Hi Éric, thanks for guiding. 

So, attached is the concatenation of two forward unified diffs for rlcompleter.py and test_rlcompleter.py -- both as of py3k trunk. Tested against Python 3.1.2 though.

P.S. hm, py3k code appeared to be surprisingly nicer -- no unicode troubles and work-arounds at all...

regards
Valery
msg121451 - (view) Author: Valery Khamenya (Valery.Khamenya) Date: 2010-11-18 10:21
Guys, do you expect anythig else from me in respect to this issue? Let me know it before my non-stopable garbage collector wipes all the details from my brain away :)
msg121631 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-11-20 13:12
I will review your patch later today.
msg122125 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-11-22 14:10
Review time!

+            elif "[" in text:
+                self.matches = self.dict_key_matches(text)
Does this complete only dicts?  What about other mappings?  What about other sequences implementing __getitem__?  One of the function name and the function docstring (“Compute matches when text contains a [”) is wrong.

I’m not familiar with rlcompleter’s internals, so I’d like a few comments sprinkled in the code.

Please wrap your lines at 79 columns, and follow other advice given at http://www.python.org/dev/patches/ for the next version of your patch.

+        The evaluation of the part before the '[' could be enhanced.
This belongs in a comment or a test, not the docstring.

+                          'DictCompleteMe[\'öh, вау!\']',
I find it more readable to avoid escaped quotes whenever possible.  Here I would use "DictCompleteMe['öh, вау!']".
msg256042 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-12-07 02:13
Some thoughts and observations from trying this patch out:

* It supports single and double quotes, but triple-quoted strings get trashed
* It supports Python 2’s u". . ." syntax, but not r". . .", b". . .", etc
* It supports integer keys, but not indexes of lists
* It does not seem to support multi-level dictionaries (e.g. config parser): d[key1][key2]
* Control codes in strings are awkward
* Escape codes in strings don’t seem to be parsed or generated properly
* The module doc string would sort of become out of date: “indexing operations are *not* evaluated”

It works for custom dictionaries, so it can invoke custom code to list the keys. Maybe this is okay; I wouldn’t expect listing an object’s keys to have a serious effect. But you could add a Mapping (and Sequence) ABC check in case the object implements keys() but is not really a dictionary.

It would be nice for this to work by default, because Tab completion is now enabled by default. But I worry if changing the default completer delimiters globally will be a compatibility problem.

There is a mega ugly regular expression in the patch. Is there another way to do whatever it is doing? Failing that, you could add comments, group names, etc to make it more understandable.
History
Date User Action Args
2015-12-07 10:05:22serhiy.storchakasetnosy: + serhiy.storchaka

versions: + Python 3.6, - Python 3.5
2015-12-07 02:13:09martin.pantersetmessages: + msg256042
2015-02-12 05:08:45rhettingersetversions: + Python 3.5, - Python 3.2
2015-02-11 10:58:28martin.pantersetnosy: + martin.panter
2010-11-22 14:10:37eric.araujosetnosy: - docs@python
messages: + msg122125

assignee: docs@python ->
components: - Documentation
2010-11-21 13:25:30rbpsetnosy: + rbp
2010-11-20 13:12:06eric.araujosetmessages: + msg121631
2010-11-18 10:40:18pitrousetnosy: + georg.brandl, facundobatista
2010-11-18 10:21:52Valery.Khamenyasetmessages: + msg121451
2010-11-12 23:44:10ezio.melottisetnosy: + ezio.melotti
2010-11-09 23:55:00Trundlesetnosy: + Trundle
2010-11-09 10:26:05Valery.Khamenyasetfiles: + patch.diff

assignee: docs@python
components: + Documentation

keywords: + patch
nosy: + docs@python
messages: + msg120855
2010-11-08 15:14:57eric.araujosetversions: - Python 2.6, Python 3.1, Python 2.7, Python 3.3
type: behavior -> enhancement

nosy: + eric.araujo
title: to introduce autocompletion for keys in dictionaries (patch attached) -> Add autocompletion for keys in dictionaries
messages: + msg120753
stage: patch review
2010-11-08 09:22:36Valery.Khamenyacreate