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

Allow reading utf-8 encoded json files #1312

Merged
merged 8 commits into from
Feb 9, 2023
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
3 changes: 3 additions & 0 deletions docs/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ Major changes
Bug fixes
~~~~~~~~~

* Allow reading utf-8 encoded json files
By :user:`Nathan Zimmerberg <nhz2>` :issue:`1308`.

* Ensure contiguous data is give to ``FSStore``. Only copying if needed.
By :user:`Mads R. B. Kristensen <madsbk>` :issue:`1285`.
* NestedDirectoryStore.listdir now returns chunk keys with the correct '/' dimension_separator.
Expand Down
1 change: 1 addition & 0 deletions fixture/utf8attrs/.zattrs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"foo": "た"}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is missing from the sdist on PyPI

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @jakirkham. Argh, I thought I had caught these issues but likely only if there's code to generate it them as well. Are you already working on a fix or shall I?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to go ahead. I might not get to this for a while

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forget if we already discussed this before, but maybe we can make some fixes to CI to ensure we catch these issues in PRs. Wrote up some thoughts on this in issue ( #1347 )

3 changes: 3 additions & 0 deletions fixture/utf8attrs/.zgroup
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"zarr_format": 2
}
Comment on lines +1 to +3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same with this one

12 changes: 6 additions & 6 deletions zarr/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class Metadata2:
ZARR_FORMAT = ZARR_FORMAT

@classmethod
def parse_metadata(cls, s: Union[MappingType, str]) -> MappingType[str, Any]:
def parse_metadata(cls, s: Union[MappingType, bytes, str]) -> MappingType[str, Any]:

# Here we allow that a store may return an already-parsed metadata object,
# or a string of JSON that we will parse here. We allow for an already-parsed
Expand All @@ -110,7 +110,7 @@ def parse_metadata(cls, s: Union[MappingType, str]) -> MappingType[str, Any]:
return meta

@classmethod
def decode_array_metadata(cls, s: Union[MappingType, str]) -> MappingType[str, Any]:
def decode_array_metadata(cls, s: Union[MappingType, bytes, str]) -> MappingType[str, Any]:
meta = cls.parse_metadata(s)

# check metadata format
Expand Down Expand Up @@ -198,7 +198,7 @@ def decode_dtype(cls, d) -> np.dtype:
return np.dtype(d)

@classmethod
def decode_group_metadata(cls, s: Union[MappingType, str]) -> MappingType[str, Any]:
def decode_group_metadata(cls, s: Union[MappingType, bytes, str]) -> MappingType[str, Any]:
meta = cls.parse_metadata(s)

# check metadata format version
Expand Down Expand Up @@ -351,7 +351,7 @@ def encode_dtype(cls, d):
return get_extended_dtype_info(np.dtype(d))

@classmethod
def decode_group_metadata(cls, s: Union[MappingType, str]) -> MappingType[str, Any]:
def decode_group_metadata(cls, s: Union[MappingType, bytes, str]) -> MappingType[str, Any]:
meta = cls.parse_metadata(s)
# 1 / 0
# # check metadata format version
Expand Down Expand Up @@ -390,7 +390,7 @@ def encode_hierarchy_metadata(cls, meta=None) -> bytes:

@classmethod
def decode_hierarchy_metadata(
cls, s: Union[MappingType, str]
cls, s: Union[MappingType, bytes, str]
) -> MappingType[str, Any]:
meta = cls.parse_metadata(s)
# check metadata format
Expand Down Expand Up @@ -495,7 +495,7 @@ def _decode_storage_transformer_metadata(cls, meta: Mapping) -> "StorageTransfor
return StorageTransformerCls.from_config(transformer_type, conf)

@classmethod
def decode_array_metadata(cls, s: Union[MappingType, str]) -> MappingType[str, Any]:
def decode_array_metadata(cls, s: Union[MappingType, bytes, str]) -> MappingType[str, Any]:
meta = cls.parse_metadata(s)

# extract array metadata fields
Expand Down
11 changes: 9 additions & 2 deletions zarr/tests/test_attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

from zarr._storage.store import meta_root
from zarr.attrs import Attributes
from zarr.storage import KVStore
from zarr.storage import KVStore, DirectoryStore
from zarr._storage.v3 import KVStoreV3
from zarr.tests.util import CountingDict, CountingDictV3
from zarr.hierarchy import group


@pytest.fixture(params=[2, 3])
Expand Down Expand Up @@ -42,11 +43,17 @@ def test_storage(self, zarr_version):
a['baz'] = 42
assert attrs_key in store
assert isinstance(store[attrs_key], bytes)
d = json.loads(str(store[attrs_key], 'ascii'))
d = json.loads(str(store[attrs_key], 'utf-8'))
if zarr_version == 3:
d = d['attributes']
assert dict(foo='bar', baz=42) == d

def test_utf8_encoding(self, zarr_version):

# fixture data
fixture = group(store=DirectoryStore('fixture'))
assert fixture['utf8attrs'].attrs.asdict() == dict(foo='た')
Comment on lines +51 to +55
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is failing there


def test_get_set_del_contains(self, zarr_version):

store = _init_store(zarr_version)
Expand Down
4 changes: 2 additions & 2 deletions zarr/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ def json_dumps(o: Any) -> bytes:
separators=(',', ': '), cls=NumberEncoder).encode('ascii')


def json_loads(s: str) -> Dict[str, Any]:
def json_loads(s: Union[bytes, str]) -> Dict[str, Any]:
"""Read JSON in a consistent way."""
return json.loads(ensure_text(s, 'ascii'))
return json.loads(ensure_text(s, 'utf-8'))


def normalize_shape(shape) -> Tuple[int]:
Expand Down