Skip to content

Commit 0b95088

Browse files
committed
Mirror structure of serialize in deserialize
1 parent fb3155a commit 0b95088

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

fsspec/spec.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,10 +1486,15 @@ def from_dict(dct: Dict[str, Any]) -> AbstractFileSystem:
14861486

14871487
json_decoder = FilesystemJSONDecoder()
14881488

1489-
def deserialize(obj: Any):
1489+
def deserialize(obj: Any) -> Any:
14901490
if isinstance(obj, dict):
1491-
return json_decoder.custom_object_hook(obj)
1492-
1491+
obj = json_decoder.custom_object_hook(obj)
1492+
if isinstance(obj, dict):
1493+
return {k: deserialize(v) for k, v in obj.items()}
1494+
1495+
if isinstance(obj, (list, tuple)):
1496+
return [deserialize(v) for v in obj]
1497+
14931498
return obj
14941499

14951500
dct = dict(dct) # Defensive copy
@@ -1501,10 +1506,10 @@ def deserialize(obj: Any):
15011506
dct.pop("cls", None)
15021507
dct.pop("protocol", None)
15031508

1504-
args = tuple(deserialize(arg) for arg in dct.pop("args", ()))
1505-
kwargs = {k: deserialize(v) for k, v in dct.items()}
1506-
1507-
return cls(*args, **kwargs)
1509+
return cls(
1510+
*deserialize(dct.pop("args", ())),
1511+
**deserialize(dct),
1512+
)
15081513

15091514
def _get_pyarrow_filesystem(self):
15101515
"""

fsspec/tests/test_spec.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,24 @@ def test_json_fs_attr():
891891
assert DummyTestFS.from_json(outc) is c
892892

893893

894+
def test_json_dict_attr():
895+
a = DummyTestFS(1)
896+
b = DummyTestFS(2, bar=Path("baz"))
897+
c = DummyTestFS(3, baz={"key": b})
898+
899+
outa = a.to_json()
900+
outb = b.to_json()
901+
outc = c.to_json()
902+
903+
assert json.loads(outc) # is valid JSON
904+
assert b != c
905+
assert "baz" in outc
906+
907+
assert DummyTestFS.from_json(outa) is a
908+
assert DummyTestFS.from_json(outb) is b
909+
assert DummyTestFS.from_json(outc) is c
910+
911+
894912
def test_dict():
895913
a = DummyTestFS(1)
896914
b = DummyTestFS(2, bar=1)
@@ -939,6 +957,24 @@ def test_dict_fs_attr():
939957
assert DummyTestFS.from_dict(outc) is c
940958

941959

960+
def test_dict_dict_attr():
961+
a = DummyTestFS(1)
962+
b = DummyTestFS(2, bar=Path("baz"))
963+
c = DummyTestFS(3, baz={"key": b})
964+
965+
outa = a.to_dict()
966+
outb = b.to_dict()
967+
outc = c.to_dict()
968+
969+
assert isinstance(outc, dict)
970+
assert b != c
971+
assert outc["baz"]["key"] == outb
972+
973+
assert DummyTestFS.from_dict(outa) is a
974+
assert DummyTestFS.from_dict(outb) is b
975+
assert DummyTestFS.from_dict(outc) is c
976+
977+
942978
def test_dict_idempotent():
943979
a = DummyTestFS(1)
944980

0 commit comments

Comments
 (0)