Skip to content

regression in JSON serialization of numpy floats #3279

@d-v-b

Description

@d-v-b
uv run --with "zarr==3.1.0" python 
Installed 8 packages in 20ms
Python 3.12.3 (main, Jun 18 2025, 17:59:45) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import zarr
>>> import numpy as np
>>> z = zarr.zeros(1,attributes={"foo": np.inf})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/bennettd/.cache/uv/archive-v0/aj-q6WYOe3uNCHBRQhpNb/lib/python3.12/site-packages/zarr/api/synchronous.py", line 1342, in zeros
    return Array(sync(async_api.zeros(shape=shape, **kwargs)))
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/bennettd/.cache/uv/archive-v0/aj-q6WYOe3uNCHBRQhpNb/lib/python3.12/site-packages/zarr/core/sync.py", line 163, in sync
    raise return_result
  File "/home/bennettd/.cache/uv/archive-v0/aj-q6WYOe3uNCHBRQhpNb/lib/python3.12/site-packages/zarr/core/sync.py", line 119, in _runner
    return await coro
           ^^^^^^^^^^
  File "/home/bennettd/.cache/uv/archive-v0/aj-q6WYOe3uNCHBRQhpNb/lib/python3.12/site-packages/zarr/api/asynchronous.py", line 1324, in zeros
    return await create(shape=shape, fill_value=0, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/bennettd/.cache/uv/archive-v0/aj-q6WYOe3uNCHBRQhpNb/lib/python3.12/site-packages/zarr/api/asynchronous.py", line 1060, in create
    return await AsyncArray._create(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/bennettd/.cache/uv/archive-v0/aj-q6WYOe3uNCHBRQhpNb/lib/python3.12/site-packages/zarr/core/array.py", line 656, in _create
    result = await cls._create_v3(
             ^^^^^^^^^^^^^^^^^^^^^
  File "/home/bennettd/.cache/uv/archive-v0/aj-q6WYOe3uNCHBRQhpNb/lib/python3.12/site-packages/zarr/core/array.py", line 809, in _create_v3
    await array._save_metadata(metadata, ensure_parents=True)
  File "/home/bennettd/.cache/uv/archive-v0/aj-q6WYOe3uNCHBRQhpNb/lib/python3.12/site-packages/zarr/core/array.py", line 1433, in _save_metadata
    to_save = metadata.to_buffer_dict(cpu_buffer_prototype)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/bennettd/.cache/uv/archive-v0/aj-q6WYOe3uNCHBRQhpNb/lib/python3.12/site-packages/zarr/core/metadata/v3.py", line 291, in to_buffer_dict
    json.dumps(d, allow_nan=False, indent=json_indent).encode()
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
          ^^^^^^^^^^^
  File "/usr/lib/python3.12/json/encoder.py", line 202, in encode
    chunks = list(chunks)
             ^^^^^^^^^^^^
  File "/usr/lib/python3.12/json/encoder.py", line 432, in _iterencode
    yield from _iterencode_dict(o, _current_indent_level)
  File "/usr/lib/python3.12/json/encoder.py", line 406, in _iterencode_dict
    yield from chunks
  File "/usr/lib/python3.12/json/encoder.py", line 398, in _iterencode_dict
    yield _floatstr(value)
          ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/json/encoder.py", line 240, in floatstr
    raise ValueError(
ValueError: Out of range float values are not JSON compliant: inf

This worked in 3.0.x, and it broke in #2874. A fix could involve bringing back the special json encoder class.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy