From ef35e73a711f980972d1ed5f7514bd816c1ccca5 Mon Sep 17 00:00:00 2001 From: Paul Moore Date: Mon, 5 Apr 2021 15:31:08 +0100 Subject: [PATCH 1/2] Make pip work with warnings converted to errors --- src/pip/__main__.py | 11 +++++++++-- tests/functional/test_warning.py | 5 +++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/pip/__main__.py b/src/pip/__main__.py index 063fd1aacfd..fe34a7b7772 100644 --- a/src/pip/__main__.py +++ b/src/pip/__main__.py @@ -1,5 +1,6 @@ import os import sys +import warnings # Remove '' and current working directory from the first entry # of sys.path, if present to avoid using current directory @@ -18,7 +19,13 @@ path = os.path.dirname(os.path.dirname(__file__)) sys.path.insert(0, path) -from pip._internal.cli.main import main as _main - if __name__ == "__main__": + # Work around the error reported in #9540, pending a proper fix. + # Note: It is essential the warning filter is set *before* importing + # pip, as the deprecation happens at import time, not runtime. + warnings.filterwarnings( + "ignore", category=DeprecationWarning, module=".*packaging\\.version" + ) + from pip._internal.cli.main import main as _main + sys.exit(_main()) diff --git a/tests/functional/test_warning.py b/tests/functional/test_warning.py index 3558704bcef..5c5b1a201ce 100644 --- a/tests/functional/test_warning.py +++ b/tests/functional/test_warning.py @@ -42,3 +42,8 @@ def test_version_warning_is_not_shown_if_python_version_is_not_2(script): def test_flag_does_nothing_if_python_version_is_not_2(script): script.pip("list", "--no-python-version-warning") + + +def test_pip_works_with_warnings_as_errors(script): + script.environ['PYTHONWARNINGS'] = 'error' + script.pip("--version") From 8d6870aba5241468897fed5cbb109e16ac319b27 Mon Sep 17 00:00:00 2001 From: Paul Moore Date: Mon, 5 Apr 2021 15:35:29 +0100 Subject: [PATCH 2/2] News file --- news/9779.bugfix.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 news/9779.bugfix.rst diff --git a/news/9779.bugfix.rst b/news/9779.bugfix.rst new file mode 100644 index 00000000000..2145b641e27 --- /dev/null +++ b/news/9779.bugfix.rst @@ -0,0 +1 @@ +Fix pip to work with warnings converted to errors.