Skip to content

Commit

Permalink
SDK - Components - Added component properties to the task factory fun…
Browse files Browse the repository at this point in the history
…ction

Problem: When the user loads component using the load_component function, the object they get back is a task factory function. Since it's a normal function object, the user cannot inspect any of the attributes of the component they just loaded (they can only see the name, description and input names). For example, the user cannot see the list of component outputs, the annotations etc.

This change fixes the issue by adding the original component properties to the function object.

Example usage:

```python
train_op = load_component_from_url(...)
print(train_op.outputs)
```
  • Loading branch information
Ark-kun committed Aug 29, 2019
1 parent d43de16 commit d3be07f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion sdk/python/kfp/components/_components.py
Expand Up @@ -267,10 +267,12 @@ def component_default_to_func_default(component_default: str, is_optional: bool)
]
factory_function_parameters = input_parameters #Outputs are no longer part of the task factory function signature. The paths are always generated by the system.

return _dynamic.create_function_from_parameters(
task_factory = _dynamic.create_function_from_parameters(
create_task_from_component_and_arguments,
factory_function_parameters,
documentation='\n'.join(func_docstring_lines),
func_name=name,
func_filename=component_filename if (component_filename and (component_filename.endswith('.yaml') or component_filename.endswith('.yml'))) else None,
)
task_factory.component_spec = component_spec
return task_factory
15 changes: 15 additions & 0 deletions sdk/python/tests/components/test_components.py
Expand Up @@ -95,6 +95,21 @@ def test_loading_minimal_component(self):
task1 = task_factory1()
assert task1.container.image == component_dict['implementation']['container']['image']

def test_accessing_component_spec_from_task_factory(self):
component_text = '''\
implementation:
container:
image: busybox
'''
task_factory1 = comp.load_component_from_text(component_text)

actual_component_spec = task_factory1.component_spec
actual_component_spec_dict = actual_component_spec.to_dict()
expected_component_spec_dict = load_yaml(component_text)
expected_component_spec = kfp.components._structures.ComponentSpec.from_dict(expected_component_spec_dict)
self.assertEqual(expected_component_spec_dict, actual_component_spec_dict)
self.assertEqual(expected_component_spec, task_factory1.component_spec)

@unittest.expectedFailure
def test_fail_on_duplicate_input_names(self):
component_text = '''\
Expand Down

0 comments on commit d3be07f

Please sign in to comment.