Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: parse pth files with semi-colons as breaks #175

Merged
merged 1 commit into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/griffe/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,13 @@ def _handle_pth_file(path: Path) -> list[Path]:
# Blank lines and lines beginning with # are skipped.
# Lines starting with import (followed by space or tab) are executed.
directories = []
for line in path.read_text(encoding="utf8").strip().splitlines(keepends=False):
for line in path.read_text(encoding="utf8").strip().replace(";", "\n").splitlines(keepends=False):
line = line.strip() # noqa: PLW2901
if _re_import_line.match(line):
editable_module = path.parent / f"{line[len('import'):].lstrip()}.py"
with suppress(UnhandledEditableModuleError):
return _handle_editable_module(editable_module)
if line and not line.startswith("#") and ";" not in line and os.path.exists(line):
if line and not line.startswith("#") and os.path.exists(line):
directories.append(Path(line))
return directories

Expand Down
18 changes: 18 additions & 0 deletions tests/test_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,24 @@ def test_pth_file_handling(tmp_path: Path) -> None:
assert directories == [Path("tests")]


def test_pth_file_handling_with_semi_colon(tmp_path: Path) -> None:
"""Assert .pth files are correctly handled.

Parameters:
tmp_path: Pytest fixture.
"""
pth_file = tmp_path / "hello.pth"
pth_file.write_text(
dedent(
"""
# comment
import thing; import\tthing; /doesnotexist; tests
""",
),
)
directories = _handle_pth_file(pth_file)
assert directories == [Path("tests")]

@pytest.mark.parametrize("editable_file_name", ["__editables_whatever.py", "_editable_impl_whatever.py"])
def test_editables_file_handling(tmp_path: Path, editable_file_name: str) -> None:
"""Assert editable modules by `editables` are handled.
Expand Down