Skip to content

Commit

Permalink
SDK/Components/Python - Made the typing.NamedTuple import optional (#717
Browse files Browse the repository at this point in the history
)

Now it's only imported if the return type is NamedTuple.
  • Loading branch information
Ark-kun authored and k8s-ci-robot committed Jan 24, 2019
1 parent 4c551ba commit 9b40886
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions sdk/python/kfp/components/_python_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ def annotation_to_type_struct(annotation):

func_name=func.__name__

#TODO: Add support for copying the NamedTuple subclass declaration code
#Adding NamedTuple import if needed
func_type_declarations_code = ""
if hasattr(return_ann, '_fields'): #NamedTuple
func_type_declarations_code = func_type_declarations_code + '\n' + 'from typing import NamedTuple'

#Source code can include decorators line @python_op. Remove them
(func_code_lines, _) = inspect.getsourcelines(func)
while func_code_lines[0].lstrip().startswith('@'): #decorator
Expand Down Expand Up @@ -157,10 +163,10 @@ def annotation_to_type_struct(annotation):

full_source = \
'''\
from typing import NamedTuple
{extra_code}
{func_type_declarations_code}
{func_code}
import sys
Expand All @@ -185,6 +191,7 @@ def annotation_to_type_struct(annotation):
'''.format(
func_name=func_name,
func_code=func_code,
func_type_declarations_code=func_type_declarations_code,
extra_code=extra_code,
input_args_parsing_code='\n'.join(input_args_parsing_code_lines),
output_files_parsing_code='\n'.join(output_files_parsing_code_lines),
Expand Down

0 comments on commit 9b40886

Please sign in to comment.