-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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 optional parameters #3079
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import luigi | ||
import mock | ||
|
||
from helpers import LuigiTestCase, with_config | ||
|
||
|
||
class OptionalParameterTest(LuigiTestCase): | ||
|
||
def actual_test(self, cls, default, expected_value, expected_type, bad_data, **kwargs): | ||
|
||
class TestConfig(luigi.Config): | ||
param = cls(default=default, **kwargs) | ||
empty_param = cls(default=default, **kwargs) | ||
|
||
def run(self): | ||
assert self.param == expected_value | ||
assert self.empty_param is None | ||
|
||
# Test parsing empty string (should be None) | ||
self.assertIsNone(cls(**kwargs).parse('')) | ||
|
||
# Test that warning is raised only with bad type | ||
with mock.patch('luigi.parameter.warnings') as warnings: | ||
TestConfig() | ||
warnings.warn.assert_not_called() | ||
|
||
if cls != luigi.OptionalChoiceParameter: | ||
with mock.patch('luigi.parameter.warnings') as warnings: | ||
TestConfig(param=None) | ||
warnings.warn.assert_not_called() | ||
|
||
with mock.patch('luigi.parameter.warnings') as warnings: | ||
TestConfig(param=bad_data) | ||
if cls == luigi.OptionalBoolParameter: | ||
warnings.warn.assert_not_called() | ||
else: | ||
warnings.warn.assert_called_with( | ||
'{} "param" with value "{}" is not of type "{}" or None.'.format( | ||
cls.__name__, | ||
bad_data, | ||
expected_type | ||
), | ||
luigi.parameter.OptionalParameterTypeWarning | ||
) | ||
|
||
# Test with value from config | ||
self.assertTrue(luigi.build([TestConfig()], local_scheduler=True)) | ||
|
||
@with_config({"TestConfig": {"param": "expected value", "empty_param": ""}}) | ||
def test_optional_parameter(self): | ||
self.actual_test(luigi.OptionalParameter, None, "expected value", "str", 0) | ||
self.actual_test(luigi.OptionalParameter, "default value", "expected value", "str", 0) | ||
|
||
@with_config({"TestConfig": {"param": "10", "empty_param": ""}}) | ||
def test_optional_int_parameter(self): | ||
self.actual_test(luigi.OptionalIntParameter, None, 10, "int", "bad data") | ||
self.actual_test(luigi.OptionalIntParameter, 1, 10, "int", "bad data") | ||
|
||
@with_config({"TestConfig": {"param": "true", "empty_param": ""}}) | ||
def test_optional_bool_parameter(self): | ||
self.actual_test(luigi.OptionalBoolParameter, None, True, "bool", "bad data") | ||
self.actual_test(luigi.OptionalBoolParameter, False, True, "bool", "bad data") | ||
|
||
@with_config({"TestConfig": {"param": "10.5", "empty_param": ""}}) | ||
def test_optional_float_parameter(self): | ||
self.actual_test(luigi.OptionalFloatParameter, None, 10.5, "float", "bad data") | ||
self.actual_test(luigi.OptionalFloatParameter, 1.5, 10.5, "float", "bad data") | ||
|
||
@with_config({"TestConfig": {"param": '{"a": 10}', "empty_param": ""}}) | ||
def test_optional_dict_parameter(self): | ||
self.actual_test(luigi.OptionalDictParameter, None, {"a": 10}, "FrozenOrderedDict", "bad data") | ||
self.actual_test(luigi.OptionalDictParameter, {"a": 1}, {"a": 10}, "FrozenOrderedDict", "bad data") | ||
|
||
@with_config({"TestConfig": {"param": "[10.5]", "empty_param": ""}}) | ||
def test_optional_list_parameter(self): | ||
self.actual_test(luigi.OptionalListParameter, None, (10.5, ), "tuple", "bad data") | ||
self.actual_test(luigi.OptionalListParameter, (1.5, ), (10.5, ), "tuple", "bad data") | ||
|
||
@with_config({"TestConfig": {"param": "[10.5]", "empty_param": ""}}) | ||
def test_optional_tuple_parameter(self): | ||
self.actual_test(luigi.OptionalTupleParameter, None, (10.5, ), "tuple", "bad data") | ||
self.actual_test(luigi.OptionalTupleParameter, (1.5, ), (10.5, ), "tuple", "bad data") | ||
|
||
@with_config({"TestConfig": {"param": "10.5", "empty_param": ""}}) | ||
def test_optional_numerical_parameter_float(self): | ||
self.actual_test(luigi.OptionalNumericalParameter, None, 10.5, "float", "bad data", var_type=float, min_value=0, max_value=100) | ||
self.actual_test(luigi.OptionalNumericalParameter, 1.5, 10.5, "float", "bad data", var_type=float, min_value=0, max_value=100) | ||
|
||
@with_config({"TestConfig": {"param": "10", "empty_param": ""}}) | ||
def test_optional_numerical_parameter_int(self): | ||
self.actual_test(luigi.OptionalNumericalParameter, None, 10, "int", "bad data", var_type=int, min_value=0, max_value=100) | ||
self.actual_test(luigi.OptionalNumericalParameter, 1, 10, "int", "bad data", var_type=int, min_value=0, max_value=100) | ||
|
||
@with_config({"TestConfig": {"param": "expected value", "empty_param": ""}}) | ||
def test_optional_choice_parameter(self): | ||
choices = ["default value", "expected value"] | ||
self.actual_test(luigi.OptionalChoiceParameter, None, "expected value", "str", "bad data", choices=choices) | ||
self.actual_test(luigi.OptionalChoiceParameter, "default value", "expected value", "str", "bad data", choices=choices) | ||
|
||
@with_config({"TestConfig": {"param": "1", "empty_param": ""}}) | ||
def test_optional_choice_parameter_int(self): | ||
choices = [0, 1, 2] | ||
self.actual_test(luigi.OptionalChoiceParameter, None, 1, "int", "bad data", var_type=int, choices=choices) | ||
self.actual_test(luigi.OptionalChoiceParameter, "default value", 1, "int", "bad data", var_type=int, choices=choices) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is OptionalListParameter intentionally a tuple? As opposed to a list?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is intentional because the
_warn_on_wrong_param_type
is called afternormalize
which callsrecursively_freeze
on the value, thus thelist
is transformed into atuple
before the type is checked. That's why we expect a tuple here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah right. I vaguely recall that confusion. I believe I added List Parameter and Tuple Parameter separately and later wished I hadn't added both. But alas, we have them both and treat them nearly the same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed it would be simpler to have only one of these 2.