Skip to content

Commit 8c73d8d

Browse files
committed
Fix resolving requirements with percent encoded characters
`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 aboutcode-org#143. Signed-off-by: Frank Viernau <[email protected]>
1 parent b2adca8 commit 8c73d8d

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/python_inspector/utils_pypi.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from typing import List
2121
from typing import NamedTuple
2222
from urllib.parse import quote_plus
23+
from urllib.parse import unquote
2324
from urllib.parse import urlparse
2425
from urllib.parse import urlunparse
2526

@@ -700,7 +701,7 @@ def from_filename(cls, filename):
700701
Return a distribution built from the data found in a `filename` string.
701702
Raise an exception if this is not a valid filename
702703
"""
703-
filename = os.path.basename(filename.strip("/"))
704+
filename = unquote(os.path.basename(filename.strip("/")))
704705
clazz = cls.get_dist_class(filename)
705706
return clazz.from_filename(filename)
706707

tests/test_resolution.py

+31
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import packvers
1515
import pytest
16+
from commoncode.system import on_mac
1617
from commoncode.testcase import FileDrivenTesting
1718
from packvers.requirements import Requirement
1819

@@ -26,6 +27,7 @@
2627
from python_inspector.resolution import parse_reqs_from_setup_py_insecurely
2728
from python_inspector.utils_pypi import PYPI_PUBLIC_REPO
2829
from python_inspector.utils_pypi import Environment
30+
from python_inspector.utils_pypi import PypiSimpleRepository
2931

3032
setup_test_env = FileDrivenTesting()
3133
setup_test_env.test_data_dir = os.path.join(os.path.dirname(__file__), "data")
@@ -130,6 +132,35 @@ def test_get_resolved_dependencies_with_tilde_requirement_using_json_api():
130132
]
131133

132134

135+
@pytest.mark.online
136+
@pytest.mark.skipif(on_mac, reason="torch is only available for linux and windows.")
137+
def test_get_resolved_dependencies_for_version_containing_local_version_identifier():
138+
req = Requirement("torch==2.0.0+cpu")
139+
req.is_requirement_resolved = True
140+
_, plist = get_resolved_dependencies(
141+
requirements=[req],
142+
environment=Environment(
143+
python_version="310",
144+
operating_system="linux",
145+
),
146+
repos=[
147+
PypiSimpleRepository(index_url="https://download.pytorch.org/whl/cpu", credentials=None)
148+
],
149+
as_tree=False,
150+
)
151+
152+
assert plist == [
153+
"pkg:pypi/[email protected]",
154+
"pkg:pypi/[email protected]",
155+
"pkg:pypi/[email protected]",
156+
"pkg:pypi/[email protected]",
157+
"pkg:pypi/[email protected]",
158+
"pkg:pypi/[email protected]",
159+
"pkg:pypi/[email protected]%2Bcpu",
160+
"pkg:pypi/[email protected]",
161+
]
162+
163+
133164
@pytest.mark.online
134165
def test_without_supported_wheels():
135166
req = Requirement("autobahn==22.3.2")

0 commit comments

Comments
 (0)