Skip to content

Commit 72d550d

Browse files
committed
Consolidate MemoryTraversable._resolve.
1 parent d001110 commit 72d550d

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

importlib_resources/tests/util.py

Lines changed: 14 additions & 13 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,13 @@ 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)
280-
return isinstance(directory, dict)
284+
return isinstance(self._resolve(), dict)
281285

282286
def is_file(self) -> bool:
283-
path = pathlib.PurePosixPath(self._fullname)
284-
directory = functools.reduce(lambda d, p: d[p], path.parts, fixtures)
285-
return not isinstance(directory, dict)
287+
return not self.is_dir()
286288

287289
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)
290+
contents = self._resolve()
290291
if isinstance(contents, dict):
291292
# Filesystem openers raise OSError when attempting to open a directory,
292293
# and that exception is mirrored here.

0 commit comments

Comments
 (0)