From 462501505bba517934a8b6362e64295af9c05429 Mon Sep 17 00:00:00 2001 From: GuyTuval Date: Sun, 18 Jul 2021 21:32:52 +0300 Subject: [PATCH 1/2] Implemented logging when passing an invalid parameter to `pip uninstall`. --- news/4958.feature.rst | 1 + src/pip/_internal/commands/uninstall.py | 11 +++++++++++ tests/functional/test_uninstall.py | 12 ++++++++++++ 3 files changed, 24 insertions(+) create mode 100644 news/4958.feature.rst diff --git a/news/4958.feature.rst b/news/4958.feature.rst new file mode 100644 index 00000000000..fbceb5cde72 --- /dev/null +++ b/news/4958.feature.rst @@ -0,0 +1 @@ +Add a warning when passing an invalid requirement to ``pip uninstall``. diff --git a/src/pip/_internal/commands/uninstall.py b/src/pip/_internal/commands/uninstall.py index 9a3c9f8815c..3d2bd7c4237 100644 --- a/src/pip/_internal/commands/uninstall.py +++ b/src/pip/_internal/commands/uninstall.py @@ -1,3 +1,4 @@ +import logging from optparse import Values from typing import List @@ -15,6 +16,9 @@ from pip._internal.utils.misc import protect_pip_from_modification_on_windows +logger = logging.getLogger(__name__) + + class UninstallCommand(Command, SessionCommandMixin): """ Uninstall packages. @@ -60,6 +64,13 @@ def run(self, options, args): ) if req.name: reqs_to_uninstall[canonicalize_name(req.name)] = req + else: + logger.warning( + "Invalid requirement: %r ignored -" + " the uninstall command expects named" + " requirements.", + name, + ) for filename in options.requirements: for parsed_req in parse_requirements( filename, diff --git a/tests/functional/test_uninstall.py b/tests/functional/test_uninstall.py index e6e32dad9a7..4fea1e2f90e 100644 --- a/tests/functional/test_uninstall.py +++ b/tests/functional/test_uninstall.py @@ -76,6 +76,18 @@ def test_basic_uninstall_with_scripts(script): ) +@pytest.mark.parametrize("name", + ["GTrolls.tar.gz", + "https://guyto.com/archives/"]) +def test_uninstall_invalid_parameter(script, caplog, name): + result = script.pip("uninstall", name, "-y", expect_error=True) + expected_message = ( + f"Invalid requirement: '{name}' ignored -" + f" the uninstall command expects named requirements." + ) + assert expected_message in result.stderr + + @pytest.mark.network def test_uninstall_easy_install_after_import(script): """ From a477a9b3d09b9aaf19dbe9013ded5b40953d7451 Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Sat, 24 Jul 2021 04:10:58 +0800 Subject: [PATCH 2/2] Fix isort error --- src/pip/_internal/commands/uninstall.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/pip/_internal/commands/uninstall.py b/src/pip/_internal/commands/uninstall.py index 3d2bd7c4237..13dadd688ea 100644 --- a/src/pip/_internal/commands/uninstall.py +++ b/src/pip/_internal/commands/uninstall.py @@ -15,7 +15,6 @@ ) from pip._internal.utils.misc import protect_pip_from_modification_on_windows - logger = logging.getLogger(__name__)