warnings.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. """Provide basic warnings used by setuptools modules.
  2. Using custom classes (other than ``UserWarning``) allow users to set
  3. ``PYTHONWARNINGS`` filters to run tests and prepare for upcoming changes in
  4. setuptools.
  5. """
  6. import os
  7. import warnings
  8. from datetime import date
  9. from inspect import cleandoc
  10. from textwrap import indent
  11. from typing import Optional, Tuple
  12. _DueDate = Tuple[int, int, int] # time tuple
  13. _INDENT = 8 * " "
  14. _TEMPLATE = f"""{80 * '*'}\n{{details}}\n{80 * '*'}"""
  15. class SetuptoolsWarning(UserWarning):
  16. """Base class in ``setuptools`` warning hierarchy."""
  17. @classmethod
  18. def emit(
  19. cls,
  20. summary: Optional[str] = None,
  21. details: Optional[str] = None,
  22. due_date: Optional[_DueDate] = None,
  23. see_docs: Optional[str] = None,
  24. see_url: Optional[str] = None,
  25. stacklevel: int = 2,
  26. **kwargs,
  27. ):
  28. """Private: reserved for ``setuptools`` internal use only"""
  29. # Default values:
  30. summary_ = summary or getattr(cls, "_SUMMARY", None) or ""
  31. details_ = details or getattr(cls, "_DETAILS", None) or ""
  32. due_date = due_date or getattr(cls, "_DUE_DATE", None)
  33. docs_ref = see_docs or getattr(cls, "_SEE_DOCS", None)
  34. docs_url = docs_ref and f"https://setuptools.pypa.io/en/latest/{docs_ref}"
  35. see_url = see_url or getattr(cls, "_SEE_URL", None)
  36. due = date(*due_date) if due_date else None
  37. text = cls._format(summary_, details_, due, see_url or docs_url, kwargs)
  38. if due and due < date.today() and _should_enforce():
  39. raise cls(text)
  40. warnings.warn(text, cls, stacklevel=stacklevel + 1)
  41. @classmethod
  42. def _format(
  43. cls,
  44. summary: str,
  45. details: str,
  46. due_date: Optional[date] = None,
  47. see_url: Optional[str] = None,
  48. format_args: Optional[dict] = None,
  49. ):
  50. """Private: reserved for ``setuptools`` internal use only"""
  51. today = date.today()
  52. summary = cleandoc(summary).format_map(format_args or {})
  53. possible_parts = [
  54. cleandoc(details).format_map(format_args or {}),
  55. (
  56. f"\nBy {due_date:%Y-%b-%d}, you need to update your project and remove "
  57. "deprecated calls\nor your builds will no longer be supported."
  58. if due_date and due_date > today
  59. else None
  60. ),
  61. (
  62. "\nThis deprecation is overdue, please update your project and remove "
  63. "deprecated\ncalls to avoid build errors in the future."
  64. if due_date and due_date < today
  65. else None
  66. ),
  67. (f"\nSee {see_url} for details." if see_url else None),
  68. ]
  69. parts = [x for x in possible_parts if x]
  70. if parts:
  71. body = indent(_TEMPLATE.format(details="\n".join(parts)), _INDENT)
  72. return "\n".join([summary, "!!\n", body, "\n!!"])
  73. return summary
  74. class InformationOnly(SetuptoolsWarning):
  75. """Currently there is no clear way of displaying messages to the users
  76. that use the setuptools backend directly via ``pip``.
  77. The only thing that might work is a warning, although it is not the
  78. most appropriate tool for the job...
  79. See pypa/packaging-problems#558.
  80. """
  81. class SetuptoolsDeprecationWarning(SetuptoolsWarning):
  82. """
  83. Base class for warning deprecations in ``setuptools``
  84. This class is not derived from ``DeprecationWarning``, and as such is
  85. visible by default.
  86. """
  87. def _should_enforce():
  88. enforce = os.getenv("SETUPTOOLS_ENFORCE_DEPRECATION", "false").lower()
  89. return enforce in ("true", "on", "ok", "1")