From 6fdf88659840a743c000968d7f9086e03803c73a Mon Sep 17 00:00:00 2001 From: Redoubts Date: Fri, 26 Apr 2024 08:01:52 -0400 Subject: [PATCH] Filter warnings from AssertionRewritingHook when execing module This method raises warnings from packages very early in the pytest collection process, way before filterwarnings can be set up. It's not interesting to raise anything here, so lets catch and ignore them --- src/_pytest/assertion/rewrite.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py index 678471ee992..1cc336dda1c 100644 --- a/src/_pytest/assertion/rewrite.py +++ b/src/_pytest/assertion/rewrite.py @@ -29,6 +29,7 @@ from typing import Tuple from typing import TYPE_CHECKING from typing import Union +import warnings from _pytest._io.saferepr import DEFAULT_REPR_MAX_SIZE from _pytest._io.saferepr import saferepr @@ -175,7 +176,9 @@ def exec_module(self, module: types.ModuleType) -> None: self._writing_pyc = False else: state.trace(f"found cached rewritten pyc for {fn}") - exec(co, module.__dict__) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + exec(co, module.__dict__) def _early_rewrite_bailout(self, name: str, state: "AssertionState") -> bool: """A fast way to get out of rewriting modules. @@ -859,8 +862,6 @@ def visit_Assert(self, assert_: ast.Assert) -> List[ast.stmt]: the expression is false. """ if isinstance(assert_.test, ast.Tuple) and len(assert_.test.elts) >= 1: - import warnings - from _pytest.warning_types import PytestAssertRewriteWarning # TODO: This assert should not be needed.