Skip to content

zsh completion still misbehaving in the presence of : #2703

Closed

Description

@Julian

Given foo containing:

#!/usr/bin/env python3
from click.shell_completion import CompletionItem
import click

class Foo(click.ParamType):
    def convert(self, value, param, ctx):
        return value

    def shell_complete(self, ctx, param, incomplete):
        return [
            CompletionItem(value=value, help="adsf") for value in 
            ["baz:quux", "spam:eggs"]
        ]

@click.command()
@click.argument("foo", type=Foo())
def main(foo):
    print(foo)

and running shell completion generation, followed by attempting to complete the command (indicated via <Tab>, produces:

⊙  PATH=$PWD:/Users/Julian/.local/share/virtualenvs/bowtie/bin/:$PATH
⊙  eval "$(_FOO_COMPLETE=zsh_source ./foo)"                                                                                                                                                                                                                                                                         
⊙  foo <Tab>
baz   -- quux:adsf
spam  -- eggs:adsf

rather than the expected:

baz:quux   -- adsf
spam:eggs -- adsf

This seems to be a regression on #1812.

The fix mentioned there (of "fake escaping" the colons via .replace(":", "\\:") seems to work.

Environment:

  • Python version:
⊙  python --version                                                                                                                                                                                                                                                                                                 julian@Airm
Python 3.11.9
  • Click version:
⊙  python -m pip show click                                                                                                                                                                                                                                                                                         julian@Airm
Name: click
Version: 8.1.7
Summary: Composable command line interface toolkit
Home-page: https://palletsprojects.com/p/click/
Author: 
Author-email: 
License: BSD-3-Clause
Location: /Users/julian/.dotfiles/.local/share/virtualenvs/bowtie/lib/python3.11/site-packages
Requires: 
Required-by: rich-click, trogon, virtue

Activity

  1. changed the title [-]zsh completion still misbehaving in the presence of :[/-] [+]zsh completion still misbehaving in the presence of `:`[/+] on Apr 4, 2024
  2. added a commit that references this issue on Apr 4, 2024
  3. the-13th-letter commented on Jan 6, 2025

    @the-13th-letter
    Contributor

    This seems to be a regression on #1812.

    The fix mentioned there (of "fake escaping" the colons via .replace(":", "\\:") seems to work.

    Are you suggesting that the application author should escape colons in the CollectionItem's .value field themselves? That would affect completions of other shells as well (Bash, Fish), and it is specific to this single completion function, even though the “colon behavior” affects all Zsh completions.

    I would argue that the proper place to fix this is in the click.shell_complete.ZshComplete.format_completions method, which specifically serializes data to be passed to the Zsh completion machinery. This is a serialization failure at its core, and that is the function that is supposedly responsible for that task.

    ...or at least, that is how I intend to fix this, for my click-powered applications, in a subclass that replaces click's stock ZshComplete class.


    That said, I would very much appreciate a click core developer chiming in here on how to submit a PR for this, specifically how to write tests that click is interacting properly with external, non-Python components such as Zsh. Or are we satisfied with testing that the serialization output is “as expected”?

  4. Julian commented on Jan 6, 2025

    @Julian
    ContributorAuthor

    Are you suggesting that the application author should escape colons in the CollectionItem's .value field themselves?

    No I was reporting that I thought application authors shouldn't have to do so, but I didn't really consider what implementation detail I hoped would change (so honestly I have no opinion there).

  5. added this to the 8.2.1 milestone on May 10, 2025
  6. modified the milestones: 8.2.1, 8.2.2 on May 14, 2025
  7. modified the milestones: 8.2.2, on Jul 16, 2025
  8. modified the milestones: , 8.2.2 on Jul 22, 2025
  9. locked as resolved and limited conversation to collaborators on Aug 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions