summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Goldbaum <nathan.goldbaum@gmail.com>2023-05-13 00:05:07 -0600
committerGitHub <noreply@github.com>2023-05-12 23:05:07 -0700
commit25908cacd19915bf3ddd659c28be28a41bd97a54 (patch)
tree032aba9a7b569fcd1a58348e849aa6a73a3a54a2
parent6d9f1ff8b9aa5fb0b06fc053ffc00352b37cc80e (diff)
downloadnumpy-25908cacd19915bf3ddd659c28be28a41bd97a54.tar.gz
BUG: properly handle tuple keys in NpZFile.__getitem__ (#23757)
* BUG: properly handle tuple keys in NpZFile.__getitem__ * TST: test tuple rendering specifically. --------- Co-authored-by: Ross Barnowski <rossbar@berkeley.edu>
-rw-r--r--numpy/lib/npyio.py2
-rw-r--r--numpy/lib/tests/test_io.py11
2 files changed, 12 insertions, 1 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py
index 22fb0eb7d..339b1dc62 100644
--- a/numpy/lib/npyio.py
+++ b/numpy/lib/npyio.py
@@ -260,7 +260,7 @@ class NpzFile(Mapping):
else:
return self.zip.read(key)
else:
- raise KeyError("%s is not a file in the archive" % key)
+ raise KeyError(f"{key} is not a file in the archive")
def __contains__(self, key):
return (key in self._files or key in self.files)
diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py
index 5a68fbc97..c1032df8e 100644
--- a/numpy/lib/tests/test_io.py
+++ b/numpy/lib/tests/test_io.py
@@ -232,6 +232,17 @@ class TestSavezLoad(RoundtripTest):
assert_equal(a, l['file_a'])
assert_equal(b, l['file_b'])
+
+ def test_tuple_getitem_raises(self):
+ # gh-23748
+ a = np.array([1, 2, 3])
+ f = BytesIO()
+ np.savez(f, a=a)
+ f.seek(0)
+ l = np.load(f)
+ with pytest.raises(KeyError, match="(1, 2)"):
+ l[1, 2]
+
def test_BagObj(self):
a = np.array([[1, 2], [3, 4]], float)
b = np.array([[1 + 2j, 2 + 7j], [3 - 6j, 4 + 12j]], complex)