Skip to content

Commit a64ab1a

Browse files
authored
Merge pull request #9779 from pfmoore/workaround_9540
Make pip work with warnings converted to errors
2 parents 3679947 + 8d6870a commit a64ab1a

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

news/9779.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix pip to work with warnings converted to errors.

src/pip/__main__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import sys
3+
import warnings
34

45
# Remove '' and current working directory from the first entry
56
# of sys.path, if present to avoid using current directory
@@ -18,7 +19,13 @@
1819
path = os.path.dirname(os.path.dirname(__file__))
1920
sys.path.insert(0, path)
2021

21-
from pip._internal.cli.main import main as _main
22-
2322
if __name__ == "__main__":
23+
# Work around the error reported in #9540, pending a proper fix.
24+
# Note: It is essential the warning filter is set *before* importing
25+
# pip, as the deprecation happens at import time, not runtime.
26+
warnings.filterwarnings(
27+
"ignore", category=DeprecationWarning, module=".*packaging\\.version"
28+
)
29+
from pip._internal.cli.main import main as _main
30+
2431
sys.exit(_main())

tests/functional/test_warning.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,8 @@ def test_version_warning_is_not_shown_if_python_version_is_not_2(script):
4242

4343
def test_flag_does_nothing_if_python_version_is_not_2(script):
4444
script.pip("list", "--no-python-version-warning")
45+
46+
47+
def test_pip_works_with_warnings_as_errors(script):
48+
script.environ['PYTHONWARNINGS'] = 'error'
49+
script.pip("--version")

0 commit comments

Comments
 (0)