Skip to content

Commit 498f313

Browse files
committed
Remove pkg_resources usages from req_install
1 parent cd01e4f commit 498f313

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/pip/_internal/req/req_install.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import zipfile
1111
from typing import Any, Collection, Dict, Iterable, List, Optional, Sequence, Union
1212

13-
from pip._vendor import pkg_resources
1413
from pip._vendor.packaging.markers import Marker
1514
from pip._vendor.packaging.requirements import Requirement
1615
from pip._vendor.packaging.specifiers import SpecifierSet
@@ -54,6 +53,7 @@
5453
hide_url,
5554
redact_auth_from_url,
5655
)
56+
from pip._internal.utils.packaging import safe_extra
5757
from pip._internal.utils.subprocess import runner_with_spinner_message
5858
from pip._internal.utils.temp_dir import TempDirectory, tempdir_kinds
5959
from pip._internal.utils.virtualenv import running_under_virtualenv
@@ -119,15 +119,14 @@ def __init__(
119119
if extras:
120120
self.extras = extras
121121
elif req:
122-
self.extras = {pkg_resources.safe_extra(extra) for extra in req.extras}
122+
self.extras = {safe_extra(extra) for extra in req.extras}
123123
else:
124124
self.extras = set()
125125
if markers is None and req:
126126
markers = req.marker
127127
self.markers = markers
128128

129-
# This holds the pkg_resources.Distribution object if this requirement
130-
# is already available:
129+
# This holds the Distribution object if this requirement is already installed.
131130
self.satisfied_by: Optional[BaseDistribution] = None
132131
# Whether the installation process should try to uninstall an existing
133132
# distribution before installing this requirement.
@@ -216,7 +215,7 @@ def format_debug(self) -> str:
216215
def name(self) -> Optional[str]:
217216
if self.req is None:
218217
return None
219-
return pkg_resources.safe_name(self.req.name)
218+
return self.req.name
220219

221220
@functools.lru_cache() # use cached_property in python 3.8+
222221
def supports_pyproject_editable(self) -> bool:

src/pip/_internal/utils/packaging.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import functools
22
import logging
3-
from typing import Optional, Tuple
3+
import re
4+
from typing import NewType, Optional, Tuple, cast
45

56
from pip._vendor.packaging import specifiers, version
67
from pip._vendor.packaging.requirements import Requirement
78

9+
NormalizedExtra = NewType("NormalizedExtra", str)
10+
811
logger = logging.getLogger(__name__)
912

1013

@@ -40,3 +43,15 @@ def get_requirement(req_string: str) -> Requirement:
4043
# minimize repeated parsing of the same string to construct equivalent
4144
# Requirement objects.
4245
return Requirement(req_string)
46+
47+
48+
def safe_extra(extra: str) -> NormalizedExtra:
49+
"""Convert an arbitrary string to a standard 'extra' name
50+
51+
Any runs of non-alphanumeric characters are replaced with a single '_',
52+
and the result is always lowercased.
53+
54+
This function is duplicated from ``pkg_resources``. Note that this is not
55+
the same to either ``canonicalize_name`` or ``_egg_link_name``.
56+
"""
57+
return cast(NormalizedExtra, re.sub("[^A-Za-z0-9.-]+", "_", extra).lower())

0 commit comments

Comments
 (0)