Skip to content

Commit 52fef81

Browse files
symonknicoddemus
andauthored
permit node to warn with any warning type, not just PytestWarning (#8052)
Co-authored-by: Bruno Oliveira <[email protected]>
1 parent afd53ed commit 52fef81

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

changelog/7615.improvement.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:meth:`Node.warn <_pytest.nodes.Node.warn>` now permits any subclass of :class:`Warning`, not just :class:`PytestWarning <pytest.PytestWarning>`.

src/_pytest/nodes.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
if TYPE_CHECKING:
3535
# Imported here due to circular import.
3636
from _pytest.main import Session
37-
from _pytest.warning_types import PytestWarning
3837
from _pytest._code.code import _TracebackStyle
3938

4039

@@ -198,27 +197,31 @@ def ihook(self):
198197
def __repr__(self) -> str:
199198
return "<{} {}>".format(self.__class__.__name__, getattr(self, "name", None))
200199

201-
def warn(self, warning: "PytestWarning") -> None:
200+
def warn(self, warning: Warning) -> None:
202201
"""Issue a warning for this Node.
203202
204203
Warnings will be displayed after the test session, unless explicitly suppressed.
205204
206205
:param Warning warning:
207-
The warning instance to issue. Must be a subclass of PytestWarning.
206+
The warning instance to issue.
208207
209-
:raises ValueError: If ``warning`` instance is not a subclass of PytestWarning.
208+
:raises ValueError: If ``warning`` instance is not a subclass of Warning.
210209
211210
Example usage:
212211
213212
.. code-block:: python
214213
215214
node.warn(PytestWarning("some message"))
216-
"""
217-
from _pytest.warning_types import PytestWarning
215+
node.warn(UserWarning("some message"))
218216
219-
if not isinstance(warning, PytestWarning):
217+
.. versionchanged:: 6.2
218+
Any subclass of :class:`Warning` is now accepted, rather than only
219+
:class:`PytestWarning <pytest.PytestWarning>` subclasses.
220+
"""
221+
# enforce type checks here to avoid getting a generic type error later otherwise.
222+
if not isinstance(warning, Warning):
220223
raise ValueError(
221-
"warning must be an instance of PytestWarning or subclass, got {!r}".format(
224+
"warning must be an instance of Warning or subclass, got {!r}".format(
222225
warning
223226
)
224227
)

testing/test_nodes.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from typing import List
2+
from typing import Type
23

34
import py
45

56
import pytest
67
from _pytest import nodes
78
from _pytest.pytester import Pytester
9+
from _pytest.warning_types import PytestWarning
810

911

1012
@pytest.mark.parametrize(
@@ -35,15 +37,33 @@ def test_node_from_parent_disallowed_arguments() -> None:
3537
nodes.Node.from_parent(None, config=None) # type: ignore[arg-type]
3638

3739

38-
def test_std_warn_not_pytestwarning(pytester: Pytester) -> None:
40+
@pytest.mark.parametrize(
41+
"warn_type, msg", [(DeprecationWarning, "deprecated"), (PytestWarning, "pytest")]
42+
)
43+
def test_node_warn_is_no_longer_only_pytest_warnings(
44+
pytester: Pytester, warn_type: Type[Warning], msg: str
45+
) -> None:
46+
items = pytester.getitems(
47+
"""
48+
def test():
49+
pass
50+
"""
51+
)
52+
with pytest.warns(warn_type, match=msg):
53+
items[0].warn(warn_type(msg))
54+
55+
56+
def test_node_warning_enforces_warning_types(pytester: Pytester) -> None:
3957
items = pytester.getitems(
4058
"""
4159
def test():
4260
pass
4361
"""
4462
)
45-
with pytest.raises(ValueError, match=".*instance of PytestWarning.*"):
46-
items[0].warn(UserWarning("some warning")) # type: ignore[arg-type]
63+
with pytest.raises(
64+
ValueError, match="warning must be an instance of Warning or subclass"
65+
):
66+
items[0].warn(Exception("ok")) # type: ignore[arg-type]
4767

4868

4969
def test__check_initialpaths_for_relpath() -> None:

0 commit comments

Comments
 (0)