Skip to content

Commit 32f642d

Browse files
committed
provide a better error message for "pip config get index-url"
1 parent 6efe708 commit 32f642d

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/pip/_internal/configuration.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ def get_value(self, key: str) -> Any:
147147
try:
148148
return self._dictionary[key]
149149
except KeyError:
150+
# disassembling triggers a more useful error message than simply
151+
# "No such key" in the case that the key isn't in the form command.option
152+
_disassemble_key(key)
150153
raise ConfigurationError(f"No such key - {orig_key}")
151154

152155
def set_value(self, key: str, value: Any) -> None:

tests/unit/test_configuration.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Tests for all things related to the configuration
22
"""
33

4+
import re
45
from unittest.mock import MagicMock
56

67
import pytest
@@ -87,6 +88,25 @@ def test_environment_config_errors_if_malformed(
8788
err.value
8889
)
8990

91+
def test_no_such_key_error_message_no_command(self) -> None:
92+
self.configuration.load_only = kinds.GLOBAL
93+
self.configuration.load()
94+
expected_msg = (
95+
"Key does not contain dot separated section and key. "
96+
"Perhaps you wanted to use 'global.index-url' instead?"
97+
)
98+
pat = f"^{re.escape(expected_msg)}$"
99+
with pytest.raises(ConfigurationError, match=pat):
100+
self.configuration.get_value("index-url")
101+
102+
def test_no_such_key_error_message_missing_option(self) -> None:
103+
self.configuration.load_only = kinds.GLOBAL
104+
self.configuration.load()
105+
expected_msg = "No such key - global.index-url"
106+
pat = f"^{re.escape(expected_msg)}$"
107+
with pytest.raises(ConfigurationError, match=pat):
108+
self.configuration.get_value("global.index-url")
109+
90110

91111
class TestConfigurationPrecedence(ConfigurationMixin):
92112
# Tests for methods to that determine the order of precedence of

0 commit comments

Comments
 (0)