| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import dataclasses |
| 4 |
import inspect |
| 5 |
from types import FunctionType |
| 6 |
from typing import Any |
| 7 |
from typing import final |
| 8 |
from typing import Generic |
| 9 |
from typing import TypeVar |
| 10 |
import warnings |
| 11 |
|
| 12 |
|
| 13 |
class PytestWarning(UserWarning): |
| 14 |
"""Base class for all warnings emitted by pytest.""" |
| 15 |
|
| 16 |
__module__ = "pytest" |
| 17 |
|
| 18 |
|
| 19 |
@final |
| 20 |
class PytestAssertRewriteWarning(PytestWarning): |
| 21 |
"""Warning emitted by the pytest assert rewrite module.""" |
| 22 |
|
| 23 |
__module__ = "pytest" |
| 24 |
|
| 25 |
|
| 26 |
@final |
| 27 |
class PytestCacheWarning(PytestWarning): |
| 28 |
"""Warning emitted by the cache plugin in various situations.""" |
| 29 |
|
| 30 |
__module__ = "pytest" |
| 31 |
|
| 32 |
|
| 33 |
@final |
| 34 |
class PytestConfigWarning(PytestWarning): |
| 35 |
"""Warning emitted for configuration issues.""" |
| 36 |
|
| 37 |
__module__ = "pytest" |
| 38 |
|
| 39 |
|
| 40 |
@final |
| 41 |
class PytestCollectionWarning(PytestWarning): |
| 42 |
"""Warning emitted when pytest is not able to collect a file or symbol in a module.""" |
| 43 |
|
| 44 |
__module__ = "pytest" |
| 45 |
|
| 46 |
|
| 47 |
class PytestDeprecationWarning(PytestWarning, DeprecationWarning): |
| 48 |
"""Warning class for features that will be removed in a future version.""" |
| 49 |
|
| 50 |
__module__ = "pytest" |
| 51 |
|
| 52 |
|
| 53 |
class PytestRemovedIn10Warning(PytestDeprecationWarning): |
| 54 |
"""Warning class for features that will be removed in pytest 10.""" |
| 55 |
|
| 56 |
__module__ = "pytest" |
| 57 |
|
| 58 |
|
| 59 |
@final |
| 60 |
class PytestExperimentalApiWarning(PytestWarning, FutureWarning): |
| 61 |
"""Warning category used to denote experiments in pytest. |
| 62 |
|
| 63 |
Use sparingly as the API might change or even be removed completely in a |
| 64 |
future version. |
| 65 |
""" |
| 66 |
|
| 67 |
__module__ = "pytest" |
| 68 |
|
| 69 |
@classmethod |
| 70 |
def simple(cls, apiname: str) -> PytestExperimentalApiWarning: |
| 71 |
return cls(f"{apiname} is an experimental api that may change over time") |
| 72 |
|
| 73 |
|
| 74 |
@final |
| 75 |
class PytestReturnNotNoneWarning(PytestWarning): |
| 76 |
""" |
| 77 |
Warning emitted when a test function returns a value other than ``None``. |
| 78 |
|
| 79 |
See :ref:`return-not-none` for details. |
| 80 |
""" |
| 81 |
|
| 82 |
__module__ = "pytest" |
| 83 |
|
| 84 |
|
| 85 |
@final |
| 86 |
class PytestUnknownMarkWarning(PytestWarning): |
| 87 |
"""Warning emitted on use of unknown markers. |
| 88 |
|
| 89 |
See :ref:`mark` for details. |
| 90 |
""" |
| 91 |
|
| 92 |
__module__ = "pytest" |
| 93 |
|
| 94 |
|
| 95 |
@final |
| 96 |
class PytestUnraisableExceptionWarning(PytestWarning): |
| 97 |
"""An unraisable exception was reported. |
| 98 |
|
| 99 |
Unraisable exceptions are exceptions raised in :meth:`__del__ <object.__del__>` |
| 100 |
implementations and similar situations when the exception cannot be raised |
| 101 |
as normal. |
| 102 |
""" |
| 103 |
|
| 104 |
__module__ = "pytest" |
| 105 |
|
| 106 |
|
| 107 |
@final |
| 108 |
class PytestUnhandledThreadExceptionWarning(PytestWarning): |
| 109 |
"""An unhandled exception occurred in a :class:`~threading.Thread`. |
| 110 |
|
| 111 |
Such exceptions don't propagate normally. |
| 112 |
""" |
| 113 |
|
| 114 |
__module__ = "pytest" |
| 115 |
|
| 116 |
|
| 117 |
_W = TypeVar("_W", bound=PytestWarning) |
| 118 |
|
| 119 |
|
| 120 |
@final |
| 121 |
@dataclasses.dataclass |
| 122 |
class UnformattedWarning(Generic[_W]): |
| 123 |
"""A warning meant to be formatted during runtime. |
| 124 |
|
| 125 |
This is used to hold warnings that need to format their message at runtime, |
| 126 |
as opposed to a direct message. |
| 127 |
""" |
| 128 |
|
| 129 |
category: type[_W] |
| 130 |
template: str |
| 131 |
|
| 132 |
def format(self, **kwargs: Any) -> _W: |
| 133 |
"""Return an instance of the warning category, formatted with given kwargs.""" |
| 134 |
return self.category(self.template.format(**kwargs)) |
| 135 |
|
| 136 |
|
| 137 |
@final |
| 138 |
class PytestFDWarning(PytestWarning): |
| 139 |
"""When the lsof plugin finds leaked fds.""" |
| 140 |
|
| 141 |
__module__ = "pytest" |
| 142 |
|
| 143 |
|
| 144 |
def warn_explicit_for(method: FunctionType, message: PytestWarning) -> None: |
| 145 |
""" |
| 146 |
Issue the warning :param:`message` for the definition of the given :param:`method` |
| 147 |
|
| 148 |
this helps to log warnings for functions defined prior to finding an issue with them |
| 149 |
(like hook wrappers being marked in a legacy mechanism) |
| 150 |
""" |
| 151 |
lineno = method.__code__.co_firstlineno |
| 152 |
filename = inspect.getfile(method) |
| 153 |
module = method.__module__ |
| 154 |
mod_globals = method.__globals__ |
| 155 |
try: |
| 156 |
warnings.warn_explicit( |
| 157 |
message, |
| 158 |
type(message), |
| 159 |
filename=filename, |
| 160 |
module=module, |
| 161 |
registry=mod_globals.setdefault("__warningregistry__", {}), |
| 162 |
lineno=lineno, |
| 163 |
) |
| 164 |
except Warning as w: |
| 165 |
# If warnings are errors (e.g. -Werror), location information gets lost, so we add it to the message. |
| 166 |
raise type(w)(f"{w}\n at {filename}:{lineno}") from None |