Skip to content

Commit 239a307

Browse files
GuyTuvaluranusjr
andauthored
Error handling upon uninstall invalid parameter (#10171)
Co-authored-by: Tzu-ping Chung <[email protected]>
1 parent 5e86264 commit 239a307

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

news/4958.feature.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add a warning when passing an invalid requirement to ``pip uninstall``.

src/pip/_internal/commands/uninstall.py

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
from optparse import Values
23
from typing import List
34

@@ -14,6 +15,8 @@
1415
)
1516
from pip._internal.utils.misc import protect_pip_from_modification_on_windows
1617

18+
logger = logging.getLogger(__name__)
19+
1720

1821
class UninstallCommand(Command, SessionCommandMixin):
1922
"""
@@ -58,6 +61,13 @@ def run(self, options: Values, args: List[str]) -> int:
5861
)
5962
if req.name:
6063
reqs_to_uninstall[canonicalize_name(req.name)] = req
64+
else:
65+
logger.warning(
66+
"Invalid requirement: %r ignored -"
67+
" the uninstall command expects named"
68+
" requirements.",
69+
name,
70+
)
6171
for filename in options.requirements:
6272
for parsed_req in parse_requirements(
6373
filename,

tests/functional/test_uninstall.py

+12
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ def test_basic_uninstall_with_scripts(script):
7676
)
7777

7878

79+
@pytest.mark.parametrize("name",
80+
["GTrolls.tar.gz",
81+
"https://guyto.com/archives/"])
82+
def test_uninstall_invalid_parameter(script, caplog, name):
83+
result = script.pip("uninstall", name, "-y", expect_error=True)
84+
expected_message = (
85+
f"Invalid requirement: '{name}' ignored -"
86+
f" the uninstall command expects named requirements."
87+
)
88+
assert expected_message in result.stderr
89+
90+
7991
@pytest.mark.network
8092
def test_uninstall_easy_install_after_import(script):
8193
"""

0 commit comments

Comments
 (0)