Skip to content

Commit 21d703e

Browse files
authored
Merge pull request #248 from python/bpo-47142/refine-Traversable
bpo-47142: Refine Traversable.
2 parents 4bcd1e6 + ab11f01 commit 21d703e

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

.coveragerc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ omit =
44
*/.tox/*
55
*/_itertools.py
66
*/_legacy.py
7+
*/simple.py
78

89
[report]
910
show_missing = True

importlib_resources/abc.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ class Traversable(Protocol):
5555
"""
5656
An object with a subset of pathlib.Path methods suitable for
5757
traversing directories and opening files.
58+
59+
Any exceptions that occur when accessing the backing resource
60+
may propagate unaltered.
5861
"""
5962

6063
@abc.abstractmethod
@@ -90,9 +93,13 @@ def is_file(self) -> bool:
9093
"""
9194

9295
@abc.abstractmethod
93-
def joinpath(self, child: StrPath) -> "Traversable":
96+
def joinpath(self, *descendants: StrPath) -> "Traversable":
9497
"""
95-
Return Traversable child in self
98+
Return Traversable resolved with any descendants applied.
99+
100+
Each descendant should be a path segment relative to self
101+
and each may contain multiple levels separated by
102+
``posixpath.sep`` (``/``).
96103
"""
97104

98105
def __truediv__(self, child: StrPath) -> "Traversable":

importlib_resources/simple.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,13 @@ def iterdir(self):
9999
def open(self, *args, **kwargs):
100100
raise IsADirectoryError()
101101

102-
def joinpath(self, name):
102+
def joinpath(self, *names):
103+
if not names:
104+
return self
105+
name, rest = names[0], names[1:]
103106
return next(
104107
traversable for traversable in self.iterdir() if traversable.name == name
105-
)
108+
).joinpath(*rest)
106109

107110

108111
class TraversableReader(TraversableResources, SimpleReader):

0 commit comments

Comments
 (0)