Skip to content

User-provided attributes dictionary is mutated by the public API #4059

Description

@stibrew

Zarr version

v3.2.1

Numcodecs version

0.16.5

Python Version

3.12

Operating System

Linux

Installation

using pip into virtual environment

Description

When creating a Zarr group or array with a user-provided attributes dictionary, the original dictionary gets mutated when the user subsequently modifies attributes via the public API (attrs.setitem, attrs.put, or del attrs[key]).

Steps to reproduce

# /// script
# requires-python = ">=3.12"
# dependencies = [
#   "zarr@git+https://github.com/zarr-developers/zarr-python.git@main",
# ]
# ///
#
# This script automatically imports the development branch of zarr to check for issues

import zarr
init_dict = {"foo": "bar"}
user_attrs = dict(init_dict)
print("User's dict before passing to Zarr:", user_attrs)
g = zarr.group(attributes=user_attrs)
g.attrs["new_key"] = "new_value"
print("User's dict after passing to Zarr:", user_attrs)
print("Expected:   ", init_dict)
assert user_attrs == init_dict, "FAIL: Original dict mutated by"
zarr.print_debug_info()

Additional output

There is a key called cache_attrs under zarr.group() which is not yet implemented. Is there a reason to pursue that over just copying the attributes which user provides?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugPotential issues with the zarr-python library

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions