Issue a warning for this Node. Warnings will be displayed after the test session, unless explicitly suppressed. :param Warning warning: The warning instance to issue. :raises ValueError: If ``warning`` instance is not a subclass of Warning. Example usa
(self, warning: Warning)
| 233 | return "<{} {}>".format(self.__class__.__name__, getattr(self, "name", None)) |
| 234 | |
| 235 | def warn(self, warning: Warning) -> None: |
| 236 | """Issue a warning for this Node. |
| 237 | |
| 238 | Warnings will be displayed after the test session, unless explicitly suppressed. |
| 239 | |
| 240 | :param Warning warning: |
| 241 | The warning instance to issue. |
| 242 | |
| 243 | :raises ValueError: If ``warning`` instance is not a subclass of Warning. |
| 244 | |
| 245 | Example usage: |
| 246 | |
| 247 | .. code-block:: python |
| 248 | |
| 249 | node.warn(PytestWarning("some message")) |
| 250 | node.warn(UserWarning("some message")) |
| 251 | |
| 252 | .. versionchanged:: 6.2 |
| 253 | Any subclass of :class:`Warning` is now accepted, rather than only |
| 254 | :class:`PytestWarning <pytest.PytestWarning>` subclasses. |
| 255 | """ |
| 256 | # enforce type checks here to avoid getting a generic type error later otherwise. |
| 257 | if not isinstance(warning, Warning): |
| 258 | raise ValueError( |
| 259 | f"warning must be an instance of Warning or subclass, got {warning!r}" |
| 260 | ) |
| 261 | path, lineno = get_fslocation_from_item(self) |
| 262 | assert lineno is not None |
| 263 | warnings.warn_explicit( |
| 264 | warning, |
| 265 | category=None, |
| 266 | filename=str(path), |
| 267 | lineno=lineno + 1, |
| 268 | ) |
| 269 | |
| 270 | # Methods for ordering nodes. |
| 271 |