-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Metadata backend with importlib.metadata #10709
Changes from all commits
f13fa41
d72356d
60a7ad3
a9cf547
e3952f8
846d8e5
e371bf2
a640a24
675de98
4fd0e09
b0ec2c0
1d22560
1544a31
17355f8
4c096b7
5e9c5ad
0f085e1
41de887
173ef62
321c967
eea84b3
8073f65
a55f0dd
b894081
f9e554c
ee81f71
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Start migration of distribution metadata implementation from ``pkg_resources`` | ||
to ``importlib.metadata``. The new implementation is currently not exposed in | ||
any user-facing way, but included in the code base for easier development. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from ._dists import Distribution | ||
from ._envs import Environment | ||
|
||
__all__ = ["Distribution", "Environment"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import importlib.metadata | ||
from typing import Any, Optional, Protocol, cast | ||
|
||
|
||
class BasePath(Protocol): | ||
"""A protocol that various path objects conform. | ||
|
||
This exists because importlib.metadata uses both ``pathlib.Path`` and | ||
``zipfile.Path``, and we need a common base for type hints (Union does not | ||
work well since ``zipfile.Path`` is too new for our linter setup). | ||
|
||
This does not mean to be exhaustive, but only contains things that present | ||
in both classes *that we need*. | ||
""" | ||
|
||
name: str | ||
|
||
@property | ||
def parent(self) -> "BasePath": | ||
raise NotImplementedError() | ||
|
||
|
||
def get_info_location(d: importlib.metadata.Distribution) -> Optional[BasePath]: | ||
"""Find the path to the distribution's metadata directory. | ||
|
||
HACK: This relies on importlib.metadata's private ``_path`` attribute. Not | ||
all distributions exist on disk, so importlib.metadata is correct to not | ||
expose the attribute as public. But pip's code base is old and not as clean, | ||
so we do this to avoid having to rewrite too many things. Hopefully we can | ||
eliminate this some day. | ||
""" | ||
return getattr(d, "_path", None) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add a warning message here, for when this doesn't get a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function does expect non- There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. /cc @jaraco @warsaw @brettcannon for awareness about this hack. Personally, I'm fine with this -- I don't imagine there being too many code changes in importlib-metadata, in the 3.11+ standard library. If there are, hopefully, we'll be able to deal with them in an expedient manner on pip's side. :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer that pip determine why it cares about the "location" of the metadata and ask what interfaces would be appropriate for a package not on the file system to satisfy those needs. But for now, this hack is probably acceptable. Out of curiousity, what breaks if a Distribution doesn't have a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Distributions without |
||
|
||
|
||
def get_dist_name(dist: importlib.metadata.Distribution) -> str: | ||
"""Get the distribution's project name. | ||
|
||
The ``name`` attribute is only available in Python 3.10 or later. We are | ||
targeting exactly that, but Mypy does not know this. | ||
""" | ||
return cast(Any, dist).name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a note for the future -- this is intended to be a temporary measure. We'll swap this out for a more "sane" selection logic in the future.