From 6c995be4bec24899ed47d81ad6d34d8016108654 Mon Sep 17 00:00:00 2001 From: Maxime Liquet <35924738+maximlt@users.noreply.github.com> Date: Sun, 12 Mar 2023 18:26:15 +0100 Subject: [PATCH] Raise an error when setting `None` to a base Parameter with `allow_None=False` (#691) --- param/parameterized.py | 2 ++ tests/API1/testparameterizedobject.py | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/param/parameterized.py b/param/parameterized.py index 607e01f94..5da660071 100644 --- a/param/parameterized.py +++ b/param/parameterized.py @@ -1255,6 +1255,8 @@ def __set__(self, obj, val): def _validate_value(self, value, allow_None): """Implements validation for parameter value""" + if value is None and allow_None is False: + raise ValueError("Parameter %r does not accept `None`." % (self.name)) def _validate(self, val): """Implements validation for the parameter value and attributes""" diff --git a/tests/API1/testparameterizedobject.py b/tests/API1/testparameterizedobject.py index b6efaecc0..1bf27c93b 100644 --- a/tests/API1/testparameterizedobject.py +++ b/tests/API1/testparameterizedobject.py @@ -392,6 +392,30 @@ class TestPO1(param.Parameterized): x = param.Number(default=numbergen.UniformRandom(lbound=-1,ubound=1,seed=1),bounds=(-1,1)) y = param.Number(default=1,bounds=(-1,1)) + +class TestParameter(API1TestCase): + + def setUp(self): + super(TestParameter, self).setUp() + + class _TestParameter(param.Parameterized): + a = param.Parameter(default='') + b = param.Parameter(default='',allow_None=True) + c = param.Parameter(default=None) + + self._TestParameter = _TestParameter + + def test_handling_of_None(self): + t = self._TestParameter() + + with self.assertRaises(ValueError): + t.a = None + + t.b = None + + assert t.c is None + + class TestNumberParameter(API1TestCase): def test_outside_bounds(self):