Skip to content

Commit

Permalink
Fix OptionalParameter.normalize
Browse files Browse the repository at this point in the history
Change-Id: Iecea2d9d1eaddfa1600f567bf7d5bc78015d5777
  • Loading branch information
adrien-berchet committed Nov 27, 2020
1 parent d11bb77 commit ab657d5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
9 changes: 8 additions & 1 deletion luigi_tools/parameters.py
Expand Up @@ -73,12 +73,19 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def parse(self, x):
"""Parse the given value if it is not ``None`` or ``'null'``."""
"""Parse the given value if it is not an empty string, ``None`` or ``'null'``."""
if x and x.lower() != "null":
return self._base_cls.parse(self, x)
else:
return None

def normalize(self, x):
"""Normalize the given value if it is not ``None``."""
if x is not None:
return self._base_cls.normalize(self, x)
else:
return None

def _warn_on_wrong_param_type(self, param_name, param_value):
if self.__class__ != self._cls: # pragma: no cover
return
Expand Down
12 changes: 11 additions & 1 deletion tests/test_parameters.py
Expand Up @@ -80,12 +80,14 @@ class TaskOptionalParameter(luigi.Task):
default=0.75, min_value=0, max_value=1, var_type=float
)
d = luigi_tools.parameters.OptionalRatioParameter(default=0.5)
e = luigi_tools.parameters.OptionalIntParameter(default=None)
expected_a = luigi.IntParameter(default=1)
expected_b = luigi.FloatParameter(default=1.5)
expected_c = luigi.NumericalParameter(
default=0.75, min_value=0, max_value=1, var_type=float
)
expected_d = luigi_tools.parameters.RatioParameter(default=0.5)
expected_e = luigi.Parameter(default=None)

def run(self):
print(
Expand All @@ -97,17 +99,20 @@ def run(self):
self.c,
"self.d =",
self.d,
"self.e =",
self.e,
)
assert self.a == self.expected_a
assert self.b == self.expected_b
assert self.c == self.expected_c
assert self.d == self.expected_d
assert self.e == self.expected_e
create_empty_file(self.output().path)

def output(self):
return luigi.LocalTarget(
luigi_tools_working_directory
/ f"test_optional_parameter_{self.a}_{self.b}_{self.c}_{self.d}.test"
/ f"test_optional_parameter_{self.a}_{self.b}_{self.c}_{self.d}_{self.e}.test"
)

return TaskOptionalParameter
Expand All @@ -124,6 +129,7 @@ def output(self):
"b": "null",
"c": "null",
"d": "null",
"e": "null",
}
}
)
Expand All @@ -135,6 +141,7 @@ def output(self):
expected_b=None,
expected_c=None,
expected_d=None,
expected_e=None,
)
],
local_scheduler=True,
Expand All @@ -151,6 +158,8 @@ def output(self):
"expected_c": "0.8",
"d": "0.9",
"expected_d": "0.9",
"e": "1",
"expected_e": "1",
}
}
)
Expand All @@ -162,6 +171,7 @@ def output(self):
expected_b=0.7,
expected_c=0.8,
expected_d=0.9,
expected_e=1,
)
],
local_scheduler=True,
Expand Down

0 comments on commit ab657d5

Please sign in to comment.