-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
google-oss-prow
merged 8 commits into
kubeflow:master
from
connor-mccarthy:use-create-for-resource-put-in-cli
Apr 29, 2022
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9e7bf5d
add deprecated alias group
connor-mccarthy 3135bc1
add deprecated alias group tests
connor-mccarthy 989302c
implement deprecated alias group
connor-mccarthy 67d915f
clean up other alias tests
connor-mccarthy ed62d58
clean up docstring
connor-mccarthy 03fd680
fix type annotations / mypy
connor-mccarthy eb8e803
Merge branch 'master' into use-create-for-resource-put-in-cli
connor-mccarthy e2c7b76
format with yapf
connor-mccarthy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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:
DeprecatedAliasGroup
is not defined yet:I opted for omission over using
Any
. WDYT?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like that approach 👍🏻
There was a problem hiding this comment.
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)