deprecation.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. """
  2. A module that implements tooling to enable easy warnings about deprecations.
  3. """
  4. # The following comment should be removed at some point in the future.
  5. # mypy: disallow-untyped-defs=False
  6. from __future__ import absolute_import
  7. import logging
  8. import warnings
  9. from pip._vendor.packaging.version import parse
  10. from pip import __version__ as current_version
  11. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  12. if MYPY_CHECK_RUNNING:
  13. from typing import Any, Optional
  14. DEPRECATION_MSG_PREFIX = "DEPRECATION: "
  15. class PipDeprecationWarning(Warning):
  16. pass
  17. _original_showwarning = None # type: Any
  18. # Warnings <-> Logging Integration
  19. def _showwarning(message, category, filename, lineno, file=None, line=None):
  20. if file is not None:
  21. if _original_showwarning is not None:
  22. _original_showwarning(
  23. message, category, filename, lineno, file, line,
  24. )
  25. elif issubclass(category, PipDeprecationWarning):
  26. # We use a specially named logger which will handle all of the
  27. # deprecation messages for pip.
  28. logger = logging.getLogger("pip._internal.deprecations")
  29. logger.warning(message)
  30. else:
  31. _original_showwarning(
  32. message, category, filename, lineno, file, line,
  33. )
  34. def install_warning_logger():
  35. # type: () -> None
  36. # Enable our Deprecation Warnings
  37. warnings.simplefilter("default", PipDeprecationWarning, append=True)
  38. global _original_showwarning
  39. if _original_showwarning is None:
  40. _original_showwarning = warnings.showwarning
  41. warnings.showwarning = _showwarning
  42. def deprecated(reason, replacement, gone_in, issue=None):
  43. # type: (str, Optional[str], Optional[str], Optional[int]) -> None
  44. """Helper to deprecate existing functionality.
  45. reason:
  46. Textual reason shown to the user about why this functionality has
  47. been deprecated.
  48. replacement:
  49. Textual suggestion shown to the user about what alternative
  50. functionality they can use.
  51. gone_in:
  52. The version of pip does this functionality should get removed in.
  53. Raises errors if pip's current version is greater than or equal to
  54. this.
  55. issue:
  56. Issue number on the tracker that would serve as a useful place for
  57. users to find related discussion and provide feedback.
  58. Always pass replacement, gone_in and issue as keyword arguments for clarity
  59. at the call site.
  60. """
  61. # Construct a nice message.
  62. # This is eagerly formatted as we want it to get logged as if someone
  63. # typed this entire message out.
  64. sentences = [
  65. (reason, DEPRECATION_MSG_PREFIX + "{}"),
  66. (gone_in, "pip {} will remove support for this functionality."),
  67. (replacement, "A possible replacement is {}."),
  68. (issue, (
  69. "You can find discussion regarding this at "
  70. "https://github.com/pypa/pip/issues/{}."
  71. )),
  72. ]
  73. message = " ".join(
  74. template.format(val) for val, template in sentences if val is not None
  75. )
  76. # Raise as an error if it has to be removed.
  77. if gone_in is not None and parse(current_version) >= parse(gone_in):
  78. raise PipDeprecationWarning(message)
  79. warnings.warn(message, category=PipDeprecationWarning, stacklevel=2)