click icon indicating copy to clipboard operation
click copied to clipboard

[rejected AI] Envvar for multiple=True + nargs>1 silently drops incomplete trailing tokens

Open ShamikOfficial opened this issue 2 months ago • 3 comments

When an option uses multiple=True and nargs > 1, an environment-variable value whose whitespace-split token count is not a multiple of nargs is accepted with exit code 0, and the trailing incomplete group is silently discarded.

The same values on the CLI are rejected (e.g. Got unexpected extra argument). Non-multiple nargs > 1 options already reject incomplete envvar values with BadParameter (Takes N values but M were given.). The multiple=True path should do the same instead of truncating.

Root cause: Option.value_from_envvar() calls batch(), which uses zip(..., strict=False) and drops remainder tokens.

Reproduction

import click
from click.testing import CliRunner

runner = CliRunner()

@click.command()
@click.option("--arg", nargs=2, multiple=True)
def cli(arg):
    click.echo(repr(arg))

# Envvar: silently drops "c"
r = runner.invoke(cli, [], auto_envvar_prefix="TEST", env={"TEST_ARG": "a b c"})
print(r.exit_code, r.output.strip())
# Actual: 0  "(('a', 'b'),)"

# CLI: rejects the extra token
r2 = runner.invoke(cli, ["--arg", "a", "b", "c"])
print(r2.exit_code)
print(r2.output)
# Actual: exit 2, "Got unexpected extra argument (c)"

Also reproduces with envvar="ARG" and env={"ARG": "a b c"}.

With five tokens ("a b c d e"), the result is (('a', 'b'), ('c', 'd')) — e is silently discarded.

Expected behavior

Envvar parsing should raise BadParameter (same family of arity error used for non-multiple nargs envvars), instead of silently dropping tokens and exiting 0.

Environment

  • Python version: 3.12.10
  • Click version: main @ 36baa15 (8.5.1 unreleased)

AI assistance disclosure: This bug report was prepared with assistance from Cursor (AI coding agent). I reproduced and validated the behavior locally.

ShamikOfficial avatar Sep 05 '26 21:09 ShamikOfficial

Opened a fix in a follow-up PR from ShamikOfficial:fix/envvar-multiple-nargs-truncation (will link once the PR URL is available). AI assistance disclosure: developed with Cursor; I reproduced/validated locally.

ShamikOfficial avatar Sep 05 '26 21:09 ShamikOfficial

Fix PR: https://github.com/pallets/click/pull/3844

ShamikOfficial avatar Sep 05 '26 21:09 ShamikOfficial

https://palletsprojects.com/contributing/llm-ai

davidism avatar Sep 06 '26 12:09 davidism