Skip to content

Commit

Permalink
Change compute_module_name to return Optional
Browse files Browse the repository at this point in the history
This is better to enforce callers to check for it instead of ending up with '' and possibly breaking later.
  • Loading branch information
nicoddemus committed Apr 7, 2024
1 parent 981e399 commit 34196a9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
13 changes: 8 additions & 5 deletions src/_pytest/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,14 +789,15 @@ def resolve_pkg_root_and_module_name(
start = pkg_root if pkg_root is not None else path.parent

Check warning on line 789 in src/_pytest/pathlib.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/pathlib.py#L789

Added line #L789 was not covered by tests
for candidate in (start, *start.parents):
module_name = compute_module_name(candidate, path)

Check warning on line 791 in src/_pytest/pathlib.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/pathlib.py#L791

Added line #L791 was not covered by tests
if is_importable(module_name, path):
if module_name and is_importable(module_name, path):
# Point the pkg_root to the root of the namespace package.
pkg_root = candidate
break

Check warning on line 795 in src/_pytest/pathlib.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/pathlib.py#L794-L795

Added lines #L794 - L795 were not covered by tests

if pkg_root is not None:
module_name = compute_module_name(pkg_root, path)
return pkg_root, module_name
if module_name:
return pkg_root, module_name

raise CouldNotResolvePathError(f"Could not resolve for {path}")

Expand Down Expand Up @@ -827,16 +828,18 @@ def is_importable(module_name: str, module_path: Path) -> bool:
return spec_matches_module_path(spec, module_path)

Check warning on line 828 in src/_pytest/pathlib.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/pathlib.py#L828

Added line #L828 was not covered by tests


def compute_module_name(root: Path, module_path: Path) -> str:
def compute_module_name(root: Path, module_path: Path) -> Optional[str]:
"""Compute a module name based on a path and a root anchor."""
try:
path_without_suffix = module_path.with_suffix("")
except ValueError:

Check warning on line 835 in src/_pytest/pathlib.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/pathlib.py#L835

Added line #L835 was not covered by tests
# Empty paths (such as Path.cwd()) might break meta_path hooks (like our own assertion rewriter).
return ""
return None

Check warning on line 837 in src/_pytest/pathlib.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/pathlib.py#L837

Added line #L837 was not covered by tests

names = list(path_without_suffix.relative_to(root).parts)
if names and names[-1] == "__init__":
if not names:
return None

Check warning on line 841 in src/_pytest/pathlib.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/pathlib.py#L841

Added line #L841 was not covered by tests
if names[-1] == "__init__":
names.pop()
return ".".join(names)

Expand Down
4 changes: 2 additions & 2 deletions testing/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1400,8 +1400,8 @@ def test_is_importable(pytester: Pytester) -> None:


def test_compute_module_name(tmp_path: Path) -> None:
assert compute_module_name(tmp_path, tmp_path) == ""
assert compute_module_name(Path(), Path()) == ""
assert compute_module_name(tmp_path, tmp_path) is None
assert compute_module_name(Path(), Path()) is None

assert compute_module_name(tmp_path, tmp_path / "mod.py") == "mod"
assert compute_module_name(tmp_path, tmp_path / "src/app/bar") == "src.app.bar"
Expand Down

0 comments on commit 34196a9

Please sign in to comment.