Skip to content
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

Improve message when an exception is raised during default value parsing #3115

Merged
merged 2 commits into from
Oct 23, 2021
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
6 changes: 5 additions & 1 deletion luigi/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,11 @@ def get_param_values(cls, params, args, kwargs):
# Then use the defaults for anything not filled in
for param_name, param_obj in params:
if param_name not in result:
if not param_obj.has_task_value(task_family, param_name):
try:
has_task_value = param_obj.has_task_value(task_family, param_name)
except Exception as exc:
raise ValueError("%s: Error when parsing the default value of '%s'" % (exc_desc, param_name)) from exc
if not has_task_value:
raise parameter.MissingParameterException("%s: requires the '%s' parameter to be set" % (exc_desc, param_name))
result[param_name] = param_obj.task_value(task_family, param_name)

Expand Down
12 changes: 8 additions & 4 deletions test/parameter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,22 +821,26 @@ def testTimeDelta8601Weeks(self):
p = luigi.TimeDeltaParameter(config_path=dict(section="foo", name="bar"))
self.assertEqual(timedelta(weeks=5), _value(p))

@mock.patch('luigi.parameter.ParameterException')
@with_config({"foo": {"bar": "P3Y6M4DT12H30M5S"}})
def testTimeDelta8601YearMonthNotSupported(self):
def testTimeDelta8601YearMonthNotSupported(self, exc):
def f():
return _value(luigi.TimeDeltaParameter(config_path=dict(section="foo", name="bar")))
self.assertRaises(luigi.parameter.ParameterException, f) # ISO 8601 durations with years or months are not supported
self.assertRaises(ValueError, f) # ISO 8601 durations with years or months are not supported
exc.assert_called_once_with("Invalid time delta - could not parse P3Y6M4DT12H30M5S")

@with_config({"foo": {"bar": "PT6M"}})
def testTimeDelta8601MAfterT(self):
p = luigi.TimeDeltaParameter(config_path=dict(section="foo", name="bar"))
self.assertEqual(timedelta(minutes=6), _value(p))

@mock.patch('luigi.parameter.ParameterException')
@with_config({"foo": {"bar": "P6M"}})
def testTimeDelta8601MBeforeT(self):
def testTimeDelta8601MBeforeT(self, exc):
def f():
return _value(luigi.TimeDeltaParameter(config_path=dict(section="foo", name="bar")))
self.assertRaises(luigi.parameter.ParameterException, f) # ISO 8601 durations with months are not supported
self.assertRaises(ValueError, f) # ISO 8601 durations with months are not supported
exc.assert_called_once_with("Invalid time delta - could not parse P6M")

def testHasDefaultNoSection(self):
self.assertRaises(luigi.parameter.MissingParameterException,
Expand Down
10 changes: 9 additions & 1 deletion test/task_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import pickle
import warnings

from helpers import unittest, LuigiTestCase
from helpers import unittest, LuigiTestCase, with_config
from datetime import datetime, timedelta

import luigi
Expand Down Expand Up @@ -161,6 +161,14 @@ class ATask(luigi.Task):
task = ATask()
self.assertEqual(task.disable_window_seconds, 17)

@with_config({"ATaskWithBadParam": {"bad_param": "bad_value"}})
def test_bad_param(self):
class ATaskWithBadParam(luigi.Task):
bad_param = luigi.IntParameter()

with self.assertRaisesRegex(ValueError, r"ATaskWithBadParam\[args=\(\), kwargs={}\]: Error when parsing the default value of 'bad_param'"):
ATaskWithBadParam()


class ExternalizeTaskTest(LuigiTestCase):

Expand Down