Skip to content

Commit b532078

Browse files
added type guards for MemoryFileSystem and name property to MemoryFile class (#1574)
* fix: _strip_protocol to stringify path * Use rsplit instead of split for name in MemoryFile * Added test, altered _strip_protocol to correct windows paths * Removed redundant check from prior commit * fix liint issues * Update fsspec/implementations/tests/test_memory.py Co-authored-by: Martin Durant <[email protected]> * Rolled back name property in MemoryFileSystem * Update fsspec/implementations/memory.py Co-authored-by: Martin Durant <[email protected]> * Changed memory file system to only accept PurePosixPath * Handle PureWindowsPath in memfs * Separated path tests to be platform specific, allowed for c:/ to show in memoryfs paths * formatted code using black * Moved imports in test_memory to fix lint issues * Fixed import order in memory.py --------- Co-authored-by: Martin Durant <[email protected]>
1 parent 346a589 commit b532078

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

fsspec/implementations/memory.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
from datetime import datetime, timezone
55
from errno import ENOTEMPTY
66
from io import BytesIO
7+
from pathlib import PurePath, PureWindowsPath
78
from typing import Any, ClassVar
89

910
from fsspec import AbstractFileSystem
11+
from fsspec.implementations.local import LocalFileSystem
12+
from fsspec.utils import stringify_path
1013

1114
logger = logging.getLogger("fsspec.memoryfs")
1215

@@ -25,6 +28,12 @@ class MemoryFileSystem(AbstractFileSystem):
2528

2629
@classmethod
2730
def _strip_protocol(cls, path):
31+
if isinstance(path, PurePath):
32+
if isinstance(path, PureWindowsPath):
33+
return LocalFileSystem._strip_protocol(path)
34+
else:
35+
path = stringify_path(path)
36+
2837
if path.startswith("memory://"):
2938
path = path[len("memory://") :]
3039
if "::" in path or "://" in path:

fsspec/implementations/tests/test_memory.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
from pathlib import PurePosixPath, PureWindowsPath
23

34
import pytest
45

@@ -363,3 +364,19 @@ def test_cp_two_files(m):
363364
"/target/file0",
364365
"/target/file1",
365366
]
367+
368+
369+
def test_open_path_posix(m):
370+
path = PurePosixPath("/myfile/foo/bar")
371+
with m.open(path, "wb") as f:
372+
f.write(b"some\nlines\nof\ntext")
373+
374+
assert m.read_text(path) == "some\nlines\nof\ntext"
375+
376+
377+
def test_open_path_windows(m):
378+
path = PureWindowsPath("C:\\myfile\\foo\\bar")
379+
with m.open(path, "wb") as f:
380+
f.write(b"some\nlines\nof\ntext")
381+
382+
assert m.read_text(path) == "some\nlines\nof\ntext"

0 commit comments

Comments
 (0)