Skip to content

Fix unhelpful KeyError when using invalid VIRTUALENV_DISCOVERY value#3031

Merged
gaborbernat merged 3 commits intopypa:mainfrom
veeceey:fix/issue-2896-discovery-error-message
Feb 15, 2026
Merged

Fix unhelpful KeyError when using invalid VIRTUALENV_DISCOVERY value#3031
gaborbernat merged 3 commits intopypa:mainfrom
veeceey:fix/issue-2896-discovery-error-message

Conversation

@veeceey
Copy link
Contributor

@veeceey veeceey commented Feb 13, 2026

Fixes #2896

Problem

When an invalid discovery method is specified via the VIRTUALENV_DISCOVERY environment variable, virtualenv shows an unhelpful KeyError instead of a clear error message. This is especially confusing when the error appears buried in tox stacktraces.

Before:

KeyError: 'pyenv'

After:

ValueError: Invalid discovery method 'pyenv'. Available options: 'builtin'

Changes

  • Added validation in get_discover() to check if the specified discovery method exists before attempting to access it
  • Raises a clear ValueError with a helpful message listing all available discovery methods
  • Added test case to ensure the error handling works correctly

Testing

Manually tested with:

VIRTUALENV_DISCOVERY=pyenv virtualenv myenv
VIRTUALENV_DISCOVERY=nonexistent virtualenv myenv

Both now show clear error messages instead of KeyError. All existing tests pass.

veeceey and others added 3 commits February 13, 2026 00:50
When an invalid discovery method is specified via the VIRTUALENV_DISCOVERY
environment variable, virtualenv now raises a clear ValueError listing all
available discovery methods instead of showing a confusing KeyError.

This improves the user experience by making it immediately clear what
went wrong and what the valid options are, especially useful when the
error appears in the middle of a long tox stacktrace.

Fixes pypa#2896
@gaborbernat gaborbernat enabled auto-merge (squash) February 15, 2026 05:12
@gaborbernat gaborbernat merged commit 004e463 into pypa:main Feb 15, 2026
51 checks passed
veeceey added a commit to veeceey/virtualenv that referenced this pull request Feb 16, 2026
The test_invalid_discovery_method_via_env test was expecting a ValueError
with the old error format from PR pypa#3031, but our changes raise a
RuntimeError with improved messaging. Update the test assertions to match.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
veeceey added a commit to veeceey/virtualenv that referenced this pull request Feb 16, 2026
The test_invalid_discovery_method_via_env test was expecting a ValueError
with the old error format from PR pypa#3031, but our changes raise a
RuntimeError with improved messaging. Update the test assertions to match.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

Crude error message if using VIRTUALENV_DISCOVERY=pyenv

2 participants

Comments