Skip to content

Commit 81805a5

Browse files
victorvpaulochrahunt
authored andcommitted
Add option to silence warnings related to deprecation of Python versions (#6739)
* Add option to silence warnings related to deprecation of Python versions * Move skip_if_python2 and skip_if_not_python2 decorator declaratios to test/lib/__init__.py and use them in test_warning.py * Add tests to ensure that python version deprecation warning is shown correctly and can be silenced by a flag. * Add new test to ensure that --no-python-version-warning flag does nothing if python version is not 2
1 parent 9573c2b commit 81805a5

File tree

6 files changed

+63
-5
lines changed

6 files changed

+63
-5
lines changed

news/6673.feature

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add option ``--no-python-version-warning`` to silence warnings
2+
related to deprecation of Python versions.

src/pip/_internal/cli/base_command.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ def _main(self, args):
122122
user_log_file=options.log,
123123
)
124124

125-
if sys.version_info[:2] == (2, 7):
125+
if (
126+
sys.version_info[:2] == (2, 7) and
127+
not options.no_python_version_warning
128+
):
126129
message = (
127130
"A future version of pip will drop support for Python 2.7. "
128131
"More details about Python 2 support in pip, can be found at "

src/pip/_internal/cli/cmdoptions.py

+11
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,16 @@ def check_list_path_option(options):
891891
)
892892

893893

894+
no_python_version_warning = partial(
895+
Option,
896+
'--no-python-version-warning',
897+
dest='no_python_version_warning',
898+
action='store_true',
899+
default=False,
900+
help='Silence deprecation warnings for upcoming unsupported Pythons.',
901+
) # type: Callable[..., Option]
902+
903+
894904
##########
895905
# groups #
896906
##########
@@ -918,6 +928,7 @@ def check_list_path_option(options):
918928
no_cache,
919929
disable_pip_version_check,
920930
no_color,
931+
no_python_version_warning,
921932
]
922933
} # type: Dict[str, Any]
923934

tests/functional/test_install.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from os.path import curdir, join, pardir
1010

1111
import pytest
12-
from pip._vendor.six import PY2
1312

1413
from pip import __version__ as pip_current_version
1514
from pip._internal.cli.status_codes import ERROR, SUCCESS
@@ -27,6 +26,8 @@
2726
pyversion,
2827
pyversion_tuple,
2928
requirements_file,
29+
skip_if_not_python2,
30+
skip_if_python2,
3031
)
3132
from tests.lib.filesystem import make_socket_file
3233
from tests.lib.local_repos import local_checkout
@@ -38,9 +39,6 @@
3839
server_running,
3940
)
4041

41-
skip_if_python2 = pytest.mark.skipif(PY2, reason="Non-Python 2 only")
42-
skip_if_not_python2 = pytest.mark.skipif(not PY2, reason="Python 2 only")
43-
4442

4543
@pytest.mark.parametrize('command', ('install', 'wheel'))
4644
@pytest.mark.parametrize('variant', ('missing_setuptools', 'bad_setuptools'))

tests/functional/test_warning.py

+39
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import platform
12
import textwrap
23

34
import pytest
45

6+
from tests.lib import skip_if_not_python2, skip_if_python2
7+
58

69
@pytest.fixture
710
def warnings_demo(tmpdir):
@@ -28,3 +31,39 @@ def test_deprecation_warnings_can_be_silenced(script, warnings_demo):
2831
script.environ['PYTHONWARNINGS'] = 'ignore'
2932
result = script.run('python', warnings_demo)
3033
assert result.stderr == ''
34+
35+
36+
DEPRECATION_TEXT = "drop support for Python 2.7"
37+
CPYTHON_DEPRECATION_TEXT = "January 1st, 2020"
38+
39+
40+
@skip_if_python2
41+
def test_version_warning_is_not_shown_if_python_version_is_not_2(script):
42+
result = script.pip("debug", allow_stderr_warning=True)
43+
assert DEPRECATION_TEXT not in result.stderr, str(result)
44+
assert CPYTHON_DEPRECATION_TEXT not in result.stderr, str(result)
45+
46+
47+
@skip_if_python2
48+
def test_flag_does_nothing_if_python_version_is_not_2(script):
49+
script.pip("list", "--no-python-version-warning")
50+
51+
52+
@skip_if_not_python2
53+
def test_version_warning_is_shown_if_python_version_is_2(script):
54+
result = script.pip("debug", allow_stderr_warning=True)
55+
assert DEPRECATION_TEXT in result.stderr, str(result)
56+
if platform.python_implementation() == 'CPython':
57+
assert CPYTHON_DEPRECATION_TEXT in result.stderr, str(result)
58+
else:
59+
assert CPYTHON_DEPRECATION_TEXT not in result.stderr, str(result)
60+
61+
62+
@skip_if_not_python2
63+
def test_version_warning_is_not_shown_when_flag_is_passed(script):
64+
result = script.pip(
65+
"debug", "--no-python-version-warning", allow_stderr_warning=True
66+
)
67+
assert DEPRECATION_TEXT not in result.stderr, str(result)
68+
assert CPYTHON_DEPRECATION_TEXT not in result.stderr, str(result)
69+
assert "--no-python-version-warning" not in result.stderr

tests/lib/__init__.py

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from textwrap import dedent
1212

1313
import pytest
14+
from pip._vendor.six import PY2
1415
from scripttest import FoundDir, TestFileEnvironment
1516

1617
from pip._internal.index.collector import LinkCollector
@@ -1047,3 +1048,7 @@ def need_mercurial(fn):
10471048
return pytest.mark.mercurial(need_executable(
10481049
'Mercurial', ('hg', 'version')
10491050
)(fn))
1051+
1052+
1053+
skip_if_python2 = pytest.mark.skipif(PY2, reason="Non-Python 2 only")
1054+
skip_if_not_python2 = pytest.mark.skipif(not PY2, reason="Python 2 only")

0 commit comments

Comments
 (0)