From fa20a0a330175502b61de89e5dd8e10f3f77ede7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filipe=20La=C3=ADns?= Date: Mon, 24 May 2021 15:51:44 +0100 Subject: [PATCH 1/3] meta: add Path protocol for PathDistribution MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is needed to properly annotate code that uses PathDistribution, otherwise I need to keep redefining the protocol in my code. Signed-off-by: Filipe Laíns --- importlib_metadata/__init__.py | 4 ++-- importlib_metadata/_meta.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/importlib_metadata/__init__.py b/importlib_metadata/__init__.py index 2259fe1c..d1642d70 100644 --- a/importlib_metadata/__init__.py +++ b/importlib_metadata/__init__.py @@ -15,7 +15,7 @@ import collections from . import _adapters, _meta -from ._meta import PackageMetadata +from ._meta import PackageMetadata, Path from ._collections import FreezableDefaultDict, Pair from ._compat import ( NullFinder, @@ -783,7 +783,7 @@ def invalidate_caches(cls): class PathDistribution(Distribution): - def __init__(self, path): + def __init__(self, path: Path): """Construct a distribution from a path to the metadata directory. :param path: A pathlib.Path or similar object supporting diff --git a/importlib_metadata/_meta.py b/importlib_metadata/_meta.py index 5cb690fe..f7763722 100644 --- a/importlib_metadata/_meta.py +++ b/importlib_metadata/_meta.py @@ -28,3 +28,17 @@ def json(self) -> Dict[str, Union[str, List[str]]]: """ A JSON-compatible form of the metadata. """ + + +class Path(Protocol): + def joinpath(self) -> 'Path': + ... # pragma: no cover + + def __div__(self) -> 'Path': + ... # pragma: no cover + + def parent(self) -> 'Path': + ... # pragma: no cover + + def read_text(self) -> str: + ... # pragma: no cover From 38350dcb3d416f7bb31de6424e118f8cb79021c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filipe=20La=C3=ADns?= Date: Mon, 24 May 2021 16:07:55 +0100 Subject: [PATCH 2/3] importlib_metadata: fix flake8 error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Filipe Laíns --- importlib_metadata/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/importlib_metadata/__init__.py b/importlib_metadata/__init__.py index d1642d70..5bbe9d39 100644 --- a/importlib_metadata/__init__.py +++ b/importlib_metadata/__init__.py @@ -15,7 +15,6 @@ import collections from . import _adapters, _meta -from ._meta import PackageMetadata, Path from ._collections import FreezableDefaultDict, Pair from ._compat import ( NullFinder, @@ -24,6 +23,7 @@ ) from ._functools import method_cache from ._itertools import unique_everseen +from ._meta import PackageMetadata, Path from contextlib import suppress from importlib import import_module From e27d0792ff10a40b0bcce81365dbb1269769bd96 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Wed, 26 May 2021 12:18:12 -0400 Subject: [PATCH 3/3] Move docstrings about interface to the protocol. Rename to SimplePath to easily distinguish. Add changelog. --- CHANGES.rst | 6 ++++++ importlib_metadata/__init__.py | 9 ++++----- importlib_metadata/_meta.py | 12 ++++++++---- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index a9ef4870..80284daf 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,3 +1,9 @@ +v4.1.0 +======= + +* #315: Add ``SimplePath`` protocol for interface clarity + in ``PathDistribution``. + v4.0.1 ======= diff --git a/importlib_metadata/__init__.py b/importlib_metadata/__init__.py index 5bbe9d39..ed42bd04 100644 --- a/importlib_metadata/__init__.py +++ b/importlib_metadata/__init__.py @@ -23,7 +23,7 @@ ) from ._functools import method_cache from ._itertools import unique_everseen -from ._meta import PackageMetadata, Path +from ._meta import PackageMetadata, SimplePath from contextlib import suppress from importlib import import_module @@ -783,11 +783,10 @@ def invalidate_caches(cls): class PathDistribution(Distribution): - def __init__(self, path: 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 diff --git a/importlib_metadata/_meta.py b/importlib_metadata/_meta.py index f7763722..dd68c429 100644 --- a/importlib_metadata/_meta.py +++ b/importlib_metadata/_meta.py @@ -30,14 +30,18 @@ def json(self) -> Dict[str, Union[str, List[str]]]: """ -class Path(Protocol): - def joinpath(self) -> 'Path': +class SimplePath(Protocol): + """ + A minimal subset of pathlib.Path required by PathDistribution. + """ + + def joinpath(self) -> 'SimplePath': ... # pragma: no cover - def __div__(self) -> 'Path': + def __div__(self) -> 'SimplePath': ... # pragma: no cover - def parent(self) -> 'Path': + def parent(self) -> 'SimplePath': ... # pragma: no cover def read_text(self) -> str: