Raise FileNotFoundError from MemoryFileSystem.rm for a missing path - #2140
Merged
Merged
Conversation
A non-recursive expand_path keeps a literal path whether or not it
exists, and MemoryFileSystem.rm skipped any expanded path that did not
exist. That skip is meant for directories missing from pseudo_dirs,
which vanish once the files under them are deleted earlier in the same
call, but it also swallowed paths that never existed. So rm("/missing")
succeeded silently, where rm_file, rm(recursive=True) and the other
filesystems raise. Because FSMap.__delitem__ turns that error into
KeyError, `del mapper["missing"]` on a memory-backed mapper did not
raise either.
Record which expanded paths exist before deleting anything, and only
skip a path that existed at the start of the call.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
MemoryFileSystem.rmsilently succeeds for a path that does not exist:LocalFileSystemraises in all three cases, as does the baseAbstractFileSystem.rm, which callsrm_fileon every expanded path.It is visible through the mapper too.
FSMap.__delitem__turns the filesystem error intoKeyError, so on a memory-backed mapper a missing key deletes without error, unlikedictand a file-backed mapper:Cause
A non-recursive
expand_pathkeeps a literal path whether or not it exists.MemoryFileSystem.rmthen skips any expanded path that does not exist:The comment above that branch explains its purpose: a directory that is not in
pseudo_dirsonly exists while files sit under it, so deleting those files earlier in the loop makes it vanish. That case is real —rm("/a", recursive=True)afterpipe("/a/b/c", ...)depends on it — but the same branch also swallows paths that never existed.Change
Record which expanded paths exist before deleting anything. A path that no longer exists is still skipped if it existed at the start of the call — the vanishing implicit directory — and otherwise raises
FileNotFoundError, likerm_file.The raise happens at the same point in the reversed loop where
AbstractFileSystem.rmwould raise, so the ordering matches the other filesystems. Recursive and glob calls are unchanged:expand_pathalready drops non-existent paths for them.Tests
In
test_memory.py:test_rm_missing_path_raisestest_rm_list_with_missing_path_raisestest_mapper_delitem_missing_key_raises_keyerrortest_rm_recursive_still_removes_implicit_parents— a guard for the case the skip exists for, so this fix cannot regress itAgainst
master'smemory.py, the first three fail and the guard passes. With the change, all four pass.test_memory.py,test_mapping.py,test_core.py,test_local.pyandtest_utils.pytogether: 368 passed before, 372 after, no failures. A wider run found nothing new; every non-passing test also failed onmasterand came from a dependency I don't have installed (aiohttp,pytest-mock).ruff0.14.3 (the pre-commit pin),ruff formatandcodespellare clean. I added a changelog entry under Dev → Fixes.🤖 Generated with Claude Code