Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid clashing class_ slot in List parameter #456

Merged
merged 8 commits into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions param/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1359,11 +1359,12 @@ class List(Parameter):
items in the list are checked to be of that type.
"""

__slots__ = ['class_','bounds']
__slots__ = ['bounds', 'item_type', 'class_']

def __init__(self,default=[],class_=None,instantiate=True,
def __init__(self,default=[],class_=None,item_type=None,instantiate=True,
bounds=(0,None),**params):
self.class_ = class_
jlstevens marked this conversation as resolved.
Show resolved Hide resolved
self.item_type = item_type or class_
self.class_ = self.item_type
self.bounds = bounds
Parameter.__init__(self,default=default,instantiate=instantiate,
**params)
Expand Down Expand Up @@ -1396,9 +1397,9 @@ def _validate(self, val):
self._check_type(val)

def _check_type(self,val):
if self.class_ is not None:
if self.item_type is not None:
for v in val:
assert isinstance(v,self.class_),repr(self.name)+": "+repr(v)+" is not an instance of " + repr(self.class_) + "."
assert isinstance(v,self.item_type),repr(self.name)+": "+repr(v)+" is not an instance of " + repr(self.item_type) + "."


class HookList(List):
Expand Down
6 changes: 3 additions & 3 deletions param/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,12 @@ def range_schema(cls, p, safe=False):
@classmethod
def list_schema(cls, p, safe=False):
schema = { "type": "array"}
if safe is True and p.class_ is None:
if safe is True and p.item_type is None:
msg = ('List without a class specified cannot be guaranteed '
'to be safe for serialization')
raise UnsafeserializableException(msg)
if p.class_ is not None and p.class_ in cls.json_schema_literal_types:
schema['items'] = {"type": cls.json_schema_literal_types[p.class_]}
if p.item_type is not None and p.item_type in cls.json_schema_literal_types:
schema['items'] = {"type": cls.json_schema_literal_types[p.item_type]}
return schema

@classmethod
Expand Down