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/Components/Python - Made the typing.NamedTuple import optional #717

Merged
Changes from all commits
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
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