Skip to content

Commit

Permalink
Merge pull request #3455 from bukzor/enable-warning-filters
Browse files Browse the repository at this point in the history
enable python -W with respect to PipDeprecationWarning
  • Loading branch information
xavfernandez committed Feb 11, 2016
2 parents 496b7c1 + 584d96b commit 8233a83
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 23 deletions.
4 changes: 0 additions & 4 deletions pip/__init__.py
Expand Up @@ -197,10 +197,6 @@ def main(args=None):
if args is None:
args = sys.argv[1:]

# Enable our Deprecation Warnings
for deprecation_warning in deprecation.DEPRECATIONS:
warnings.simplefilter("default", deprecation_warning)

# Configure our deprecation warnings to be sent through loggers
deprecation.install_warning_logger()

Expand Down
4 changes: 2 additions & 2 deletions pip/index.py
Expand Up @@ -19,7 +19,7 @@
from pip.utils import (
cached_property, splitext, normalize_path,
ARCHIVE_EXTENSIONS, SUPPORTED_EXTENSIONS, canonicalize_name)
from pip.utils.deprecation import RemovedInPip9Warning
from pip.utils.deprecation import RemovedInPip9Warning, RemovedInPip10Warning
from pip.utils.logging import indent_log
from pip.exceptions import (
DistributionNotFound, BestVersionAlreadyInstalled, InvalidWheelFilename,
Expand Down Expand Up @@ -1028,7 +1028,7 @@ def fmt_ctl_no_use_wheel(fmt_ctl):
fmt_ctl_no_binary(fmt_ctl)
warnings.warn(
'--no-use-wheel is deprecated and will be removed in the future. '
' Please use --no-binary :all: instead.', DeprecationWarning,
' Please use --no-binary :all: instead.', RemovedInPip10Warning,
stacklevel=2)


Expand Down
32 changes: 16 additions & 16 deletions pip/utils/deprecation.py
Expand Up @@ -11,23 +11,20 @@ class PipDeprecationWarning(Warning):
pass


class RemovedInPip9Warning(PipDeprecationWarning, DeprecationWarning):
class Pending(object):
pass


class RemovedInPip10Warning(PipDeprecationWarning, PendingDeprecationWarning):
class RemovedInPip9Warning(PipDeprecationWarning):
pass


class Python26DeprecationWarning(
PipDeprecationWarning, PendingDeprecationWarning
):
class RemovedInPip10Warning(PipDeprecationWarning, Pending):
pass


DEPRECATIONS = [
RemovedInPip9Warning, RemovedInPip10Warning, Python26DeprecationWarning
]
class Python26DeprecationWarning(PipDeprecationWarning, Pending):
pass


# Warnings <-> Logging Integration
Expand All @@ -53,22 +50,25 @@ def _showwarning(message, category, filename, lineno, file=None, line=None):
# want it to appear as if someone typed this entire message out.
log_message = "DEPRECATION: %s" % message

# Things that are DeprecationWarnings will be removed in the very
# next version of pip. We want these to be more obvious so we
# use the ERROR logging level while the PendingDeprecationWarnings
# are still have at least 2 versions to go until they are removed
# so they can just be warnings.
if issubclass(category, DeprecationWarning):
logger.error(log_message)
else:
# PipDeprecationWarnings that are Pending still have at least 2
# versions to go until they are removed so they can just be
# warnings. Otherwise, they will be removed in the very next
# version of pip. We want these to be more obvious so we use the
# ERROR logging level.
if issubclass(category, Pending):
logger.warning(log_message)
else:
logger.error(log_message)
else:
_warnings_showwarning(
message, category, filename, lineno, file, line,
)


def install_warning_logger():
# Enable our Deprecation Warnings
warnings.simplefilter("default", PipDeprecationWarning, append=True)

global _warnings_showwarning

if _warnings_showwarning is None:
Expand Down
6 changes: 5 additions & 1 deletion tests/functional/test_install_reqs.py
Expand Up @@ -233,8 +233,12 @@ def test_nowheel_user_with_prefix_in_pydistutils_cfg(script, data, virtualenv):
prefix=%s""" % script.scratch_path))

result = script.pip('install', '--no-use-wheel', '--user', '--no-index',
'-f', data.find_links, 'requiresupper')
'-f', data.find_links, 'requiresupper',
expect_stderr=True)
assert 'installed requiresupper' in result.stdout
assert ('DEPRECATION: --no-use-wheel is deprecated and will be removed '
'in the future. Please use --no-binary :all: instead.\n'
) in result.stderr


def test_install_option_in_requirements_file(script, data, virtualenv):
Expand Down
36 changes: 36 additions & 0 deletions tests/functional/test_warning.py
@@ -0,0 +1,36 @@
import pytest

PY26_WARNING = "Python 2.6 is no longer supported"


@pytest.mark.skipif("sys.version_info >= (2,7)")
def test_python26_options(script):
result = script.run(
'python', '-m', 'pip.__main__', 'list', expect_stderr=True,
)
assert PY26_WARNING in result.stderr
result = script.run('python', '-W', 'ignore', '-m', 'pip.__main__', 'list')
assert result.stderr == ''


@pytest.mark.skipif("sys.version_info < (2,7)")
def test_environ(script, tmpdir):
"""$PYTHONWARNINGS was added in python2.7"""
demo = tmpdir.join('warnings_demo.py')
demo.write('''
from pip.utils import deprecation
deprecation.install_warning_logger()
from logging import basicConfig
basicConfig()
from warnings import warn
warn("deprecated!", deprecation.PipDeprecationWarning)
''')

result = script.run('python', demo, expect_stderr=True)
assert result.stderr == 'ERROR:pip.deprecations:DEPRECATION: deprecated!\n'

script.environ['PYTHONWARNINGS'] = 'ignore'
result = script.run('python', demo)
assert result.stderr == ''

0 comments on commit 8233a83

Please sign in to comment.