pallets / pallets/click

Context different in `click.pass_context` and `shell_complete`

Open
#2,303 1 comment 14 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

shell completion
Dominant language
Python
Stars
17.7k
Forks
2.3k
Avg merge
1d 30m
Merged PRs (30d)
18

Description

Discussed in https://github.com/pallets/click/discussions/2302

Originally posted by amal-thundiyil June 2, 2022

Description

Steps to reproduce

  1. Run pip install . in the folder with yourscript.py and setup.py in a virtual environment.
  2. Setup shell completion by following the docs.
  3. Run yourscript clone and notice output without errors.
  4. Run youscript clone and press <TAB> for shell completion and see the error.

File name: yourscript.py

import os
import click


class Repo(object):
    def __init__(self, home=None, debug=False):
        self.home = os.path.abspath(home or ".")
        self.debug = debug


def tab_completer(ctx, param, incomplete):
    return [ctx.obj.home]


@click.group()
@click.pass_context
def cli(ctx):
    ctx.obj = Repo()


@cli.command()
@click.argument("option", shell_complete=tab_completer, required=False)
@click.pass_context
def clone(ctx, option):
    click.echo(ctx.obj.home)

File name: setup.py

from setuptools import setup

setup(
    name="yourscript",
    version="0.1",
    py_modules=["yourscript"],
    install_requires=[
        "Click",
    ],
    entry_points="""
        [console_scripts]
        yourscript=yourscript:cli
    """,
)

Stack trace:

  File "/home/amal/Documents/random/click-test/venv/bin/yourscript", line 11, in <module>
    load_entry_point('yourscript==0.1', 'console_scripts', 'yourscript')()
  File "/home/amal/Documents/random/click-test/venv/lib/python3.8/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/home/amal/Documents/random/click-test/venv/lib/python3.8/site-packages/click/core.py", line 1050, in main
    self._main_shell_completion(extra, prog_name, complete_var)
  File "/home/amal/Documents/random/click-test/venv/lib/python3.8/site-packages/click/core.py", line 1125, in _main_shell_completion
    rv = shell_complete(self, ctx_args, prog_name, complete_var, instruction)
  File "/home/amal/Documents/random/click-test/venv/lib/python3.8/site-packages/click/shell_completion.py", line 49, in shell_complete
    echo(comp.complete())
  File "/home/amal/Documents/random/click-test/venv/lib/python3.8/site-packages/click/shell_completion.py", line 291, in complete
    completions = self.get_completions(args, incomplete)
  File "/home/amal/Documents/random/click-test/venv/lib/python3.8/site-packages/click/shell_completion.py", line 273, in get_completions
    return obj.shell_complete(ctx, incomplete)
  File "/home/amal/Documents/random/click-test/venv/lib/python3.8/site-packages/click/core.py", line 2397, in shell_complete
    results = self._custom_shell_complete(ctx, self, incomplete)
  File "/home/amal/Documents/random/click-test/yourscript.py", line 12, in tab_completer
    return [ctx.obj.home]
AttributeError: 'NoneType' object has no attribute 'home'

Expected Result

Output should tab complete to print out the current directory to the console.

Versions

  • Click version: 8.1.3
  • Python version: 3.8

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Trace shell completion from click/core.py through click/shell_completion.py, comparing the context passed to click.pass_context and the tab_completer callback. Reproduce the example with yourscript.py and shell completion, then inspect the related discussions and issues for intended context behavior. Done means the provided completion callback can access the expected context and the reproduction no longer raises AttributeError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.