From acb40b621fcb5ca243a04e6fa7b4955967ab0889 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 3 Apr 2022 12:05:05 -0400 Subject: [PATCH 1/3] Update Traversable docs to clarify behavior on joinpath and expectations on exceptions. Update joinpath to accept multiple arguments. Updated simple.ResourceContainer to match the protocol. --- importlib_resources/abc.py | 11 +++++++++-- importlib_resources/simple.py | 7 +++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/importlib_resources/abc.py b/importlib_resources/abc.py index a2b0af62..da69af4f 100644 --- a/importlib_resources/abc.py +++ b/importlib_resources/abc.py @@ -55,6 +55,9 @@ class Traversable(Protocol): """ An object with a subset of pathlib.Path methods suitable for traversing directories and opening files. + + Any exceptions that occur when accessing the backing resource + may propagate unaltered. """ @abc.abstractmethod @@ -90,9 +93,13 @@ def is_file(self) -> bool: """ @abc.abstractmethod - def joinpath(self, child: StrPath) -> "Traversable": + def joinpath(self, *descendants: StrPath) -> "Traversable": """ - Return Traversable child in self + Return Traversable resolved with any descendants applied. + + Each descendant should be a path segment relative to self + and main contain multiple levels separated by + `posixpath.sep` (``/``). """ def __truediv__(self, child: StrPath) -> "Traversable": diff --git a/importlib_resources/simple.py b/importlib_resources/simple.py index da073cbd..1f6e43a2 100644 --- a/importlib_resources/simple.py +++ b/importlib_resources/simple.py @@ -99,10 +99,13 @@ def iterdir(self): def open(self, *args, **kwargs): raise IsADirectoryError() - def joinpath(self, name): + def joinpath(self, *names): + if not names: + return self + name, rest = names[0], names[1:] return next( traversable for traversable in self.iterdir() if traversable.name == name - ) + ).joinpath(*rest) class TraversableReader(TraversableResources, SimpleReader): From 85840e266b07dea17e439f6f3e148e12b804b0d9 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Fri, 8 Apr 2022 14:11:13 -0400 Subject: [PATCH 2/3] Ignore coverage for simple.py --- .coveragerc | 1 + 1 file changed, 1 insertion(+) diff --git a/.coveragerc b/.coveragerc index b3df18a1..3b2d64ce 100644 --- a/.coveragerc +++ b/.coveragerc @@ -4,6 +4,7 @@ omit = */.tox/* */_itertools.py */_legacy.py + */simple.py [report] show_missing = True From ab11f0117a17f07963a9c705e0e3efed40ba413a Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Wed, 13 Apr 2022 08:26:49 -0400 Subject: [PATCH 3/3] Correct typo and clarify 'each' in joinpath docstring. --- importlib_resources/abc.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/importlib_resources/abc.py b/importlib_resources/abc.py index da69af4f..5e38ba61 100644 --- a/importlib_resources/abc.py +++ b/importlib_resources/abc.py @@ -98,8 +98,8 @@ def joinpath(self, *descendants: StrPath) -> "Traversable": Return Traversable resolved with any descendants applied. Each descendant should be a path segment relative to self - and main contain multiple levels separated by - `posixpath.sep` (``/``). + and each may contain multiple levels separated by + ``posixpath.sep`` (``/``). """ def __truediv__(self, child: StrPath) -> "Traversable":