Skip to content

BUG: Allow reading non-npy files in npz and add test #29412

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
Jul 22, 2025
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: 1 addition & 1 deletion numpy/lib/_npyio_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def __getitem__(self, key):
max_header_size=self.max_header_size
)
else:
return bytes.read(key)
return bytes.read()

def __contains__(self, key):
return (key in self._files)
Expand Down
17 changes: 17 additions & 0 deletions numpy/lib/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import threading
import time
import warnings
import zipfile
from ctypes import c_bool
from datetime import datetime
from io import BytesIO, StringIO
Expand Down Expand Up @@ -217,6 +218,22 @@ def roundtrip(self, *args, **kwargs):
self.arr_reloaded.fid.close()
os.remove(self.arr_reloaded.fid.name)

def test_load_non_npy(self):
"""Test loading non-.npy files and name mapping in .npz."""
with temppath(prefix="numpy_test_npz_load_non_npy_", suffix=".npz") as tmp:
with zipfile.ZipFile(tmp, "w") as npz:
with npz.open("test1.npy", "w") as out_file:
np.save(out_file, np.arange(10))
with npz.open("test2", "w") as out_file:
np.save(out_file, np.arange(10))
with npz.open("metadata", "w") as out_file:
out_file.write(b"Name: Test")
with np.load(tmp) as npz:
assert len(npz["test1"]) == 10
assert len(npz["test1.npy"]) == 10
assert len(npz["test2"]) == 10
assert npz["metadata"] == b"Name: Test"

@pytest.mark.skipif(IS_PYPY, reason="Hangs on PyPy")
@pytest.mark.skipif(not IS_64BIT, reason="Needs 64bit platform")
@pytest.mark.slow
Expand Down
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