Simplify _none_value for List/Dict configtypes
We don't need to check for fixed_keys/required_keys in get_obj (only get_py), and we don't need to care about mutability in get_py.
This commit is contained in:
parent
da0a2b8578
commit
9d8b76e497
@ -189,6 +189,8 @@ class KeyConfig:
|
|||||||
raise configexc.DuplicateKeyError(key)
|
raise configexc.DuplicateKeyError(key)
|
||||||
|
|
||||||
bindings = self._config.get_obj('bindings.commands')
|
bindings = self._config.get_obj('bindings.commands')
|
||||||
|
if mode not in bindings:
|
||||||
|
bindings[mode] = {}
|
||||||
bindings[mode][key] = command
|
bindings[mode][key] = command
|
||||||
self._config.update_mutables(save_yaml=save_yaml)
|
self._config.update_mutables(save_yaml=save_yaml)
|
||||||
|
|
||||||
@ -198,11 +200,13 @@ class KeyConfig:
|
|||||||
|
|
||||||
bindings_commands = self._config.get_obj('bindings.commands')
|
bindings_commands = self._config.get_obj('bindings.commands')
|
||||||
|
|
||||||
if key in bindings_commands:
|
if key in val.bindings.commands[mode]:
|
||||||
# In custom bindings -> remove it
|
# In custom bindings -> remove it
|
||||||
del bindings_commands[mode][key]
|
del bindings_commands[mode][key]
|
||||||
elif key in val.bindings.default[mode]:
|
elif key in val.bindings.default[mode]:
|
||||||
# In default bindings -> shadow it with None
|
# In default bindings -> shadow it with None
|
||||||
|
if mode not in bindings_commands:
|
||||||
|
bindings_commands[mode] = {}
|
||||||
bindings_commands[mode][key] = None
|
bindings_commands[mode][key] = None
|
||||||
else:
|
else:
|
||||||
raise configexc.KeybindingError("Can't find binding '{}' in section '{}'!"
|
raise configexc.KeybindingError("Can't find binding '{}' in section '{}'!"
|
||||||
|
@ -389,18 +389,6 @@ class List(BaseType):
|
|||||||
self.valtype = valtype
|
self.valtype = valtype
|
||||||
self.length = length
|
self.length = length
|
||||||
|
|
||||||
def _none_value(self, value=None):
|
|
||||||
"""Return the value to be used when the setting is None.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
value: An existing value to be returned.
|
|
||||||
"""
|
|
||||||
if value is None:
|
|
||||||
return []
|
|
||||||
else:
|
|
||||||
assert value == [], value
|
|
||||||
return value
|
|
||||||
|
|
||||||
def get_name(self):
|
def get_name(self):
|
||||||
name = super().get_name()
|
name = super().get_name()
|
||||||
if self._show_valtype:
|
if self._show_valtype:
|
||||||
@ -413,7 +401,7 @@ class List(BaseType):
|
|||||||
def from_str(self, value):
|
def from_str(self, value):
|
||||||
self._basic_str_validation(value)
|
self._basic_str_validation(value)
|
||||||
if not value:
|
if not value:
|
||||||
return self._none_value()
|
return None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
yaml_val = utils.yaml_load(value)
|
yaml_val = utils.yaml_load(value)
|
||||||
@ -428,7 +416,7 @@ class List(BaseType):
|
|||||||
def to_py(self, value):
|
def to_py(self, value):
|
||||||
self._basic_py_validation(value, list)
|
self._basic_py_validation(value, list)
|
||||||
if not value:
|
if not value:
|
||||||
return self._none_value(value)
|
return []
|
||||||
|
|
||||||
if self.length is not None and len(value) != self.length:
|
if self.length is not None and len(value) != self.length:
|
||||||
raise configexc.ValidationError(value, "Exactly {} values need to "
|
raise configexc.ValidationError(value, "Exactly {} values need to "
|
||||||
@ -1052,31 +1040,10 @@ class Dict(BaseType):
|
|||||||
raise configexc.ValidationError(
|
raise configexc.ValidationError(
|
||||||
value, "Required keys {}".format(self.required_keys))
|
value, "Required keys {}".format(self.required_keys))
|
||||||
|
|
||||||
def _none_value(self, value=None):
|
|
||||||
"""Return the value to be used when the setting is None.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
value: An existing value to be returned.
|
|
||||||
"""
|
|
||||||
if self.fixed_keys is None:
|
|
||||||
if value is None:
|
|
||||||
return {}
|
|
||||||
else:
|
|
||||||
assert value == {}, value
|
|
||||||
return value
|
|
||||||
else:
|
|
||||||
if value is None:
|
|
||||||
return {key: {} for key in self.fixed_keys}
|
|
||||||
else:
|
|
||||||
assert value == {}, value
|
|
||||||
for key in self.fixed_keys:
|
|
||||||
value[key] = {}
|
|
||||||
return value
|
|
||||||
|
|
||||||
def from_str(self, value):
|
def from_str(self, value):
|
||||||
self._basic_str_validation(value)
|
self._basic_str_validation(value)
|
||||||
if not value:
|
if not value:
|
||||||
return self._none_value()
|
return None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
yaml_val = utils.yaml_load(value)
|
yaml_val = utils.yaml_load(value)
|
||||||
@ -1091,7 +1058,11 @@ class Dict(BaseType):
|
|||||||
def to_py(self, value):
|
def to_py(self, value):
|
||||||
self._basic_py_validation(value, dict)
|
self._basic_py_validation(value, dict)
|
||||||
if not value:
|
if not value:
|
||||||
return self._none_value(value)
|
if self.fixed_keys is None:
|
||||||
|
return {}
|
||||||
|
else:
|
||||||
|
return {key: self.valtype.to_py(None)
|
||||||
|
for key in self.fixed_keys}
|
||||||
|
|
||||||
self._validate_keys(value)
|
self._validate_keys(value)
|
||||||
|
|
||||||
@ -1197,7 +1168,7 @@ class ShellCommand(List):
|
|||||||
def from_str(self, value):
|
def from_str(self, value):
|
||||||
self._basic_str_validation(value)
|
self._basic_str_validation(value)
|
||||||
if not value:
|
if not value:
|
||||||
return self._none_value()
|
return None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
split_val = shlex.split(value)
|
split_val = shlex.split(value)
|
||||||
@ -1316,17 +1287,13 @@ class Padding(Dict):
|
|||||||
_show_valtype = False
|
_show_valtype = False
|
||||||
|
|
||||||
def __init__(self, none_ok=False):
|
def __init__(self, none_ok=False):
|
||||||
super().__init__(keytype=String(), valtype=Int(minval=0),
|
super().__init__(keytype=String(),
|
||||||
|
valtype=Int(minval=0, none_ok=none_ok),
|
||||||
fixed_keys=['top', 'bottom', 'left', 'right'],
|
fixed_keys=['top', 'bottom', 'left', 'right'],
|
||||||
none_ok=none_ok)
|
none_ok=none_ok)
|
||||||
|
|
||||||
def _none_value(self, _value=None):
|
|
||||||
return None
|
|
||||||
|
|
||||||
def to_py(self, value):
|
def to_py(self, value):
|
||||||
d = super().to_py(value)
|
d = super().to_py(value)
|
||||||
if not d:
|
|
||||||
return None
|
|
||||||
return PaddingValues(**d)
|
return PaddingValues(**d)
|
||||||
|
|
||||||
|
|
||||||
|
@ -253,12 +253,16 @@ class TestAll:
|
|||||||
def test_none_ok_true(self, klass):
|
def test_none_ok_true(self, klass):
|
||||||
"""Test None and empty string values with none_ok=True."""
|
"""Test None and empty string values with none_ok=True."""
|
||||||
typ = klass(none_ok=True)
|
typ = klass(none_ok=True)
|
||||||
if isinstance(typ, (configtypes.Dict, configtypes.List)):
|
if isinstance(typ, configtypes.Padding):
|
||||||
expected = typ._none_value()
|
to_py_expected = configtypes.PaddingValues(None, None, None, None)
|
||||||
|
elif isinstance(typ, configtypes.Dict):
|
||||||
|
to_py_expected = {}
|
||||||
|
elif isinstance(typ, configtypes.List):
|
||||||
|
to_py_expected = []
|
||||||
else:
|
else:
|
||||||
expected = None
|
to_py_expected = None
|
||||||
assert typ.from_str('') == expected
|
assert typ.from_str('') is None
|
||||||
assert typ.to_py(None) == expected
|
assert typ.to_py(None) == to_py_expected
|
||||||
assert typ.to_str(None) == ''
|
assert typ.to_str(None) == ''
|
||||||
|
|
||||||
@pytest.mark.parametrize('method, value', [
|
@pytest.mark.parametrize('method, value', [
|
||||||
@ -575,7 +579,8 @@ class TestList:
|
|||||||
except configexc.ValidationError:
|
except configexc.ValidationError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
assert typ.from_str(typ.to_str(converted)) == converted
|
expected = converted if converted else None
|
||||||
|
assert typ.from_str(typ.to_str(converted)) == expected
|
||||||
|
|
||||||
@hypothesis.given(val=strategies.lists(strategies.just('foo')))
|
@hypothesis.given(val=strategies.lists(strategies.just('foo')))
|
||||||
def test_hypothesis_text(self, klass, val):
|
def test_hypothesis_text(self, klass, val):
|
||||||
@ -1434,7 +1439,8 @@ class TestDict:
|
|||||||
none_ok=True)
|
none_ok=True)
|
||||||
try:
|
try:
|
||||||
converted = d.to_py(val)
|
converted = d.to_py(val)
|
||||||
assert d.from_str(d.to_str(converted)) == converted
|
expected = converted if converted else None
|
||||||
|
assert d.from_str(d.to_str(converted)) == expected
|
||||||
except configexc.ValidationError:
|
except configexc.ValidationError:
|
||||||
# Invalid unicode in the string, etc...
|
# Invalid unicode in the string, etc...
|
||||||
hypothesis.assume(False)
|
hypothesis.assume(False)
|
||||||
|
Loading…
Reference in New Issue
Block a user