Skip to content

_is_incomplete_option incorrectly processes last_option in loop #2813

Closed

Description

@msaipraneeth

The _is_incomplete_option function in the Click library has a logic issue in its loop that sets the last_option variable. The function does not break the loop after assigning last_option, which leads to wrong shell completion.

Replication:

See https://github.com/msaipraneeth/click-tuple-param

RCA:

We can reproduce issue If we have flag option before tuple option

Environment:

  • Python version: 3.11
  • Click version: 8.1.7

Possible solution :

Once we identify the last option we have the break the loop in _is_incomplete_option function

def _is_incomplete_option(ctx: Context, args: t.List[str], param: Parameter) -> bool:
    """Determine if the given parameter is an option that needs a value.

    :param args: List of complete args before the incomplete value.
    :param param: Option object being checked.
    """
    if not isinstance(param, Option):
        return False

    if param.is_flag or param.count:
        return False

    last_option = None

    for index, arg in enumerate(reversed(args)):
        if index + 1 > param.nargs:
            break

        if _start_of_option(ctx, arg):
            last_option = arg
            break  # <-------------------------------------------------- break the loop on finding the option

    return last_option is not None and last_option in param.opts

Activity

  1. seebi commented on Dec 2, 2024

    @seebi

    @msaipraneeth Thanks for looking into this

  2. added this to the milestone on Aug 12, 2025
  3. modified the milestones: , 8.3.0 on Aug 24, 2025
  4. locked as resolved and limited conversation to collaborators on Sep 8, 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