Skip to content

Commit

Permalink
SDK/Components - Switching to map-based syntax for the arguments. (#29)
Browse files Browse the repository at this point in the history
* Switching to map-based syntax for the arguments.

The list-based syntax is going to be deprecated.

* Switched to map-style arguments in _func_to_component_spec

* Updated testdata
  • Loading branch information
Ark-kun authored and k8s-ci-robot committed Nov 5, 2018
1 parent e77a91f commit 5409792
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions sdk/python/kfp/compiler/_component_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def _generate_pythonop(component_func, target_image):
'name': input,
'type': 'str'
})
component_artifact['implementation']['dockerContainer']['arguments'].append(['Value', input])
component_artifact['implementation']['dockerContainer']['arguments'].append({'value': input})
return _create_task_factory_from_component_dict(component_artifact)

def build_python_component(component_func, staging_gcs_path, target_image, build_image=True, timeout=600, namespace='kubeflow'):
Expand Down Expand Up @@ -370,4 +370,4 @@ def build_docker_image(staging_gcs_path, target_image, dockerfile_path, timeout=
logging.getLogger().setLevel('INFO')
builder = ImageBuilder(gcs_base=staging_gcs_path, target_image=target_image)
builder.build_image_from_dockerfile(dockerfile_path=dockerfile_path, timeout=timeout, namespace=namespace)
logging.info('Build image complete.')
logging.info('Build image complete.')
20 changes: 10 additions & 10 deletions sdk/python/kfp/components/_python_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,24 @@ def _func_to_component_spec(func, extra_code='', base_image=_default_base_image)

def annotation_to_argument_kind_and_type_name(annotation):
if not annotation or annotation == inspect.Parameter.empty:
return ('Value', None)
return ('value', None)
if hasattr(annotation, '__origin__'): #Generic type
type_name = annotation.__origin__.__name__
type_args = annotation.__args__
#if len(type_args) != 1:
# raise TypeError('Unsupported generic type {}'.format(type_name))
inner_type = type_args[0]
if type_name == InputFile.__name__:
return ('File', inner_type.__name__)
return ('file', inner_type.__name__)
elif type_name == OutputFile.__name__:
return ('Output', inner_type.__name__)
return ('output', inner_type.__name__)
if isinstance(annotation, type):
return ('Value', annotation.__name__)
return ('value', annotation.__name__)
else:
#!!! It's important to preserve string anotations as strings. Annotations that are neither types nor strings are converted to strings.
#Materializer adds double quotes to the types it does not recognize. - fix it to not quote strings.
#We need two kind of strings: we can use any type name for component YAML, but for generated Python code we must use valid python type annotations.
return ('Value', "'" + str(annotation) + "'")
return ('value', "'" + str(annotation) + "'")

for parameter in parameters:
annotation = parameter.annotation
Expand All @@ -91,14 +91,14 @@ def annotation_to_argument_kind_and_type_name(annotation):
parameter_to_type_name[parameter.name] = parameter_type_name

#TODO: Humanize the input/output names
arguments.append([argument_kind, parameter.name])
arguments.append({argument_kind: parameter.name})

parameter_spec = OrderedDict([('name', parameter.name)])
if parameter_type_name:
parameter_spec['type'] = parameter_type_name
if argument_kind == 'Value' or argument_kind == 'File':
if argument_kind == 'value' or argument_kind == 'file':
inputs.append(parameter_spec)
elif argument_kind == 'Output':
elif argument_kind == 'output':
outputs.append(parameter_spec)
else:
#Cannot happen
Expand All @@ -120,15 +120,15 @@ def annotation_to_argument_kind_and_type_name(annotation):
output_spec['type'] = output_type_name
outputs.append(output_spec)
extra_output_names.append(field_name)
arguments.append(['Output', field_name])
arguments.append({'output': field_name})
else:
output_spec = OrderedDict([('name', single_output_name_const)])
(_, output_type_name) = annotation_to_argument_kind_and_type_name(signature.return_annotation)
if output_type_name:
output_spec['type'] = output_type_name
outputs.append(output_spec)
extra_output_names.append(single_output_pythonic_name_const)
arguments.append(['Output', single_output_name_const])
arguments.append({'output': single_output_name_const})

func_name=func.__name__

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ implementation:
import fire
fire.Fire(add_wrapper)
arguments:
- [Value, a]
- [Value, b]
- [Output, Output]
- {value: a}
- {value: b}
- {output: Output}

0 comments on commit 5409792

Please sign in to comment.