From 3d64f9589f445c63a7fcb0277c40fe59abc5eabc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Alexandre=20C=C3=B4t=C3=A9?= Date: Thu, 19 Jan 2017 17:34:57 -0500 Subject: [PATCH 1/2] Clarified error messages in LazyTractogram --- nibabel/streamlines/tests/test_tractogram.py | 2 ++ nibabel/streamlines/tractogram.py | 12 +++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/nibabel/streamlines/tests/test_tractogram.py b/nibabel/streamlines/tests/test_tractogram.py index d21e88e0f3..cfa62793b0 100644 --- a/nibabel/streamlines/tests/test_tractogram.py +++ b/nibabel/streamlines/tests/test_tractogram.py @@ -586,6 +586,8 @@ def test_lazy_tractogram_creation(self): # Creating LazyTractogram with generators is not allowed as # generators get exhausted and are not reusable unlike generator function. assert_raises(TypeError, LazyTractogram, streamlines) + assert_raises(TypeError, LazyTractogram, + data_per_point={"none": None}) assert_raises(TypeError, LazyTractogram, data_per_streamline=data_per_streamline) assert_raises(TypeError, LazyTractogram, DATA['streamlines'], diff --git a/nibabel/streamlines/tractogram.py b/nibabel/streamlines/tractogram.py index b386f27fa5..a21651959f 100644 --- a/nibabel/streamlines/tractogram.py +++ b/nibabel/streamlines/tractogram.py @@ -165,8 +165,11 @@ def __getitem__(self, key): return self.store[key]() def __setitem__(self, key, value): - if value is not None and not callable(value): # TODO: why None? - raise TypeError("`value` must be a generator function or None.") + if not callable(value): + msg = ("Values in a `LazyDict` must be generator functions." + " Those are functions which whenever are called return an" + " instantiated generator.") + raise TypeError(msg) self.store[key] = value def __delitem__(self, key): @@ -604,7 +607,10 @@ def _apply_affine(): def _set_streamlines(self, value): if value is not None and not callable(value): - raise TypeError("`streamlines` must be a generator function.") + msg = ("`streamlines` must be a generator function. That is a" + " function which whenever is called returns an" + " instantiated generator.") + raise TypeError(msg) self._streamlines = value @property From 531842ec4b476908fe78910e5c3dacbec94edf57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Alexandre=20C=C3=B4t=C3=A9?= Date: Sun, 26 Feb 2017 22:31:33 -0500 Subject: [PATCH 2/2] Improve error message as suggested by @matthew-brett --- nibabel/streamlines/tractogram.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/nibabel/streamlines/tractogram.py b/nibabel/streamlines/tractogram.py index a21651959f..40b1c3aea7 100644 --- a/nibabel/streamlines/tractogram.py +++ b/nibabel/streamlines/tractogram.py @@ -167,7 +167,7 @@ def __getitem__(self, key): def __setitem__(self, key, value): if not callable(value): msg = ("Values in a `LazyDict` must be generator functions." - " Those are functions which whenever are called return an" + " These are functions which, when called, return an" " instantiated generator.") raise TypeError(msg) self.store[key] = value @@ -608,8 +608,8 @@ def _apply_affine(): def _set_streamlines(self, value): if value is not None and not callable(value): msg = ("`streamlines` must be a generator function. That is a" - " function which whenever is called returns an" - " instantiated generator.") + " function which, when called, returns an instantiated" + " generator.") raise TypeError(msg) self._streamlines = value