Skip to content

Commit b55ec00

Browse files
authored
Merge pull request #10410 from sbidoul/refactor-freeze-sbi
2 parents 58b1b73 + 6ac3ffe commit b55ec00

File tree

2 files changed

+20
-42
lines changed

2 files changed

+20
-42
lines changed

news/10410.feature.rst

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
``pip freeze`` will now always fallback to reporting the editable project
2+
location when it encounters a VCS error while analyzing an editable
3+
requirement. Before, it sometimes reported the requirement as non-editable.

src/pip/_internal/operations/freeze.py

+17-42
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
11
import collections
22
import logging
33
import os
4-
from typing import (
5-
Container,
6-
Dict,
7-
Iterable,
8-
Iterator,
9-
List,
10-
NamedTuple,
11-
Optional,
12-
Set,
13-
Union,
14-
)
4+
from typing import Container, Dict, Iterable, Iterator, List, NamedTuple, Optional, Set
155

16-
from pip._vendor.packaging.requirements import Requirement
176
from pip._vendor.packaging.utils import canonicalize_name
187
from pip._vendor.packaging.version import Version
198

@@ -30,8 +19,7 @@
3019

3120

3221
class _EditableInfo(NamedTuple):
33-
requirement: Optional[str]
34-
editable: bool
22+
requirement: str
3523
comments: List[str]
3624

3725

@@ -164,11 +152,9 @@ def _format_as_name_version(dist: BaseDistribution) -> str:
164152

165153
def _get_editable_info(dist: BaseDistribution) -> _EditableInfo:
166154
"""
167-
Compute and return values (req, editable, comments) for use in
155+
Compute and return values (req, comments) for use in
168156
FrozenRequirement.from_dist().
169157
"""
170-
if not dist.editable:
171-
return _EditableInfo(requirement=None, editable=False, comments=[])
172158
editable_project_location = dist.editable_project_location
173159
assert editable_project_location
174160
location = os.path.normcase(os.path.abspath(editable_project_location))
@@ -186,7 +172,6 @@ def _get_editable_info(dist: BaseDistribution) -> _EditableInfo:
186172
)
187173
return _EditableInfo(
188174
requirement=location,
189-
editable=True,
190175
comments=[f"# Editable install with no version control ({display})"],
191176
)
192177

@@ -198,44 +183,35 @@ def _get_editable_info(dist: BaseDistribution) -> _EditableInfo:
198183
display = _format_as_name_version(dist)
199184
return _EditableInfo(
200185
requirement=location,
201-
editable=True,
202186
comments=[f"# Editable {vcs_name} install with no remote ({display})"],
203187
)
204188
except RemoteNotValidError as ex:
205189
display = _format_as_name_version(dist)
206190
return _EditableInfo(
207191
requirement=location,
208-
editable=True,
209192
comments=[
210193
f"# Editable {vcs_name} install ({display}) with either a deleted "
211194
f"local remote or invalid URI:",
212195
f"# '{ex.url}'",
213196
],
214197
)
215-
216198
except BadCommand:
217199
logger.warning(
218200
"cannot determine version of editable source in %s "
219201
"(%s command not found in path)",
220202
location,
221203
vcs_backend.name,
222204
)
223-
return _EditableInfo(requirement=None, editable=True, comments=[])
224-
205+
return _EditableInfo(requirement=location, comments=[])
225206
except InstallationError as exc:
226-
logger.warning(
227-
"Error when trying to get requirement for VCS system %s, "
228-
"falling back to uneditable format",
229-
exc,
230-
)
207+
logger.warning("Error when trying to get requirement for VCS system %s", exc)
231208
else:
232-
return _EditableInfo(requirement=req, editable=True, comments=[])
209+
return _EditableInfo(requirement=req, comments=[])
233210

234211
logger.warning("Could not determine repository location of %s", location)
235212

236213
return _EditableInfo(
237-
requirement=None,
238-
editable=False,
214+
requirement=location,
239215
comments=["## !! Could not determine repository location"],
240216
)
241217

@@ -244,7 +220,7 @@ class FrozenRequirement:
244220
def __init__(
245221
self,
246222
name: str,
247-
req: Union[str, Requirement],
223+
req: str,
248224
editable: bool,
249225
comments: Iterable[str] = (),
250226
) -> None:
@@ -256,19 +232,18 @@ def __init__(
256232

257233
@classmethod
258234
def from_dist(cls, dist: BaseDistribution) -> "FrozenRequirement":
259-
# TODO `get_requirement_info` is taking care of editable requirements.
260-
# TODO This should be refactored when we will add detection of
261-
# editable that provide .dist-info metadata.
262-
req, editable, comments = _get_editable_info(dist)
263-
if req is None and not editable:
264-
# if PEP 610 metadata is present, attempt to use it
235+
editable = dist.editable
236+
if editable:
237+
req, comments = _get_editable_info(dist)
238+
else:
239+
comments = []
265240
direct_url = dist.direct_url
266241
if direct_url:
242+
# if PEP 610 metadata is present, use it
267243
req = direct_url_as_pep440_direct_reference(direct_url, dist.raw_name)
268-
comments = []
269-
if req is None:
270-
# name==version requirement
271-
req = _format_as_name_version(dist)
244+
else:
245+
# name==version requirement
246+
req = _format_as_name_version(dist)
272247

273248
return cls(dist.raw_name, req, editable, comments=comments)
274249

0 commit comments

Comments
 (0)