Skip to content

Commit dcf9da9

Browse files
committed
recwarn: minor style improvements
In preparation for next commit.
1 parent 8e56795 commit dcf9da9

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/_pytest/recwarn.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ def __exit__(
314314
):
315315
return
316316

317-
def found_str():
317+
def found_str() -> str:
318318
return pformat([record.message for record in self], indent=2)
319319

320320
try:
@@ -341,14 +341,19 @@ def found_str():
341341
module=w.__module__,
342342
source=w.source,
343343
)
344-
# Check warnings has valid argument type (#10865).
345-
wrn: warnings.WarningMessage
346-
for wrn in self:
347-
self._validate_message(wrn)
348-
349-
@staticmethod
350-
def _validate_message(wrn: Any) -> None:
351-
if not isinstance(msg := wrn.message.args[0], str):
352-
raise TypeError(
353-
f"Warning message must be str, got {msg!r} (type {type(msg).__name__})"
354-
)
344+
345+
# Currently in Python it is possible to pass other types than an
346+
# `str` message when creating `Warning` instances, however this
347+
# causes an exception when :func:`warnings.filterwarnings` is used
348+
# to filter those warnings. See
349+
# https://github.com/python/cpython/issues/103577 for a discussion.
350+
# While this can be considered a bug in CPython, we put guards in
351+
# pytest as the error message produced without this check in place
352+
# is confusing (#10865).
353+
for w in self:
354+
msg = w.message.args[0] # type: ignore[union-attr]
355+
if isinstance(msg, str):
356+
continue
357+
raise TypeError(
358+
f"Warning message must be str, got {msg!r} (type {type(msg).__name__})"
359+
)

0 commit comments

Comments
 (0)