Remove the parameter enforce_type from set_override and set_default
It's no longer possible to assign a value of a different type in the config via an override. This behavior was changed in the last release to default to disallow overrides of different types, and in this release it is no longer able to be overridden Change-Id: I8a5e868e0adc2c7b6f46f56ee77a8129c34badb9
This commit is contained in:
parent
85daf7a6eb
commit
27d781ea45
@ -2705,12 +2705,8 @@ class ConfigOpts(collections.Mapping):
|
||||
__import__(module_str)
|
||||
self._get_group(group)
|
||||
|
||||
@removals.removed_kwarg('enforce_type', "The argument enforce_type has "
|
||||
"changed its default value to True and then will"
|
||||
" be removed completely.",
|
||||
version='4.0', removal_version='5.0')
|
||||
@__clear_cache
|
||||
def set_override(self, name, override, group=None, enforce_type=True):
|
||||
def set_override(self, name, override, group=None):
|
||||
"""Override an opt value.
|
||||
|
||||
Override the command line, config file and default values of a
|
||||
@ -2719,21 +2715,15 @@ class ConfigOpts(collections.Mapping):
|
||||
:param name: the name/dest of the opt
|
||||
:param override: the override value
|
||||
:param group: an option OptGroup object or group name
|
||||
:param enforce_type: a boolean whether to convert the override
|
||||
value to the option's type, None is *not* converted even
|
||||
if enforce_type is True.
|
||||
|
||||
:raises: NoSuchOptError, NoSuchGroupError
|
||||
"""
|
||||
opt_info = self._get_opt_info(name, group)
|
||||
opt_info['override'] = self._get_enforced_type_value(
|
||||
opt_info['opt'], override, enforce_type)
|
||||
opt_info['opt'], override)
|
||||
|
||||
@removals.removed_kwarg('enforce_type', "The argument enforce_type has "
|
||||
"changed its default value to True and then will"
|
||||
" be removed completely.",
|
||||
version='4.0', removal_version='5.0')
|
||||
@__clear_cache
|
||||
def set_default(self, name, default, group=None, enforce_type=True):
|
||||
def set_default(self, name, default, group=None):
|
||||
"""Override an opt's default value.
|
||||
|
||||
Override the default value of given option. A command line or
|
||||
@ -2742,27 +2732,18 @@ class ConfigOpts(collections.Mapping):
|
||||
:param name: the name/dest of the opt
|
||||
:param default: the default value
|
||||
:param group: an option OptGroup object or group name
|
||||
:param enforce_type: a boolean whether to convert the default
|
||||
value to the option's type, None is *not* converted even
|
||||
if enforce_type is True.
|
||||
|
||||
:raises: NoSuchOptError, NoSuchGroupError
|
||||
"""
|
||||
opt_info = self._get_opt_info(name, group)
|
||||
opt_info['default'] = self._get_enforced_type_value(
|
||||
opt_info['opt'], default, enforce_type)
|
||||
opt_info['opt'], default)
|
||||
|
||||
def _get_enforced_type_value(self, opt, value, enforce_type):
|
||||
def _get_enforced_type_value(self, opt, value):
|
||||
if value is None:
|
||||
return None
|
||||
try:
|
||||
converted = self._convert_value(value, opt)
|
||||
except (ValueError, TypeError):
|
||||
if enforce_type:
|
||||
raise
|
||||
if enforce_type:
|
||||
return converted
|
||||
else:
|
||||
return value
|
||||
|
||||
return self._convert_value(value, opt)
|
||||
|
||||
@__clear_cache
|
||||
def clear_override(self, name, group=None):
|
||||
|
@ -63,18 +63,10 @@ class Config(fixtures.Fixture):
|
||||
the specified configuration option group, otherwise the overrides
|
||||
are applied to the ``default`` group.
|
||||
|
||||
If a `enforce_type` is supplied, will convert the override
|
||||
value to the option's type before overriding.
|
||||
"""
|
||||
|
||||
group = kw.pop('group', None)
|
||||
enforce_type = kw.pop('enforce_type', True)
|
||||
for k, v in kw.items():
|
||||
if enforce_type is False:
|
||||
self.conf.set_override(k, v, group, enforce_type=False)
|
||||
else:
|
||||
# this removes the deprecation warning if you are just
|
||||
# using defaults
|
||||
self.conf.set_override(k, v, group)
|
||||
|
||||
def _unregister_config_opts(self):
|
||||
|
@ -3301,15 +3301,6 @@ class OverridesTestCase(BaseTestCase):
|
||||
self.conf.clear_override('foo')
|
||||
self.assertIsNone(self.conf.foo)
|
||||
|
||||
def test_no_enforce_type_str_override(self):
|
||||
self.conf.register_opt(cfg.StrOpt('foo'))
|
||||
self.conf.set_override('foo', True, enforce_type=False)
|
||||
self.conf([])
|
||||
# Ensure we don't change the provided type by mistake
|
||||
self.assertEqual(True, self.conf.foo)
|
||||
self.conf.clear_override('foo')
|
||||
self.assertIsNone(self.conf.foo)
|
||||
|
||||
def test_enforce_type_wrong_type_override(self):
|
||||
self.conf.register_opt(cfg.IntOpt('foo'))
|
||||
self.assertRaises(ValueError, self.conf.set_override,
|
||||
|
@ -46,12 +46,7 @@ class ConfigTestCase(base.BaseTestCase):
|
||||
f = self._make_fixture()
|
||||
self.assertEqual(5, f.conf.get('test2'))
|
||||
self.assertEqual('a', f.conf.get('test3'))
|
||||
# with enforce_type=False
|
||||
f.config(test2=-1, enforce_type=False)
|
||||
self.assertEqual(-1, f.conf.get('test2'))
|
||||
f.config(test3='c', enforce_type=False)
|
||||
self.assertEqual('c', f.conf.get('test3'))
|
||||
# with enforce_type=True
|
||||
# enforce type will always be true now
|
||||
self.assertRaises(ValueError, f.config, test2=-1)
|
||||
self.assertRaises(ValueError, f.config, test3='c')
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user