Remove pkg_resources usages from req_install

This commit is contained in:
Tzu-ping Chung 2021-11-22 17:44:56 +08:00
parent cd01e4fd8f
commit 498f313853
2 changed files with 20 additions and 6 deletions

View File

@ -10,7 +10,6 @@ import uuid
import zipfile
from typing import Any, Collection, Dict, Iterable, List, Optional, Sequence, Union
from pip._vendor import pkg_resources
from pip._vendor.packaging.markers import Marker
from pip._vendor.packaging.requirements import Requirement
from pip._vendor.packaging.specifiers import SpecifierSet
@ -54,6 +53,7 @@ from pip._internal.utils.misc import (
hide_url,
redact_auth_from_url,
)
from pip._internal.utils.packaging import safe_extra
from pip._internal.utils.subprocess import runner_with_spinner_message
from pip._internal.utils.temp_dir import TempDirectory, tempdir_kinds
from pip._internal.utils.virtualenv import running_under_virtualenv
@ -119,15 +119,14 @@ class InstallRequirement:
if extras:
self.extras = extras
elif req:
self.extras = {pkg_resources.safe_extra(extra) for extra in req.extras}
self.extras = {safe_extra(extra) for extra in req.extras}
else:
self.extras = set()
if markers is None and req:
markers = req.marker
self.markers = markers
# This holds the pkg_resources.Distribution object if this requirement
# is already available:
# This holds the Distribution object if this requirement is already installed.
self.satisfied_by: Optional[BaseDistribution] = None
# Whether the installation process should try to uninstall an existing
# distribution before installing this requirement.
@ -216,7 +215,7 @@ class InstallRequirement:
def name(self) -> Optional[str]:
if self.req is None:
return None
return pkg_resources.safe_name(self.req.name)
return self.req.name
@functools.lru_cache() # use cached_property in python 3.8+
def supports_pyproject_editable(self) -> bool:

View File

@ -1,10 +1,13 @@
import functools
import logging
from typing import Optional, Tuple
import re
from typing import NewType, Optional, Tuple, cast
from pip._vendor.packaging import specifiers, version
from pip._vendor.packaging.requirements import Requirement
NormalizedExtra = NewType("NormalizedExtra", str)
logger = logging.getLogger(__name__)
@ -40,3 +43,15 @@ def get_requirement(req_string: str) -> Requirement:
# minimize repeated parsing of the same string to construct equivalent
# Requirement objects.
return Requirement(req_string)
def safe_extra(extra: str) -> NormalizedExtra:
"""Convert an arbitrary string to a standard 'extra' name
Any runs of non-alphanumeric characters are replaced with a single '_',
and the result is always lowercased.
This function is duplicated from ``pkg_resources``. Note that this is not
the same to either ``canonicalize_name`` or ``_egg_link_name``.
"""
return cast(NormalizedExtra, re.sub("[^A-Za-z0-9.-]+", "_", extra).lower())