Make from_obj() work for List/Dict configtypes
We can't easily make it work for ListOrValue as we don't know which of both we get at this point.
This commit is contained in:
parent
c03ef10d54
commit
990c0707f4
@ -451,7 +451,7 @@ class List(BaseType):
|
|||||||
def from_obj(self, value):
|
def from_obj(self, value):
|
||||||
if value is None:
|
if value is None:
|
||||||
return []
|
return []
|
||||||
return value
|
return [self.valtype.from_obj(v) for v in value]
|
||||||
|
|
||||||
def to_py(self, value):
|
def to_py(self, value):
|
||||||
self._basic_py_validation(value, list)
|
self._basic_py_validation(value, list)
|
||||||
@ -1199,7 +1199,9 @@ class Dict(BaseType):
|
|||||||
def from_obj(self, value):
|
def from_obj(self, value):
|
||||||
if value is None:
|
if value is None:
|
||||||
return {}
|
return {}
|
||||||
return value
|
|
||||||
|
return {self.keytype.from_obj(key): self.valtype.from_obj(val)
|
||||||
|
for key, val in value.items()}
|
||||||
|
|
||||||
def _fill_fixed_keys(self, value):
|
def _fill_fixed_keys(self, value):
|
||||||
"""Fill missing fixed keys with a None-value."""
|
"""Fill missing fixed keys with a None-value."""
|
||||||
|
@ -533,6 +533,14 @@ class FlagListSubclass(configtypes.FlagList):
|
|||||||
'foo', 'bar', 'baz')
|
'foo', 'bar', 'baz')
|
||||||
|
|
||||||
|
|
||||||
|
class FromObjType(configtypes.BaseType):
|
||||||
|
|
||||||
|
"""Config type to test from_obj for List/Dict."""
|
||||||
|
|
||||||
|
def from_obj(self, obj):
|
||||||
|
return int(obj)
|
||||||
|
|
||||||
|
|
||||||
class TestList:
|
class TestList:
|
||||||
|
|
||||||
"""Test List and FlagList."""
|
"""Test List and FlagList."""
|
||||||
@ -647,6 +655,12 @@ class TestList:
|
|||||||
with pytest.raises(AssertionError):
|
with pytest.raises(AssertionError):
|
||||||
typ.to_doc([['foo']])
|
typ.to_doc([['foo']])
|
||||||
|
|
||||||
|
def test_from_obj_sub(self):
|
||||||
|
"""Make sure the list calls from_obj() on sub-types."""
|
||||||
|
typ = configtypes.List(valtype=FromObjType())
|
||||||
|
value = typ.from_obj(['1', '2'])
|
||||||
|
assert value == [1, 2]
|
||||||
|
|
||||||
|
|
||||||
class TestFlagList:
|
class TestFlagList:
|
||||||
|
|
||||||
@ -1665,6 +1679,13 @@ class TestDict:
|
|||||||
print(doc)
|
print(doc)
|
||||||
assert doc == expected
|
assert doc == expected
|
||||||
|
|
||||||
|
def test_from_obj_sub(self):
|
||||||
|
"""Make sure the dict calls from_obj() on sub-types."""
|
||||||
|
typ = configtypes.Dict(keytype=configtypes.String(),
|
||||||
|
valtype=FromObjType())
|
||||||
|
value = typ.from_obj({'1': '2'})
|
||||||
|
assert value == {'1': 2}
|
||||||
|
|
||||||
|
|
||||||
def unrequired_class(**kwargs):
|
def unrequired_class(**kwargs):
|
||||||
return configtypes.File(required=False, **kwargs)
|
return configtypes.File(required=False, **kwargs)
|
||||||
|
Loading…
Reference in New Issue
Block a user