Skip to content

Commit 71f265f

Browse files
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
1 parent 7259e8d commit 71f265f

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

src/_pytest/pathlib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ def resolve_package_path(path: Path) -> Optional[Path]:
680680
result = None
681681
for parent in itertools.chain((path,), path.parents):
682682
if parent.is_dir():
683-
if not parent.joinpath("__init__.py").is_file():
683+
if not (parent / "__init__.py").is_file():
684684
break
685685
if not parent.name.isidentifier():
686686
break

testing/test_pathlib.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,18 +345,18 @@ def test_resolve_package_path(tmp_path: Path) -> None:
345345
(pkg / "subdir").mkdir()
346346
(pkg / "subdir/__init__.py").touch()
347347
assert resolve_package_path(pkg) == pkg
348-
assert resolve_package_path(pkg.joinpath("subdir", "__init__.py")) == pkg
348+
assert resolve_package_path(pkg / "subdir/__init__.py") == pkg
349349

350350

351351
def test_package_unimportable(tmp_path: Path) -> None:
352352
pkg = tmp_path / "pkg1-1"
353353
pkg.mkdir()
354354
pkg.joinpath("__init__.py").touch()
355-
subdir = pkg.joinpath("subdir")
355+
subdir = pkg / "subdir"
356356
subdir.mkdir()
357-
pkg.joinpath("subdir/__init__.py").touch()
357+
(pkg / "subdir/__init__.py").touch()
358358
assert resolve_package_path(subdir) == subdir
359-
xyz = subdir.joinpath("xyz.py")
359+
xyz = subdir / "xyz.py"
360360
xyz.touch()
361361
assert resolve_package_path(xyz) == subdir
362362
assert not resolve_package_path(pkg)

0 commit comments

Comments
 (0)