Skip to content

Fix AbstractFileSystem.from_dict not being idempotent #1627

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

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions fsspec/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -1470,6 +1470,8 @@ def from_dict(dct: Dict[str, Any]) -> AbstractFileSystem:
"""
from .json import FilesystemJSONDecoder

dct = dict(dct) # Defensive copy

cls = FilesystemJSONDecoder.try_resolve_fs_cls(dct)
if cls is None:
raise ValueError("Not a serialized AbstractFileSystem")
Expand Down
24 changes: 24 additions & 0 deletions fsspec/tests/test_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,30 @@ def test_json_fs_attr():
assert DummyTestFS.from_json(outb) is b


def test_dict():
a = DummyTestFS(1)
b = DummyTestFS(2, bar=1)

outa = a.to_dict()
outb = b.to_dict()

assert isinstance(outa, dict)
assert a != b
assert "bar" in outb

assert DummyTestFS.from_dict(outa) is a
assert DummyTestFS.from_dict(outb) is b


def test_dict_idempotent():
a = DummyTestFS(1)

outa = a.to_dict()

assert DummyTestFS.from_dict(outa) is a
assert DummyTestFS.from_dict(outa) is a


def test_from_dict_valid():
fs = DummyTestFS.from_dict({"cls": "fsspec.tests.test_spec.DummyTestFS"})
assert isinstance(fs, DummyTestFS)
Expand Down