Skip to content

Commit

Permalink
fix(dependency): do not discard unknown urls
Browse files Browse the repository at this point in the history
Prior to this change, when parsing PEP 508 requirement strings,
the `Dependency` class silently discarded any URI tokens that cannot
be validated as existing files.

Resolves: python-poetry/poetry#10068
  • Loading branch information
abn committed Jan 17, 2025
1 parent 528e796 commit 52c6d5c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/poetry/core/packages/dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,12 @@ def _make_file_or_dir_dep(
) -> FileDependency | DirectoryDependency | None:
"""
Helper function to create a file or directoru dependency with the given arguments.
If path is not a file or directory that exists, `None` is returned.
If path is not a file or directory that exists, a guess is made based on the suffix
of the given path. This is done to prevent dependendencies from being parsed as normal
dependencies. This allows for downstream error handling.
See also: poetry#10068
"""
from poetry.core.packages.directory_dependency import DirectoryDependency
from poetry.core.packages.file_dependency import FileDependency
Expand All @@ -506,13 +511,15 @@ def _make_file_or_dir_dep(
# a base path was specified, so we should respect that
_path = Path(base) / path

if _path.is_file():
# we check if it is a file (if it exists) or rely on suffix to guess
is_file = _path.is_file() if _path.exists() else path.suffix != ""

if is_file:
return FileDependency(
name, path, base=base, directory=subdirectory, extras=extras
)
elif _path.is_dir():
if subdirectory:
path = path / subdirectory
return DirectoryDependency(name, path, base=base, extras=extras)

return None
if subdirectory:
path = path / subdirectory

return DirectoryDependency(name, path, base=base, extras=extras)
23 changes: 23 additions & 0 deletions tests/packages/test_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from poetry.core.constraints.version.exceptions import ParseConstraintError
from poetry.core.packages.dependency import Dependency
from poetry.core.packages.directory_dependency import DirectoryDependency
from poetry.core.packages.file_dependency import FileDependency
from poetry.core.version.markers import InvalidMarkerError
from poetry.core.version.markers import parse_marker
from poetry.core.version.requirements import InvalidRequirementError
Expand Down Expand Up @@ -218,6 +220,27 @@ def test_to_pep_508_with_invalid_requirement(requirement: str) -> None:
_ = Dependency.create_from_pep_508(requirement)


@pytest.mark.parametrize(
("requirement", "dependency_type"),
[
(
"eflips-depot @ [email protected]:mpm-tu-berlin/eflips-depot.git@feature/allow-only-oppo-charging",
DirectoryDependency,
),
(
"eflips-depot @ [email protected]:mpm-tu-berlin/eflips-depot.git@feature/allow-only-oppo-charging.whl",
FileDependency,
),
],
)
def test_to_pep_508_with_invalid_path_requirement(
requirement: str, dependency_type: type[FileDependency | DirectoryDependency]
) -> None:
dependency = Dependency.create_from_pep_508(requirement)
assert isinstance(dependency, dependency_type)
assert dependency.source_url


def test_complete_name() -> None:
assert Dependency("foo", ">=1.2.3").complete_name == "foo"
assert (
Expand Down

0 comments on commit 52c6d5c

Please sign in to comment.