Skip to content

Commit 939ca74

Browse files
authored
Merge branch 'main' into disable_assertion_rewriting_external_modules
2 parents 651f7e0 + 85a76b8 commit 939ca74

File tree

10 files changed

+153
-85
lines changed

10 files changed

+153
-85
lines changed

.github/workflows/deploy.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
persist-credentials: false
3232

3333
- name: Build and Check Package
34-
uses: hynek/build-and-inspect-python-package@v2.12.0
34+
uses: hynek/build-and-inspect-python-package@b5076c307dc91924a82ad150cdd1533b444d3310
3535
with:
3636
attest-build-provenance-github: 'true'
3737

@@ -56,7 +56,7 @@ jobs:
5656
path: dist
5757

5858
- name: Publish package to PyPI
59-
uses: pypa/gh-action-pypi-publish@v1.12.4
59+
uses: pypa/gh-action-pypi-publish@76f52bc884231f62b9a034ebfe128415bbaabdfc
6060
with:
6161
attestations: true
6262

@@ -109,7 +109,7 @@ jobs:
109109
tox -e generate-gh-release-notes -- "$VERSION" scripts/latest-release-notes.md
110110
111111
- name: Publish GitHub Release
112-
uses: softprops/action-gh-release@v2
112+
uses: softprops/action-gh-release@da05d552573ad5aba039eaac05058a918a7bf631
113113
with:
114114
body_path: scripts/latest-release-notes.md
115115
files: dist/*

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
fetch-depth: 0
4141
persist-credentials: false
4242
- name: Build and Check Package
43-
uses: hynek/build-and-inspect-python-package@v2.12.0
43+
uses: hynek/build-and-inspect-python-package@b5076c307dc91924a82ad150cdd1533b444d3310
4444

4545
build:
4646
needs: [package]
@@ -262,7 +262,7 @@ jobs:
262262

263263
- name: Upload coverage to Codecov
264264
if: "matrix.use_coverage"
265-
uses: codecov/codecov-action@v5
265+
uses: codecov/codecov-action@ad3126e916f78f00edff4ed0317cf185271ccc2d
266266
with:
267267
fail_ci_if_error: false
268268
files: ./coverage.xml

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/astral-sh/ruff-pre-commit
3-
rev: "v0.11.5"
3+
rev: "v0.11.10"
44
hooks:
55
- id: ruff
66
args: ["--fix"]
@@ -12,7 +12,7 @@ repos:
1212
- id: end-of-file-fixer
1313
- id: check-yaml
1414
- repo: https://github.com/woodruffw/zizmor-pre-commit
15-
rev: v1.5.2
15+
rev: v1.7.0
1616
hooks:
1717
- id: zizmor
1818
- repo: https://github.com/adamchainz/blacken-docs

changelog/13420.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added ``lru_cache`` to ``nodes._check_initialpaths_for_relpath``.

changelog/5473.improvement.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Replace `:` with `;` in the assertion rewrite warning message so it can be filtered using standard Python warning filters before calling :func:`pytest.main`.

doc/en/reference/plugin_list.rst

Lines changed: 115 additions & 59 deletions
Large diffs are not rendered by default.

src/_pytest/assertion/rewrite.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ def _warn_already_imported(self, name: str) -> None:
282282

283283
self.config.issue_config_time_warning(
284284
PytestAssertRewriteWarning(
285-
f"Module already imported so cannot be rewritten: {name}"
285+
f"Module already imported so cannot be rewritten; {name}"
286286
),
287287
stacklevel=5,
288288
)

src/_pytest/nodes.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from collections.abc import Iterator
88
from collections.abc import MutableMapping
99
from functools import cached_property
10+
from functools import lru_cache
1011
from inspect import signature
1112
import os
1213
import pathlib
@@ -543,8 +544,11 @@ def _traceback_filter(self, excinfo: ExceptionInfo[BaseException]) -> Traceback:
543544
return excinfo.traceback
544545

545546

546-
def _check_initialpaths_for_relpath(session: Session, path: Path) -> str | None:
547-
for initial_path in session._initialpaths:
547+
@lru_cache(maxsize=1000)
548+
def _check_initialpaths_for_relpath(
549+
initial_paths: frozenset[Path], path: Path
550+
) -> str | None:
551+
for initial_path in initial_paths:
548552
if commonpath(path, initial_path) == initial_path:
549553
rel = str(path.relative_to(initial_path))
550554
return "" if rel == "." else rel
@@ -594,7 +598,7 @@ def __init__(
594598
try:
595599
nodeid = str(self.path.relative_to(session.config.rootpath))
596600
except ValueError:
597-
nodeid = _check_initialpaths_for_relpath(session, path)
601+
nodeid = _check_initialpaths_for_relpath(session._initialpaths, path)
598602

599603
if nodeid and os.sep != SEP:
600604
nodeid = nodeid.replace(os.sep, SEP)

testing/test_assertrewrite.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1202,7 +1202,23 @@ def test_rewrite_warning(self, pytester: Pytester) -> None:
12021202
)
12031203
# needs to be a subprocess because pytester explicitly disables this warning
12041204
result = pytester.runpytest_subprocess()
1205-
result.stdout.fnmatch_lines(["*Module already imported*: _pytest"])
1205+
result.stdout.fnmatch_lines(["*Module already imported*; _pytest"])
1206+
1207+
def test_rewrite_warning_ignore(self, pytester: Pytester) -> None:
1208+
pytester.makeconftest(
1209+
"""
1210+
import pytest
1211+
pytest.register_assert_rewrite("_pytest")
1212+
"""
1213+
)
1214+
# needs to be a subprocess because pytester explicitly disables this warning
1215+
result = pytester.runpytest_subprocess(
1216+
"-W",
1217+
"ignore:Module already imported so cannot be rewritten; _pytest:pytest.PytestAssertRewriteWarning",
1218+
)
1219+
# Previously, when the message pattern used to contain an extra `:`, an error was raised.
1220+
assert not result.stderr.str().strip()
1221+
result.stdout.no_fnmatch_line("*Module already imported*; _pytest")
12061222

12071223
def test_rewrite_module_imported_from_conftest(self, pytester: Pytester) -> None:
12081224
pytester.makeconftest(

testing/test_nodes.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
from pathlib import Path
55
import re
6-
from typing import cast
76
import warnings
87

98
from _pytest import nodes
@@ -103,24 +102,15 @@ def test__check_initialpaths_for_relpath() -> None:
103102
"""Ensure that it handles dirs, and does not always use dirname."""
104103
cwd = Path.cwd()
105104

106-
class FakeSession1:
107-
_initialpaths = frozenset({cwd})
105+
initial_paths = frozenset({cwd})
108106

109-
session = cast(pytest.Session, FakeSession1)
110-
111-
assert nodes._check_initialpaths_for_relpath(session, cwd) == ""
107+
assert nodes._check_initialpaths_for_relpath(initial_paths, cwd) == ""
112108

113109
sub = cwd / "file"
114-
115-
class FakeSession2:
116-
_initialpaths = frozenset({cwd})
117-
118-
session = cast(pytest.Session, FakeSession2)
119-
120-
assert nodes._check_initialpaths_for_relpath(session, sub) == "file"
110+
assert nodes._check_initialpaths_for_relpath(initial_paths, sub) == "file"
121111

122112
outside = Path("/outside-this-does-not-exist")
123-
assert nodes._check_initialpaths_for_relpath(session, outside) is None
113+
assert nodes._check_initialpaths_for_relpath(initial_paths, outside) is None
124114

125115

126116
def test_failure_with_changed_cwd(pytester: Pytester) -> None:

0 commit comments

Comments
 (0)