deprecation.py 3.2 KB

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