Fix unhelpful KeyError when using invalid VIRTUALENV_DISCOVERY value#3031
Merged
gaborbernat merged 3 commits intopypa:mainfrom Feb 15, 2026
Merged
Conversation
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
for more information, see https://pre-commit.ci
gaborbernat
approved these changes
Feb 15, 2026
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2896
Problem
When an invalid discovery method is specified via the
VIRTUALENV_DISCOVERYenvironment variable, virtualenv shows an unhelpfulKeyErrorinstead of a clear error message. This is especially confusing when the error appears buried in tox stacktraces.Before:
After:
Changes
get_discover()to check if the specified discovery method exists before attempting to access itValueErrorwith a helpful message listing all available discovery methodsTesting
Manually tested with:
Both now show clear error messages instead of KeyError. All existing tests pass.