forked from kubeflow/pipelines
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk): rename CLI methods to 'create' (kubeflow#7607)
* add deprecated alias group * add deprecated alias group tests * implement deprecated alias group * clean up other alias tests * clean up docstring * fix type annotations / mypy * format with yapf
- Loading branch information
1 parent
8a8420e
commit dbafea5
Showing
5 changed files
with
137 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
""" | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |