deprecation.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 # NOTE: tests patch this name.
  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. *,
  41. reason: str,
  42. replacement: Optional[str],
  43. gone_in: Optional[str],
  44. feature_flag: Optional[str] = None,
  45. issue: Optional[int] = None,
  46. ) -> None:
  47. """Helper to deprecate existing functionality.
  48. reason:
  49. Textual reason shown to the user about why this functionality has
  50. been deprecated. Should be a complete sentence.
  51. replacement:
  52. Textual suggestion shown to the user about what alternative
  53. functionality they can use.
  54. gone_in:
  55. The version of pip does this functionality should get removed in.
  56. Raises an error if pip's current version is greater than or equal to
  57. this.
  58. feature_flag:
  59. Command-line flag of the form --use-feature={feature_flag} for testing
  60. upcoming functionality.
  61. issue:
  62. Issue number on the tracker that would serve as a useful place for
  63. users to find related discussion and provide feedback.
  64. """
  65. # Determine whether or not the feature is already gone in this version.
  66. is_gone = gone_in is not None and parse(current_version) >= parse(gone_in)
  67. message_parts = [
  68. (reason, f"{DEPRECATION_MSG_PREFIX}{{}}"),
  69. (
  70. gone_in,
  71. "pip {} will enforce this behaviour change."
  72. if not is_gone
  73. else "Since pip {}, this is no longer supported.",
  74. ),
  75. (
  76. replacement,
  77. "A possible replacement is {}.",
  78. ),
  79. (
  80. feature_flag,
  81. "You can use the flag --use-feature={} to test the upcoming behaviour."
  82. if not is_gone
  83. else None,
  84. ),
  85. (
  86. issue,
  87. "Discussion can be found at https://github.com/pypa/pip/issues/{}",
  88. ),
  89. ]
  90. message = " ".join(
  91. format_str.format(value)
  92. for value, format_str in message_parts
  93. if format_str is not None and value is not None
  94. )
  95. # Raise as an error if this behaviour is deprecated.
  96. if is_gone:
  97. raise PipDeprecationWarning(message)
  98. warnings.warn(message, category=PipDeprecationWarning, stacklevel=2)