Skip to content

Commit 30bc726

Browse files
committed
Consolidate MemoryTraversable._resolve.
1 parent d001110 commit 30bc726

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

importlib_resources/tests/util.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,18 @@ def __init__(self, module, fullname):
260260
self._module = module
261261
self._fullname = fullname
262262

263-
def iterdir(self):
263+
def _resolve(self):
264+
"""
265+
Fully traverse the `fixtures` dictionary.
266+
267+
This should be wrapped in a `try/except KeyError`
268+
but it is not currently needed and lowers the code coverage numbers.
269+
"""
264270
path = pathlib.PurePosixPath(self._fullname)
265-
directory = functools.reduce(lambda d, p: d[p], path.parts, fixtures)
271+
return functools.reduce(lambda d, p: d[p], path.parts, fixtures)
272+
273+
def iterdir(self):
274+
directory = self._resolve()
266275
if not isinstance(directory, dict):
267276
# Filesystem openers raise OSError, and that exception is mirrored here.
268277
raise OSError(f"{self._fullname} is not a directory")
@@ -272,21 +281,15 @@ def iterdir(self):
272281
)
273282

274283
def is_dir(self) -> bool:
275-
path = pathlib.PurePosixPath(self._fullname)
276-
# Fully traverse the `fixtures` dictionary.
277-
# This should be wrapped in a `try/except KeyError`
278-
# but it is not currently needed, and lowers the code coverage numbers.
279-
directory = functools.reduce(lambda d, p: d[p], path.parts, fixtures)
284+
directory = self._resolve()
280285
return isinstance(directory, dict)
281286

282287
def is_file(self) -> bool:
283-
path = pathlib.PurePosixPath(self._fullname)
284-
directory = functools.reduce(lambda d, p: d[p], path.parts, fixtures)
288+
directory = self._resolve()
285289
return not isinstance(directory, dict)
286290

287291
def open(self, mode='r', encoding=None, errors=None, *_, **__):
288-
path = pathlib.PurePosixPath(self._fullname)
289-
contents = functools.reduce(lambda d, p: d[p], path.parts, fixtures)
292+
contents = self._resolve()
290293
if isinstance(contents, dict):
291294
# Filesystem openers raise OSError when attempting to open a directory,
292295
# and that exception is mirrored here.

0 commit comments

Comments
 (0)