Skip to content

Commit

Permalink
Ensure canonicalization of extras when creating ireqs
Browse files Browse the repository at this point in the history
_compat now exports install_req_from_line and canonicalize_ireq
  • Loading branch information
AndydeCleyre committed Nov 2, 2023
1 parent 58e68ed commit 5db5631
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 10 deletions.
4 changes: 4 additions & 0 deletions piptools/_compat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
from .pip_compat import (
PIP_VERSION,
Distribution,
canonicalize_ireq,
create_wheel_cache,
get_dev_pkgs,
install_req_from_line,
parse_requirements,
)

__all__ = [
"PIP_VERSION",
"Distribution",
"parse_requirements",
"install_req_from_line",
"create_wheel_cache",
"get_dev_pkgs",
"canonicalize_ireq",
]
23 changes: 21 additions & 2 deletions piptools/_compat/pip_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import optparse
from dataclasses import dataclass
from typing import TYPE_CHECKING, Iterable, Iterator, Set, cast
from typing import TYPE_CHECKING, Any, Iterable, Iterator, Set, cast

import pip
from pip._internal.cache import WheelCache
Expand All @@ -13,7 +13,11 @@
from pip._internal.network.session import PipSession
from pip._internal.req import InstallRequirement
from pip._internal.req import parse_requirements as _parse_requirements
from pip._internal.req.constructors import (
install_req_from_line as _install_req_from_line,
)
from pip._internal.req.constructors import install_req_from_parsed_requirement
from pip._vendor.packaging.utils import canonicalize_name
from pip._vendor.packaging.version import parse as parse_version
from pip._vendor.pkg_resources import Requirement

Expand Down Expand Up @@ -63,6 +67,19 @@ def _from_importlib(cls, dist: _ImportLibDist) -> Distribution:
return cls(dist._dist.name, dist._dist.version, requires, dist.direct_url)


def canonicalize_ireq(ireq: InstallRequirement) -> None:
if hasattr(ireq.req, "extras") and ireq.req.extras:
ireq.req.extras = set(map(canonicalize_name, ireq.req.extras))
if hasattr(ireq, "extras") and ireq.extras:
ireq.extras = set(map(canonicalize_name, ireq.extras))


def install_req_from_line(*args: Any, **kwargs: Any) -> InstallRequirement:
ireq = _install_req_from_line(*args, **kwargs)
canonicalize_ireq(ireq)
return ireq


def parse_requirements(
filename: str,
session: PipSession,
Expand All @@ -74,7 +91,9 @@ def parse_requirements(
for parsed_req in _parse_requirements(
filename, session, finder=finder, options=options, constraint=constraint
):
yield install_req_from_parsed_requirement(parsed_req, isolated=isolated)
ireq = install_req_from_parsed_requirement(parsed_req, isolated=isolated)
canonicalize_ireq(ireq)
yield ireq


def create_wheel_cache(cache_dir: str, format_control: str | None = None) -> WheelCache:
Expand Down
8 changes: 6 additions & 2 deletions piptools/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import build.env
import pyproject_hooks
from pip._internal.req import InstallRequirement
from pip._internal.req.constructors import install_req_from_line, parse_req_from_line
from pip._internal.req.constructors import parse_req_from_line

from ._compat import canonicalize_ireq, install_req_from_line

PYPROJECT_TOML = "pyproject.toml"

Expand Down Expand Up @@ -139,13 +141,15 @@ def _prepare_requirements(
replaced_package_name = req.replace(package_name, str(package_dir), 1)
parts = parse_req_from_line(replaced_package_name, comes_from)

yield InstallRequirement(
ireq = InstallRequirement(
parts.requirement,
comes_from,
link=parts.link,
markers=parts.markers,
extras=parts.extras,
)
canonicalize_ireq(ireq)
yield ireq


def _prepare_build_requirements(
Expand Down
3 changes: 1 addition & 2 deletions piptools/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
update_env_context_manager,
)
from pip._internal.req import InstallRequirement
from pip._internal.req.constructors import install_req_from_line
from pip._internal.resolution.resolvelib.base import Candidate
from pip._internal.resolution.resolvelib.candidates import ExtrasCandidate
from pip._internal.resolution.resolvelib.resolver import Resolver
Expand All @@ -27,7 +26,7 @@
from piptools.cache import DependencyCache
from piptools.repositories.base import BaseRepository

from ._compat import create_wheel_cache
from ._compat import create_wheel_cache, install_req_from_line
from .exceptions import PipToolsError
from .logging import log
from .utils import (
Expand Down
3 changes: 1 addition & 2 deletions piptools/scripts/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
from build import BuildBackendException
from click.utils import LazyFile, safecall
from pip._internal.req import InstallRequirement
from pip._internal.req.constructors import install_req_from_line
from pip._internal.utils.misc import redact_auth_from_url

from .._compat import parse_requirements
from .._compat import install_req_from_line, parse_requirements
from ..build import build_project_metadata
from ..cache import DependencyCache
from ..exceptions import NoCandidateFound, PipToolsError
Expand Down
3 changes: 1 addition & 2 deletions piptools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import click
from click.utils import LazyFile
from pip._internal.req import InstallRequirement
from pip._internal.req.constructors import install_req_from_line
from pip._internal.resolution.resolvelib.base import Requirement as PipRequirement
from pip._internal.utils.misc import redact_auth_from_url
from pip._internal.vcs import is_url
Expand All @@ -33,7 +32,7 @@
from pip._vendor.packaging.version import Version
from pip._vendor.pkg_resources import get_distribution

from piptools._compat import PIP_VERSION
from piptools._compat import PIP_VERSION, install_req_from_line
from piptools.locations import DEFAULT_CONFIG_FILE_NAMES
from piptools.subprocess_utils import run_python_snippet

Expand Down

0 comments on commit 5db5631

Please sign in to comment.