Skip to content

Commit cfbe319

Browse files
sashko1988Oleksandr Zavertnievpre-commit-ci[bot]
authored
Fix: #13420 - Add cache for nodes._check_initialpaths_for_relpath (#13422)
* add lru_cache to nodes._check_initialpaths_for_relpath update tests * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Oleksandr Zavertniev <oleksandr.zavertniev@yellowbrick.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 919ae9d commit cfbe319

File tree

3 files changed

+12
-17
lines changed

3 files changed

+12
-17
lines changed

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``.

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_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)