336: Param.__dispatch() now returns None for any in (None, '', u'', tuple(), []) regardless whether Param is multivalue

This commit is contained in:
Jason Gerard DeRose
2008-09-24 07:05:43 +00:00
parent e63c462f31
commit fb57b91937
2 changed files with 20 additions and 10 deletions

View File

@@ -106,12 +106,10 @@ class Param(plugable.ReadOnly):
lock(self)
def __dispatch(self, value, scalar):
if value is None:
return None
if value in (None, '', tuple(), []):
return
if self.multivalue:
if type(value) in (tuple, list):
if len(value) == 0:
return None
return tuple(
scalar(v, i) for (i, v) in enumerate(value)
)
@@ -148,7 +146,7 @@ class Param(plugable.ReadOnly):
def __convert_scalar(self, value, index=None):
if value is None:
return None
return
converted = self.type(value)
if converted is None:
raise errors.ConversionError(
@@ -158,7 +156,12 @@ class Param(plugable.ReadOnly):
def convert(self, value):
"""
Convert/coerce ``value`` to Python type for this parameter.
Convert/coerce ``value`` to Python type for this `Param`.
If ``value`` can not be converted, ConversionError is raised.
If ``value`` is None, conversion is not attempted and None is
returned.
:param value: A proposed value for this parameter.
"""

View File

@@ -145,10 +145,12 @@ class test_Param(ClassChecker):
type_ = ipa_types.Int()
okay = (7, 7L, 7.0, ' 7 ')
fail = ('7.0', '7L', 'whatever', object)
none = (None, '', u'', tuple(), [])
# Scenario 1: multivalue=False
o = self.cls(name, type_)
assert o.convert(None) is None
for n in none:
assert o.convert(n) is None
for value in okay:
new = o.convert(value)
assert new == 7
@@ -162,8 +164,8 @@ class test_Param(ClassChecker):
# Scenario 2: multivalue=True
o = self.cls(name, type_, multivalue=True)
for none in (None, tuple(), []):
assert o.convert(none) is None
for n in none:
assert o.convert(n) is None
for value in okay:
assert o.convert((value,)) == (7,)
assert o.convert([value]) == (7,)
@@ -188,6 +190,7 @@ class test_Param(ClassChecker):
t = ipa_types.Unicode()
callback = lambda value: value.lower()
values = (None, u'Hello', (u'Hello',), 'hello', ['hello'])
none = (None, '', u'', tuple(), [])
# Scenario 1: multivalue=False, normalize=None
o = self.cls(name, t)
@@ -201,6 +204,8 @@ class test_Param(ClassChecker):
assert o.normalize(v) == 'hello'
for v in [None, 42, (u'Hello',)]: # Not basestring
assert o.normalize(v) is v
for n in none:
assert o.normalize(n) is None
# Scenario 3: multivalue=True, normalize=None
o = self.cls(name, t, multivalue=True)
@@ -215,8 +220,10 @@ class test_Param(ClassChecker):
for value in [(u'Hello',), (u'hello',), 'Hello', ['Hello']]: # Okay
assert o.normalize(value) == (u'hello',)
fail = 42 # Not basestring
for v in [[fail], (u'hello', fail)]: # Non unicode member
for v in [[fail], (u'hello', fail)]: # Non basestring member
assert o.normalize(v) == tuple(v)
for n in none:
assert o.normalize(n) is None
def test_validate(self):
"""