Skip to content
New issue

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

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

Already on GitHub? # to your account

feat: Add is_editable wrapper function #37

Merged
merged 6 commits into from
Feb 6, 2024
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
2 changes: 1 addition & 1 deletion .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: 2
build:
os: ubuntu-22.04
tools:
python: "3.11"
python: "3.12"
jobs:
post_checkout:
- git fetch --unshallow || true
Expand Down
4 changes: 4 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,7 @@ else:
```{eval-rst}
.. autofunction:: pep610.read_from_distribution
```

```{eval-rst}
.. autofunction:: pep610.is_editable
```
27 changes: 26 additions & 1 deletion src/pep610/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import typing as t
from dataclasses import dataclass
from functools import singledispatch
from importlib.metadata import version
from importlib.metadata import distribution, version

if t.TYPE_CHECKING:
import sys
Expand Down Expand Up @@ -325,13 +325,38 @@ def read_from_distribution(dist: Distribution) -> VCSData | ArchiveData | DirDat

Returns:
The parsed PEP 610 file.

>>> import importlib.metadata
>>> dist = importlib.metadata.distribution("pep610")
>>> read_from_distribution(dist) # doctest: +SKIP
DirData(url='file:///home/user/pep610', dir_info=DirInfo(editable=False))
"""
if contents := dist.read_text("direct_url.json"):
return _parse(contents)

return None


def is_editable(distribution_name: str) -> bool:
"""Wrapper around :func:`read_from_distribution` to check if a distribution is editable.

Args:
distribution_name: The distribution name.

Returns:
Whether the distribution is editable.

Raises:
importlib_metadata.PackageNotFoundError: If the distribution is not found.

>>> is_editable("pep610") # doctest: +SKIP
False
""" # noqa: DAR402, RUF100
dist = distribution(distribution_name)
data = read_from_distribution(dist)
return isinstance(data, DirData) and data.dir_info.is_editable()


def write_to_distribution(dist: PathDistribution, data: dict) -> int:
"""Write the direct URL data to a distribution.

Expand Down
52 changes: 52 additions & 0 deletions tests/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
HashData,
VCSData,
VCSInfo,
is_editable,
read_from_distribution,
to_dict,
write_to_distribution,
Expand Down Expand Up @@ -321,3 +322,54 @@ def test_no_file(tmp_path: Path):
"""Test that a missing file is read back as None."""
dist = Distribution.at(tmp_path)
assert read_from_distribution(dist) is None


@pytest.mark.parametrize(
("data", "expected"),
[
pytest.param(
{
"url": "file:///home/user/project",
"dir_info": {"editable": True},
},
True,
id="editable",
),
pytest.param(
{
"url": "file:///home/user/project",
"dir_info": {"editable": False},
},
False,
id="not_editable",
),
pytest.param(
{
"url": "file:///home/user/project",
"dir_info": {},
},
False,
id="no_editable_info",
),
pytest.param(
{
"url": "https://github.com/pypa/pip.git",
"vcs_info": {
"vcs": "git",
"requested_revision": "1.3.1",
"resolved_revision_type": "tag",
"commit_id": "7921be1537eac1e97bc40179a57f0349c2aee67d",
},
},
False,
id="vcs_git",
),
],
)
def test_is_editable(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, data: dict, expected: bool): # noqa: FBT001
"""Test the is_editable function."""
dist = Distribution.at(tmp_path)
write_to_distribution(dist, data)

monkeypatch.setattr("pep610.distribution", lambda _: dist)
assert is_editable("my_package") is expected