From fb295839023014b05265c07bb2e293e815f010fc Mon Sep 17 00:00:00 2001 From: tusenka Date: Sun, 11 May 2025 20:19:19 +0300 Subject: [PATCH 01/19] 13403: Disable assertion rewriting for external modules --- changelog/13403.bugfix.rst | 1 + src/_pytest/assertion/rewrite.py | 16 ++++++++++++++++ testing/test_assertrewrite.py | 23 +++++++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 changelog/13403.bugfix.rst diff --git a/changelog/13403.bugfix.rst b/changelog/13403.bugfix.rst new file mode 100644 index 00000000000..132cbfe0010 --- /dev/null +++ b/changelog/13403.bugfix.rst @@ -0,0 +1 @@ +Disable assertion rewriting of external modules diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py index c4782c7c5a8..b70ebf0a979 100644 --- a/src/_pytest/assertion/rewrite.py +++ b/src/_pytest/assertion/rewrite.py @@ -218,6 +218,10 @@ def _early_rewrite_bailout(self, name: str, state: AssertionState) -> bool: if fnmatch_ex(pat, path): return False + root_path = self._get_root_path() + if not path.is_relative_to(root_path): + return True + if self._is_marked_for_rewrite(name, state): return False @@ -238,6 +242,10 @@ def _should_rewrite(self, name: str, fn: str, state: AssertionState) -> bool: # modules not passed explicitly on the command line are only # rewritten if they match the naming convention for test files fn_path = PurePath(fn) + root_path = self._get_root_path() + if not fn_path.is_relative_to(root_path): + return False + for pat in self.fnpats: if fnmatch_ex(pat, fn_path): state.trace(f"matched test file {fn!r}") @@ -245,6 +253,14 @@ def _should_rewrite(self, name: str, fn: str, state: AssertionState) -> bool: return self._is_marked_for_rewrite(name, state) + @staticmethod + def _get_root_path(): + try: + root_path = os.getcwd() + return root_path + except FileNotFoundError: + return os.path.dirname(os.path.abspath(sys.argv[0])) + def _is_marked_for_rewrite(self, name: str, state: AssertionState) -> bool: try: return self._marked_for_rewrite_cache[name] diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index e2e448fe5e6..c63b2ff78ee 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -22,6 +22,8 @@ from unittest import mock import zipfile +from _pytest.monkeypatch import MonkeyPatch + import _pytest._code from _pytest._io.saferepr import DEFAULT_REPR_MAX_SIZE from _pytest.assertion import util @@ -1971,6 +1973,27 @@ def test_simple_failure(): assert hook.find_spec("file") is not None assert self.find_spec_calls == ["file"] + + def test_assert_excluded_rootpath( + self, pytester: Pytester, hook: AssertionRewritingHook, monkeypatch + ) -> None: + """ + If test files contained outside rootdir, then skip them + """ + pytester.makepyfile( + **{ + "file.py": """\ + def test_simple_failure(): + assert 1 + 1 == 3 + """ + } + ) + root_path= "{0}/tests".format(os.getcwd()) + monkeypatch.setattr("os.getcwd", lambda: root_path) + with mock.patch.object(hook, "fnpats", ["*.py"]): + assert hook.find_spec("file") is None + + @pytest.mark.skipif( sys.platform.startswith("win32"), reason="cannot remove cwd on Windows" ) From 14b03825635a87f2727394052a227d341beea575 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 13 May 2025 03:16:21 +0000 Subject: [PATCH 02/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- testing/test_assertrewrite.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index c63b2ff78ee..6db7cb77f51 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -22,8 +22,6 @@ from unittest import mock import zipfile -from _pytest.monkeypatch import MonkeyPatch - import _pytest._code from _pytest._io.saferepr import DEFAULT_REPR_MAX_SIZE from _pytest.assertion import util @@ -1973,7 +1971,6 @@ def test_simple_failure(): assert hook.find_spec("file") is not None assert self.find_spec_calls == ["file"] - def test_assert_excluded_rootpath( self, pytester: Pytester, hook: AssertionRewritingHook, monkeypatch ) -> None: @@ -1988,12 +1985,11 @@ def test_simple_failure(): """ } ) - root_path= "{0}/tests".format(os.getcwd()) + root_path = f"{os.getcwd()}/tests" monkeypatch.setattr("os.getcwd", lambda: root_path) with mock.patch.object(hook, "fnpats", ["*.py"]): assert hook.find_spec("file") is None - @pytest.mark.skipif( sys.platform.startswith("win32"), reason="cannot remove cwd on Windows" ) From 10a27096380494ff419e10a104fe25999720b343 Mon Sep 17 00:00:00 2001 From: tusenka Date: Tue, 13 May 2025 16:44:56 +0300 Subject: [PATCH 03/19] 13403: Disable assertion rewriting for external modules - move root path to AssertState --- src/_pytest/assertion/__init__.py | 3 ++- src/_pytest/assertion/rewrite.py | 15 +++------------ testing/test_assertrewrite.py | 4 +++- 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/src/_pytest/assertion/__init__.py b/src/_pytest/assertion/__init__.py index 532b96fe431..5032752ef0f 100644 --- a/src/_pytest/assertion/__init__.py +++ b/src/_pytest/assertion/__init__.py @@ -3,6 +3,7 @@ from __future__ import annotations +import os from collections.abc import Generator import sys from typing import Any @@ -107,7 +108,7 @@ def __init__(self, config: Config, mode) -> None: self.mode = mode self.trace = config.trace.root.get("assertion") self.hook: rewrite.AssertionRewritingHook | None = None - + self.root_path=os.getcwd() def install_importhook(config: Config) -> rewrite.AssertionRewritingHook: """Try to install the rewrite hook, raise SystemError if it fails.""" diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py index b70ebf0a979..e62451245bf 100644 --- a/src/_pytest/assertion/rewrite.py +++ b/src/_pytest/assertion/rewrite.py @@ -133,7 +133,7 @@ def find_spec( return importlib.util.spec_from_file_location( name, - fn, + fn , loader=self, submodule_search_locations=spec.submodule_search_locations, ) @@ -218,8 +218,7 @@ def _early_rewrite_bailout(self, name: str, state: AssertionState) -> bool: if fnmatch_ex(pat, path): return False - root_path = self._get_root_path() - if not path.is_relative_to(root_path): + if not path.is_relative_to(state.root_path): return True if self._is_marked_for_rewrite(name, state): @@ -242,8 +241,7 @@ def _should_rewrite(self, name: str, fn: str, state: AssertionState) -> bool: # modules not passed explicitly on the command line are only # rewritten if they match the naming convention for test files fn_path = PurePath(fn) - root_path = self._get_root_path() - if not fn_path.is_relative_to(root_path): + if not fn_path.is_relative_to(state.root_path): return False for pat in self.fnpats: @@ -253,13 +251,6 @@ def _should_rewrite(self, name: str, fn: str, state: AssertionState) -> bool: return self._is_marked_for_rewrite(name, state) - @staticmethod - def _get_root_path(): - try: - root_path = os.getcwd() - return root_path - except FileNotFoundError: - return os.path.dirname(os.path.abspath(sys.argv[0])) def _is_marked_for_rewrite(self, name: str, state: AssertionState) -> bool: try: diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index 6db7cb77f51..fe9c83ad646 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -12,6 +12,7 @@ import inspect import marshal import os +from os import mkdir from pathlib import Path import py_compile import re @@ -1986,7 +1987,8 @@ def test_simple_failure(): } ) root_path = f"{os.getcwd()}/tests" - monkeypatch.setattr("os.getcwd", lambda: root_path) + mkdir(root_path) + monkeypatch.chdir(root_path) with mock.patch.object(hook, "fnpats", ["*.py"]): assert hook.find_spec("file") is None From fc0c87cd36e32769927329a57de48379c50fb2b6 Mon Sep 17 00:00:00 2001 From: tusenka Date: Sun, 11 May 2025 20:19:19 +0300 Subject: [PATCH 04/19] 13403: Disable assertion rewriting for external modules --- testing/test_assertrewrite.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index fe9c83ad646..9118abcb3bb 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -23,6 +23,8 @@ from unittest import mock import zipfile +from _pytest.monkeypatch import MonkeyPatch + import _pytest._code from _pytest._io.saferepr import DEFAULT_REPR_MAX_SIZE from _pytest.assertion import util From 39183650012e0a2e4c9711b8e13c5315d56c30e6 Mon Sep 17 00:00:00 2001 From: tusenka Date: Thu, 15 May 2025 08:55:59 +0300 Subject: [PATCH 05/19] 13403: Disable assertion rewriting for external modules - refactor --- src/_pytest/assertion/__init__.py | 11 ++++++++++- testing/test_assertrewrite.py | 18 ++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/_pytest/assertion/__init__.py b/src/_pytest/assertion/__init__.py index 5032752ef0f..67b6c25f23f 100644 --- a/src/_pytest/assertion/__init__.py +++ b/src/_pytest/assertion/__init__.py @@ -108,7 +108,16 @@ def __init__(self, config: Config, mode) -> None: self.mode = mode self.trace = config.trace.root.get("assertion") self.hook: rewrite.AssertionRewritingHook | None = None - self.root_path=os.getcwd() + + @property + def root_path(self): + try: + return os.getcwd() + except FileNotFoundError: + # Fixes for py's trying to os.getcwd() on py34 + # when current working directory doesn't exist (previously triggered via xdist only). + # Ref: https://github.com/pytest-dev/py/pull/207 + return os.path.dirname(os.path.abspath(sys.argv[0])) def install_importhook(config: Config) -> rewrite.AssertionRewritingHook: """Try to install the rewrite hook, raise SystemError if it fails.""" diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index 9118abcb3bb..49256f205c4 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -1900,7 +1900,6 @@ def hook( if PathFinder.find_spec has been called. """ import importlib.machinery - self.find_spec_calls: list[str] = [] self.initial_paths: set[Path] = set() @@ -1974,6 +1973,7 @@ def test_simple_failure(): assert hook.find_spec("file") is not None assert self.find_spec_calls == ["file"] + def test_assert_excluded_rootpath( self, pytester: Pytester, hook: AssertionRewritingHook, monkeypatch ) -> None: @@ -1988,12 +1988,26 @@ def test_simple_failure(): """ } ) + with mock.patch.object(hook, "fnpats", ["*.py"]): + assert hook.find_spec("file") is not None root_path = f"{os.getcwd()}/tests" - mkdir(root_path) + + if not os.path.exists(root_path): + mkdir(root_path) monkeypatch.chdir(root_path) with mock.patch.object(hook, "fnpats", ["*.py"]): assert hook.find_spec("file") is None + + def test_assert_excluded_rewrite_for_plugins( + self, pytester: Pytester, hook: AssertionRewritingHook, monkeypatch + ) -> None: + plugins= {"ayncio", "fnpats", "pytest_bdd", "django", "mock", "pytest_twisted", "trio"} + with mock.patch.object(hook, "fnpats", ["*.py"]): + for plugin in plugins: + assert hook.find_spec(plugin) is None + + @pytest.mark.skipif( sys.platform.startswith("win32"), reason="cannot remove cwd on Windows" ) From 62fe69dd26cf989850817f4b11f561ddb7f90ae1 Mon Sep 17 00:00:00 2001 From: tusenka Date: Fri, 16 May 2025 19:10:24 +0300 Subject: [PATCH 06/19] 13403: Disable assertion rewriting for external modules - refactor --- src/_pytest/assertion/rewrite.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py index e62451245bf..62ecaafd3bb 100644 --- a/src/_pytest/assertion/rewrite.py +++ b/src/_pytest/assertion/rewrite.py @@ -218,12 +218,13 @@ def _early_rewrite_bailout(self, name: str, state: AssertionState) -> bool: if fnmatch_ex(pat, path): return False - if not path.is_relative_to(state.root_path): - return True if self._is_marked_for_rewrite(name, state): return False + if not path.is_relative_to(state.root_path): + return True + state.trace(f"early skip of rewriting module: {name}") return True @@ -241,11 +242,9 @@ def _should_rewrite(self, name: str, fn: str, state: AssertionState) -> bool: # modules not passed explicitly on the command line are only # rewritten if they match the naming convention for test files fn_path = PurePath(fn) - if not fn_path.is_relative_to(state.root_path): - return False for pat in self.fnpats: - if fnmatch_ex(pat, fn_path): + if fnmatch_ex(pat, fn_path) and fn_path.is_relative_to(state.root_path): state.trace(f"matched test file {fn!r}") return True From 86711034c7ce96fb487f70a0e6da2e60ac58024e Mon Sep 17 00:00:00 2001 From: tusenka Date: Fri, 16 May 2025 19:12:34 +0300 Subject: [PATCH 07/19] 13403: Disable assertion rewriting for external modules - refactor --- src/_pytest/assertion/rewrite.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py index 62ecaafd3bb..3e579f936e3 100644 --- a/src/_pytest/assertion/rewrite.py +++ b/src/_pytest/assertion/rewrite.py @@ -222,9 +222,6 @@ def _early_rewrite_bailout(self, name: str, state: AssertionState) -> bool: if self._is_marked_for_rewrite(name, state): return False - if not path.is_relative_to(state.root_path): - return True - state.trace(f"early skip of rewriting module: {name}") return True From 3beb48e5daab86fc1c709756ea129a1e87706f43 Mon Sep 17 00:00:00 2001 From: tusenka Date: Sun, 18 May 2025 06:51:36 +0300 Subject: [PATCH 08/19] 13403: Disable assertion rewriting for external modules - add tests --- src/_pytest/assertion/__init__.py | 5 ++- src/_pytest/assertion/rewrite.py | 3 +- testing/test_assertrewrite.py | 54 +++++++++++++++++++++++++------ 3 files changed, 50 insertions(+), 12 deletions(-) diff --git a/src/_pytest/assertion/__init__.py b/src/_pytest/assertion/__init__.py index 67b6c25f23f..8616abb5731 100644 --- a/src/_pytest/assertion/__init__.py +++ b/src/_pytest/assertion/__init__.py @@ -110,7 +110,10 @@ def __init__(self, config: Config, mode) -> None: self.hook: rewrite.AssertionRewritingHook | None = None @property - def root_path(self): + def rootpath(self): + """ + get current root path (current working dir) + """ try: return os.getcwd() except FileNotFoundError: diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py index 3e579f936e3..0ab8931622e 100644 --- a/src/_pytest/assertion/rewrite.py +++ b/src/_pytest/assertion/rewrite.py @@ -218,7 +218,6 @@ def _early_rewrite_bailout(self, name: str, state: AssertionState) -> bool: if fnmatch_ex(pat, path): return False - if self._is_marked_for_rewrite(name, state): return False @@ -241,7 +240,7 @@ def _should_rewrite(self, name: str, fn: str, state: AssertionState) -> bool: fn_path = PurePath(fn) for pat in self.fnpats: - if fnmatch_ex(pat, fn_path) and fn_path.is_relative_to(state.root_path): + if fnmatch_ex(pat, fn_path) and fn_path.is_relative_to(state.rootpath): state.trace(f"matched test file {fn!r}") return True diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index 49256f205c4..364140e9f03 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -1974,11 +1974,11 @@ def test_simple_failure(): assert self.find_spec_calls == ["file"] - def test_assert_excluded_rootpath( + def test_assert_rewrites_only_rootpath( self, pytester: Pytester, hook: AssertionRewritingHook, monkeypatch ) -> None: """ - If test files contained outside rootdir, then skip them + If test files contained outside the rootpath, then skip them """ pytester.makepyfile( **{ @@ -1990,22 +1990,58 @@ def test_simple_failure(): ) with mock.patch.object(hook, "fnpats", ["*.py"]): assert hook.find_spec("file") is not None - root_path = f"{os.getcwd()}/tests" - if not os.path.exists(root_path): - mkdir(root_path) - monkeypatch.chdir(root_path) + rootpath = f"{os.getcwd()}/tests" + if not os.path.exists(rootpath): + mkdir(rootpath) + monkeypatch.chdir(rootpath) with mock.patch.object(hook, "fnpats", ["*.py"]): assert hook.find_spec("file") is None + def test_assert_correct_for_conftfest( + self, pytester: Pytester, hook: AssertionRewritingHook, monkeypatch + ) -> None: + """ + Conftest is always rewritten regardless of the working dir + """ + pytester.makeconftest( + """ + import pytest + @pytest.fixture + def fix(): return 1 + """ + ) + + rootpath = f"{os.getcwd()}/tests" + if not os.path.exists(rootpath): + mkdir(rootpath) + monkeypatch.chdir(rootpath) + + with mock.patch.object(hook, "fnpats", ["*.py"]): + assert hook.find_spec("conftest") is not None + + def test_assert_excluded_rewrite_for_plugins( self, pytester: Pytester, hook: AssertionRewritingHook, monkeypatch ) -> None: - plugins= {"ayncio", "fnpats", "pytest_bdd", "django", "mock", "pytest_twisted", "trio"} + pkgdir = pytester.mkpydir("plugin") + pkgdir.joinpath("__init__.py").write_text( + "import pytest\n" + "@pytest.fixture\n" + "def special_asserter():\n" + " def special_assert(x, y):\n" + " assert x == y\n" + " return special_assert\n", + encoding="utf-8", + ) + pytester.makeconftest('pytest_plugins = ["plugin"]') + rootpath = f"{os.getcwd()}/tests" + if not os.path.exists(rootpath): + mkdir(rootpath) + monkeypatch.chdir(rootpath) with mock.patch.object(hook, "fnpats", ["*.py"]): - for plugin in plugins: - assert hook.find_spec(plugin) is None + assert hook.find_spec("plugin") is not None @pytest.mark.skipif( From 0015d0aa748adfec6ff9de7a39d69746013baa29 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 16 May 2025 16:13:51 +0000 Subject: [PATCH 09/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/_pytest/assertion/__init__.py | 3 ++- src/_pytest/assertion/rewrite.py | 3 +-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/_pytest/assertion/__init__.py b/src/_pytest/assertion/__init__.py index 8616abb5731..76d3235a441 100644 --- a/src/_pytest/assertion/__init__.py +++ b/src/_pytest/assertion/__init__.py @@ -3,8 +3,8 @@ from __future__ import annotations -import os from collections.abc import Generator +import os import sys from typing import Any from typing import TYPE_CHECKING @@ -122,6 +122,7 @@ def rootpath(self): # Ref: https://github.com/pytest-dev/py/pull/207 return os.path.dirname(os.path.abspath(sys.argv[0])) + def install_importhook(config: Config) -> rewrite.AssertionRewritingHook: """Try to install the rewrite hook, raise SystemError if it fails.""" config.stash[assertstate_key] = AssertionState(config, "rewrite") diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py index 0ab8931622e..861ec77a70b 100644 --- a/src/_pytest/assertion/rewrite.py +++ b/src/_pytest/assertion/rewrite.py @@ -133,7 +133,7 @@ def find_spec( return importlib.util.spec_from_file_location( name, - fn , + fn, loader=self, submodule_search_locations=spec.submodule_search_locations, ) @@ -246,7 +246,6 @@ def _should_rewrite(self, name: str, fn: str, state: AssertionState) -> bool: return self._is_marked_for_rewrite(name, state) - def _is_marked_for_rewrite(self, name: str, state: AssertionState) -> bool: try: return self._marked_for_rewrite_cache[name] From d5eb2a64c0c969556451e2a074506029681c68c3 Mon Sep 17 00:00:00 2001 From: tusenka Date: Mon, 19 May 2025 06:48:02 +0300 Subject: [PATCH 10/19] 13403: Disable assertion rewriting for external modules - add test for plugins --- testing/test_assertrewrite.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index 364140e9f03..775c5b2d877 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -373,6 +373,7 @@ def test_rewrites_plugin_as_a_package(self, pytester: Pytester) -> None: pytester.makeconftest('pytest_plugins = ["plugin"]') pytester.makepyfile("def test(special_asserter): special_asserter(1, 2)\n") result = pytester.runpytest() + result.stdout.fnmatch_lines(["*assert 1 == 2*"]) def test_honors_pep_235(self, pytester: Pytester, monkeypatch) -> None: @@ -1999,11 +2000,11 @@ def test_simple_failure(): assert hook.find_spec("file") is None - def test_assert_correct_for_conftfest( + def test_assert_rewrite_correct_for_conftfest( self, pytester: Pytester, hook: AssertionRewritingHook, monkeypatch ) -> None: """ - Conftest is always rewritten regardless of the working dir + Conftest is always rewritten regardless of the root dir """ pytester.makeconftest( """ @@ -2022,9 +2023,12 @@ def fix(): return 1 assert hook.find_spec("conftest") is not None - def test_assert_excluded_rewrite_for_plugins( + def test_assert_rewrite_correct_for_plugins( self, pytester: Pytester, hook: AssertionRewritingHook, monkeypatch ) -> None: + """ + Plugins has always been rewritten regardless of the root dir + """ pkgdir = pytester.mkpydir("plugin") pkgdir.joinpath("__init__.py").write_text( "import pytest\n" @@ -2035,10 +2039,10 @@ def test_assert_excluded_rewrite_for_plugins( " return special_assert\n", encoding="utf-8", ) - pytester.makeconftest('pytest_plugins = ["plugin"]') + hook.mark_rewrite("plugin") rootpath = f"{os.getcwd()}/tests" if not os.path.exists(rootpath): - mkdir(rootpath) + mkdir(rootpath) monkeypatch.chdir(rootpath) with mock.patch.object(hook, "fnpats", ["*.py"]): assert hook.find_spec("plugin") is not None From 392a01d69cef5f6e77883402d897f95bda5ed5ce Mon Sep 17 00:00:00 2001 From: tusenka Date: Thu, 22 May 2025 07:11:34 +0300 Subject: [PATCH 11/19] 13403: Disable assertion rewriting for external modules - add test for plugins --- src/_pytest/assertion/__init__.py | 2 +- testing/test_assertrewrite.py | 37 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/_pytest/assertion/__init__.py b/src/_pytest/assertion/__init__.py index 76d3235a441..6a8afac0dbd 100644 --- a/src/_pytest/assertion/__init__.py +++ b/src/_pytest/assertion/__init__.py @@ -120,7 +120,7 @@ def rootpath(self): # Fixes for py's trying to os.getcwd() on py34 # when current working directory doesn't exist (previously triggered via xdist only). # Ref: https://github.com/pytest-dev/py/pull/207 - return os.path.dirname(os.path.abspath(sys.argv[0])) + return os.path.abspath(os.sep) def install_importhook(config: Config) -> rewrite.AssertionRewritingHook: diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index 775c5b2d877..b1ef304951b 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -22,6 +22,7 @@ from typing import cast from unittest import mock import zipfile +from mock.mock import Mock from _pytest.monkeypatch import MonkeyPatch @@ -38,6 +39,7 @@ from _pytest.assertion.rewrite import rewrite_asserts from _pytest.config import Config from _pytest.config import ExitCode +from _pytest.monkeypatch import MonkeyPatch from _pytest.pathlib import make_numbered_dir from _pytest.pytester import Pytester import pytest @@ -1298,6 +1300,41 @@ def test_meta_path(): ) assert pytester.runpytest().ret == 0 + + def test_rootpath_base(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> None: + """ + Base cases for get rootpath from AssertionState + """ + from _pytest.assertion import AssertionState + config = pytester.parseconfig() + monkeypatch.chdir(pytester.path) + state = AssertionState(config, "rewrite") + assert state.rootpath == str(pytester.path) + new_rootpath = pytester.path + "/test" + if not os.path.exists(new_rootpath): + os.mkdir(new_rootpath) + monkeypatch.chdir(new_rootpath) + state = AssertionState(config, "rewrite") + assert state.rootpath == new_rootpath + + + @pytest.mark.skipif( + sys.platform.startswith("win32"), reason="cannot remove cwd on Windows" + ) + @pytest.mark.skipif( + sys.platform.startswith("sunos5"), reason="cannot remove cwd on Solaris" + ) + def test_rootpath_cwd_removed(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> None: + # Setup conditions for py's trying to os.getcwd() on py34 + # when current working directory doesn't exist (previously triggered via xdist only). + # Ref: https://github.com/pytest-dev/py/pull/207 + from _pytest.assertion import AssertionState + config = pytester.parseconfig() + monkeypatch.setattr(target=os, name="getcwd", value=Mock(side_effect=FileNotFoundError)) + state = AssertionState(config, "rewrite") + assert state.rootpath == os.path.abspath(os.sep) + + def test_write_pyc(self, pytester: Pytester, tmp_path) -> None: from _pytest.assertion import AssertionState from _pytest.assertion.rewrite import _write_pyc From ccb0dca9496aa0b6f58843ef2d39fcff39a51f38 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 22 May 2025 04:12:42 +0000 Subject: [PATCH 12/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/_pytest/assertion/__init__.py | 2 +- testing/test_assertrewrite.py | 27 ++++++++++++++------------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/_pytest/assertion/__init__.py b/src/_pytest/assertion/__init__.py index 6a8afac0dbd..9e6c86b095a 100644 --- a/src/_pytest/assertion/__init__.py +++ b/src/_pytest/assertion/__init__.py @@ -112,7 +112,7 @@ def __init__(self, config: Config, mode) -> None: @property def rootpath(self): """ - get current root path (current working dir) + Get current root path (current working dir) """ try: return os.getcwd() diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index b1ef304951b..c66f837be02 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -22,6 +22,7 @@ from typing import cast from unittest import mock import zipfile + from mock.mock import Mock from _pytest.monkeypatch import MonkeyPatch @@ -1300,12 +1301,12 @@ def test_meta_path(): ) assert pytester.runpytest().ret == 0 - def test_rootpath_base(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> None: """ Base cases for get rootpath from AssertionState """ from _pytest.assertion import AssertionState + config = pytester.parseconfig() monkeypatch.chdir(pytester.path) state = AssertionState(config, "rewrite") @@ -1317,23 +1318,26 @@ def test_rootpath_base(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> No state = AssertionState(config, "rewrite") assert state.rootpath == new_rootpath - @pytest.mark.skipif( sys.platform.startswith("win32"), reason="cannot remove cwd on Windows" ) @pytest.mark.skipif( sys.platform.startswith("sunos5"), reason="cannot remove cwd on Solaris" ) - def test_rootpath_cwd_removed(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> None: + def test_rootpath_cwd_removed( + self, pytester: Pytester, monkeypatch: MonkeyPatch + ) -> None: # Setup conditions for py's trying to os.getcwd() on py34 # when current working directory doesn't exist (previously triggered via xdist only). # Ref: https://github.com/pytest-dev/py/pull/207 from _pytest.assertion import AssertionState + config = pytester.parseconfig() - monkeypatch.setattr(target=os, name="getcwd", value=Mock(side_effect=FileNotFoundError)) + monkeypatch.setattr( + target=os, name="getcwd", value=Mock(side_effect=FileNotFoundError) + ) state = AssertionState(config, "rewrite") - assert state.rootpath == os.path.abspath(os.sep) - + assert state.rootpath == os.path.abspath(os.sep) def test_write_pyc(self, pytester: Pytester, tmp_path) -> None: from _pytest.assertion import AssertionState @@ -1938,6 +1942,7 @@ def hook( if PathFinder.find_spec has been called. """ import importlib.machinery + self.find_spec_calls: list[str] = [] self.initial_paths: set[Path] = set() @@ -2011,7 +2016,6 @@ def test_simple_failure(): assert hook.find_spec("file") is not None assert self.find_spec_calls == ["file"] - def test_assert_rewrites_only_rootpath( self, pytester: Pytester, hook: AssertionRewritingHook, monkeypatch ) -> None: @@ -2031,12 +2035,11 @@ def test_simple_failure(): rootpath = f"{os.getcwd()}/tests" if not os.path.exists(rootpath): - mkdir(rootpath) + mkdir(rootpath) monkeypatch.chdir(rootpath) with mock.patch.object(hook, "fnpats", ["*.py"]): assert hook.find_spec("file") is None - def test_assert_rewrite_correct_for_conftfest( self, pytester: Pytester, hook: AssertionRewritingHook, monkeypatch ) -> None: @@ -2053,12 +2056,11 @@ def fix(): return 1 rootpath = f"{os.getcwd()}/tests" if not os.path.exists(rootpath): - mkdir(rootpath) + mkdir(rootpath) monkeypatch.chdir(rootpath) with mock.patch.object(hook, "fnpats", ["*.py"]): assert hook.find_spec("conftest") is not None - def test_assert_rewrite_correct_for_plugins( self, pytester: Pytester, hook: AssertionRewritingHook, monkeypatch @@ -2079,12 +2081,11 @@ def test_assert_rewrite_correct_for_plugins( hook.mark_rewrite("plugin") rootpath = f"{os.getcwd()}/tests" if not os.path.exists(rootpath): - mkdir(rootpath) + mkdir(rootpath) monkeypatch.chdir(rootpath) with mock.patch.object(hook, "fnpats", ["*.py"]): assert hook.find_spec("plugin") is not None - @pytest.mark.skipif( sys.platform.startswith("win32"), reason="cannot remove cwd on Windows" ) From e0bbaa3cc7f255b1b3c34935d669115a7259aae7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 16 May 2025 16:13:51 +0000 Subject: [PATCH 13/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/_pytest/assertion/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/_pytest/assertion/__init__.py b/src/_pytest/assertion/__init__.py index 9e6c86b095a..c4f1d18412e 100644 --- a/src/_pytest/assertion/__init__.py +++ b/src/_pytest/assertion/__init__.py @@ -123,6 +123,7 @@ def rootpath(self): return os.path.abspath(os.sep) + def install_importhook(config: Config) -> rewrite.AssertionRewritingHook: """Try to install the rewrite hook, raise SystemError if it fails.""" config.stash[assertstate_key] = AssertionState(config, "rewrite") From dbc5a594ab6a7e1a96a6135ff788b9352a71a4b8 Mon Sep 17 00:00:00 2001 From: tusenka Date: Sat, 24 May 2025 14:15:54 +0300 Subject: [PATCH 14/19] 13403: Disable assertion rewriting for external modules - add test for plugins --- src/_pytest/assertion/__init__.py | 1 - testing/test_assertrewrite.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/_pytest/assertion/__init__.py b/src/_pytest/assertion/__init__.py index c4f1d18412e..9e6c86b095a 100644 --- a/src/_pytest/assertion/__init__.py +++ b/src/_pytest/assertion/__init__.py @@ -123,7 +123,6 @@ def rootpath(self): return os.path.abspath(os.sep) - def install_importhook(config: Config) -> rewrite.AssertionRewritingHook: """Try to install the rewrite hook, raise SystemError if it fails.""" config.stash[assertstate_key] = AssertionState(config, "rewrite") diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index c66f837be02..81b8923f642 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -1311,7 +1311,7 @@ def test_rootpath_base(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> No monkeypatch.chdir(pytester.path) state = AssertionState(config, "rewrite") assert state.rootpath == str(pytester.path) - new_rootpath = pytester.path + "/test" + new_rootpath = str(pytester.path) + "/test" if not os.path.exists(new_rootpath): os.mkdir(new_rootpath) monkeypatch.chdir(new_rootpath) From 23e0d70e714597388fc730149bb82e5e9d8b8f80 Mon Sep 17 00:00:00 2001 From: tusenka Date: Sun, 25 May 2025 19:04:28 +0300 Subject: [PATCH 15/19] 13403: Disable assertion rewriting for external modules - eliminate os.getcwd --- src/_pytest/assertion/__init__.py | 12 ++----- src/_pytest/pytester.py | 5 +++ testing/test_assertrewrite.py | 52 ++++++++++++++++++------------- 3 files changed, 38 insertions(+), 31 deletions(-) diff --git a/src/_pytest/assertion/__init__.py b/src/_pytest/assertion/__init__.py index 9e6c86b095a..bb3a47ed28b 100644 --- a/src/_pytest/assertion/__init__.py +++ b/src/_pytest/assertion/__init__.py @@ -107,20 +107,14 @@ class AssertionState: def __init__(self, config: Config, mode) -> None: self.mode = mode self.trace = config.trace.root.get("assertion") + self.config=config self.hook: rewrite.AssertionRewritingHook | None = None @property def rootpath(self): + """Get current root path (current working dir) """ - Get current root path (current working dir) - """ - try: - return os.getcwd() - except FileNotFoundError: - # Fixes for py's trying to os.getcwd() on py34 - # when current working directory doesn't exist (previously triggered via xdist only). - # Ref: https://github.com/pytest-dev/py/pull/207 - return os.path.abspath(os.sep) + return str(self.config.invocation_params.dir) def install_importhook(config: Config) -> rewrite.AssertionRewritingHook: diff --git a/src/_pytest/pytester.py b/src/_pytest/pytester.py index 11127a88bb8..206456c47ba 100644 --- a/src/_pytest/pytester.py +++ b/src/_pytest/pytester.py @@ -749,6 +749,11 @@ def chdir(self) -> None: This is done automatically upon instantiation. """ self._monkeypatch.chdir(self.path) + self._monkeypatch.setattr(self._request.config,"invocation_params", Config.InvocationParams( + args= self._request.config.invocation_params.args, + plugins=self._request.config.invocation_params.plugins, + dir=Path(self._path), + )) def _makefile( self, diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index 81b8923f642..b0a02ba980a 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -1308,13 +1308,16 @@ def test_rootpath_base(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> No from _pytest.assertion import AssertionState config = pytester.parseconfig() - monkeypatch.chdir(pytester.path) state = AssertionState(config, "rewrite") - assert state.rootpath == str(pytester.path) - new_rootpath = str(pytester.path) + "/test" + assert state.rootpath == str(config.invocation_params.dir) + new_rootpath =str(pytester.path / "test") if not os.path.exists(new_rootpath): os.mkdir(new_rootpath) - monkeypatch.chdir(new_rootpath) + monkeypatch.setattr(config,"invocation_params", Config.InvocationParams( + args= (), + plugins=(), + dir=Path(new_rootpath), + )) state = AssertionState(config, "rewrite") assert state.rootpath == new_rootpath @@ -1324,20 +1327,6 @@ def test_rootpath_base(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> No @pytest.mark.skipif( sys.platform.startswith("sunos5"), reason="cannot remove cwd on Solaris" ) - def test_rootpath_cwd_removed( - self, pytester: Pytester, monkeypatch: MonkeyPatch - ) -> None: - # Setup conditions for py's trying to os.getcwd() on py34 - # when current working directory doesn't exist (previously triggered via xdist only). - # Ref: https://github.com/pytest-dev/py/pull/207 - from _pytest.assertion import AssertionState - - config = pytester.parseconfig() - monkeypatch.setattr( - target=os, name="getcwd", value=Mock(side_effect=FileNotFoundError) - ) - state = AssertionState(config, "rewrite") - assert state.rootpath == os.path.abspath(os.sep) def test_write_pyc(self, pytester: Pytester, tmp_path) -> None: from _pytest.assertion import AssertionState @@ -2036,7 +2025,11 @@ def test_simple_failure(): rootpath = f"{os.getcwd()}/tests" if not os.path.exists(rootpath): mkdir(rootpath) - monkeypatch.chdir(rootpath) + monkeypatch.setattr(pytester._request.config,"invocation_params", Config.InvocationParams( + args= (), + plugins=(), + dir=Path(rootpath), + )) with mock.patch.object(hook, "fnpats", ["*.py"]): assert hook.find_spec("file") is None @@ -2057,8 +2050,15 @@ def fix(): return 1 rootpath = f"{os.getcwd()}/tests" if not os.path.exists(rootpath): mkdir(rootpath) - monkeypatch.chdir(rootpath) - + monkeypatch.setattr( + pytester._request.config, + "invocation_params", + Config.InvocationParams( + args= (), + plugins=(), + dir=Path(rootpath), + ) + ) with mock.patch.object(hook, "fnpats", ["*.py"]): assert hook.find_spec("conftest") is not None @@ -2082,7 +2082,15 @@ def test_assert_rewrite_correct_for_plugins( rootpath = f"{os.getcwd()}/tests" if not os.path.exists(rootpath): mkdir(rootpath) - monkeypatch.chdir(rootpath) + monkeypatch.setattr( + pytester._request.config, + "invocation_params", + Config.InvocationParams( + args= (), + plugins=(), + dir=Path(rootpath), + ) + ) with mock.patch.object(hook, "fnpats", ["*.py"]): assert hook.find_spec("plugin") is not None From 559a5c045d17fbc69977fcef8164f1e3b8116ee3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 27 May 2025 15:54:29 +0000 Subject: [PATCH 16/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/_pytest/assertion/__init__.py | 5 +-- src/_pytest/pytester.py | 10 +++-- testing/test_assertrewrite.py | 63 ++++++++++++++++--------------- 3 files changed, 42 insertions(+), 36 deletions(-) diff --git a/src/_pytest/assertion/__init__.py b/src/_pytest/assertion/__init__.py index bb3a47ed28b..04d4c96b1e3 100644 --- a/src/_pytest/assertion/__init__.py +++ b/src/_pytest/assertion/__init__.py @@ -107,13 +107,12 @@ class AssertionState: def __init__(self, config: Config, mode) -> None: self.mode = mode self.trace = config.trace.root.get("assertion") - self.config=config + self.config = config self.hook: rewrite.AssertionRewritingHook | None = None @property def rootpath(self): - """Get current root path (current working dir) - """ + """Get current root path (current working dir)""" return str(self.config.invocation_params.dir) diff --git a/src/_pytest/pytester.py b/src/_pytest/pytester.py index 206456c47ba..e1cea040e9f 100644 --- a/src/_pytest/pytester.py +++ b/src/_pytest/pytester.py @@ -749,11 +749,15 @@ def chdir(self) -> None: This is done automatically upon instantiation. """ self._monkeypatch.chdir(self.path) - self._monkeypatch.setattr(self._request.config,"invocation_params", Config.InvocationParams( - args= self._request.config.invocation_params.args, + self._monkeypatch.setattr( + self._request.config, + "invocation_params", + Config.InvocationParams( + args=self._request.config.invocation_params.args, plugins=self._request.config.invocation_params.plugins, dir=Path(self._path), - )) + ), + ) def _makefile( self, diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index b0a02ba980a..260e836d554 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -23,10 +23,6 @@ from unittest import mock import zipfile -from mock.mock import Mock - -from _pytest.monkeypatch import MonkeyPatch - import _pytest._code from _pytest._io.saferepr import DEFAULT_REPR_MAX_SIZE from _pytest.assertion import util @@ -1310,14 +1306,18 @@ def test_rootpath_base(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> No config = pytester.parseconfig() state = AssertionState(config, "rewrite") assert state.rootpath == str(config.invocation_params.dir) - new_rootpath =str(pytester.path / "test") + new_rootpath = str(pytester.path / "test") if not os.path.exists(new_rootpath): os.mkdir(new_rootpath) - monkeypatch.setattr(config,"invocation_params", Config.InvocationParams( - args= (), - plugins=(), - dir=Path(new_rootpath), - )) + monkeypatch.setattr( + config, + "invocation_params", + Config.InvocationParams( + args=(), + plugins=(), + dir=Path(new_rootpath), + ), + ) state = AssertionState(config, "rewrite") assert state.rootpath == new_rootpath @@ -1327,7 +1327,6 @@ def test_rootpath_base(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> No @pytest.mark.skipif( sys.platform.startswith("sunos5"), reason="cannot remove cwd on Solaris" ) - def test_write_pyc(self, pytester: Pytester, tmp_path) -> None: from _pytest.assertion import AssertionState from _pytest.assertion.rewrite import _write_pyc @@ -2025,11 +2024,15 @@ def test_simple_failure(): rootpath = f"{os.getcwd()}/tests" if not os.path.exists(rootpath): mkdir(rootpath) - monkeypatch.setattr(pytester._request.config,"invocation_params", Config.InvocationParams( - args= (), - plugins=(), - dir=Path(rootpath), - )) + monkeypatch.setattr( + pytester._request.config, + "invocation_params", + Config.InvocationParams( + args=(), + plugins=(), + dir=Path(rootpath), + ), + ) with mock.patch.object(hook, "fnpats", ["*.py"]): assert hook.find_spec("file") is None @@ -2051,13 +2054,13 @@ def fix(): return 1 if not os.path.exists(rootpath): mkdir(rootpath) monkeypatch.setattr( - pytester._request.config, - "invocation_params", - Config.InvocationParams( - args= (), - plugins=(), - dir=Path(rootpath), - ) + pytester._request.config, + "invocation_params", + Config.InvocationParams( + args=(), + plugins=(), + dir=Path(rootpath), + ), ) with mock.patch.object(hook, "fnpats", ["*.py"]): assert hook.find_spec("conftest") is not None @@ -2083,13 +2086,13 @@ def test_assert_rewrite_correct_for_plugins( if not os.path.exists(rootpath): mkdir(rootpath) monkeypatch.setattr( - pytester._request.config, - "invocation_params", - Config.InvocationParams( - args= (), - plugins=(), - dir=Path(rootpath), - ) + pytester._request.config, + "invocation_params", + Config.InvocationParams( + args=(), + plugins=(), + dir=Path(rootpath), + ), ) with mock.patch.object(hook, "fnpats", ["*.py"]): assert hook.find_spec("plugin") is not None From a3cacb87f318ad57fecf5e953018756423319e3c Mon Sep 17 00:00:00 2001 From: tusenka Date: Sun, 1 Jun 2025 07:18:52 +0300 Subject: [PATCH 17/19] 13403: Disable assertion rewriting for external modules - fix ruff --- src/_pytest/assertion/__init__.py | 14 ++++---------- src/_pytest/assertion/rewrite.py | 1 - testing/test_assertrewrite.py | 12 +++--------- 3 files changed, 7 insertions(+), 20 deletions(-) diff --git a/src/_pytest/assertion/__init__.py b/src/_pytest/assertion/__init__.py index 04d4c96b1e3..57a92a0a143 100644 --- a/src/_pytest/assertion/__init__.py +++ b/src/_pytest/assertion/__init__.py @@ -3,22 +3,16 @@ from __future__ import annotations -from collections.abc import Generator -import os import sys -from typing import Any -from typing import TYPE_CHECKING +from collections.abc import Generator +from typing import Any , TYPE_CHECKING -from _pytest.assertion import rewrite -from _pytest.assertion import truncate -from _pytest.assertion import util +from _pytest.assertion import rewrite , truncate , util from _pytest.assertion.rewrite import assertstate_key -from _pytest.config import Config -from _pytest.config import hookimpl +from _pytest.config import Config , hookimpl from _pytest.config.argparsing import Parser from _pytest.nodes import Item - if TYPE_CHECKING: from _pytest.main import Session diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py index 861ec77a70b..4d4db99eb67 100644 --- a/src/_pytest/assertion/rewrite.py +++ b/src/_pytest/assertion/rewrite.py @@ -238,7 +238,6 @@ def _should_rewrite(self, name: str, fn: str, state: AssertionState) -> bool: # modules not passed explicitly on the command line are only # rewritten if they match the naming convention for test files fn_path = PurePath(fn) - for pat in self.fnpats: if fnmatch_ex(pat, fn_path) and fn_path.is_relative_to(state.rootpath): state.trace(f"matched test file {fn!r}") diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index 260e836d554..0439ec3c1b1 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -1298,9 +1298,7 @@ def test_meta_path(): assert pytester.runpytest().ret == 0 def test_rootpath_base(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> None: - """ - Base cases for get rootpath from AssertionState - """ + """Base cases for get rootpath from AssertionState""" from _pytest.assertion import AssertionState config = pytester.parseconfig() @@ -2007,9 +2005,7 @@ def test_simple_failure(): def test_assert_rewrites_only_rootpath( self, pytester: Pytester, hook: AssertionRewritingHook, monkeypatch ) -> None: - """ - If test files contained outside the rootpath, then skip them - """ + """If test files contained outside the rootpath, then skip them""" pytester.makepyfile( **{ "file.py": """\ @@ -2039,9 +2035,7 @@ def test_simple_failure(): def test_assert_rewrite_correct_for_conftfest( self, pytester: Pytester, hook: AssertionRewritingHook, monkeypatch ) -> None: - """ - Conftest is always rewritten regardless of the root dir - """ + """Conftest is always rewritten regardless of the root dir""" pytester.makeconftest( """ import pytest From 33587517eaa9746ad279db4c83493cf2a57be9f3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 1 Jun 2025 04:29:41 +0000 Subject: [PATCH 18/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/_pytest/assertion/__init__.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/_pytest/assertion/__init__.py b/src/_pytest/assertion/__init__.py index 57a92a0a143..956e97a1909 100644 --- a/src/_pytest/assertion/__init__.py +++ b/src/_pytest/assertion/__init__.py @@ -3,16 +3,21 @@ from __future__ import annotations -import sys from collections.abc import Generator -from typing import Any , TYPE_CHECKING +import sys +from typing import Any +from typing import TYPE_CHECKING -from _pytest.assertion import rewrite , truncate , util +from _pytest.assertion import rewrite +from _pytest.assertion import truncate +from _pytest.assertion import util from _pytest.assertion.rewrite import assertstate_key -from _pytest.config import Config , hookimpl +from _pytest.config import Config +from _pytest.config import hookimpl from _pytest.config.argparsing import Parser from _pytest.nodes import Item + if TYPE_CHECKING: from _pytest.main import Session From 6ec5c30b31a0e4b6791816172d1c0e64de6c156a Mon Sep 17 00:00:00 2001 From: Irina Date: Sun, 1 Jun 2025 18:24:49 +0300 Subject: [PATCH 19/19] Update testing/test_assertrewrite.py Co-authored-by: Bruno Oliveira --- testing/test_assertrewrite.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index 0439ec3c1b1..539d76a7b46 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -2005,7 +2005,7 @@ def test_simple_failure(): def test_assert_rewrites_only_rootpath( self, pytester: Pytester, hook: AssertionRewritingHook, monkeypatch ) -> None: - """If test files contained outside the rootpath, then skip them""" + """Do not rewrite assertions in tests outside `AssertState.rootpath` (#13403).""" pytester.makepyfile( **{ "file.py": """\