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

SDK - Lightweight - Added support for "None" default values #1626

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 19 additions & 4 deletions sdk/python/kfp/components/_python_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,12 @@ def annotation_to_type_struct(annotation):
input_spec = InputSpec(
name=parameter.name,
type=type_struct,
default=str(parameter.default) if parameter.default is not inspect.Parameter.empty else None,
)
if parameter.default is not inspect.Parameter.empty:
if parameter.default is None:
input_spec.optional = True
else:
input_spec.default = str(parameter.default)
inputs.append(input_spec)

#Analyzing the return type annotations.
Expand Down Expand Up @@ -245,16 +249,27 @@ def _func_to_component_spec(func, extra_code='', base_image=_default_base_image,
arguments = []
for input in component_spec.inputs:
param_flag = "--" + input.name.replace("_", "-")
is_required = not input.optional and input.default is None
Ark-kun marked this conversation as resolved.
Show resolved Hide resolved
line = '_parser.add_argument("{param_flag}", dest="{param_var}", type={param_type}, required={is_required}, default={default_repr})'.format(
param_flag=param_flag,
param_var=input.name,
param_type=(input.type if input.type in ['int', 'float', 'bool'] else 'str'),
is_required=str(input.default is None), # TODO: Handle actual 'None' defaults!
is_required=str(is_required),
default_repr=repr(str(input.default)) if input.default is not None else None,
)
arg_parse_code_lines.append(line)
arguments.append(param_flag)
arguments.append(InputValuePlaceholder(input.name))
if is_required:
arguments.append(param_flag)
arguments.append(InputValuePlaceholder(input.name))
else:
arguments.append(
IfPlaceholder(
IfPlaceholderStructure(
condition=IsPresentPlaceholder(input.name),
then_value=[param_flag, InputValuePlaceholder(input.name)],
)
)
)

if component_spec.outputs:
param_flag="----output-paths"
Expand Down
1 change: 1 addition & 0 deletions sdk/python/kfp/components/_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
'OutputPathPlaceholder',
'ConcatPlaceholder',
'IsPresentPlaceholder',
'IfPlaceholderStructure',
'IfPlaceholder',

'ContainerSpec',
Expand Down
9 changes: 9 additions & 0 deletions sdk/python/tests/components/test_python_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,15 @@ def add_multiply_two_numbers(a: float = 3, b: float = 5) -> NamedTuple('DummyNam
self.assertEqual(component_spec.inputs[0].default, '3')
self.assertEqual(component_spec.inputs[1].default, '5')

def test_handling_default_value_of_none(self):
def assert_is_none(a, b, arg=None) -> int:
assert arg is None
return 1

func = assert_is_none
op = comp.func_to_container_op(func, output_component_file='comp.yaml')
self.helper_test_2_in_1_out_component_using_local_call(func, op)

def test_end_to_end_python_component_pipeline_compilation(self):
import kfp.components as comp

Expand Down