Skip to content

Commit f34d6ab

Browse files
authored
Merge pull request #11710 from daniil-konovalenko/fix/req_install-mypy
Resolves #11704
2 parents 07a360d + 5540331 commit f34d6ab

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

news/11704.bugfix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix an issue when an already existing in-memory distribution would cause
2+
exceptions in ``pip install``

src/pip/_internal/req/req_install.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,11 @@ def __str__(self) -> str:
195195
else:
196196
s = "<InstallRequirement>"
197197
if self.satisfied_by is not None:
198-
s += " in {}".format(display_path(self.satisfied_by.location))
198+
if self.satisfied_by.location is not None:
199+
location = display_path(self.satisfied_by.location)
200+
else:
201+
location = "<memory>"
202+
s += f" in {location}"
199203
if self.comes_from:
200204
if isinstance(self.comes_from, str):
201205
comes_from: Optional[str] = self.comes_from

tests/functional/test_install.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2351,3 +2351,45 @@ def test_install_8559_wheel_package_present(
23512351
allow_stderr_warning=False,
23522352
)
23532353
assert DEPRECATION_MSG_PREFIX not in result.stderr
2354+
2355+
2356+
@pytest.mark.skipif(
2357+
sys.version_info < (3, 11),
2358+
reason="3.11 required to find distributions via importlib metadata",
2359+
)
2360+
def test_install_existing_memory_distribution(script: PipTestEnvironment) -> None:
2361+
sitecustomize_text = textwrap.dedent(
2362+
"""
2363+
import sys
2364+
from importlib.metadata import Distribution, DistributionFinder
2365+
2366+
2367+
EXAMPLE_METADATA = '''Metadata-Version: 2.1
2368+
Name: example
2369+
Version: 1.0.0
2370+
2371+
'''
2372+
2373+
class ExampleDistribution(Distribution):
2374+
def locate_file(self, path):
2375+
return path
2376+
2377+
def read_text(self, filename):
2378+
if filename == 'METADATA':
2379+
return EXAMPLE_METADATA
2380+
2381+
2382+
class CustomFinder(DistributionFinder):
2383+
def find_distributions(self, context=None):
2384+
return [ExampleDistribution()]
2385+
2386+
2387+
sys.meta_path.append(CustomFinder())
2388+
"""
2389+
)
2390+
with open(script.site_packages_path / "sitecustomize.py", "w") as sitecustomize:
2391+
sitecustomize.write(sitecustomize_text)
2392+
2393+
result = script.pip("install", "example")
2394+
2395+
assert "Requirement already satisfied: example in <memory>" in result.stdout

0 commit comments

Comments
 (0)