From 990c0707f4533bab35be1c24dd7dca759fc8fdcd Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 12 Mar 2018 08:00:18 +0100 Subject: [PATCH] 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. --- qutebrowser/config/configtypes.py | 6 ++++-- tests/unit/config/test_configtypes.py | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/qutebrowser/config/configtypes.py b/qutebrowser/config/configtypes.py index 86b72a9ff..e84d55075 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)