deprecation.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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._internal.utils.typing import MYPY_CHECK_RUNNING
  8. if MYPY_CHECK_RUNNING:
  9. from typing import Any
  10. class PipDeprecationWarning(Warning):
  11. pass
  12. class Pending(object):
  13. pass
  14. class RemovedInPip11Warning(PipDeprecationWarning):
  15. pass
  16. class RemovedInPip12Warning(PipDeprecationWarning, Pending):
  17. pass
  18. # Warnings <-> Logging Integration
  19. _warnings_showwarning = None # type: Any
  20. def _showwarning(message, category, filename, lineno, file=None, line=None):
  21. if file is not None:
  22. if _warnings_showwarning is not None:
  23. _warnings_showwarning(
  24. message, category, filename, lineno, file, line,
  25. )
  26. else:
  27. if issubclass(category, PipDeprecationWarning):
  28. # We use a specially named logger which will handle all of the
  29. # deprecation messages for pip.
  30. logger = logging.getLogger("pip._internal.deprecations")
  31. # This is purposely using the % formatter here instead of letting
  32. # the logging module handle the interpolation. This is because we
  33. # want it to appear as if someone typed this entire message out.
  34. log_message = "DEPRECATION: %s" % message
  35. # PipDeprecationWarnings that are Pending still have at least 2
  36. # versions to go until they are removed so they can just be
  37. # warnings. Otherwise, they will be removed in the very next
  38. # version of pip. We want these to be more obvious so we use the
  39. # ERROR logging level.
  40. if issubclass(category, Pending):
  41. logger.warning(log_message)
  42. else:
  43. logger.error(log_message)
  44. else:
  45. _warnings_showwarning(
  46. message, category, filename, lineno, file, line,
  47. )
  48. def install_warning_logger():
  49. # Enable our Deprecation Warnings
  50. warnings.simplefilter("default", PipDeprecationWarning, append=True)
  51. global _warnings_showwarning
  52. if _warnings_showwarning is None:
  53. _warnings_showwarning = warnings.showwarning
  54. warnings.showwarning = _showwarning