You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

deprecation.py 3.0KB

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