Path: blob/main/test/lib/python3.9/site-packages/pip/_internal/utils/deprecation.py
4804 views
"""1A module that implements tooling to enable easy warnings about deprecations.2"""34import logging5import warnings6from typing import Any, Optional, TextIO, Type, Union78from pip._vendor.packaging.version import parse910from pip import __version__ as current_version # NOTE: tests patch this name.1112DEPRECATION_MSG_PREFIX = "DEPRECATION: "131415class PipDeprecationWarning(Warning):16pass171819_original_showwarning: Any = None202122# Warnings <-> Logging Integration23def _showwarning(24message: Union[Warning, str],25category: Type[Warning],26filename: str,27lineno: int,28file: Optional[TextIO] = None,29line: Optional[str] = None,30) -> None:31if file is not None:32if _original_showwarning is not None:33_original_showwarning(message, category, filename, lineno, file, line)34elif issubclass(category, PipDeprecationWarning):35# We use a specially named logger which will handle all of the36# deprecation messages for pip.37logger = logging.getLogger("pip._internal.deprecations")38logger.warning(message)39else:40_original_showwarning(message, category, filename, lineno, file, line)414243def install_warning_logger() -> None:44# Enable our Deprecation Warnings45warnings.simplefilter("default", PipDeprecationWarning, append=True)4647global _original_showwarning4849if _original_showwarning is None:50_original_showwarning = warnings.showwarning51warnings.showwarning = _showwarning525354def deprecated(55*,56reason: str,57replacement: Optional[str],58gone_in: Optional[str],59feature_flag: Optional[str] = None,60issue: Optional[int] = None,61) -> None:62"""Helper to deprecate existing functionality.6364reason:65Textual reason shown to the user about why this functionality has66been deprecated. Should be a complete sentence.67replacement:68Textual suggestion shown to the user about what alternative69functionality they can use.70gone_in:71The version of pip does this functionality should get removed in.72Raises an error if pip's current version is greater than or equal to73this.74feature_flag:75Command-line flag of the form --use-feature={feature_flag} for testing76upcoming functionality.77issue:78Issue number on the tracker that would serve as a useful place for79users to find related discussion and provide feedback.80"""8182# Determine whether or not the feature is already gone in this version.83is_gone = gone_in is not None and parse(current_version) >= parse(gone_in)8485message_parts = [86(reason, f"{DEPRECATION_MSG_PREFIX}{{}}"),87(88gone_in,89"pip {} will enforce this behaviour change."90if not is_gone91else "Since pip {}, this is no longer supported.",92),93(94replacement,95"A possible replacement is {}.",96),97(98feature_flag,99"You can use the flag --use-feature={} to test the upcoming behaviour."100if not is_gone101else None,102),103(104issue,105"Discussion can be found at https://github.com/pypa/pip/issues/{}",106),107]108109message = " ".join(110format_str.format(value)111for value, format_str in message_parts112if format_str is not None and value is not None113)114115# Raise as an error if this behaviour is deprecated.116if is_gone:117raise PipDeprecationWarning(message)118119warnings.warn(message, category=PipDeprecationWarning, stacklevel=2)120121122