Coverage for /opt/homebrew/lib/python3.11/site-packages/_pytest/warning_types.py: 84%
64 statements
« prev ^ index » next coverage.py v7.2.3, created at 2023-05-04 13:14 +0700
« prev ^ index » next coverage.py v7.2.3, created at 2023-05-04 13:14 +0700
1import inspect
2import warnings
3from types import FunctionType
4from typing import Any
5from typing import Generic
6from typing import Type
7from typing import TypeVar
9import attr
11from _pytest.compat import final
14class PytestWarning(UserWarning):
15 """Base class for all warnings emitted by pytest."""
17 __module__ = "pytest"
20@final
21class PytestAssertRewriteWarning(PytestWarning):
22 """Warning emitted by the pytest assert rewrite module."""
24 __module__ = "pytest"
27@final
28class PytestCacheWarning(PytestWarning):
29 """Warning emitted by the cache plugin in various situations."""
31 __module__ = "pytest"
34@final
35class PytestConfigWarning(PytestWarning):
36 """Warning emitted for configuration issues."""
38 __module__ = "pytest"
41@final
42class PytestCollectionWarning(PytestWarning):
43 """Warning emitted when pytest is not able to collect a file or symbol in a module."""
45 __module__ = "pytest"
48class PytestDeprecationWarning(PytestWarning, DeprecationWarning):
49 """Warning class for features that will be removed in a future version."""
51 __module__ = "pytest"
54class PytestRemovedIn8Warning(PytestDeprecationWarning):
55 """Warning class for features that will be removed in pytest 8."""
57 __module__ = "pytest"
60class PytestReturnNotNoneWarning(PytestRemovedIn8Warning):
61 """Warning emitted when a test function is returning value other than None."""
63 __module__ = "pytest"
66@final
67class PytestExperimentalApiWarning(PytestWarning, FutureWarning):
68 """Warning category used to denote experiments in pytest.
70 Use sparingly as the API might change or even be removed completely in a
71 future version.
72 """
74 __module__ = "pytest"
76 @classmethod
77 def simple(cls, apiname: str) -> "PytestExperimentalApiWarning":
78 return cls(
79 "{apiname} is an experimental api that may change over time".format(
80 apiname=apiname
81 )
82 )
85@final
86class PytestUnhandledCoroutineWarning(PytestReturnNotNoneWarning):
87 """Warning emitted for an unhandled coroutine.
89 A coroutine was encountered when collecting test functions, but was not
90 handled by any async-aware plugin.
91 Coroutine test functions are not natively supported.
92 """
94 __module__ = "pytest"
97@final
98class PytestUnknownMarkWarning(PytestWarning):
99 """Warning emitted on use of unknown markers.
101 See :ref:`mark` for details.
102 """
104 __module__ = "pytest"
107@final
108class PytestUnraisableExceptionWarning(PytestWarning):
109 """An unraisable exception was reported.
111 Unraisable exceptions are exceptions raised in :meth:`__del__ <object.__del__>`
112 implementations and similar situations when the exception cannot be raised
113 as normal.
114 """
116 __module__ = "pytest"
119@final
120class PytestUnhandledThreadExceptionWarning(PytestWarning):
121 """An unhandled exception occurred in a :class:`~threading.Thread`.
123 Such exceptions don't propagate normally.
124 """
126 __module__ = "pytest"
129_W = TypeVar("_W", bound=PytestWarning)
132@final
133@attr.s(auto_attribs=True)
134class UnformattedWarning(Generic[_W]):
135 """A warning meant to be formatted during runtime.
137 This is used to hold warnings that need to format their message at runtime,
138 as opposed to a direct message.
139 """
141 category: Type["_W"]
142 template: str
144 def format(self, **kwargs: Any) -> _W:
145 """Return an instance of the warning category, formatted with given kwargs."""
146 return self.category(self.template.format(**kwargs))
149def warn_explicit_for(method: FunctionType, message: PytestWarning) -> None:
150 """
151 Issue the warning :param:`message` for the definition of the given :param:`method`
153 this helps to log warnigns for functions defined prior to finding an issue with them
154 (like hook wrappers being marked in a legacy mechanism)
155 """
156 lineno = method.__code__.co_firstlineno
157 filename = inspect.getfile(method)
158 module = method.__module__
159 mod_globals = method.__globals__
160 try:
161 warnings.warn_explicit(
162 message,
163 type(message),
164 filename=filename,
165 module=module,
166 registry=mod_globals.setdefault("__warningregistry__", {}),
167 lineno=lineno,
168 )
169 except Warning as w:
170 # If warnings are errors (e.g. -Werror), location information gets lost, so we add it to the message.
171 raise type(w)(f"{w}\n at {filename}:{lineno}") from None