Skip to content

Commit bd6877e

Browse files
Improve nodes._check_initialpaths_for_relpath performance (#13448)
Use `Path` facilities to greatly improve performance of `nodes._check_initialpaths_for_relpath` (see #13448 for performance comparison). Related: #13420, #13413. --------- Co-authored-by: Bruno Oliveira <[email protected]>
1 parent 6f86813 commit bd6877e

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ Nikolay Kondratyev
332332
Nipunn Koorapati
333333
Oleg Pidsadnyi
334334
Oleg Sushchenko
335+
Oleksandr Zavertniev
335336
Olga Matoula
336337
Oliver Bestwalter
337338
Omar Kohl

changelog/13420.improvement.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improved test collection performance by optimizing path resolution used in ``FSCollector``.

src/_pytest/nodes.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
from _pytest.mark.structures import NodeKeywords
3939
from _pytest.outcomes import fail
4040
from _pytest.pathlib import absolutepath
41-
from _pytest.pathlib import commonpath
4241
from _pytest.stash import Stash
4342
from _pytest.warning_types import PytestWarning
4443

@@ -548,10 +547,13 @@ def _traceback_filter(self, excinfo: ExceptionInfo[BaseException]) -> Traceback:
548547
def _check_initialpaths_for_relpath(
549548
initial_paths: frozenset[Path], path: Path
550549
) -> str | None:
551-
for initial_path in initial_paths:
552-
if commonpath(path, initial_path) == initial_path:
553-
rel = str(path.relative_to(initial_path))
554-
return "" if rel == "." else rel
550+
if path in initial_paths:
551+
return ""
552+
553+
for parent in path.parents:
554+
if parent in initial_paths:
555+
return str(path.relative_to(parent))
556+
555557
return None
556558

557559

0 commit comments

Comments
 (0)