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

feat(sdk): rename CLI methods to 'create' #7607

Merged
Show file tree
Hide file tree
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: 8 additions & 3 deletions sdk/python/kfp/cli/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@
from kfp import client
from kfp.cli.output import OutputFormat
from kfp.cli.output import print_output
from kfp.cli.utils import deprecated_alias_group
from kfp.cli.utils import parsing


@click.group()
@click.group(
cls=deprecated_alias_group.deprecated_alias_group_factory({
'upload': 'create',
'upload-version': 'create-version'
}))
def pipeline():
"""Manage pipeline resources."""
pass
Expand All @@ -41,7 +46,7 @@ def pipeline():
help=parsing.get_param_descr(client.Client.upload_pipeline, 'description'))
@click.argument('package-file')
@click.pass_context
def upload(ctx: click.Context,
def create(ctx: click.Context,
pipeline_name: str,
package_file: str,
description: str = None):
Expand Down Expand Up @@ -82,7 +87,7 @@ def upload(ctx: click.Context,
)
@click.argument('package-file')
@click.pass_context
def upload_version(ctx: click.Context,
def create_version(ctx: click.Context,
package_file: str,
pipeline_version: str,
pipeline_id: Optional[str] = None,
Expand Down
7 changes: 5 additions & 2 deletions sdk/python/kfp/cli/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@
from kfp import client
from kfp.cli.output import OutputFormat
from kfp.cli.output import print_output
from kfp.cli.utils import deprecated_alias_group
from kfp.cli.utils import parsing


@click.group()
@click.group(
cls=deprecated_alias_group.deprecated_alias_group_factory(
{'submit': 'create'}))
def run():
"""Manage run resources."""
pass
Expand Down Expand Up @@ -114,7 +117,7 @@ def list(ctx: click.Context, experiment_id: str, page_token: str, max_size: int,
type=int)
@click.argument('args', nargs=-1)
@click.pass_context
def submit(ctx: click.Context, experiment_name: str, run_name: str,
def create(ctx: click.Context, experiment_name: str, run_name: str,
package_file: str, pipeline_id: str, pipeline_name: str, watch: bool,
timeout: int, version: str, args: List[str]):
"""Submit a pipeline run."""
Expand Down
5 changes: 3 additions & 2 deletions sdk/python/kfp/cli/utils/aliased_plurals_group_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ def command():

class TestAliasedPluralsGroup(unittest.TestCase):

def setUp(self):
self.runner = testing.CliRunner()
@classmethod
def setUpClass(cls):
cls.runner = testing.CliRunner()

def test_aliases_default_success(self):
result = self.runner.invoke(cli, ['command'])
Expand Down
66 changes: 66 additions & 0 deletions sdk/python/kfp/cli/utils/deprecated_alias_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Copyright 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.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Dict, List, Tuple, Union

import click


def deprecated_alias_group_factory(
deprecated_map: Dict[str,
str]) -> 'DeprecatedAliasGroup': # type: ignore
"""Closure that returns a class that implements the deprecated alias group.

Args:
deprecated_map (Dict[str, str]): Dictionary mapping old deprecated names to new names.


Returns:
DeprecatedAliasGroup: A class that implements the deprecated alias group.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: type annotate this in the function signature?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is possible. My attempts:

  1. This doesn't work, because DeprecatedAliasGroup is not defined yet:
def deprecated_alias_group_factory(...) -> DeprecatedAliasGroup:
  1. And this doesn't work, because mypy does not support this type for forward ref (mypy error: Name "DeprecatedAliasGroup" is not defined):
def deprecated_alias_group_factory(...) -> 'DeprecatedAliasGroup':

I opted for omission over using Any. WDYT?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def deprecated_alias_group_factory(...) -> 'DeprecatedAliasGroup':

We have been using this forward declaration pattern in our code base. Now I do recall that mypy would complain on such annotation, but we weren't enforcing the mypy check in the past.
One way to make it "work" is to disable the mypy error on this specific line. Then the annotation might be slightly more helpful for devs looking at this code.
It's very minor. Feel free to ignore my original comment. :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that approach 👍🏻

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated (and also fixed one other mypy error)

"""

class DeprecatedAliasGroup(click.Group):

def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.deprecated_map = deprecated_map

def get_command(self, ctx: click.Context,
cmd_name: str) -> click.Command:
# using the correct name
command = click.Group.get_command(self, ctx, cmd_name)
if command is not None:
return command

# using the deprecated alias
correct_name = self.deprecated_map.get(cmd_name)
if correct_name is not None:
command = click.Group.get_command(self, ctx, correct_name)
click.echo(
f"Warning: '{cmd_name}' is deprecated, use '{correct_name}' instead.",
err=True)

if command is not None:
return command

raise click.UsageError(f"Unrecognized command '{cmd_name}'.")

def resolve_command(
self, ctx: click.Context, args: List[str]
) -> Tuple[Union[str, None], Union[click.Command, None], List[str]]:
# always return the full command name
_, cmd, args = super().resolve_command(ctx, args)
return cmd.name, cmd, args # type: ignore

return DeprecatedAliasGroup
55 changes: 55 additions & 0 deletions sdk/python/kfp/cli/utils/deprecated_alias_group_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright 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.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

import click
from click import testing
from kfp.cli.utils import deprecated_alias_group


@click.group(
cls=deprecated_alias_group.deprecated_alias_group_factory(
{'deprecated': 'new'}))
def cli():
pass


@cli.command()
def new():
click.echo('Called new command.')


class TestAliasedPluralsGroup(unittest.TestCase):

@classmethod
def setUpClass(cls):
cls.runner = testing.CliRunner()

def test_new_call(self):
result = self.runner.invoke(cli, ['new'])
self.assertEqual(result.exit_code, 0)
self.assertEqual(result.output, 'Called new command.\n')

def test_deprecated_call(self):
result = self.runner.invoke(cli, ['deprecated'])
self.assertEqual(result.exit_code, 0)
self.assertTrue('Called new command.\n' in result.output)
self.assertTrue(
"Warning: 'deprecated' is deprecated, use 'new' instead." in
result.output)


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