fixes in datatypes

- Enum.__call__(self, value) should raise BadValueError when
  value is not hashable
- added TextType.copy (DataType.copy will not work here)

Change-Id: I6c460991fa1e6595a939fb0814f490d2188c97b4
Reviewed-on: https://forge.frm2.tum.de/review/21015
Tested-by: JenkinsCodeReview <bjoern_pedersen@frm2.tum.de>
Reviewed-by: Markus Zolliker <markus.zolliker@psi.ch>
This commit is contained in:
zolliker 2019-08-15 15:42:43 +02:00
parent cd1b4cf4e2
commit 74eb33b789
2 changed files with 6 additions and 2 deletions

View File

@ -339,7 +339,7 @@ class EnumType(DataType):
"""return the validated (internal) value or raise""" """return the validated (internal) value or raise"""
try: try:
return self._enum[value] return self._enum[value]
except KeyError: except (KeyError, TypeError): # TypeError will be raised when value is not hashable
raise BadValueError(u'%r is not a member of enum %r' % (value, self._enum)) raise BadValueError(u'%r is not a member of enum %r' % (value, self._enum))
def from_string(self, text): def from_string(self, text):
@ -469,6 +469,10 @@ class TextType(StringType):
def __repr__(self): def __repr__(self):
return u'TextType(%d, %d)' % (self.minsize, self.maxsize) return u'TextType(%d, %d)' % (self.minsize, self.maxsize)
def copy(self):
# DataType.copy will not work, because it is exported as 'string'
return TextType(self.maxsize)
# Bool is a special enum # Bool is a special enum
class BoolType(DataType): class BoolType(DataType):

View File

@ -174,7 +174,7 @@ def test_EnumType():
dt(-9) dt(-9)
with pytest.raises(ValueError): with pytest.raises(ValueError):
dt(u'XX') dt(u'XX')
with pytest.raises(TypeError): with pytest.raises(ValueError):
dt([19, u'X']) dt([19, u'X'])
assert dt(u'a') == 3 assert dt(u'a') == 3