Skip to content

Fallback to a placeholder if a distribution's location is None #11710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions news/11704.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix an issue when an already existing in-memory distribution would cause
exceptions in ``pip install``
6 changes: 5 additions & 1 deletion src/pip/_internal/req/req_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,11 @@ def __str__(self) -> str:
else:
s = "<InstallRequirement>"
if self.satisfied_by is not None:
s += " in {}".format(display_path(self.satisfied_by.location))
if self.satisfied_by.location is not None:
location = display_path(self.satisfied_by.location)
else:
location = "<memory>"
s += f" in {location}"
if self.comes_from:
if isinstance(self.comes_from, str):
comes_from: Optional[str] = self.comes_from
Expand Down
42 changes: 42 additions & 0 deletions tests/functional/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -2351,3 +2351,45 @@ def test_install_8559_wheel_package_present(
allow_stderr_warning=False,
)
assert DEPRECATION_MSG_PREFIX not in result.stderr


@pytest.mark.skipif(
sys.version_info < (3, 11),
reason="3.11 required to find distributions via importlib metadata",
)
def test_install_existing_memory_distribution(script: PipTestEnvironment) -> None:
sitecustomize_text = textwrap.dedent(
"""
import sys
from importlib.metadata import Distribution, DistributionFinder


EXAMPLE_METADATA = '''Metadata-Version: 2.1
Name: example
Version: 1.0.0

'''

class ExampleDistribution(Distribution):
def locate_file(self, path):
return path

def read_text(self, filename):
if filename == 'METADATA':
return EXAMPLE_METADATA


class CustomFinder(DistributionFinder):
def find_distributions(self, context=None):
return [ExampleDistribution()]


sys.meta_path.append(CustomFinder())
"""
)
with open(script.site_packages_path / "sitecustomize.py", "w") as sitecustomize:
sitecustomize.write(sitecustomize_text)

result = script.pip("install", "example")

assert "Requirement already satisfied: example in <memory>" in result.stdout