Skip to content

Commit 4b35763

Browse files
committed
pip config normalizes names, converting underscores into dashes. closes pypa#9330
1 parent 04f754d commit 4b35763

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

news/9330.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
``pip config`` normalizes names, converting underscores into dashes

src/pip/_internal/configuration.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,16 @@ def items(self) -> Iterable[Tuple[str, Any]]:
142142

143143
def get_value(self, key: str) -> Any:
144144
"""Get a value from the configuration."""
145+
orig_key = key
146+
key = _normalize_name(key)
145147
try:
146148
return self._dictionary[key]
147149
except KeyError:
148-
raise ConfigurationError(f"No such key - {key}")
150+
raise ConfigurationError(f"No such key - {orig_key}")
149151

150152
def set_value(self, key: str, value: Any) -> None:
151153
"""Modify a value in the configuration."""
154+
key = _normalize_name(key)
152155
self._ensure_have_load_only()
153156

154157
assert self.load_only
@@ -167,11 +170,13 @@ def set_value(self, key: str, value: Any) -> None:
167170

168171
def unset_value(self, key: str) -> None:
169172
"""Unset a value in the configuration."""
173+
orig_key = key
174+
key = _normalize_name(key)
170175
self._ensure_have_load_only()
171176

172177
assert self.load_only
173178
if key not in self._config[self.load_only]:
174-
raise ConfigurationError(f"No such key - {key}")
179+
raise ConfigurationError(f"No such key - {orig_key}")
175180

176181
fname, parser = self._get_parser_to_modify()
177182

tests/unit/test_configuration.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,8 @@ class TestConfigurationModification(ConfigurationMixin):
185185
def test_no_specific_given_modification(self) -> None:
186186
self.configuration.load()
187187

188-
try:
188+
with pytest.raises(ConfigurationError):
189189
self.configuration.set_value("test.hello", "10")
190-
except ConfigurationError:
191-
pass
192-
else:
193-
assert False, "Should have raised an error."
194190

195191
def test_site_modification(self) -> None:
196192
self.configuration.load_only = kinds.SITE
@@ -241,3 +237,16 @@ def test_global_modification(self) -> None:
241237
# get the path to user config file
242238
assert mymock.call_count == 1
243239
assert mymock.call_args[0][0] == (get_configuration_files()[kinds.GLOBAL][-1])
240+
241+
def test_normalization(self) -> None:
242+
# underscores and dashes can be used interchangeably.
243+
# internally, underscores get converted into dashes before reading/writing file
244+
self.configuration.load_only = kinds.GLOBAL
245+
self.configuration.load()
246+
self.configuration.set_value("global.index-url", "example.org")
247+
assert self.configuration.get_value("global.index_url") == "example.org"
248+
assert self.configuration.get_value("global.index-url") == "example.org"
249+
self.configuration.unset_value("global.index-url")
250+
pat = r"^No such key - global\.index-url$"
251+
with pytest.raises(ConfigurationError, match=pat):
252+
self.configuration.get_value("global.index-url")

0 commit comments

Comments
 (0)