From 1b3f272d7ce6b25ec5dd68d54d1a140abd159a3f Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 16 Dec 2023 12:36:06 -0500 Subject: [PATCH] Corrected the interface for SimplePath to encompass the expectations of locate_file and PackagePath. --- importlib_metadata/__init__.py | 8 ++++---- importlib_metadata/_meta.py | 10 ++++++++-- newsfragments/+b15724f6.bugfix.rst | 1 + 3 files changed, 13 insertions(+), 6 deletions(-) create mode 100644 newsfragments/+b15724f6.bugfix.rst diff --git a/importlib_metadata/__init__.py b/importlib_metadata/__init__.py index 748f5e99..6108dbc3 100644 --- a/importlib_metadata/__init__.py +++ b/importlib_metadata/__init__.py @@ -320,7 +320,7 @@ def read_text(self, encoding: str = 'utf-8') -> str: # type: ignore[override] def read_binary(self) -> bytes: return self.locate().read_bytes() - def locate(self) -> pathlib.Path: + def locate(self) -> SimplePath: """Return a path-like object for this path""" return self.dist.locate_file(self) @@ -387,9 +387,9 @@ def read_text(self, filename) -> Optional[str]: """ @abc.abstractmethod - def locate_file(self, path: StrPath) -> pathlib.Path: + def locate_file(self, path: StrPath) -> SimplePath: """ - Given a path to a file in this distribution, return a path + Given a path to a file in this distribution, return a SimplePath to it. """ @@ -854,7 +854,7 @@ def read_text(self, filename: StrPath) -> Optional[str]: read_text.__doc__ = Distribution.read_text.__doc__ - def locate_file(self, path: StrPath) -> pathlib.Path: + def locate_file(self, path: StrPath) -> SimplePath: return self._path.parent / path @property diff --git a/importlib_metadata/_meta.py b/importlib_metadata/_meta.py index f670016d..be30c15e 100644 --- a/importlib_metadata/_meta.py +++ b/importlib_metadata/_meta.py @@ -46,7 +46,7 @@ def json(self) -> Dict[str, Union[str, List[str]]]: class SimplePath(Protocol[_T]): """ - A minimal subset of pathlib.Path required by PathDistribution. + A minimal subset of pathlib.Path required by Distribution. """ def joinpath(self, other: Union[str, _T]) -> _T: @@ -59,5 +59,11 @@ def __truediv__(self, other: Union[str, _T]) -> _T: def parent(self) -> _T: ... # pragma: no cover - def read_text(self) -> str: + def read_text(self, encoding=None) -> str: + ... # pragma: no cover + + def read_bytes(self) -> bytes: + ... # pragma: no cover + + def exists(self) -> bool: ... # pragma: no cover diff --git a/newsfragments/+b15724f6.bugfix.rst b/newsfragments/+b15724f6.bugfix.rst new file mode 100644 index 00000000..807be3ab --- /dev/null +++ b/newsfragments/+b15724f6.bugfix.rst @@ -0,0 +1 @@ +Corrected the interface for SimplePath to encompass the expectations of locate_file and PackagePath. \ No newline at end of file