Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IDLE: Include keywords in module-level autocomplete list #81946

Closed
tirkarthi opened this issue Aug 5, 2019 · 16 comments
Closed

IDLE: Include keywords in module-level autocomplete list #81946

tirkarthi opened this issue Aug 5, 2019 · 16 comments
Assignees
Labels
3.8 only security fixes 3.9 only security fixes 3.10 only security fixes topic-IDLE type-feature A feature request or enhancement

Comments

@tirkarthi
Copy link
Member

BPO 37765
Nosy @rhettinger, @terryjreedy, @taleinat, @csabella, @miss-islington, @tirkarthi
PRs
  • bpo-37765: Add keywords to IDLE tab completions #15138
  • [3.9] bpo-37765: Add keywords to IDLE tab completions (GH-15138) #21423
  • [3.8] bpo-37765: Add keywords to IDLE tab completions (GH-15138) #21424
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/terryjreedy'
    closed_at = <Date 2020-07-09.23:47:45.339>
    created_at = <Date 2019-08-05.18:03:28.625>
    labels = ['expert-IDLE', 'type-feature', '3.8', '3.9', '3.10']
    title = 'IDLE: Include keywords in module-level autocomplete list'
    updated_at = <Date 2020-07-09.23:47:45.335>
    user = 'https://github.com/tirkarthi'

    bugs.python.org fields:

    activity = <Date 2020-07-09.23:47:45.335>
    actor = 'terry.reedy'
    assignee = 'terry.reedy'
    closed = True
    closed_date = <Date 2020-07-09.23:47:45.339>
    closer = 'terry.reedy'
    components = ['IDLE']
    creation = <Date 2019-08-05.18:03:28.625>
    creator = 'xtreak'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 37765
    keywords = ['patch']
    message_count = 16.0
    messages = ['349061', '349064', '349065', '349067', '349096', '349362', '349363', '349365', '373250', '373251', '373252', '373365', '373429', '373430', '373431', '373432']
    nosy_count = 6.0
    nosy_names = ['rhettinger', 'terry.reedy', 'taleinat', 'cheryl.sabella', 'miss-islington', 'xtreak']
    pr_nums = ['15138', '21423', '21424']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue37765'
    versions = ['Python 3.8', 'Python 3.9', 'Python 3.10']

    @tirkarthi
    Copy link
    Member Author

    Currently, the basic repl for python provides keywords as part of autocompletion but IDLE doesn't provide them. I was trying to build an async repl on top of IDLE to support top level await statements as part of IDLE since "python -m asyncio" doesn't provide a good repl and found during usage keywords like async/await being part of autocomplete to provide a good experience like the basic repl to type faster. I couldn't find any old issues with search around why keywords were excluded so I thought of filing a new one for this suggestion.

    @tirkarthi tirkarthi added the 3.9 only security fixes label Aug 5, 2019
    @tirkarthi tirkarthi added topic-IDLE type-feature A feature request or enhancement labels Aug 5, 2019
    @taleinat
    Copy link
    Contributor

    taleinat commented Aug 5, 2019

    The global completion list (i.e. when not completing a file name or object attribute) is already full of all the built-ins, imported modules and variables. So IMO we'd need a good reason to add yet more options into the completions list.

    Personally, I don't think that adding all of the keywords to that list would be helpful: They are all short words and most of them must be memorized anyways to work with Python.

    For instance, I don't recall this being brought up by those who often teach newcomers with IDLE, such as Raymond Hettinger, when discussing what's missing in IDLE. I'd be happy to get more input from them on this.

    @taleinat
    Copy link
    Contributor

    taleinat commented Aug 5, 2019

    To be clear, I'm currently -1 on this suggestion.

    @terryjreedy
    Copy link
    Member

    If keywords are included when the REPL has tab completions (which Windows doesn't), then it is plausible that IDLE should. It could be considered part of 'Shell should (mostly) imitate REPL'. But I can see Tal's point, though the relative expansion is pretty small. And there is nothing on the master completions issue bpo-27609 where I collected known not-yet-rejected suggestions and ideas.

    The implementation is trivial. Add two new lines to autocomplete.py. So you can easily patch a private copy. I am preparing a minimal PR.

    import keyword  # add
    ...
        def fetch_completions(self, what, mode):
    ...
                        bigl = eval("dir()", namespace)
                        bigl.extend(keyword.kwlist)  # add
                        bigl.sort()

    True, False, and None are also in builtins, so cannot serve as a test.
    ---

    A separate idea: annotate completion list, at least as an option, with 'keyword' or class, possibly prefixed with 'built-in', so 'built-in function', 'function', and so on.

    @terryjreedy terryjreedy added 3.7 (EOL) end of life 3.8 only security fixes labels Aug 5, 2019
    @terryjreedy terryjreedy changed the title Include keywords in autocomplete list for IDLE IDLE: Include keywords in __main__ autocomplete list Aug 5, 2019
    @tirkarthi
    Copy link
    Member Author

    Thanks Terry, I used a similar patch. My main use case was around typing where normal shell autocompletes it and was curious if it was intentional. I didn't know that windows didn't give keywords. The keywords are short and added very rarely and perhaps the bigger completion list to actual usage might be low since no one opened this issue as Tal mentioned I am open to others feedback.

    @terryjreedy
    Copy link
    Member

    After more research, I am more inclined to add the keywords to the global identifiers list. The IDLE doc says that they should be present.

    "Show Completions
    Open a scrollable list allowing selection of keywords and attributes."

    If this issue were rejected, the line above would have to be changed. (Other needed updates are another issue.) It was added to idlelib/help.txt (since replaced by help.html) by KBK in

    Revision: 209de1f
    Author: Kurt B. Kaiser <kbk@shore.net>
    Date: 2/8/2007 5:58:18 PM
    Message: ... Added AutoComplete instructions to IDLE Help.

    (I checked 2.6 fetch_completions and keywords were not actually included in the global ('') list.)

    The completion menu entry was copied to idle.rst in patches for bpo-5066 by Jessica McKeller and Todd Rovito. Both docs were updated. Another 5 people, at least, reviewed, including 3 core developers. It was committed by a 4th, Andrew Svetlov. So 9 people besides me did not notice the discrepancy and may have thought 'keywords, fine', like I have been.

    One can invoke Show Completions not following anything to get a list of names that one should not use. For that use, keywords should be included. This list currently has 230+ identifiers, 150+ all lowercase. keyword.kwlist would add 32 (35 - 3 builtins). It is not a huge increase.

    @rhettinger
    Copy link
    Contributor

    Since the word "main" is short and since the dunder prefix is common, I don't expect to get much value out of adding '__main__'. ISTM, this just increases the risk of a false positive for a given dunder method.

    To be clear, I'm currently -1 on this suggestion.

    I concur.

    @terryjreedy
    Copy link
    Member

    Raymond, since there is no proposal to 'add __main__', I don't understand your response. For the case in question, the completion list continues to be based on the keys in __main__.__builtins__.__dict__, and main__.__dict__, as it always has been.

    @terryjreedy
    Copy link
    Member

    Cheryl said "This looks good" on the PR." while noting that True should not be added as After trying out REPL completions in macOS Terminal, I *really* want to be able to type 'im'<tab> and have 'import' appear. (When there is just one match, it is filled in without displaying a list of one item.) I increasingly suffer from 'dystypia' (which I coined as the reverse of 'dyslexia'), and 'import' is one of my worst words. And I have to type it daily. On bpo-17238, Ramchandra Apte also requested completion of 'import'.

    Sorting keywords by length, we get:
    >>> sorted(keyword.kwlist, key=lambda s: len(s))
    ['as', 'if', 'in', 'is', 'or', 'and', 'def', 'del', 'for', 'not', 'try', 'None', 'True', 'elif', 'else', 'from', 'pass', 'with', 'False', 'async', 'await', 'break', 'class', 'raise', 'while', 'yield', 'assert', 'except', 'global', 'import', 'lambda', 'return', 'finally', 'continue', 'nonlocal', '__peg_parser__']

    I agree that adding 2 and 3 letter keywords is not useful. Among 4 letter keywords, None and True are already present from builtins. 'elif' and 'else' would need at least 3 and 4 keystrokes to complete ('e', <Tab>, Down for 'else', <Enter>). 'from' would need at least 4 because of 'filter' and 'frozenset'. 'pass' would need 3 because of 'pow'. 'with' would require at least 5 if 'while' were added. So skip length 4 keywords also.

    So I am changing the proposal to adding the 17 keywords (other than False, already present) of length 5 or more. These include 'async' and 'await', requested by Karthikeyan in the opening post above.

    @terryjreedy terryjreedy added 3.10 only security fixes and removed 3.7 (EOL) end of life labels Jul 7, 2020
    @terryjreedy terryjreedy changed the title IDLE: Include keywords in __main__ autocomplete list IDLE: Include 'long' keywords in __main__ autocomplete list Jul 7, 2020
    @terryjreedy terryjreedy changed the title IDLE: Include 'long' keywords in __main__ autocomplete list IDLE: Include longer keywords in __main__ autocomplete list Jul 7, 2020
    @taleinat
    Copy link
    Contributor

    taleinat commented Jul 7, 2020

    Auto-completion is not just about saving keystrokes. For example, I assume that many Python beginners just read through the completion list to see what the options are. This inconsistency would be hard to grok.

    I think that including only some of the keywords in the completions list could potentially be very confusing. We'd have "class" but not "def", "finally" but not "else", "while" but not "for".

    If the standard REPL completes keywords (at least on some platforms) that's a good enough argument to include them in IDLE, in my opinion.

    @taleinat
    Copy link
    Contributor

    taleinat commented Jul 7, 2020

    Also, note that the keywords would only be included in the suggested completions when not in a string and when not completing an attribute. So, for example, such a change could not possibly affect the completion of dunder method names.

    @terryjreedy
    Copy link
    Member

    Tal, I suggested the compromise because of your original objection. Since you think half is worse than all, I will revert the change. It did get me to do a needed rewrite of the Completions section of the IDLE doc.

    @terryjreedy terryjreedy changed the title IDLE: Include longer keywords in __main__ autocomplete list IDLE: Include keywords in module-level autocomplete list Jul 9, 2020
    @terryjreedy
    Copy link
    Member

    New changeset bce2eb4 by Terry Jan Reedy in branch 'master':
    bpo-37765: Add keywords to IDLE tab completions (GH-15138)
    bce2eb4

    @miss-islington
    Copy link
    Contributor

    New changeset fd27fb7 by Miss Islington (bot) in branch '3.9':
    bpo-37765: Add keywords to IDLE tab completions (GH-15138)
    fd27fb7

    @miss-islington
    Copy link
    Contributor

    New changeset 3d1c06e by Miss Islington (bot) in branch '3.8':
    bpo-37765: Add keywords to IDLE tab completions (GH-15138)
    3d1c06e

    @terryjreedy
    Copy link
    Member

    PR 15138 always adds keywords to the big list for the current module. They are also normally present in the small list, when it only excludes '_' names. But if the module being edited contains '__all__', the small list, which is the first list presented, is currently just __all__. This excludes builtins and now keywords and possibly non-_ names defined in the module. I think this restriction is a mistake; __all__ defines a limited external view of the module. It is not intended to restrict the names used in the module. I will remove the restriction (and a crash bug it contains) in a partly completed PR for bpo-37766.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes 3.9 only security fixes 3.10 only security fixes topic-IDLE type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants