prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  23.2: Unable to add zsh completion as documented
  ### Description
  
  Attempting to follow the steps in the [documentation for adding zsh shell completion](https://pip.pypa.io/en/stable/user_guide/#command-completion) raises an error regarding the use of `compadd` outside of a completion function. This seems to be new with 23.2 and added in https://github.com/pypa/pip/pull/11417.
  
  ### Expected behavior
  
  As in previous versions, following the documented steps should successfully add completion for pip.
  
  ### pip version
  
  23.2
  
  ### Python version
  
  3.11.4
  
  ### OS
  
  macOS 12.6.8
  
  ### How to Reproduce
  
  As documented:
  1. Execute `python -m pip completion --zsh >> ~/.zprofile` or add eval "`pip completion --zsh`" to `.zprofile` or `.zshrc`
  2. Start a new shell
  
  ### Output
  
  From startup:
  ```
  /dev/fd/16:compadd:4: can only be called from completion function
  ```
  
  ### Code of Conduct
  
  - [X] I agree to follow the [PSF Code of Conduct](https://www.python.org/psf/conduct/).
  
  </issue>
  <code>
  [start of src/pip/_internal/commands/completion.py]
  1 import sys
  2 import textwrap
  3 from optparse import Values
  4 from typing import List
  5 
  6 from pip._internal.cli.base_command import Command
  7 from pip._internal.cli.status_codes import SUCCESS
  8 from pip._internal.utils.misc import get_prog
  9 
  10 BASE_COMPLETION = """
  11 # pip {shell} completion start{script}# pip {shell} completion end
  12 """
  13 
  14 COMPLETION_SCRIPTS = {
  15     "bash": """
  16         _pip_completion()
  17         {{
  18             COMPREPLY=( $( COMP_WORDS="${{COMP_WORDS[*]}}" \\
  19                            COMP_CWORD=$COMP_CWORD \\
  20                            PIP_AUTO_COMPLETE=1 $1 2>/dev/null ) )
  21         }}
  22         complete -o default -F _pip_completion {prog}
  23     """,
  24     "zsh": """
  25         #compdef -P pip[0-9.]#
  26         compadd $( COMP_WORDS="$words[*]" \\
  27                    COMP_CWORD=$((CURRENT-1)) \\
  28                    PIP_AUTO_COMPLETE=1 $words[1] 2>/dev/null )
  29     """,
  30     "fish": """
  31         function __fish_complete_pip
  32             set -lx COMP_WORDS (commandline -o) ""
  33             set -lx COMP_CWORD ( \\
  34                 math (contains -i -- (commandline -t) $COMP_WORDS)-1 \\
  35             )
  36             set -lx PIP_AUTO_COMPLETE 1
  37             string split \\  -- (eval $COMP_WORDS[1])
  38         end
  39         complete -fa "(__fish_complete_pip)" -c {prog}
  40     """,
  41     "powershell": """
  42         if ((Test-Path Function:\\TabExpansion) -and -not `
  43             (Test-Path Function:\\_pip_completeBackup)) {{
  44             Rename-Item Function:\\TabExpansion _pip_completeBackup
  45         }}
  46         function TabExpansion($line, $lastWord) {{
  47             $lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart()
  48             if ($lastBlock.StartsWith("{prog} ")) {{
  49                 $Env:COMP_WORDS=$lastBlock
  50                 $Env:COMP_CWORD=$lastBlock.Split().Length - 1
  51                 $Env:PIP_AUTO_COMPLETE=1
  52                 (& {prog}).Split()
  53                 Remove-Item Env:COMP_WORDS
  54                 Remove-Item Env:COMP_CWORD
  55                 Remove-Item Env:PIP_AUTO_COMPLETE
  56             }}
  57             elseif (Test-Path Function:\\_pip_completeBackup) {{
  58                 # Fall back on existing tab expansion
  59                 _pip_completeBackup $line $lastWord
  60             }}
  61         }}
  62     """,
  63 }
  64 
  65 
  66 class CompletionCommand(Command):
  67     """A helper command to be used for command completion."""
  68 
  69     ignore_require_venv = True
  70 
  71     def add_options(self) -> None:
  72         self.cmd_opts.add_option(
  73             "--bash",
  74             "-b",
  75             action="store_const",
  76             const="bash",
  77             dest="shell",
  78             help="Emit completion code for bash",
  79         )
  80         self.cmd_opts.add_option(
  81             "--zsh",
  82             "-z",
  83             action="store_const",
  84             const="zsh",
  85             dest="shell",
  86             help="Emit completion code for zsh",
  87         )
  88         self.cmd_opts.add_option(
  89             "--fish",
  90             "-f",
  91             action="store_const",
  92             const="fish",
  93             dest="shell",
  94             help="Emit completion code for fish",
  95         )
  96         self.cmd_opts.add_option(
  97             "--powershell",
  98             "-p",
  99             action="store_const",
  100             const="powershell",
  101             dest="shell",
  102             help="Emit completion code for powershell",
  103         )
  104 
  105         self.parser.insert_option_group(0, self.cmd_opts)
  106 
  107     def run(self, options: Values, args: List[str]) -> int:
  108         """Prints the completion code of the given shell"""
  109         shells = COMPLETION_SCRIPTS.keys()
  110         shell_options = ["--" + shell for shell in sorted(shells)]
  111         if options.shell in shells:
  112             script = textwrap.dedent(
  113                 COMPLETION_SCRIPTS.get(options.shell, "").format(prog=get_prog())
  114             )
  115             print(BASE_COMPLETION.format(script=script, shell=options.shell))
  116             return SUCCESS
  117         else:
  118             sys.stderr.write(
  119                 "ERROR: You must pass {}\n".format(" or ".join(shell_options))
  120             )
  121             return SUCCESS
  122
  [end of src/pip/_internal/commands/completion.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/src/pip/_internal/commands/completion.py b/src/pip/_internal/commands/completion.py
  --- a/src/pip/_internal/commands/completion.py
  +++ b/src/pip/_internal/commands/completion.py
  @@ -23,9 +23,18 @@
       """,
       "zsh": """
           #compdef -P pip[0-9.]#
  -        compadd $( COMP_WORDS="$words[*]" \\
  -                   COMP_CWORD=$((CURRENT-1)) \\
  -                   PIP_AUTO_COMPLETE=1 $words[1] 2>/dev/null )
  +        __pip() {{
  +          compadd $( COMP_WORDS="$words[*]" \\
  +                     COMP_CWORD=$((CURRENT-1)) \\
  +                     PIP_AUTO_COMPLETE=1 $words[1] 2>/dev/null )
  +        }}
  +        if [[ $zsh_eval_context[-1] == loadautofunc ]]; then
  +          # autoload from fpath, call function directly
  +          __pip "$@"
  +        else
  +          # eval/source/. command, register function for later
  +          compdef __pip -P 'pip[0-9.]#'
  +        fi
       """,
       "fish": """
           function __fish_complete_pip
  
verification_info: {"golden_diff": "diff --git a/src/pip/_internal/commands/completion.py b/src/pip/_internal/commands/completion.py\n--- a/src/pip/_internal/commands/completion.py\n+++ b/src/pip/_internal/commands/completion.py\n@@ -23,9 +23,18 @@\n     \"\"\",\n     \"zsh\": \"\"\"\n         #compdef -P pip[0-9.]#\n-        compadd $( COMP_WORDS=\"$words[*]\" \\\\\n-                   COMP_CWORD=$((CURRENT-1)) \\\\\n-                   PIP_AUTO_COMPLETE=1 $words[1] 2>/dev/null )\n+        __pip() {{\n+          compadd $( COMP_WORDS=\"$words[*]\" \\\\\n+                     COMP_CWORD=$((CURRENT-1)) \\\\\n+                     PIP_AUTO_COMPLETE=1 $words[1] 2>/dev/null )\n+        }}\n+        if [[ $zsh_eval_context[-1] == loadautofunc ]]; then\n+          # autoload from fpath, call function directly\n+          __pip \"$@\"\n+        else\n+          # eval/source/. command, register function for later\n+          compdef __pip -P 'pip[0-9.]#'\n+        fi\n     \"\"\",\n     \"fish\": \"\"\"\n         function __fish_complete_pip\n", "issue": "23.2: Unable to add zsh completion as documented\n### Description\r\n\r\nAttempting to follow the steps in the [documentation for adding zsh shell completion](https://pip.pypa.io/en/stable/user_guide/#command-completion) raises an error regarding the use of `compadd` outside of a completion function. This seems to be new with 23.2 and added in https://github.com/pypa/pip/pull/11417.\r\n\r\n### Expected behavior\r\n\r\nAs in previous versions, following the documented steps should successfully add completion for pip.\r\n\r\n### pip version\r\n\r\n23.2\r\n\r\n### Python version\r\n\r\n3.11.4\r\n\r\n### OS\r\n\r\nmacOS 12.6.8\r\n\r\n### How to Reproduce\r\n\r\nAs documented:\r\n1. Execute `python -m pip completion --zsh >> ~/.zprofile` or add eval \"`pip completion --zsh`\" to `.zprofile` or `.zshrc`\r\n2. Start a new shell\r\n\r\n### Output\r\n\r\nFrom startup:\r\n```\r\n/dev/fd/16:compadd:4: can only be called from completion function\r\n```\r\n\r\n### Code of Conduct\r\n\r\n- [X] I agree to follow the [PSF Code of Conduct](https://www.python.org/psf/conduct/).\n", "before_files": [{"content": "import sys\nimport textwrap\nfrom optparse import Values\nfrom typing import List\n\nfrom pip._internal.cli.base_command import Command\nfrom pip._internal.cli.status_codes import SUCCESS\nfrom pip._internal.utils.misc import get_prog\n\nBASE_COMPLETION = \"\"\"\n# pip {shell} completion start{script}# pip {shell} completion end\n\"\"\"\n\nCOMPLETION_SCRIPTS = {\n    \"bash\": \"\"\"\n        _pip_completion()\n        {{\n            COMPREPLY=( $( COMP_WORDS=\"${{COMP_WORDS[*]}}\" \\\\\n                           COMP_CWORD=$COMP_CWORD \\\\\n                           PIP_AUTO_COMPLETE=1 $1 2>/dev/null ) )\n        }}\n        complete -o default -F _pip_completion {prog}\n    \"\"\",\n    \"zsh\": \"\"\"\n        #compdef -P pip[0-9.]#\n        compadd $( COMP_WORDS=\"$words[*]\" \\\\\n                   COMP_CWORD=$((CURRENT-1)) \\\\\n                   PIP_AUTO_COMPLETE=1 $words[1] 2>/dev/null )\n    \"\"\",\n    \"fish\": \"\"\"\n        function __fish_complete_pip\n            set -lx COMP_WORDS (commandline -o) \"\"\n            set -lx COMP_CWORD ( \\\\\n                math (contains -i -- (commandline -t) $COMP_WORDS)-1 \\\\\n            )\n            set -lx PIP_AUTO_COMPLETE 1\n            string split \\\\  -- (eval $COMP_WORDS[1])\n        end\n        complete -fa \"(__fish_complete_pip)\" -c {prog}\n    \"\"\",\n    \"powershell\": \"\"\"\n        if ((Test-Path Function:\\\\TabExpansion) -and -not `\n            (Test-Path Function:\\\\_pip_completeBackup)) {{\n            Rename-Item Function:\\\\TabExpansion _pip_completeBackup\n        }}\n        function TabExpansion($line, $lastWord) {{\n            $lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart()\n            if ($lastBlock.StartsWith(\"{prog} \")) {{\n                $Env:COMP_WORDS=$lastBlock\n                $Env:COMP_CWORD=$lastBlock.Split().Length - 1\n                $Env:PIP_AUTO_COMPLETE=1\n                (& {prog}).Split()\n                Remove-Item Env:COMP_WORDS\n                Remove-Item Env:COMP_CWORD\n                Remove-Item Env:PIP_AUTO_COMPLETE\n            }}\n            elseif (Test-Path Function:\\\\_pip_completeBackup) {{\n                # Fall back on existing tab expansion\n                _pip_completeBackup $line $lastWord\n            }}\n        }}\n    \"\"\",\n}\n\n\nclass CompletionCommand(Command):\n    \"\"\"A helper command to be used for command completion.\"\"\"\n\n    ignore_require_venv = True\n\n    def add_options(self) -> None:\n        self.cmd_opts.add_option(\n            \"--bash\",\n            \"-b\",\n            action=\"store_const\",\n            const=\"bash\",\n            dest=\"shell\",\n            help=\"Emit completion code for bash\",\n        )\n        self.cmd_opts.add_option(\n            \"--zsh\",\n            \"-z\",\n            action=\"store_const\",\n            const=\"zsh\",\n            dest=\"shell\",\n            help=\"Emit completion code for zsh\",\n        )\n        self.cmd_opts.add_option(\n            \"--fish\",\n            \"-f\",\n            action=\"store_const\",\n            const=\"fish\",\n            dest=\"shell\",\n            help=\"Emit completion code for fish\",\n        )\n        self.cmd_opts.add_option(\n            \"--powershell\",\n            \"-p\",\n            action=\"store_const\",\n            const=\"powershell\",\n            dest=\"shell\",\n            help=\"Emit completion code for powershell\",\n        )\n\n        self.parser.insert_option_group(0, self.cmd_opts)\n\n    def run(self, options: Values, args: List[str]) -> int:\n        \"\"\"Prints the completion code of the given shell\"\"\"\n        shells = COMPLETION_SCRIPTS.keys()\n        shell_options = [\"--\" + shell for shell in sorted(shells)]\n        if options.shell in shells:\n            script = textwrap.dedent(\n                COMPLETION_SCRIPTS.get(options.shell, \"\").format(prog=get_prog())\n            )\n            print(BASE_COMPLETION.format(script=script, shell=options.shell))\n            return SUCCESS\n        else:\n            sys.stderr.write(\n                \"ERROR: You must pass {}\\n\".format(\" or \".join(shell_options))\n            )\n            return SUCCESS\n", "path": "src/pip/_internal/commands/completion.py"}]}