Skip to content

Commit 3d26321

Browse files
pre-commit-ci[bot]Tusenka
authored andcommitted
13403: Disable assertion rewriting for external modules
1 parent c99e436 commit 3d26321

File tree

25 files changed

+248
-89
lines changed

25 files changed

+248
-89
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 3 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.11"
3+
rev: "v0.11.12"
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.8.0
15+
rev: v1.9.0
1616
hooks:
1717
- id: zizmor
1818
- repo: https://github.com/adamchainz/blacken-docs
@@ -32,7 +32,7 @@ repos:
3232
hooks:
3333
- id: python-use-type-annotations
3434
- repo: https://github.com/pre-commit/mirrors-mypy
35-
rev: v1.15.0
35+
rev: v1.16.0
3636
hooks:
3737
- id: mypy
3838
files: ^(src/|testing/|scripts/)

changelog/13403.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Disable assertion for modules outside current working dir(cwd)

changelog/13492.doc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed outdated warning about ``faulthandler`` not working on Windows.

doc/en/how-to/failures.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ on the command-line.
112112

113113
Also the :confval:`faulthandler_timeout=X<faulthandler_timeout>` configuration option can be used
114114
to dump the traceback of all threads if a test takes longer than ``X``
115-
seconds to finish (not available on Windows).
115+
seconds to finish.
116116

117117
.. note::
118118

doc/en/reference/plugin_list.rst

Lines changed: 92 additions & 60 deletions
Large diffs are not rendered by default.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ disable = [
339339
]
340340

341341
[tool.codespell]
342-
ignore-words-list = "afile,asend,asser,assertio,feld,hove,ned,noes,notin,paramete,parth,socio-economic,tesults,varius,wil"
342+
ignore-words-list = "afile,asend,asser,assertio,feld,hove,ned,noes,notin,paramete,parth,tesults,varius,wil"
343343
skip = "AUTHORS,*/plugin_list.rst"
344344
write-changes = true
345345

src/_pytest/assertion/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class AssertionState:
110110
def __init__(self, config: Config, mode) -> None:
111111
self.mode = mode
112112
self.trace = config.trace.root.get("assertion")
113+
self.invocation_path = str(config.invocation_params.dir)
113114
self.hook: rewrite.AssertionRewritingHook | None = None
114115

115116

src/_pytest/assertion/rewrite.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,9 @@ def _should_rewrite(self, name: str, fn: str, state: AssertionState) -> bool:
239239
# rewritten if they match the naming convention for test files
240240
fn_path = PurePath(fn)
241241
for pat in self.fnpats:
242-
if fnmatch_ex(pat, fn_path):
242+
if fnmatch_ex(pat, fn_path) and fn_path.is_relative_to(
243+
state.invocation_path
244+
):
243245
state.trace(f"matched test file {fn!r}")
244246
return True
245247

@@ -702,7 +704,6 @@ def run(self, mod: ast.Module) -> None:
702704
if doc is not None and self.is_rewrite_disabled(doc):
703705
return
704706
pos = 0
705-
item = None
706707
for item in mod.body:
707708
if (
708709
expect_docstring

src/_pytest/config/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ def get_config(
284284
args: list[str] | None = None,
285285
plugins: Sequence[str | _PluggyPlugin] | None = None,
286286
) -> Config:
287-
# subsequent calls to main will create a fresh instance
287+
# Subsequent calls to main will create a fresh instance.
288288
pluginmanager = PytestPluginManager()
289289
config = Config(
290290
pluginmanager,
@@ -330,21 +330,21 @@ def _prepareconfig(
330330
)
331331
raise TypeError(msg.format(args, type(args)))
332332

333-
config = get_config(args, plugins)
334-
pluginmanager = config.pluginmanager
333+
initial_config = get_config(args, plugins)
334+
pluginmanager = initial_config.pluginmanager
335335
try:
336336
if plugins:
337337
for plugin in plugins:
338338
if isinstance(plugin, str):
339339
pluginmanager.consider_pluginarg(plugin)
340340
else:
341341
pluginmanager.register(plugin)
342-
config = pluginmanager.hook.pytest_cmdline_parse(
342+
config: Config = pluginmanager.hook.pytest_cmdline_parse(
343343
pluginmanager=pluginmanager, args=args
344344
)
345345
return config
346346
except BaseException:
347-
config._ensure_unconfigure()
347+
initial_config._ensure_unconfigure()
348348
raise
349349

350350

src/_pytest/fixtures.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,8 +1106,7 @@ def execute(self, request: SubRequest) -> FixtureValue:
11061106
exc, exc_tb = self.cached_result[2]
11071107
raise exc.with_traceback(exc_tb)
11081108
else:
1109-
result = self.cached_result[0]
1110-
return result
1109+
return self.cached_result[0]
11111110
# We have a previous but differently parametrized fixture instance
11121111
# so we need to tear it down before creating a new one.
11131112
self.finish(request)
@@ -1123,10 +1122,12 @@ def execute(self, request: SubRequest) -> FixtureValue:
11231122
ihook = request.node.ihook
11241123
try:
11251124
# Setup the fixture, run the code in it, and cache the value
1126-
# in self.cached_result
1127-
result = ihook.pytest_fixture_setup(fixturedef=self, request=request)
1125+
# in self.cached_result.
1126+
result: FixtureValue = ihook.pytest_fixture_setup(
1127+
fixturedef=self, request=request
1128+
)
11281129
finally:
1129-
# schedule our finalizer, even if the setup failed
1130+
# Schedule our finalizer, even if the setup failed.
11301131
request.node.addfinalizer(finalizer)
11311132

11321133
return result

0 commit comments

Comments
 (0)