deprecation.py 3.1 KB

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