Skip to content

Commit

Permalink
Fix resolving requirements with percent encoded characters
Browse files Browse the repository at this point in the history
`Distribution.from_link()` derives the version string of a package from
the given (percent encoded) `Link.url`. That derivation lacks the
decoding, so the resulting version string may also contain percent
encoded characters in which case the dependency resolution fails.

Fix the resolution by URL adding the missing unquoting.

Fixes #143.

Signed-off-by: Frank Viernau <[email protected]>
  • Loading branch information
fviernau committed Sep 25, 2023
1 parent f288e25 commit 326f62d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/python_inspector/utils_pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from typing import List
from typing import NamedTuple
from urllib.parse import quote_plus
from urllib.parse import unquote
from urllib.parse import urlparse
from urllib.parse import urlunparse

Expand Down Expand Up @@ -675,7 +676,7 @@ def from_link(cls, link: Link):
"""
requires_python = link.python_requires
path_or_url = link.url
filename = os.path.basename(path_or_url.strip("/"))
filename = os.path.basename(unquote(path_or_url).strip("/"))
dist = cls.from_filename(filename)
dist.path_or_url = path_or_url
dist.python_requires = requires_python
Expand Down
29 changes: 29 additions & 0 deletions tests/test_resolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from python_inspector.resolution import parse_reqs_from_setup_py_insecurely
from python_inspector.utils_pypi import PYPI_PUBLIC_REPO
from python_inspector.utils_pypi import Environment
from python_inspector.utils_pypi import PypiSimpleRepository

setup_test_env = FileDrivenTesting()
setup_test_env.test_data_dir = os.path.join(os.path.dirname(__file__), "data")
Expand Down Expand Up @@ -130,6 +131,34 @@ def test_get_resolved_dependencies_with_tilde_requirement_using_json_api():
]


@pytest.mark.online
def test_get_resolved_dependencies_with_url_encoded_char_plus():
req = Requirement("torch==2.0.0+cpu")
req.is_requirement_resolved = True
_, plist = get_resolved_dependencies(
requirements=[req],
environment=Environment(
python_version="310",
operating_system="linux",
),
repos=[
PypiSimpleRepository(index_url="https://download.pytorch.org/whl/cpu", credentials=None)
],
as_tree=False,
)

assert plist == [
"pkg:pypi/[email protected]",
"pkg:pypi/[email protected]",
"pkg:pypi/[email protected]",
"pkg:pypi/[email protected]",
"pkg:pypi/[email protected]",
"pkg:pypi/[email protected]",
"pkg:pypi/[email protected]%2Bcpu",
"pkg:pypi/[email protected]",
]


@pytest.mark.online
def test_without_supported_wheels():
req = Requirement("autobahn==22.3.2")
Expand Down

0 comments on commit 326f62d

Please sign in to comment.