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 28, 2022
1 parent 6a7036d commit 5680c6c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 23 deletions.
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
22 changes: 11 additions & 11 deletions sdk/python/kfp/components/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from collections import abc
from typing import (Any, Dict, ForwardRef, Iterable, Iterator, Mapping,
MutableMapping, MutableSequence, Optional, OrderedDict,
Protocol, Sequence, Tuple, Type, TypeVar, Union)
Sequence, Tuple, Type, TypeVar, Union)

PRIMITIVE_TYPES = {int, str, float, bool}

Expand Down 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
7 changes: 2 additions & 5 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 Expand Up @@ -472,6 +472,3 @@ def test_is_same_as_typing_version(self):

if __name__ == '__main__':
unittest.main()

# # TODO: test no subtype
# # TODO: adjust special types to actually use that data structure
4 changes: 2 additions & 2 deletions sdk/python/kfp/components/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,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 @@ -527,7 +527,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

0 comments on commit 5680c6c

Please sign in to comment.