Skip to content

Commit

Permalink
assorted code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
connor-mccarthy committed Apr 29, 2022
1 parent 4345f7d commit 644fb4b
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 30 deletions.
14 changes: 7 additions & 7 deletions sdk/python/kfp/compiler/compiler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ def pipeline_hello_world(text: str = 'hi there'):
v2.compiler.Compiler().compile(
pipeline_func=pipeline_hello_world, package_path=temp_filepath)

with open(temp_filepath, "r") as f:
with open(temp_filepath, 'r') as f:
yaml.load(f)

def test_import_modules(self): # pylint: disable=no-self-use
Expand All @@ -625,7 +625,7 @@ def pipeline_hello_world(text: str = 'hi there'):
compiler.Compiler().compile(
pipeline_func=pipeline_hello_world, package_path=temp_filepath)

with open(temp_filepath, "r") as f:
with open(temp_filepath, 'r') as f:
yaml.load(f)

def test_import_object(self): # pylint: disable=no-self-use
Expand All @@ -650,7 +650,7 @@ def pipeline_hello_world(text: str = 'hi there'):
Compiler().compile(
pipeline_func=pipeline_hello_world, package_path=temp_filepath)

with open(temp_filepath, "r") as f:
with open(temp_filepath, 'r') as f:
yaml.load(f)


Expand All @@ -670,8 +670,8 @@ def my_pipeline():
return my_pipeline

@parameterized.parameters(
{"extension": ".yaml"},
{"extension": ".yml"},
{'extension': '.yaml'},
{'extension': '.yml'},
)
def test_can_write_to_yaml(self, extension):

Expand Down Expand Up @@ -701,7 +701,7 @@ def test_can_write_to_json(self):

target_file = os.path.join(tmpdir, 'result.json')
with self.assertWarnsRegex(DeprecationWarning,
r"Compiling to JSON is deprecated"):
r'Compiling to JSON is deprecated'):
compiler.Compiler().compile(
pipeline_func=pipeline_spec, package_path=target_file)
with open(target_file) as f:
Expand Down Expand Up @@ -745,7 +745,7 @@ def test_compile_pipeline_with_default_value(self):

@dsl.pipeline(name='test-pipeline')
def simple_pipeline():
producer = producer_op(location="1", nodefault="string")
producer = producer_op(location='1', nodefault='string')

target_json_file = os.path.join(tmpdir, 'result.json')
compiler.Compiler().compile(
Expand Down
4 changes: 2 additions & 2 deletions sdk/python/kfp/components/base_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ def __init__(self, component_spec: structures.ComponentSpec):
# Arguments typed as PipelineTaskFinalStatus are special arguments that
# do not count as user inputs. Instead, they are reserved to for the
# (backend) system to pass a value.
self._component_inputs = set([
self._component_inputs = {
input_name for input_name, input_spec in (
self.component_spec.inputs or {}).items()
if not type_utils.is_task_final_status_type(input_spec.type)
])
}

def __call__(self, *args, **kwargs) -> pipeline_task.PipelineTask:
"""Creates a PipelineTask object.
Expand Down
11 changes: 6 additions & 5 deletions sdk/python/kfp/components/base_component_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,25 +93,26 @@ def test_instantiate_component_with_positional_arugment(self):
with self.assertRaisesRegex(
TypeError,
'Components must be instantiated using keyword arguments.'
' Positional parameters are not allowed \(found 3 such'
' parameters for component "component_1"\).'):
r' Positional parameters are not allowed \(found 3 such'
r' parameters for component "component_1"\).'):
component_op('abc', 1, 2.3)

def test_instantiate_component_with_unexpected_keyword_arugment(self):
with self.assertRaisesRegex(
TypeError,
'component_1\(\) got an unexpected keyword argument "input0".'):
r'component_1\(\) got an unexpected keyword argument "input0".'
):
component_op(input1='abc', input2=1, input3=2.3, input0='extra')

def test_instantiate_component_with_missing_arugments(self):
with self.assertRaisesRegex(
TypeError,
'component_1\(\) missing 1 required argument: input1.'):
r'component_1\(\) missing 1 required argument: input1.'):
component_op(input2=1)

with self.assertRaisesRegex(
TypeError,
'component_1\(\) missing 2 required arguments: input1, input2.'
r'component_1\(\) missing 2 required arguments: input1, input2.'
):
component_op()

Expand Down
20 changes: 10 additions & 10 deletions sdk/python/kfp/components/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,33 +203,33 @@ def base_model_format(x: BaseModelType) -> str:
CHARS = 0

def first_level_indent(string: str, chars: int = 1) -> str:
return "\n".join(" " * chars + p for p in string.split("\n"))
return '\n'.join(' ' * chars + p for p in string.split('\n'))

def body_level_indent(string: str, chars=4) -> str:
a, *b = string.split("\n")
return a + "\n" + first_level_indent(
"\n".join(b),
a, *b = string.split('\n')
return a + '\n' + first_level_indent(
'\n'.join(b),
chars=chars,
) if b else a

def parts() -> Iterator[str]:
if dataclasses.is_dataclass(x):
yield type(x).__name__ + "("
yield type(x).__name__ + '('

def fields() -> Iterator[str]:
for field in dataclasses.fields(x):
nindent = CHARS + len(field.name) + 4
value = getattr(x, field.name)
rep_value = base_model_format(value)
yield (" " * (CHARS + 3) + body_level_indent(
f"{field.name}={rep_value}", chars=nindent))
yield (' ' * (CHARS + 3) + body_level_indent(
f'{field.name}={rep_value}', chars=nindent))

yield ",\n".join(fields())
yield " " * CHARS + ")"
yield ',\n'.join(fields())
yield ' ' * CHARS + ')'
else:
yield pprint.pformat(x)

return "\n".join(parts())
return '\n'.join(parts())


def convert_object_to_dict(obj: BaseModelType,
Expand Down
4 changes: 2 additions & 2 deletions sdk/python/kfp/components/base_model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ class MyClass(base_model.BaseModel):
z: Dict[str, int]
_aliases = {'x': 'a', 'z': 'b'}

res = MyClass(x=1, y=[2], z={"key": 3}).to_dict(by_alias=True)
self.assertEqual(res, {'a': 1, 'y': [2], 'b': {"key": 3}})
res = MyClass(x=1, y=[2], z={'key': 3}).to_dict(by_alias=True)
self.assertEqual(res, {'a': 1, 'y': [2], 'b': {'key': 3}})

def test_to_dict_by_alias_nested(self):

Expand Down
2 changes: 1 addition & 1 deletion sdk/python/kfp/components/pipeline_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def expand_command_part(arg) -> Union[str, List[str], None]:
return expanded_result

else:
raise TypeError('Unrecognized argument type: {}'.format(arg))
raise TypeError(f'Unrecognized argument type: {arg}')

def expand_argument_list(argument_list) -> Optional[List[str]]:
if argument_list is None:
Expand Down
4 changes: 2 additions & 2 deletions sdk/python/kfp/components/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def convert_str_or_dict_to_placeholder(

if not has_one_entry:
raise ValueError(
f"Got unexpected dictionary {res}. Expected a dictionary with one entry."
f'Got unexpected dictionary {res}. Expected a dictionary with one entry.'
)

first_key = list(res.keys())[0]
Expand Down Expand Up @@ -554,7 +554,7 @@ def convert_v1_if_present_placholder_to_v2(
return arg
else:
raise TypeError(
f"Unexpected argument {arg} of type {type(arg)}.")
f'Unexpected argument {arg} of type {type(arg)}.')

implementation = component_dict['implementation']['container']
implementation['command'] = [
Expand Down
2 changes: 1 addition & 1 deletion sdk/python/kfp/components/structures_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021 The Kubeflow Authors
# Copyright 2021-2022 The Kubeflow Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down

0 comments on commit 644fb4b

Please sign in to comment.