8000 Reconcile underscore/dash config style handling (#2688) by soxofaan · Pull Request #2691 · spotify/luigi · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Reconcile underscore/dash config style handling (#2688) #2691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion luigi/configuration/cfg_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,18 @@ def _get_with_default(self, method, section, option, default, expected_type=None
Raises an exception if the default value is not None and doesn't match the expected_type.
"""
try:
return method(self, section, option, **kwargs)
try:
# Underscore-style is the recommended configuration style
option = option.replace('-', '_')
return method(self, section, option, **kwargs)
except (NoOptionError, NoSectionError):
# Support dash-style option names (with deprecation warning).
option_alias = option.replace('_', '-')
value = method(self, section, option_alias, **kwargs)
warn = 'Configuration [{s}] {o} (with dashes) should be avoided. Please use underscores: {u}.'.format(
s=section, o=option_alias, u=option)
warnings.warn(warn, DeprecationWarning)
return value
except (NoOptionError, NoSectionError):
if default is LuigiConfigParser.NO_DEFAULT:
raise
Expand Down
3 changes: 0 additions & 3 deletions luigi/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,6 @@ def _value_iterator(self, task_name, param_name):
found = getattr(cp_parser.known_args, dest, None)
yield (self._parse_or_no_value(found), None)
yield (self._get_value_from_config(task_name, param_name), None)
yield (self._get_value_from_config(task_name, param_name.replace('_', '-')),
'Configuration [{}] {} (with dashes) should be avoided. Please use underscores.'.format(
task_name, param_name))
if self._config_path:
yield (self._get_value_from_config(self._config_path['section'], self._config_path['name']),
'The use of the configuration [{}] {} is deprecated. Please use [{}] {}'.format(
Expand Down
20 changes: 20 additions & 0 deletions test/config_env_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,23 @@ def test_env_interpolation(self):

with self.assertRaises(InterpolationMissingEnvvarError):
config.get("test", "d")

@with_config({"test": {
"foo-bar": "fob",
"baz_qux": "bax",
}})
def test_underscore_vs_dash_style(self):
config = get_config()
self.assertEqual(config.get("test", "foo-bar"), "fob")
self.assertEqual(config.get("test", "foo_bar"), "fob")
self.assertEqual(config.get("test", "baz-qux"), "bax")
self.assertEqual(config.get("test", "baz_qux"), "bax")

@with_config({"test": {
"foo-bar": "fob",
"foo_bar": "bax",
}})
def test_underscore_vs_dash_style_priority(self):
config = get_config()
self.assertEqual(config.get("test", "foo-bar"), "bax")
self.assertEqual(config.get("test", "foo_bar"), "bax")
7 changes: 6 additions & 1 deletion test/parameter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,11 @@ def test_use_config_class_with_configuration_2(self):
self.assertEqual(MyConfigWithoutSection().mc_r, 555)
self.assertEqual(MyConfigWithoutSection().mc_s, 888)

@with_config({"MyConfig": {"mc_p": "555", "mc-p": "666", "mc-q": "777"}})
def test_configuration_style(self):
self.assertEqual(MyConfig().mc_p, 555)
self.assertEqual(MyConfig().mc_q, 777)

def test_misc_1(self):
class Dogs(luigi.Config):
n_dogs = luigi.IntParameter()
Expand Down Expand Up @@ -1005,7 +1010,7 @@ def testOverrideSchedulerPort(self):
@with_config({"core": {"scheduler-port": '6544'}})
def testOverrideSchedulerPort2(self):
if six.PY3:
with self.assertWarnsRegex(DeprecationWarning, r'scheduler_port \(with dashes\) should be avoided'):
with self.assertWarnsRegex(DeprecationWarning, r'scheduler-port \(with dashes\) should be avoided'):
env_params = luigi.interface.core()
else:
env_params = luigi.interface.core()
Expand Down
0