Merge "Remove the parameter enforce_type from set_override and set_default"

This commit is contained in:
Zuul 2017-10-25 17:37:13 +00:00 committed by Gerrit Code Review
commit a3e7f8aa17
4 changed files with 11 additions and 52 deletions

View File

@ -2708,12 +2708,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
@ -2722,21 +2718,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
@ -2745,27 +2735,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):

View File

@ -63,19 +63,11 @@ 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)
self.conf.set_override(k, v, group)
def _unregister_config_opts(self):
for group in self._registered_config_opts:

View File

@ -3304,15 +3304,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,

View File

@ -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')