Skip to content

Commit

Permalink
Merge pull request #2087 from macro1/relative-paths
Browse files Browse the repository at this point in the history
Preserve relative paths for editable dependencies in output
  • Loading branch information
macro1 authored May 10, 2024
2 parents 6209efd + 322afa7 commit fb7d14a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
18 changes: 17 additions & 1 deletion piptools/_compat/pip_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from pip._internal.metadata import BaseDistribution
from pip._internal.metadata.pkg_resources import Distribution as _PkgResourcesDist
from pip._internal.models.direct_url import DirectUrl
from pip._internal.models.link import Link
from pip._internal.network.session import PipSession
from pip._internal.req import InstallRequirement
from pip._internal.req import parse_requirements as _parse_requirements
Expand Down Expand Up @@ -63,6 +64,14 @@ def _from_importlib(cls, dist: _ImportLibDist) -> Distribution:
return cls(dist._dist.name, dist._dist.version, requires, dist.direct_url)


class FileLink(Link):

@property
def file_path(self) -> str:
# overriding the actual property to bypass some validation
return self._url


def parse_requirements(
filename: str,
session: PipSession,
Expand All @@ -74,7 +83,14 @@ def parse_requirements(
for parsed_req in _parse_requirements(
filename, session, finder=finder, options=options, constraint=constraint
):
yield install_req_from_parsed_requirement(parsed_req, isolated=isolated)
install_req = install_req_from_parsed_requirement(parsed_req, isolated=isolated)
if install_req.editable and not parsed_req.requirement.startswith("file://"):
# ``Link.url`` is what is saved to the output file
# we set the url directly to undo the transformation in pip's Link class
file_link = FileLink(install_req.link.url)
file_link._url = parsed_req.requirement
install_req.link = file_link
yield install_req


def create_wheel_cache(cache_dir: str, format_control: str | None = None) -> WheelCache:
Expand Down
3 changes: 3 additions & 0 deletions tests/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
TEST_DATA_PATH = os.path.join(os.path.dirname(__file__), "test_data")
MINIMAL_WHEELS_PATH = os.path.join(TEST_DATA_PATH, "minimal_wheels")
PACKAGES_PATH = os.path.join(TEST_DATA_PATH, "packages")
PACKAGES_RELATIVE_PATH = os.path.relpath(
PACKAGES_PATH, os.path.commonpath([os.getcwd(), PACKAGES_PATH])
)
29 changes: 29 additions & 0 deletions tests/test_pip_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from __future__ import annotations

import os
import tempfile
from pathlib import Path, PurePosixPath

import pytest

from piptools._compat.pip_compat import parse_requirements
from piptools.repositories import PyPIRepository

from .constants import PACKAGES_RELATIVE_PATH


def test_parse_requirements_preserve_editable_relative_path(tmp_path, repository):
test_package_path = str(
PurePosixPath(Path(PACKAGES_RELATIVE_PATH)) / "small_fake_a"
)
requirements_in_path = str(tmp_path / "requirements.in")

with open(requirements_in_path, "w") as requirements_in_file:
requirements_in_file.write(f"-e {test_package_path}")

[install_requirement] = parse_requirements(
requirements_in_path, session=repository.session
)

assert install_requirement.link.url == test_package_path
assert install_requirement.link.file_path == test_package_path

0 comments on commit fb7d14a

Please sign in to comment.