From f3c52dc2870f1e91bfe64a11eabab8d11f8478a8 Mon Sep 17 00:00:00 2001 From: Vincent Schut Date: Mon, 5 Dec 2016 10:44:58 +0100 Subject: [PATCH 1/4] raise AttributeError in __getattr__ if lookup fails --- zarr/hierarchy.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/zarr/hierarchy.py b/zarr/hierarchy.py index 31cb062c91..9c92f4b122 100644 --- a/zarr/hierarchy.py +++ b/zarr/hierarchy.py @@ -319,7 +319,10 @@ def _delitem_nosync(self, item): def __getattr__(self, item): # allow access to group members via dot notation - return self.__getitem__(item) + try: + return self.__getitem__(item) + except KeyError: + raise AttributeError def group_keys(self): """Return an iterator over member names for groups only. From 6cbb94cc96475f2922c478fa4e300b2f729063aa Mon Sep 17 00:00:00 2001 From: Vincent Schut Date: Mon, 5 Dec 2016 12:10:20 +0100 Subject: [PATCH 2/4] add unit test --- zarr/tests/test_hierarchy.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/zarr/tests/test_hierarchy.py b/zarr/tests/test_hierarchy.py index fca7093c0b..489b15db3d 100644 --- a/zarr/tests/test_hierarchy.py +++ b/zarr/tests/test_hierarchy.py @@ -492,6 +492,8 @@ def test_getattr(self): # test eq(g1['foo'], g1.foo) eq(g2['bar'], g2.bar) + # test that hasattr returns False instead of an exception (issue #88) + eq(hasattr(g1, '__unexistingattribute__'), False) def test_group_repr(self): g = self.create_group() From 61f09de27ef39dc1056d598357b109aa359ea7f8 Mon Sep 17 00:00:00 2001 From: Vincent Schut Date: Mon, 5 Dec 2016 12:27:22 +0100 Subject: [PATCH 3/4] fix (unrelated) flake8 error to make travis happy --- zarr/tests/test_codecs.py | 1 + 1 file changed, 1 insertion(+) diff --git a/zarr/tests/test_codecs.py b/zarr/tests/test_codecs.py index 974ba99f2b..7538813e6c 100644 --- a/zarr/tests/test_codecs.py +++ b/zarr/tests/test_codecs.py @@ -149,6 +149,7 @@ def _test_decode_lossy(self, arr, decimal, **kwargs): order=order) assert_array_almost_equal(arr, out, decimal=decimal) + test_arrays = [ np.arange(1000, dtype='i4'), np.linspace(1000, 1001, 1000, dtype='f8'), From a7b0850dc158ae0ec5bec621321bc5e54a1f7d52 Mon Sep 17 00:00:00 2001 From: Vincent Schut Date: Mon, 5 Dec 2016 13:39:03 +0100 Subject: [PATCH 4/4] replace eq(..., False) with assert_false --- zarr/tests/test_hierarchy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zarr/tests/test_hierarchy.py b/zarr/tests/test_hierarchy.py index 489b15db3d..a13fb29e05 100644 --- a/zarr/tests/test_hierarchy.py +++ b/zarr/tests/test_hierarchy.py @@ -493,7 +493,7 @@ def test_getattr(self): eq(g1['foo'], g1.foo) eq(g2['bar'], g2.bar) # test that hasattr returns False instead of an exception (issue #88) - eq(hasattr(g1, '__unexistingattribute__'), False) + assert_false(hasattr(g1, 'unexistingattribute')) def test_group_repr(self): g = self.create_group()