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 Oct 10, 2023
1 parent b2adca8 commit 8c73d8d
Show file tree
Hide file tree
Showing 2 changed files with 33 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 @@ -700,7 +701,7 @@ def from_filename(cls, filename):
Return a distribution built from the data found in a `filename` string.
Raise an exception if this is not a valid filename
"""
filename = os.path.basename(filename.strip("/"))
filename = unquote(os.path.basename(filename.strip("/")))
clazz = cls.get_dist_class(filename)
return clazz.from_filename(filename)

Expand Down
31 changes: 31 additions & 0 deletions tests/test_resolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import packvers
import pytest
from commoncode.system import on_mac
from commoncode.testcase import FileDrivenTesting
from packvers.requirements import Requirement

Expand All @@ -26,6 +27,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 +132,35 @@ def test_get_resolved_dependencies_with_tilde_requirement_using_json_api():
]


@pytest.mark.online
@pytest.mark.skipif(on_mac, reason="torch is only available for linux and windows.")
def test_get_resolved_dependencies_for_version_containing_local_version_identifier():
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 8c73d8d

Please sign in to comment.