Path: blob/master/venv/Lib/site-packages/pip/_internal/utils/deprecation.py
811 views
"""1A module that implements tooling to enable easy warnings about deprecations.2"""34# The following comment should be removed at some point in the future.5# mypy: disallow-untyped-defs=False67from __future__ import absolute_import89import logging10import warnings1112from pip._vendor.packaging.version import parse1314from pip import __version__ as current_version15from pip._internal.utils.typing import MYPY_CHECK_RUNNING1617if MYPY_CHECK_RUNNING:18from typing import Any, Optional192021DEPRECATION_MSG_PREFIX = "DEPRECATION: "222324class PipDeprecationWarning(Warning):25pass262728_original_showwarning = None # type: Any293031# Warnings <-> Logging Integration32def _showwarning(message, category, filename, lineno, file=None, line=None):33if file is not None:34if _original_showwarning is not None:35_original_showwarning(36message, category, filename, lineno, file, line,37)38elif issubclass(category, PipDeprecationWarning):39# We use a specially named logger which will handle all of the40# deprecation messages for pip.41logger = logging.getLogger("pip._internal.deprecations")42logger.warning(message)43else:44_original_showwarning(45message, category, filename, lineno, file, line,46)474849def install_warning_logger():50# type: () -> None51# Enable our Deprecation Warnings52warnings.simplefilter("default", PipDeprecationWarning, append=True)5354global _original_showwarning5556if _original_showwarning is None:57_original_showwarning = warnings.showwarning58warnings.showwarning = _showwarning596061def deprecated(reason, replacement, gone_in, issue=None):62# type: (str, Optional[str], Optional[str], Optional[int]) -> None63"""Helper to deprecate existing functionality.6465reason:66Textual reason shown to the user about why this functionality has67been deprecated.68replacement:69Textual suggestion shown to the user about what alternative70functionality they can use.71gone_in:72The version of pip does this functionality should get removed in.73Raises errors if pip's current version is greater than or equal to74this.75issue:76Issue number on the tracker that would serve as a useful place for77users to find related discussion and provide feedback.7879Always pass replacement, gone_in and issue as keyword arguments for clarity80at the call site.81"""8283# Construct a nice message.84# This is eagerly formatted as we want it to get logged as if someone85# typed this entire message out.86sentences = [87(reason, DEPRECATION_MSG_PREFIX + "{}"),88(gone_in, "pip {} will remove support for this functionality."),89(replacement, "A possible replacement is {}."),90(issue, (91"You can find discussion regarding this at "92"https://github.com/pypa/pip/issues/{}."93)),94]95message = " ".join(96template.format(val) for val, template in sentences if val is not None97)9899# Raise as an error if it has to be removed.100if gone_in is not None and parse(current_version) >= parse(gone_in):101raise PipDeprecationWarning(message)102103warnings.warn(message, category=PipDeprecationWarning, stacklevel=2)104105106