summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Bruhin <git@the-compiler.org>2018-03-12 08:00:18 +0100
committerFlorian Bruhin <git@the-compiler.org>2018-03-12 08:03:42 +0100
commit4bff190db950203e443610b7a9e9028d7b5916f8 (patch)
tree0446cfa0cfd2a5ba6fd2c909caa7207d4083a74c
parent8b588d2635766396e1441786c4950247f512cdd9 (diff)
downloadqutebrowser-4bff190db950203e443610b7a9e9028d7b5916f8.tar.gz
qutebrowser-4bff190db950203e443610b7a9e9028d7b5916f8.zip
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. (cherry picked from commit 990c0707f4533bab35be1c24dd7dca759fc8fdcd)
-rw-r--r--qutebrowser/config/configtypes.py6
-rw-r--r--tests/unit/config/test_configtypes.py21
2 files changed, 25 insertions, 2 deletions
diff --git a/qutebrowser/config/configtypes.py b/qutebrowser/config/configtypes.py
index 14855bf03..71ff898ea 100644
--- a/qutebrowser/config/configtypes.py
+++ b/qutebrowser/config/configtypes.py
@@ -451,7 +451,7 @@ class List(BaseType):
def from_obj(self, value):
if value is None:
return []
- return value
+ return [self.valtype.from_obj(v) for v in value]
def to_py(self, value):
self._basic_py_validation(value, list)
@@ -1199,7 +1199,9 @@ class Dict(BaseType):
def from_obj(self, value):
if value is None:
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):
"""Fill missing fixed keys with a None-value."""
diff --git a/tests/unit/config/test_configtypes.py b/tests/unit/config/test_configtypes.py
index 4dd7837fb..930234425 100644
--- a/tests/unit/config/test_configtypes.py
+++ b/tests/unit/config/test_configtypes.py
@@ -533,6 +533,14 @@ class FlagListSubclass(configtypes.FlagList):
'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:
"""Test List and FlagList."""
@@ -647,6 +655,12 @@ class TestList:
with pytest.raises(AssertionError):
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:
@@ -1665,6 +1679,13 @@ class TestDict:
print(doc)
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):
return configtypes.File(required=False, **kwargs)