Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use packaging.version to check version equality #9085

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions news/9083.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
New resolver: Check version equality with ``packaging.version`` to avoid edge
cases if a wheel used different version normalization logic in its filename
and metadata.
5 changes: 3 additions & 2 deletions src/pip/_internal/resolution/resolvelib/candidates.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,10 @@ def __init__(
)
# Version may not be present for PEP 508 direct URLs
if version is not None:
assert str(version) == wheel.version, (
wheel_version = Version(wheel.version)
assert version == wheel_version, (
"{!r} != {!r} for wheel {}".format(
version, wheel.version, name
version, wheel_version, name
)
)

Expand Down
43 changes: 43 additions & 0 deletions tests/functional/test_new_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
create_basic_wheel_for_package,
create_test_package_with_setup,
)
from tests.lib.wheel import make_wheel


def assert_installed(script, **kwargs):
Expand Down Expand Up @@ -1089,3 +1090,45 @@ def test_new_resolver_presents_messages_when_backtracking_a_lot(script, N):
assert result.stdout.count("This could take a while.") >= 2
if N >= 13:
assert "press Ctrl + C" in result.stdout


@pytest.mark.parametrize(
"metadata_version",
[
"0.1.0+local.1", # Normalized form.
"0.1.0+local_1", # Non-normalized form containing an underscore.

# Non-normalized form containing a dash. This is allowed, installation
# works correctly, but assert_installed() fails because pkg_resources
# cannot handle it correctly. Nobody is complaining about it right now,
# we're probably dropping it for importlib.metadata soon(tm), so let's
# ignore it for the time being.
pytest.param("0.1.0+local-1", marks=pytest.mark.xfail),
],
ids=["meta_dot", "meta_underscore", "meta_dash"],
)
@pytest.mark.parametrize(
"filename_version",
[
("0.1.0+local.1"), # Tools are encouraged to use this.
("0.1.0+local_1"), # But this is allowed (version not normalized).
],
ids=["file_dot", "file_underscore"],
)
def test_new_resolver_check_wheel_version_normalized(
script,
metadata_version,
filename_version,
):
filename = "simple-{}-py2.py3-none-any.whl".format(filename_version)

wheel_builder = make_wheel(name="simple", version=metadata_version)
wheel_builder.save_to(script.scratch_path / filename)

script.pip(
"install",
"--no-cache-dir", "--no-index",
"--find-links", script.scratch_path,
"simple"
)
assert_installed(script, simple="0.1.0+local.1")