diff --git a/changelog/7864.improvement.rst b/changelog/7864.improvement.rst new file mode 100644 index 00000000000..d2b2a3e4635 --- /dev/null +++ b/changelog/7864.improvement.rst @@ -0,0 +1 @@ +better handle errors raised while applying filterwarnings diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index 7e486e99e50..9ee4843e5b0 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -1569,7 +1569,14 @@ def parse_warning_filter( parts.append("") action_, message, category_, module, lineno_ = [s.strip() for s in parts] action: str = warnings._getaction(action_) # type: ignore[attr-defined] - category: Type[Warning] = warnings._getcategory(category_) # type: ignore[attr-defined] + try: + category: Type[Warning] = warnings._getcategory(category_) # type: ignore[attr-defined] # noqa: E501 + except Exception as e: + raise Exception( + "Error while processing warning rules. Please make sure importing " + "the warning classes defined in filterwarning doesn't trigger an " + "exception." + ) from e if message and escape: message = re.escape(message) if module and escape: