Development of an internal social media platform with personalised dashboards for students
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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. # Enable our Deprecation Warnings
  33. warnings.simplefilter("default", PipDeprecationWarning, append=True)
  34. global _original_showwarning
  35. if _original_showwarning is None:
  36. _original_showwarning = warnings.showwarning
  37. warnings.showwarning = _showwarning
  38. def deprecated(reason, replacement, gone_in, issue=None):
  39. # type: (str, Optional[str], Optional[str], Optional[int]) -> None
  40. """Helper to deprecate existing functionality.
  41. reason:
  42. Textual reason shown to the user about why this functionality has
  43. been deprecated.
  44. replacement:
  45. Textual suggestion shown to the user about what alternative
  46. functionality they can use.
  47. gone_in:
  48. The version of pip does this functionality should get removed in.
  49. Raises errors if pip's current version is greater than or equal to
  50. this.
  51. issue:
  52. Issue number on the tracker that would serve as a useful place for
  53. users to find related discussion and provide feedback.
  54. Always pass replacement, gone_in and issue as keyword arguments for clarity
  55. at the call site.
  56. """
  57. # Construct a nice message.
  58. # This is purposely eagerly formatted as we want it to appear as if someone
  59. # typed this entire message out.
  60. message = "DEPRECATION: " + reason
  61. if replacement is not None:
  62. message += " A possible replacement is {}.".format(replacement)
  63. if issue is not None:
  64. url = "https://github.com/pypa/pip/issues/" + str(issue)
  65. message += " You can find discussion regarding this at {}.".format(url)
  66. # Raise as an error if it has to be removed.
  67. if gone_in is not None and parse(current_version) >= parse(gone_in):
  68. raise PipDeprecationWarning(message)
  69. warnings.warn(message, category=PipDeprecationWarning, stacklevel=2)