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

added type guards for MemoryFileSystem and name property to MemoryFile class #1574

Merged
merged 14 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions fsspec/implementations/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing import Any, ClassVar

from fsspec import AbstractFileSystem
from fsspec.utils import stringify_path

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

Expand All @@ -25,6 +26,9 @@ class MemoryFileSystem(AbstractFileSystem):

@classmethod
def _strip_protocol(cls, path):
path = stringify_path(path)
if "\\" in path:
path = path.replace("\\", "/")
austinsimpsond41 marked this conversation as resolved.
Show resolved Hide resolved
if path.startswith("memory://"):
path = path[len("memory://") :]
if "::" in path or "://" in path:
Expand Down Expand Up @@ -280,6 +284,10 @@ def __init__(self, fs=None, path=None, data=None):
def size(self):
return self.getbuffer().nbytes

@property
def name(self):
return self.path.rsplit("/", 1)[-1]

def __enter__(self):
return self

Expand Down
9 changes: 8 additions & 1 deletion fsspec/implementations/tests/test_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest

from fsspec.implementations.local import LocalFileSystem, make_path_posix

from pathlib import Path

def test_1(m):
m.touch("/somefile") # NB: is found with or without initial /
Expand Down Expand Up @@ -363,3 +363,10 @@ def test_cp_two_files(m):
"/target/file0",
"/target/file1",
]

def test_open_path(m):
with m.open("/myfile/foo/bar", "wb") as f:
f.write(b"some\nlines\nof\ntext")
austinsimpsond41 marked this conversation as resolved.
Show resolved Hide resolved

path = Path("/myfile/foo/bar")
assert m.read_text(path) == "some\nlines\nof\ntext"
Loading