8000 meta: add SimplePath protocol for PathDistribution by FFY00 · Pull Request #315 · python/importlib_metadata · GitHub
[go: up one dir, main page]

Skip to content
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
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ v4.1.0

* #312: Add support for metadata 2.2 (``Dynamic`` field).

* #315: Add ``SimplePath`` protocol for interface clarity
in ``PathDistribution``.

v4.0.1
=======

Expand Down
9 changes: 4 additions & 5 deletions importlib_metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
)
from ._functools import method_cache
from ._itertools import unique_everseen
from ._meta import PackageMetadata
from ._meta import PackageMetadata, SimplePath

from contextlib import suppress
from importlib import import_module
Expand Down Expand Up @@ -783,11 +783,10 @@ def invalidate_caches(cls):


class PathDistribution(Distribution):
def __init__(self, path):
"""Construct a distribution from a path to the metadata directory.
def __init__(self, path: SimplePath):
"""Construct a distribution.

:param path: A pathlib.Path or similar object supporting
.joinpath(), __div__, .parent, and .read_text().
:param path: SimplePath indicating the metadata directory.
"""
self._path = path

Expand Down
18 changes: 18 additions & 0 deletions importlib_metadata/_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,21 @@ def json(self) -> Dict[str, Union[str, List[str]]]:
"""
A JSON-compatible form of the metadata.
"""


class SimplePath(Protocol):
"""
A minimal subset of pathlib.Path required by PathDistribution.
"""

def joinpath(self) -> 'SimplePath':
... # pragma: no cover

def __div__(self) -> 'SimplePath':
... # pragma: no cover

def parent(self) -> 'SimplePath':
... # pragma: no cover

def read_text(self) -> str:
... # pragma: no cover
0