Skip to content

Commit f78a067

Browse files
starheldlstadther
andauthored
Silence false positive UnconsumedParameterWarning (spotify#3235)
* Silence false positive UnconsumedParameterWarning Do not emit UnconsumedParameterWarning when parameter name is written in camelCase or with dashes. * Silence warnings for not consumed autoload_range and no_configure_logging parameters in core config. --------- Co-authored-by: Dillon Stadther <dlstadther@gmail.com>
1 parent 9c719b0 commit f78a067

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

luigi/configuration/cfg_parser.py

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ def before_write(self, parser, section, option, value):
117117
class LuigiConfigParser(BaseParser, ConfigParser):
118118
NO_DEFAULT = object()
119119
enabled = True
120+
optionxform = str
120121
_instance = None
121122
_config_paths = [
122123
'/etc/luigi/client.cfg', # Deprecated old-style global luigi config

luigi/interface.py

+4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ class core(task.Config):
4949
This is arguably a bit of a hack.
5050
'''
5151
use_cmdline_section = False
52+
ignore_unconsumed = {
53+
'autoload_range',
54+
'no_configure_logging',
55+
}
5256

5357
local_scheduler = parameter.BoolParameter(
5458
default=False,

luigi/task.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -437,9 +437,11 @@ def list_to_tuple(x):
437437
if not hasattr(cls, "_unconsumed_params"):
438438
cls._unconsumed_params = set()
439439
if task_family in conf.sections():
440+
ignore_unconsumed = getattr(cls, 'ignore_unconsumed', set())
440441
for key, value in conf[task_family].items():
442+
key = key.replace('-', '_')
441443
composite_key = f"{task_family}_{key}"
442-
if key not in result and composite_key not in cls._unconsumed_params:
444+
if key not in result and key not in ignore_unconsumed and composite_key not in cls._unconsumed_params:
443445
warnings.warn(
444446
"The configuration contains the parameter "
445447
f"'{key}' with value '{value}' that is not consumed by the task "

test/task_test.py

+59
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,65 @@ class TaskB(luigi.Task):
218218
f"the task '{task_name}'."
219219
)
220220

221+
@with_config(
222+
{
223+
"TaskEdgeCase": {
224+
"camelParam": "camelCase",
225+
"underscore_param": "underscore",
226+
"dash-param": "dash",
227+
},
228+
}
229+
)
230+
def test_unconsumed_params_edge_cases(self):
231+
class TaskEdgeCase(luigi.Task):
232+
camelParam = luigi.Parameter()
233+
underscore_param = luigi.Parameter()
234+
dash_param = luigi.Parameter()
235+
236+
with warnings.catch_warnings(record=True) as w:
237+
warnings.filterwarnings(
238+
action="ignore",
239+
category=Warning,
240+
)
241+
warnings.simplefilter(
242+
action="always",
243+
category=luigi.parameter.UnconsumedParameterWarning,
244+
)
245+
246+
task = TaskEdgeCase()
247+
assert len(w) == 0
248+
assert task.camelParam == "camelCase"
249+
assert task.underscore_param == "underscore"
250+
assert task.dash_param == "dash"
251+
252+
@with_config(
253+
{
254+
"TaskIgnoreUnconsumed": {
255+
"a": "a",
256+
"b": "b",
257+
"c": "c",
258+
},
259+
}
260+
)
261+
def test_unconsumed_params_ignore_unconsumed(self):
262+
class TaskIgnoreUnconsumed(luigi.Task):
263+
ignore_unconsumed = {"b", "d"}
264+
265+
a = luigi.Parameter()
266+
267+
with warnings.catch_warnings(record=True) as w:
268+
warnings.filterwarnings(
269+
action="ignore",
270+
category=Warning,
271+
)
272+
warnings.simplefilter(
273+
action="always",
274+
category=luigi.parameter.UnconsumedParameterWarning,
275+
)
276+
277+
TaskIgnoreUnconsumed()
278+
assert len(w) == 1
279+
221280

222281
class TaskFlattenOutputTest(unittest.TestCase):
223282
def test_single_task(self):

0 commit comments

Comments
 (0)