From 9a0992250d40468835fe88d5ec3c5ff9e4b5b1fd Mon Sep 17 00:00:00 2001 From: Charles Aracil Date: Fri, 9 Oct 2020 11:49:29 +0200 Subject: [PATCH] better handle errors raised while applying filterwarnings (#7864) --- AUTHORS | 1 + changelog/7864.improvement.rst | 1 + src/_pytest/config/__init__.py | 9 ++++++++- 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 changelog/7864.improvement.rst diff --git a/AUTHORS b/AUTHORS index 37c1e3c062b..07b71a44eb6 100644 --- a/AUTHORS +++ b/AUTHORS @@ -51,6 +51,7 @@ Cal Leeming Carl Friedrich Bolz Carlos Jenkins Ceridwen +Charles Aracil Charles Cloud Charles Machalow Charnjit SiNGH (CCSJ) 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: