Skip to content

Commit 326f62d

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 #143. Signed-off-by: Frank Viernau <[email protected]>
1 parent f288e25 commit 326f62d

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-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

@@ -675,7 +676,7 @@ def from_link(cls, link: Link):
675676
"""
676677
requires_python = link.python_requires
677678
path_or_url = link.url
678-
filename = os.path.basename(path_or_url.strip("/"))
679+
filename = os.path.basename(unquote(path_or_url).strip("/"))
679680
dist = cls.from_filename(filename)
680681
dist.path_or_url = path_or_url
681682
dist.python_requires = requires_python

tests/test_resolution.py

+29
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from python_inspector.resolution import parse_reqs_from_setup_py_insecurely
2727
from python_inspector.utils_pypi import PYPI_PUBLIC_REPO
2828
from python_inspector.utils_pypi import Environment
29+
from python_inspector.utils_pypi import PypiSimpleRepository
2930

3031
setup_test_env = FileDrivenTesting()
3132
setup_test_env.test_data_dir = os.path.join(os.path.dirname(__file__), "data")
@@ -130,6 +131,34 @@ def test_get_resolved_dependencies_with_tilde_requirement_using_json_api():
130131
]
131132

132133

134+
@pytest.mark.online
135+
def test_get_resolved_dependencies_with_url_encoded_char_plus():
136+
req = Requirement("torch==2.0.0+cpu")
137+
req.is_requirement_resolved = True
138+
_, plist = get_resolved_dependencies(
139+
requirements=[req],
140+
environment=Environment(
141+
python_version="310",
142+
operating_system="linux",
143+
),
144+
repos=[
145+
PypiSimpleRepository(index_url="https://download.pytorch.org/whl/cpu", credentials=None)
146+
],
147+
as_tree=False,
148+
)
149+
150+
assert plist == [
151+
"pkg:pypi/[email protected]",
152+
"pkg:pypi/[email protected]",
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]%2Bcpu",
158+
"pkg:pypi/[email protected]",
159+
]
160+
161+
133162
@pytest.mark.online
134163
def test_without_supported_wheels():
135164
req = Requirement("autobahn==22.3.2")

0 commit comments

Comments
 (0)