Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MultiplexedPath only traverses first directory #265

Closed
jooste opened this issue Sep 6, 2022 · 2 comments · Fixed by #278
Closed

MultiplexedPath only traverses first directory #265

jooste opened this issue Sep 6, 2022 · 2 comments · Fixed by #278
Assignees

Comments

@jooste
Copy link
Contributor

jooste commented Sep 6, 2022

I ran into the following issue while trying to update my MultiplexedPath example in #264 to the current structure. Let's say you have the following path structure:

data01
  |_x

data02
  |_x
    |_y
      |_z.txt

If you combine data01 and data02 in aMultiplexedPath, data01/x will make it impossible to find data02/x/y, see the following example:

from importlib_resources.readers import MultiplexedPath

p = MultiplexedPath('data01', 'data02')

In this case if you do:

print(p / 'x/y')

this will give data01/x/y instead of data02/x/y, and

for f in (p / 'x/y').iterdir():
    print(f)

will raise a FileNotFoundError.

The reason for this is that in Traversable.joinpath(), only the first path segment is used to search for a match:

names = itertools.chain.from_iterable(
    path.parts for path in map(pathlib.PurePosixPath, descendants)
)
target = next(names)

and then, only the first match is returned:

matches = (
    traversable for traversable in self.iterdir() if traversable.name == target
)
try:
    match = next(matches)

The solution to this problem could be related to the suggestion in #264, unless returning MultiplexedPaths isn't the desired behaviour of MultiplexedPath.joinpath().

@jaraco jaraco self-assigned this Oct 5, 2022
@jaraco
Copy link
Member

jaraco commented Oct 5, 2022

Agreed, MultiplexedPath is meant to honor all paths, recursively. Your suggestion looks approximately correct.

Do you have any interest in developing the fix (as a pull request), including tests to capture the missed expectation?

@jaraco jaraco added the help wanted Extra attention is needed label Oct 5, 2022
@jooste
Copy link
Contributor Author

jooste commented Oct 7, 2022

Sure! My only worry was that my implementation is based on a previous structure of MultiplexedPath. But if it looks ok I'll work on making it into a PR.

@jaraco jaraco assigned jooste and jaraco and unassigned jaraco and jooste Oct 7, 2022
@jaraco jaraco changed the title Possible bug in MultiplexedPath with partially overlapping subdirectory structure MultiplexedPath only traverses first directory Feb 6, 2023
@jaraco jaraco removed the help wanted Extra attention is needed label Feb 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants