Skip to content

Comments

Improve error message for invalid virtualenvs.prompt config#10649

Closed
aryanyk wants to merge 3 commits intopython-poetry:mainfrom
aryanyk:fix-invalid-virtualenv-prompt-error
Closed

Improve error message for invalid virtualenvs.prompt config#10649
aryanyk wants to merge 3 commits intopython-poetry:mainfrom
aryanyk:fix-invalid-virtualenv-prompt-error

Conversation

@aryanyk
Copy link

@aryanyk aryanyk commented Dec 9, 2025

Pull Request Check List

Resolves: #Resolves: #10635

Summary

Improve error message when an invalid template variable is used in
virtualenvs.prompt configuration.

Details

Previously an invalid placeholder such as {python-version} resulted in
a cryptic KeyError. This change catches the error and raises a clearer
PoetryError explaining the config source and how to fix it.

Testing

  • Reproduced and validated locally using WSL (Ubuntu)
error_regen image

Summary by Sourcery

Bug Fixes:

  • Prevent cryptic KeyError when virtualenv prompt configuration contains an unknown template variable by raising a descriptive PoetryError instead.

@sourcery-ai
Copy link

sourcery-ai bot commented Dec 9, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Improves handling of invalid template variables in the virtualenv prompt configuration by converting a raw KeyError into a clearer, user-facing PoetryError with guidance on how to fix the configuration.

Sequence diagram for handling invalid virtualenvs.prompt template variables

sequenceDiagram
    actor User
    participant PoetryCLI
    participant EnvManager
    participant PythonFormatEngine

    User->>PoetryCLI: Run_command_that_creates_virtualenv
    PoetryCLI->>EnvManager: create_venv(venv_prompt)

    alt venv_prompt_is_not_none
        EnvManager->>PythonFormatEngine: venv_prompt.format(project_name, python_version)
        alt template_variable_is_valid
            PythonFormatEngine-->>EnvManager: formatted_prompt
            EnvManager-->>PoetryCLI: virtualenv_created_with_custom_prompt
            PoetryCLI-->>User: Command_succeeds
        else template_variable_is_invalid
            PythonFormatEngine-->>EnvManager: KeyError
            EnvManager-->>PoetryCLI: PoetryError_invalid_template_variable
            PoetryCLI-->>User: Clear_error_message_with_config_guidance
        end
    else venv_prompt_is_none
        EnvManager-->>PoetryCLI: virtualenv_created_with_default_prompt
        PoetryCLI-->>User: Command_succeeds
    end
Loading

File-Level Changes

Change Details Files
Add defensive error handling around virtualenv prompt formatting to surface clearer configuration errors to users.
  • Wrap the venv_prompt.format(...) call in a try/except block to intercept KeyError caused by unknown template variables.
  • On KeyError, raise a PoetryError instead of letting the KeyError propagate, preserving the original exception as the cause.
  • Include the invalid template variable name and the configuration option path (virtualenvs.prompt) in the PoetryError message, with guidance to check the Poetry configuration file.
src/poetry/utils/env/env_manager.py

Assessment against linked issues

Issue Objective Addressed Explanation
#10635 Provide a clear and descriptive error message when an invalid template variable is used in the virtualenvs.prompt configuration (e.g., {python-version} instead of {python_version}).
#10635 Ensure the error message indicates that the problem originates from the global Poetry configuration (virtualenvs.prompt), not from Python or the local pyproject.toml.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes - here's some feedback:

  • Consider including the list of supported template variables (e.g. project_name, python_version) in the error message so users immediately know what they can use instead of the invalid placeholder.
  • If possible, reference the concrete config file path or source (e.g. poetry config --list or the actual file location) in the error message rather than the generic 'Poetry configuration file' to make it easier for users to locate and fix the setting.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider including the list of supported template variables (e.g. `project_name`, `python_version`) in the error message so users immediately know what they can use instead of the invalid placeholder.
- If possible, reference the concrete config file path or source (e.g. `poetry config --list` or the actual file location) in the error message rather than the generic 'Poetry configuration file' to make it easier for users to locate and fix the setting.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@aryanyk
Copy link
Author

aryanyk commented Dec 9, 2025

Thanks for the feedback! Makes sense — I’ll update the error message to include the supported template variables and reference the concrete config source so it’s easier for users to fix. Will push an update shortly.

@aryanyk
Copy link
Author

aryanyk commented Dec 9, 2025

I’ve updated the error message to include the supported template variables and more concrete config hints, and applied Ruff formatting. Let me know if you’d like the wording tweaked further.

@aryanyk
Copy link
Author

aryanyk commented Dec 10, 2025

Hi @radoering ,

I’ve pushed the requested changes and all CI checks are green now ✅
Could you please review the PR when you get a chance?

Thanks for your time and guidance!

Copy link
Member

@radoering radoering left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, someone else created a similar PR shortly before you: #10648.

Since that PR covers an additional error, adds tests and was the first one, I tend to prefer it - if the author addresses the review feedback.

Thank you anyway.

except KeyError as e:
raise PoetryError(
f'Invalid template variable "{e.args[0]}" in '
'global configuration option "virtualenvs.prompt".\n\n'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not necessarily in the global configuration. It can also be in the local configuration.

'global configuration option "virtualenvs.prompt".\n\n'
"Supported variables are: project_name, python_version.\n"
"You can update this setting via:\n"
" - ~/.config/pypoetry/config.toml\n"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is only the path for Linux and only the global configuration. It probably makes more sense to recommend setting it via poetry config [--local] virtualenvs.prompt <value>.

@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 20, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error message for incorrect configs is unclear

2 participants