From 71f265f1f32c7d9884b07f08819fb148b5b13521 Mon Sep 17 00:00:00 2001 From: Warren Markham Date: Sat, 9 Sep 2023 22:16:22 +1000 Subject: [PATCH] Refactor: use division operator to join paths (#11413) Starting with `resolve_package_path` and its associated tests, this refactoring seeks to make path concatenation more readable and consistent within tests/functions. As discussed in #11413: - code is free to use either `/` and `joinpath` - consistency within a function is more important than consistency across the codebase - it is nice to use `/` when it is more readable - it is nice to use `joinpath` when there is little context - be mindful that `joinpath` may be clearer when joining multiple segments --- src/_pytest/pathlib.py | 2 +- testing/test_pathlib.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/_pytest/pathlib.py b/src/_pytest/pathlib.py index 63b1345b4..68372f67c 100644 --- a/src/_pytest/pathlib.py +++ b/src/_pytest/pathlib.py @@ -680,7 +680,7 @@ def resolve_package_path(path: Path) -> Optional[Path]: result = None for parent in itertools.chain((path,), path.parents): if parent.is_dir(): - if not parent.joinpath("__init__.py").is_file(): + if not (parent / "__init__.py").is_file(): break if not parent.name.isidentifier(): break diff --git a/testing/test_pathlib.py b/testing/test_pathlib.py index 678fd27fe..50c1d5967 100644 --- a/testing/test_pathlib.py +++ b/testing/test_pathlib.py @@ -345,18 +345,18 @@ def test_resolve_package_path(tmp_path: Path) -> None: (pkg / "subdir").mkdir() (pkg / "subdir/__init__.py").touch() assert resolve_package_path(pkg) == pkg - assert resolve_package_path(pkg.joinpath("subdir", "__init__.py")) == pkg + assert resolve_package_path(pkg / "subdir/__init__.py") == pkg def test_package_unimportable(tmp_path: Path) -> None: pkg = tmp_path / "pkg1-1" pkg.mkdir() pkg.joinpath("__init__.py").touch() - subdir = pkg.joinpath("subdir") + subdir = pkg / "subdir" subdir.mkdir() - pkg.joinpath("subdir/__init__.py").touch() + (pkg / "subdir/__init__.py").touch() assert resolve_package_path(subdir) == subdir - xyz = subdir.joinpath("xyz.py") + xyz = subdir / "xyz.py" xyz.touch() assert resolve_package_path(xyz) == subdir assert not resolve_package_path(pkg)