Skip to content

Commit

Permalink
Upgrade vendored packaging lib
Browse files Browse the repository at this point in the history
  • Loading branch information
sbidoul authored and pradyunsg committed May 4, 2024
1 parent e8e592a commit 47a8480
Show file tree
Hide file tree
Showing 31 changed files with 2,662 additions and 1,143 deletions.
1 change: 1 addition & 0 deletions news/packaging.vendor.rst
@@ -0,0 +1 @@
Upgrade packaging to 23.2
2 changes: 0 additions & 2 deletions src/pip/_internal/commands/check.py
Expand Up @@ -7,7 +7,6 @@
from pip._internal.operations.check import (
check_package_set,
create_package_set_from_installed,
warn_legacy_versions_and_specifiers,
)
from pip._internal.utils.misc import write_output

Expand All @@ -22,7 +21,6 @@ class CheckCommand(Command):

def run(self, options: Values, args: List[str]) -> int:
package_set, parsing_probs = create_package_set_from_installed()
warn_legacy_versions_and_specifiers(package_set)
missing, conflicting = check_package_set(package_set)

for project_name in missing:
Expand Down
1 change: 0 additions & 1 deletion src/pip/_internal/commands/download.py
Expand Up @@ -139,7 +139,6 @@ def run(self, options: Values, args: List[str]) -> int:
downloaded.append(req.name)

preparer.prepare_linked_requirements_more(requirement_set.requirements.values())
requirement_set.warn_legacy_versions_and_specifiers()

if downloaded:
write_output("Successfully downloaded %s", " ".join(downloaded))
Expand Down
6 changes: 3 additions & 3 deletions src/pip/_internal/commands/index.py
@@ -1,8 +1,8 @@
import logging
from optparse import Values
from typing import Any, Iterable, List, Optional, Union
from typing import Any, Iterable, List, Optional

from pip._vendor.packaging.version import LegacyVersion, Version
from pip._vendor.packaging.version import Version

from pip._internal.cli import cmdoptions
from pip._internal.cli.req_command import IndexGroupCommand
Expand Down Expand Up @@ -115,7 +115,7 @@ def get_available_package_versions(self, options: Values, args: List[Any]) -> No
ignore_requires_python=options.ignore_requires_python,
)

versions: Iterable[Union[LegacyVersion, Version]] = (
versions: Iterable[Version] = (
candidate.version for candidate in finder.find_all_candidates(query)
)

Expand Down
3 changes: 0 additions & 3 deletions src/pip/_internal/commands/install.py
Expand Up @@ -387,9 +387,6 @@ def run(self, options: Values, args: List[str]) -> int:
json.dump(report.to_dict(), f, indent=2, ensure_ascii=False)

if options.dry_run:
# In non dry-run mode, the legacy versions and specifiers check
# will be done as part of conflict detection.
requirement_set.warn_legacy_versions_and_specifiers()
would_install_items = sorted(
(r.metadata["name"], r.metadata["version"])
for r in requirement_set.requirements_to_install
Expand Down
4 changes: 2 additions & 2 deletions src/pip/_internal/commands/list.py
Expand Up @@ -4,6 +4,7 @@
from typing import TYPE_CHECKING, Generator, List, Optional, Sequence, Tuple, cast

from pip._vendor.packaging.utils import canonicalize_name
from pip._vendor.packaging.version import Version

from pip._internal.cli import cmdoptions
from pip._internal.cli.req_command import IndexGroupCommand
Expand All @@ -18,7 +19,6 @@
from pip._internal.utils.misc import tabulate, write_output

if TYPE_CHECKING:
from pip._internal.metadata.base import DistributionVersion

class _DistWithLatestInfo(BaseDistribution):
"""Give the distribution object a couple of extra fields.
Expand All @@ -27,7 +27,7 @@ class _DistWithLatestInfo(BaseDistribution):
makes the rest of the code much cleaner.
"""

latest_version: DistributionVersion
latest_version: Version
latest_filetype: str

_ProcessedDists = Sequence[_DistWithLatestInfo]
Expand Down
1 change: 0 additions & 1 deletion src/pip/_internal/commands/wheel.py
Expand Up @@ -154,7 +154,6 @@ def run(self, options: Values, args: List[str]) -> int:
reqs_to_build.append(req)

preparer.prepare_linked_requirements_more(requirement_set.requirements.values())
requirement_set.warn_legacy_versions_and_specifiers()

# build wheels
build_successes, build_failures = build(
Expand Down
6 changes: 2 additions & 4 deletions src/pip/_internal/metadata/base.py
Expand Up @@ -25,7 +25,7 @@
from pip._vendor.packaging.requirements import Requirement
from pip._vendor.packaging.specifiers import InvalidSpecifier, SpecifierSet
from pip._vendor.packaging.utils import NormalizedName, canonicalize_name
from pip._vendor.packaging.version import LegacyVersion, Version
from pip._vendor.packaging.version import Version

from pip._internal.exceptions import NoneMetadataError
from pip._internal.locations import site_packages, user_site
Expand All @@ -41,8 +41,6 @@

from ._json import msg_to_json

DistributionVersion = Union[LegacyVersion, Version]

InfoPath = Union[str, pathlib.PurePath]

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -274,7 +272,7 @@ def canonical_name(self) -> NormalizedName:
raise NotImplementedError()

@property
def version(self) -> DistributionVersion:
def version(self) -> Version:
raise NotImplementedError()

@property
Expand Down
4 changes: 2 additions & 2 deletions src/pip/_internal/metadata/importlib/_dists.py
Expand Up @@ -16,13 +16,13 @@

from pip._vendor.packaging.requirements import Requirement
from pip._vendor.packaging.utils import NormalizedName, canonicalize_name
from pip._vendor.packaging.version import Version
from pip._vendor.packaging.version import parse as parse_version

from pip._internal.exceptions import InvalidWheel, UnsupportedWheel
from pip._internal.metadata.base import (
BaseDistribution,
BaseEntryPoint,
DistributionVersion,
InfoPath,
Wheel,
)
Expand Down Expand Up @@ -171,7 +171,7 @@ def canonical_name(self) -> NormalizedName:
return canonicalize_name(name)

@property
def version(self) -> DistributionVersion:
def version(self) -> Version:
return parse_version(self._dist.version)

def is_file(self, path: InfoPath) -> bool:
Expand Down
4 changes: 2 additions & 2 deletions src/pip/_internal/metadata/pkg_resources.py
Expand Up @@ -8,6 +8,7 @@
from pip._vendor import pkg_resources
from pip._vendor.packaging.requirements import Requirement
from pip._vendor.packaging.utils import NormalizedName, canonicalize_name
from pip._vendor.packaging.version import Version
from pip._vendor.packaging.version import parse as parse_version

from pip._internal.exceptions import InvalidWheel, NoneMetadataError, UnsupportedWheel
Expand All @@ -19,7 +20,6 @@
BaseDistribution,
BaseEntryPoint,
BaseEnvironment,
DistributionVersion,
InfoPath,
Wheel,
)
Expand Down Expand Up @@ -168,7 +168,7 @@ def canonical_name(self) -> NormalizedName:
return canonicalize_name(self._dist.project_name)

@property
def version(self) -> DistributionVersion:
def version(self) -> Version:
return parse_version(self._dist.version)

def is_file(self, path: InfoPath) -> bool:
Expand Down
44 changes: 3 additions & 41 deletions src/pip/_internal/operations/check.py
Expand Up @@ -5,28 +5,25 @@
from typing import Callable, Dict, List, NamedTuple, Optional, Set, Tuple

from pip._vendor.packaging.requirements import Requirement
from pip._vendor.packaging.specifiers import LegacySpecifier
from pip._vendor.packaging.utils import NormalizedName, canonicalize_name
from pip._vendor.packaging.version import LegacyVersion
from pip._vendor.packaging.version import Version

from pip._internal.distributions import make_distribution_for_install_requirement
from pip._internal.metadata import get_default_environment
from pip._internal.metadata.base import DistributionVersion
from pip._internal.req.req_install import InstallRequirement
from pip._internal.utils.deprecation import deprecated

logger = logging.getLogger(__name__)


class PackageDetails(NamedTuple):
version: DistributionVersion
version: Version
dependencies: List[Requirement]


# Shorthands
PackageSet = Dict[NormalizedName, PackageDetails]
Missing = Tuple[NormalizedName, Requirement]
Conflicting = Tuple[NormalizedName, DistributionVersion, Requirement]
Conflicting = Tuple[NormalizedName, Version, Requirement]

MissingDict = Dict[NormalizedName, List[Missing]]
ConflictingDict = Dict[NormalizedName, List[Conflicting]]
Expand Down Expand Up @@ -60,8 +57,6 @@ def check_package_set(
package name and returns a boolean.
"""

warn_legacy_versions_and_specifiers(package_set)

missing = {}
conflicting = {}

Expand Down Expand Up @@ -152,36 +147,3 @@ def _create_whitelist(
break

return packages_affected


def warn_legacy_versions_and_specifiers(package_set: PackageSet) -> None:
for project_name, package_details in package_set.items():
if isinstance(package_details.version, LegacyVersion):
deprecated(
reason=(
f"{project_name} {package_details.version} "
f"has a non-standard version number."
),
replacement=(
f"to upgrade to a newer version of {project_name} "
f"or contact the author to suggest that they "
f"release a version with a conforming version number"
),
issue=12063,
gone_in="24.1",
)
for dep in package_details.dependencies:
if any(isinstance(spec, LegacySpecifier) for spec in dep.specifier):
deprecated(
reason=(
f"{project_name} {package_details.version} "
f"has a non-standard dependency specifier {dep}."
),
replacement=(
f"to upgrade to a newer version of {project_name} "
f"or contact the author to suggest that they "
f"release a version with a conforming dependency specifiers"
),
issue=12063,
gone_in="24.1",
)
37 changes: 0 additions & 37 deletions src/pip/_internal/req/req_set.py
Expand Up @@ -2,12 +2,9 @@
from collections import OrderedDict
from typing import Dict, List

from pip._vendor.packaging.specifiers import LegacySpecifier
from pip._vendor.packaging.utils import canonicalize_name
from pip._vendor.packaging.version import LegacyVersion

from pip._internal.req.req_install import InstallRequirement
from pip._internal.utils.deprecation import deprecated

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -83,37 +80,3 @@ def requirements_to_install(self) -> List[InstallRequirement]:
for install_req in self.all_requirements
if not install_req.constraint and not install_req.satisfied_by
]

def warn_legacy_versions_and_specifiers(self) -> None:
for req in self.requirements_to_install:
version = req.get_dist().version
if isinstance(version, LegacyVersion):
deprecated(
reason=(
f"pip has selected the non standard version {version} "
f"of {req}. In the future this version will be "
f"ignored as it isn't standard compliant."
),
replacement=(
"set or update constraints to select another version "
"or contact the package author to fix the version number"
),
issue=12063,
gone_in="24.1",
)
for dep in req.get_dist().iter_dependencies():
if any(isinstance(spec, LegacySpecifier) for spec in dep.specifier):
deprecated(
reason=(
f"pip has selected {req} {version} which has non "
f"standard dependency specifier {dep}. "
f"In the future this version of {req} will be "
f"ignored as it isn't standard compliant."
),
replacement=(
"set or update constraints to select another version "
"or contact the package author to fix the version number"
),
issue=12063,
gone_in="24.1",
)
7 changes: 3 additions & 4 deletions src/pip/_internal/resolution/resolvelib/base.py
@@ -1,16 +1,15 @@
from dataclasses import dataclass
from typing import FrozenSet, Iterable, Optional, Tuple, Union
from typing import FrozenSet, Iterable, Optional, Tuple

from pip._vendor.packaging.specifiers import SpecifierSet
from pip._vendor.packaging.utils import NormalizedName
from pip._vendor.packaging.version import LegacyVersion, Version
from pip._vendor.packaging.version import Version

from pip._internal.models.link import Link, links_equivalent
from pip._internal.req.req_install import InstallRequirement
from pip._internal.utils.hashes import Hashes

CandidateLookup = Tuple[Optional["Candidate"], Optional[InstallRequirement]]
CandidateVersion = Union[LegacyVersion, Version]


def format_name(project: NormalizedName, extras: FrozenSet[NormalizedName]) -> str:
Expand Down Expand Up @@ -115,7 +114,7 @@ def name(self) -> str:
raise NotImplementedError("Override in subclass")

@property
def version(self) -> CandidateVersion:
def version(self) -> Version:
raise NotImplementedError("Override in subclass")

@property
Expand Down
16 changes: 8 additions & 8 deletions src/pip/_internal/resolution/resolvelib/candidates.py
Expand Up @@ -21,7 +21,7 @@
from pip._internal.utils.direct_url_helpers import direct_url_from_link
from pip._internal.utils.misc import normalize_version_info

from .base import Candidate, CandidateVersion, Requirement, format_name
from .base import Candidate, Requirement, format_name

if TYPE_CHECKING:
from .factory import Factory
Expand Down Expand Up @@ -145,7 +145,7 @@ def __init__(
ireq: InstallRequirement,
factory: "Factory",
name: Optional[NormalizedName] = None,
version: Optional[CandidateVersion] = None,
version: Optional[Version] = None,
) -> None:
self._link = link
self._source_link = source_link
Expand Down Expand Up @@ -190,7 +190,7 @@ def name(self) -> str:
return self.project_name

@property
def version(self) -> CandidateVersion:
def version(self) -> Version:
if self._version is None:
self._version = self.dist.version
return self._version
Expand Down Expand Up @@ -257,7 +257,7 @@ def __init__(
template: InstallRequirement,
factory: "Factory",
name: Optional[NormalizedName] = None,
version: Optional[CandidateVersion] = None,
version: Optional[Version] = None,
) -> None:
source_link = link
cache_entry = factory.get_wheel_cache_entry(source_link, name)
Expand Down Expand Up @@ -314,7 +314,7 @@ def __init__(
template: InstallRequirement,
factory: "Factory",
name: Optional[NormalizedName] = None,
version: Optional[CandidateVersion] = None,
version: Optional[Version] = None,
) -> None:
super().__init__(
link=link,
Expand Down Expand Up @@ -374,7 +374,7 @@ def name(self) -> str:
return self.project_name

@property
def version(self) -> CandidateVersion:
def version(self) -> Version:
if self._version is None:
self._version = self.dist.version
return self._version
Expand Down Expand Up @@ -473,7 +473,7 @@ def name(self) -> str:
return format_name(self.base.project_name, self.extras)

@property
def version(self) -> CandidateVersion:
def version(self) -> Version:
return self.base.version

def format_for_error(self) -> str:
Expand Down Expand Up @@ -588,7 +588,7 @@ def name(self) -> str:
return REQUIRES_PYTHON_IDENTIFIER

@property
def version(self) -> CandidateVersion:
def version(self) -> Version:
return self._version

def format_for_error(self) -> str:
Expand Down

0 comments on commit 47a8480

Please sign in to comment.