Skip to content

Commit

Permalink
Parse pkg_resources Dist versions with packaging.version
Browse files Browse the repository at this point in the history
Due to a mix of bundled and unbundled dependencies, pkg_resources
Version class may not be the same as packaging's Version class.

See: pypa/setuptools#2052
  • Loading branch information
stefanor committed Mar 2, 2021
1 parent e7d6e54 commit 2e3282d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/pip/_internal/metadata/pkg_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from pip._vendor import pkg_resources
from pip._vendor.packaging.utils import canonicalize_name
from pip._vendor.packaging.version import _BaseVersion
from pip._vendor.packaging.version import _BaseVersion, parse as parse_version

from pip._internal.utils import misc # TODO: Move definition here.
from pip._internal.utils.packaging import get_installer
Expand All @@ -16,6 +16,7 @@ class Distribution(BaseDistribution):
def __init__(self, dist):
# type: (pkg_resources.Distribution) -> None
self._dist = dist
self._version = None

@classmethod
def from_wheel(cls, path, name):
Expand Down Expand Up @@ -45,7 +46,9 @@ def canonical_name(self):
@property
def version(self):
# type: () -> _BaseVersion
return self._dist.parsed_version
if self._version is None:
self._version = parse_version(self._dist.version)
return self._version

@property
def installer(self):
Expand Down
11 changes: 8 additions & 3 deletions src/pip/_internal/resolution/resolvelib/candidates.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pip._vendor.packaging.specifiers import InvalidSpecifier, SpecifierSet
from pip._vendor.packaging.utils import canonicalize_name
from pip._vendor.packaging.version import Version, _BaseVersion
from pip._vendor.packaging.version import parse as parse_version
from pip._vendor.pkg_resources import Distribution

from pip._internal.exceptions import HashError, MetadataInconsistent
Expand Down Expand Up @@ -180,7 +181,7 @@ def name(self):
def version(self):
# type: () -> _BaseVersion
if self._version is None:
self._version = self.dist.parsed_version
self._version = parse_version(self.dist.version)
return self._version

def format_for_error(self):
Expand All @@ -206,7 +207,8 @@ def _check_metadata_consistency(self, dist):
self._name,
dist.project_name,
)
if self._version is not None and self._version != dist.parsed_version:
parsed_version = parse_version(dist.version)
if self._version is not None and self._version != parsed_version:
raise MetadataInconsistent(
self._ireq,
"version",
Expand Down Expand Up @@ -345,6 +347,7 @@ def __init__(
self.dist = dist
self._ireq = make_install_req_from_dist(dist, template)
self._factory = factory
self._version = None

# This is just logging some messages, so we can do it eagerly.
# The returned dist would be exactly the same as self.dist because we
Expand Down Expand Up @@ -387,7 +390,9 @@ def name(self):
@property
def version(self):
# type: () -> _BaseVersion
return self.dist.parsed_version
if self._version is None:
self._version = parse_version(self.dist.version)
return self._version

@property
def is_editable(self):
Expand Down
3 changes: 2 additions & 1 deletion src/pip/_internal/resolution/resolvelib/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import TYPE_CHECKING, Dict, List, Optional, Set, Tuple

from pip._vendor.packaging.utils import canonicalize_name
from pip._vendor.packaging.version import parse as parse_version
from pip._vendor.resolvelib import ResolutionImpossible
from pip._vendor.resolvelib import Resolver as RLResolver
from pip._vendor.resolvelib.resolvers import Result
Expand Down Expand Up @@ -139,7 +140,7 @@ def resolve(self, root_reqs, check_supported_wheels):
elif self.factory.force_reinstall:
# The --force-reinstall flag is set -- reinstall.
ireq.should_reinstall = True
elif installed_dist.parsed_version != candidate.version:
elif parse_version(installed_dist.version) != candidate.version:
# The installation is different in version -- reinstall.
ireq.should_reinstall = True
elif candidate.is_editable or dist_is_editable(installed_dist):
Expand Down

0 comments on commit 2e3282d

Please sign in to comment.