Skip to content

fix: empty list causes a crash due to index out of range, explictly raise ValueError now#2171

Merged
jensens merged 6 commits intocookiecutter:mainfrom
meganlkm:index-error-bugfix
Nov 8, 2025
Merged

fix: empty list causes a crash due to index out of range, explictly raise ValueError now#2171
jensens merged 6 commits intocookiecutter:mainfrom
meganlkm:index-error-bugfix

Conversation

@meganlkm
Copy link
Copy Markdown
Contributor

@meganlkm meganlkm commented Sep 30, 2025

Fixes the issue where sending an empty list in the options will raise an IndexError exception.

PR includes:

  • Returns an empty string when the options list is empty
  • test

More details:

Other observations:

  • windows tests are inconsistent. Without any changes the tests fail sometimes

@meganlkm
Copy link
Copy Markdown
Contributor Author

meganlkm commented Oct 6, 2025

@kurtmckee can you take a look?

@kurtmckee
Copy link
Copy Markdown
Member

@meganlkm I continue to think that an empty options list should manifest as a ValueError that explicitly says "the list of options is empty". This is the feedback that I previously provided in #2066, which was closed for lack of response and lack of tests.

@pydanny and @audreyfeldroy Would you take a gander at this behavioral change? Here's the summary:

When cookiecutter is told NOT TO ASK the user for input, but the list of choices to select from is empty:

  • cookiecutter currently raises an IndexError as a side effect of accessing the empty list's first element.

  • This PR modifies the behavior to return an empty string.

  • My suggestion is that an empty list should raise a ValueError to indicate that the list of choices is empty. Something like this:

        if not rendered_options:
            raise ValueError("The list of choices is empty")
        if no_input:
            return rendered_options[0]
        return read_user_choice(key, rendered_options, prompts, prefix)

The current behavior is incorrect, but I don't think the solution is to return a hard-coded empty string.

If returning an empty string seems like correct, new behavior to you, this PR is good for merging.

@jensens jensens self-requested a review November 8, 2025 10:19
@jensens
Copy link
Copy Markdown
Contributor

jensens commented Nov 8, 2025

sorry, i was about to suggest and well ... not enough coffee here

@jensens jensens changed the title fix issue where empty list causes a crash due to index out of range fix: empty list causes a crash due to index out of range, explictly raise ValueError now Nov 8, 2025
@jensens jensens self-requested a review November 8, 2025 10:40
@jensens jensens merged commit 3f87b49 into cookiecutter:main Nov 8, 2025
7 checks passed
armandmasseaugit pushed a commit to armandmasseaugit/cookiecutter that referenced this pull request Nov 19, 2025
…aise ValueError now (cookiecutter#2171)

* fix issue where empty list causes a crash due to index out of range

* Update cookiecutter/prompt.py

* Update tests/test_prompt.py

* Update cookiecutter/prompt.py

* Update tests/test_prompt.py

---------

Co-authored-by: Jens W. Klein <jk@kleinundpartner.at>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants