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)
| 272 | return "<{} {}>".format(self.__class__.__name__, getattr(self, "name", None)) |
| 273 | |
| 274 | def warn(self, warning: Warning) -> None: |
| 275 | """Issue a warning for this Node. |
| 276 | |
| 277 | Warnings will be displayed after the test session, unless explicitly suppressed. |
| 278 | |
| 279 | :param Warning warning: |
| 280 | The warning instance to issue. |
| 281 | |
| 282 | :raises ValueError: If ``warning`` instance is not a subclass of Warning. |
| 283 | |
| 284 | Example usage: |
| 285 | |
| 286 | .. code-block:: python |
| 287 | |
| 288 | node.warn(PytestWarning("some message")) |
| 289 | node.warn(UserWarning("some message")) |
| 290 | |
| 291 | .. versionchanged:: 6.2 |
| 292 | Any subclass of :class:`Warning` is now accepted, rather than only |
| 293 | :class:`PytestWarning <pytest.PytestWarning>` subclasses. |
| 294 | """ |
| 295 | # enforce type checks here to avoid getting a generic type error later otherwise. |
| 296 | if not isinstance(warning, Warning): |
| 297 | raise ValueError( |
| 298 | "warning must be an instance of Warning or subclass, got {!r}".format( |
| 299 | warning |
| 300 | ) |
| 301 | ) |
| 302 | path, lineno = get_fslocation_from_item(self) |
| 303 | assert lineno is not None |
| 304 | warnings.warn_explicit( |
| 305 | warning, |
| 306 | category=None, |
| 307 | filename=str(path), |
| 308 | lineno=lineno + 1, |
| 309 | ) |
| 310 | |
| 311 | # Methods for ordering nodes. |
| 312 |