diff --git a/src/_pytest/config/compat.py b/src/_pytest/config/compat.py index c93f3738d..731641dde 100644 --- a/src/_pytest/config/compat.py +++ b/src/_pytest/config/compat.py @@ -4,8 +4,9 @@ from pathlib import Path from typing import Optional from ..compat import LEGACY_PATH +from ..compat import legacy_path from ..deprecated import HOOK_LEGACY_PATH_ARG -from _pytest.nodes import _imply_path +from _pytest.nodes import _check_path # hookname: (Path, LEGACY_PATH) imply_paths_hooks = { @@ -52,7 +53,15 @@ class PathAwareHookProxy: ), stacklevel=2, ) - path_value, fspath_value = _imply_path(path_value, fspath_value) + if path_value is not None: + if fspath_value is not None: + _check_path(path_value, fspath_value) + else: + fspath_value = legacy_path(path_value) + else: + assert fspath_value is not None + path_value = Path(fspath_value) + kw[path_var] = path_value kw[fspath_var] = fspath_value return hook(**kw) diff --git a/src/_pytest/nodes.py b/src/_pytest/nodes.py index 0a5acbad9..33bf1062b 100644 --- a/src/_pytest/nodes.py +++ b/src/_pytest/nodes.py @@ -101,23 +101,7 @@ def _check_path(path: Path, fspath: LEGACY_PATH) -> None: ) -def _imply_path( - path: Optional[Path], fspath: Optional[LEGACY_PATH] -) -> Tuple[Path, LEGACY_PATH]: - if path is not None: - if fspath is not None: - _check_path(path, fspath) - else: - fspath = legacy_path(path) - return path, fspath - else: - assert fspath is not None - return Path(fspath), fspath - - -# Optimization: use _imply_path_only over _imply_path when only need Path. -# This is to avoid `legacy_path(path)` which is surprisingly heavy. -def _imply_path_only(path: Optional[Path], fspath: Optional[LEGACY_PATH]) -> Path: +def _imply_path(path: Optional[Path], fspath: Optional[LEGACY_PATH]) -> Path: if path is not None: if fspath is not None: _check_path(path, fspath) @@ -212,7 +196,7 @@ class Node(metaclass=NodeMeta): #: Filesystem path where this node was collected from (can be None). if path is None and fspath is None: path = getattr(parent, "path", None) - self.path = _imply_path_only(path, fspath=fspath) + self.path = _imply_path(path, fspath=fspath) # The explicit annotation is to avoid publicly exposing NodeKeywords. #: Keywords/markers collected from all scopes. @@ -589,7 +573,7 @@ class FSCollector(Collector): assert path is None path = path_or_parent - path = _imply_path_only(path, fspath=fspath) + path = _imply_path(path, fspath=fspath) if name is None: name = path.name if parent is not None and parent.path != path: