Skip to content

Commit

Permalink
Fix ClassSelector.get_range() for class_ tuple (#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr authored and ceball committed Nov 28, 2019
1 parent 57c1701 commit fbbe2d9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
9 changes: 6 additions & 3 deletions param/__init__.py
Expand Up @@ -1292,7 +1292,7 @@ def __init__(self,objects=None, default=None, instantiate=False,

class ClassSelector(SelectorBase):
"""
Parameter whose value is a specified class or an instance of that class.
Parameter allowing selection of either a subclass or an instance of a given set of classes.
By default, requires an instance, but if is_instance=False, accepts a class instead.
Both class and instance values respect the instantiate slot, though it matters only
for is_instance=True.
Expand Down Expand Up @@ -1335,8 +1335,11 @@ def get_range(self):
Only classes from modules that have been imported are added
(see concrete_descendents()).
"""
classes = concrete_descendents(self.class_)
d=OrderedDict((name,class_) for name,class_ in classes.items())
classes = self.class_ if isinstance(self.class_, tuple) else (self.class_,)
all_classes = {}
for cls in classes:
all_classes.update(concrete_descendents(cls))
d=OrderedDict((name,class_) for name,class_ in all_classes.items())
if self.allow_None:
d['None']=None
return d
Expand Down
6 changes: 6 additions & 0 deletions tests/API1/testclassselector.py
Expand Up @@ -60,6 +60,12 @@ def test_multiple_class_type_constructor2(self):
p = self.P(h=str)
self.assertEqual(p.h, str)

def test_class_selector_get_range(self):
p = self.P()
classes = p.param.g.get_range()
self.assertIn('int', classes)
self.assertIn('str', classes)

def test_multiple_class_type_error(self):
exception = "Parameter 'float' must be a subclass of \(int, str\), not 'type'"
with self.assertRaisesRegexp(ValueError, exception):
Expand Down

0 comments on commit fbbe2d9

Please sign in to comment.