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.

133 lines
3.1KB

  1. from typing import Any
  2. from typing import Generic
  3. from typing import Type
  4. from typing import TypeVar
  5. import attr
  6. from _pytest.compat import final
  7. class PytestWarning(UserWarning):
  8. """Base class for all warnings emitted by pytest."""
  9. __module__ = "pytest"
  10. @final
  11. class PytestAssertRewriteWarning(PytestWarning):
  12. """Warning emitted by the pytest assert rewrite module."""
  13. __module__ = "pytest"
  14. @final
  15. class PytestCacheWarning(PytestWarning):
  16. """Warning emitted by the cache plugin in various situations."""
  17. __module__ = "pytest"
  18. @final
  19. class PytestConfigWarning(PytestWarning):
  20. """Warning emitted for configuration issues."""
  21. __module__ = "pytest"
  22. @final
  23. class PytestCollectionWarning(PytestWarning):
  24. """Warning emitted when pytest is not able to collect a file or symbol in a module."""
  25. __module__ = "pytest"
  26. @final
  27. class PytestDeprecationWarning(PytestWarning, DeprecationWarning):
  28. """Warning class for features that will be removed in a future version."""
  29. __module__ = "pytest"
  30. @final
  31. class PytestExperimentalApiWarning(PytestWarning, FutureWarning):
  32. """Warning category used to denote experiments in pytest.
  33. Use sparingly as the API might change or even be removed completely in a
  34. future version.
  35. """
  36. __module__ = "pytest"
  37. @classmethod
  38. def simple(cls, apiname: str) -> "PytestExperimentalApiWarning":
  39. return cls(
  40. "{apiname} is an experimental api that may change over time".format(
  41. apiname=apiname
  42. )
  43. )
  44. @final
  45. class PytestUnhandledCoroutineWarning(PytestWarning):
  46. """Warning emitted for an unhandled coroutine.
  47. A coroutine was encountered when collecting test functions, but was not
  48. handled by any async-aware plugin.
  49. Coroutine test functions are not natively supported.
  50. """
  51. __module__ = "pytest"
  52. @final
  53. class PytestUnknownMarkWarning(PytestWarning):
  54. """Warning emitted on use of unknown markers.
  55. See :ref:`mark` for details.
  56. """
  57. __module__ = "pytest"
  58. @final
  59. class PytestUnraisableExceptionWarning(PytestWarning):
  60. """An unraisable exception was reported.
  61. Unraisable exceptions are exceptions raised in :meth:`__del__ <object.__del__>`
  62. implementations and similar situations when the exception cannot be raised
  63. as normal.
  64. """
  65. __module__ = "pytest"
  66. @final
  67. class PytestUnhandledThreadExceptionWarning(PytestWarning):
  68. """An unhandled exception occurred in a :class:`~threading.Thread`.
  69. Such exceptions don't propagate normally.
  70. """
  71. __module__ = "pytest"
  72. _W = TypeVar("_W", bound=PytestWarning)
  73. @final
  74. @attr.s
  75. class UnformattedWarning(Generic[_W]):
  76. """A warning meant to be formatted during runtime.
  77. This is used to hold warnings that need to format their message at runtime,
  78. as opposed to a direct message.
  79. """
  80. category = attr.ib(type=Type["_W"])
  81. template = attr.ib(type=str)
  82. def format(self, **kwargs: Any) -> _W:
  83. """Return an instance of the warning category, formatted with given kwargs."""
  84. return self.category(self.template.format(**kwargs))