diff --git a/knack/arguments.py b/knack/arguments.py index d8d5fd4..cca4b48 100644 --- a/knack/arguments.py +++ b/knack/arguments.py @@ -83,7 +83,7 @@ def __getattr__(self, name): def __setattr__(self, name, value): # pylint: disable=inconsistent-return-statements if name == 'type': - return super(CLICommandArgument, self).__setattr__(name, value) + return super().__setattr__(name, value) self.type.settings[name] = value @@ -186,7 +186,7 @@ def __call__(self, parser, namespace, values, option_string=None): else: namespace._argument_deprecations.append(deprecate_info) # pylint: disable=protected-access try: - super(DeprecatedArgumentAction, self).__call__(parser, namespace, values, option_string) + super().__call__(parser, namespace, values, option_string) except NotImplementedError: setattr(namespace, self.dest, values) @@ -209,7 +209,7 @@ def __call__(self, parser, namespace, values, option_string=None): else: namespace._argument_deprecations.append(deprecated_opt) # pylint: disable=protected-access try: - super(DeprecatedOptionAction, self).__call__(parser, namespace, values, option_string) + super().__call__(parser, namespace, values, option_string) except NotImplementedError: setattr(namespace, self.dest, values) @@ -243,7 +243,7 @@ def __call__(self, parser, namespace, values, option_string=None): else: namespace._argument_previews.append(preview_info) # pylint: disable=protected-access try: - super(PreviewArgumentAction, self).__call__(parser, namespace, values, option_string) + super().__call__(parser, namespace, values, option_string) except NotImplementedError: setattr(namespace, self.dest, values) @@ -294,7 +294,7 @@ def __call__(self, parser, namespace, values, option_string=None): else: namespace._argument_experimentals.append(experimental_info) # pylint: disable=protected-access try: - super(ExperimentalArgumentAction, self).__call__(parser, namespace, values, option_string) + super().__call__(parser, namespace, values, option_string) except NotImplementedError: setattr(namespace, self.dest, values) diff --git a/knack/commands.py b/knack/commands.py index 776cba2..b1f7eef 100644 --- a/knack/commands.py +++ b/knack/commands.py @@ -276,10 +276,10 @@ def _get_op_handler(operation): op = getattr(op, part) if isinstance(op, types.FunctionType): return op - # MethodType + # op as types.MethodType return op.__func__ - except (ValueError, AttributeError): - raise ValueError("The operation '{}' is invalid.".format(operation)) + except (ValueError, AttributeError) as ex: + raise ValueError("The operation '{}' is invalid.".format(operation)) from ex def deprecate(self, **kwargs): kwargs['object_type'] = 'command group' diff --git a/knack/deprecation.py b/knack/deprecation.py index f815c75..65a76bd 100644 --- a/knack/deprecation.py +++ b/knack/deprecation.py @@ -84,7 +84,7 @@ def _default_get_message(self): self.expiration = expiration self._cli_version = cli_ctx.get_cli_version() - super(Deprecated, self).__init__( + super().__init__( cli_ctx=cli_ctx, object_type=object_type, target=target, @@ -136,4 +136,4 @@ def get_implicit_deprecation_message(self): 'tag_func': lambda _: '', 'message_func': get_implicit_deprecation_message }) - super(ImplicitDeprecated, self).__init__(**kwargs) + super().__init__(**kwargs) diff --git a/knack/experimental.py b/knack/experimental.py index e1f6249..c0c36a9 100644 --- a/knack/experimental.py +++ b/knack/experimental.py @@ -52,7 +52,7 @@ def __init__(self, cli_ctx, object_type='', target=None, tag_func=None, message_ def _default_get_message(self): return status_tag_messages['experimental'].format("This " + self.object_type) - super(ExperimentalItem, self).__init__( + super().__init__( cli_ctx=cli_ctx, object_type=object_type, target=target, @@ -73,4 +73,4 @@ def get_implicit_experimental_message(self): 'tag_func': lambda _: '', 'message_func': get_implicit_experimental_message }) - super(ImplicitExperimentalItem, self).__init__(**kwargs) + super().__init__(**kwargs) diff --git a/knack/help.py b/knack/help.py index bf8a902..ee09652 100644 --- a/knack/help.py +++ b/knack/help.py @@ -91,7 +91,7 @@ def _normalize_text(s): def __init__(self, **kwargs): self._short_summary = '' self._long_summary = '' - super(HelpObject, self).__init__(**kwargs) + super().__init__(**kwargs) @property def short_summary(self): @@ -122,7 +122,7 @@ def _load_help_file_from_string(text): return text def __init__(self, help_ctx, delimiters): # pylint: disable=too-many-statements - super(HelpFile, self).__init__() + super().__init__() self.help_ctx = help_ctx self.delimiters = delimiters self.name = delimiters.split()[-1] if delimiters else delimiters @@ -241,7 +241,7 @@ class GroupHelpFile(HelpFile): def __init__(self, help_ctx, delimiters, parser): - super(GroupHelpFile, self).__init__(help_ctx, delimiters) + super().__init__(help_ctx, delimiters) self.type = 'group' self.children = [] @@ -265,7 +265,7 @@ class CommandHelpFile(HelpFile): def __init__(self, help_ctx, delimiters, parser): - super(CommandHelpFile, self).__init__(help_ctx, delimiters) + super().__init__(help_ctx, delimiters) self.type = 'command' self.parameters = [] @@ -324,7 +324,7 @@ def _add_parameter_help(self, param): self.parameters.append(HelpParameter(**param_kwargs)) def _load_from_data(self, data): - super(CommandHelpFile, self)._load_from_data(data) + super()._load_from_data(data) if isinstance(data, str) or not self.parameters or not data.get('parameters'): return @@ -344,7 +344,7 @@ class HelpParameter(HelpObject): # pylint: disable=too-many-instance-attributes def __init__(self, name_source, description, required, choices=None, default=None, group_name=None, deprecate_info=None, preview_info=None, experimental_info=None, default_value_source=None): - super(HelpParameter, self).__init__() + super().__init__() self.name_source = name_source self.name = ' '.join(sorted(name_source)) self.required = required diff --git a/knack/output.py b/knack/output.py index a2e0e6b..a986755 100644 --- a/knack/output.py +++ b/knack/output.py @@ -74,11 +74,11 @@ def format_table(obj): should_sort_keys = not obj.is_query_active and not obj.table_transformer to = _TableOutput(should_sort_keys) return to.dump(result_list) - except: + except Exception as ex: logger.debug(traceback.format_exc()) raise CLIError("Table output unavailable. " "Use the --query option to specify an appropriate query. " - "Use --debug for more info.") + "Use --debug for more info.") from ex def format_tsv(obj): diff --git a/knack/parser.py b/knack/parser.py index 9d126a8..4013b77 100644 --- a/knack/parser.py +++ b/knack/parser.py @@ -107,7 +107,7 @@ def __init__(self, cli_ctx=None, cli_help=None, **kwargs): # or description for a command. We better stash it away before handing it off for # "normal" argparse handling... self._description = kwargs.pop('description', None) - super(CLICommandParser, self).__init__(**kwargs) + super().__init__(**kwargs) def load_command_table(self, command_loader): """ Process the command table and load it into the parser @@ -221,7 +221,7 @@ def _get_subparser(self, path, group_table=None): return parent_subparser def validation_error(self, message): - return super(CLICommandParser, self).error(message) + return super().error(message) def is_group(self): """ Determine if this parser instance represents a group @@ -257,7 +257,7 @@ def parse_args(self, args=None, namespace=None): by ArgumentParser.parse_args as usual """ self._expand_prefixed_files(args) - return super(CLICommandParser, self).parse_args(args) + return super().parse_args(args) def _check_value(self, action, value): # Override to customize the error message when a argument is not among the available choices diff --git a/knack/preview.py b/knack/preview.py index 23df058..d5dc5f8 100644 --- a/knack/preview.py +++ b/knack/preview.py @@ -52,7 +52,7 @@ def __init__(self, cli_ctx, object_type='', target=None, tag_func=None, message_ def _default_get_message(self): return status_tag_messages['preview'].format("This " + self.object_type) - super(PreviewItem, self).__init__( + super().__init__( cli_ctx=cli_ctx, object_type=object_type, target=target, @@ -73,4 +73,4 @@ def get_implicit_preview_message(self): 'tag_func': lambda _: '', 'message_func': get_implicit_preview_message }) - super(ImplicitPreviewItem, self).__init__(**kwargs) + super().__init__(**kwargs) diff --git a/knack/query.py b/knack/query.py index fa4a457..3515804 100644 --- a/knack/query.py +++ b/knack/query.py @@ -22,9 +22,9 @@ def jmespath_type(raw_query): from jmespath import compile as compile_jmespath try: return compile_jmespath(raw_query) - except KeyError: + except KeyError as ex: # Raise a ValueError which argparse can handle - raise ValueError + raise ValueError from ex @staticmethod def on_global_arguments(_, **kwargs): diff --git a/knack/testsdk/base.py b/knack/testsdk/base.py index f9cfcb0..0b522c8 100644 --- a/knack/testsdk/base.py +++ b/knack/testsdk/base.py @@ -26,7 +26,7 @@ class IntegrationTestBase(unittest.TestCase): def __init__(self, cli, method_name): - super(IntegrationTestBase, self).__init__(method_name) + super().__init__(method_name) self.cli = cli self.diagnose = os.environ.get(ENV_TEST_DIAGNOSE, None) == 'True' @@ -82,7 +82,7 @@ class LiveTest(IntegrationTestBase): class ScenarioTest(IntegrationTestBase): # pylint: disable=too-many-instance-attributes def __init__(self, cli, method_name, filter_headers=None): - super(ScenarioTest, self).__init__(cli, method_name) + super().__init__(cli, method_name) self.name_replacer = GeneralNameReplacer() self.recording_processors = [LargeRequestBodyProcessor(), LargeResponseBodyProcessor(), @@ -113,7 +113,7 @@ def __init__(self, cli, method_name, filter_headers=None): self.original_env = os.environ.copy() def setUp(self): - super(ScenarioTest, self).setUp() + super().setUp() # set up cassette cm = self.vcr.use_cassette(self.recording_file) @@ -251,7 +251,7 @@ def _in_process_execute(self, command): self.exit_code = self.cli.invoke(shlex.split(command), out_file=out_buffer) or 0 self.output = out_buffer.getvalue() except vcr.errors.CannotOverwriteExistingCassetteException as ex: - raise AssertionError(ex) + raise AssertionError(ex) from ex except CliExecutionError as ex: if ex.exception: raise ex.exception diff --git a/knack/testsdk/checkers.py b/knack/testsdk/checkers.py index cec9a81..a97e983 100644 --- a/knack/testsdk/checkers.py +++ b/knack/testsdk/checkers.py @@ -63,9 +63,9 @@ def __call__(self, execution_result): try: data = execution_result.output.strip() assert not data or data in none_strings - except AssertionError: + except AssertionError as ex: raise AssertionError("Actual value '{}' != Expected value falsy (None, '', []) or " - "string in {}".format(data, none_strings)) + "string in {}".format(data, none_strings)) from ex class StringCheck(object): # pylint: disable=too-few-public-methods @@ -76,9 +76,9 @@ def __call__(self, execution_result): try: result = execution_result.output.strip().strip('"') assert result == self.expected_result - except AssertionError: + except AssertionError as ex: raise AssertionError( - "Actual value '{}' != Expected value {}".format(result, self.expected_result)) + "Actual value '{}' != Expected value {}".format(result, self.expected_result)) from ex class StringContainCheck(object): # pylint: disable=too-few-public-methods @@ -89,7 +89,7 @@ def __call__(self, execution_result): try: result = execution_result.output.strip('"') assert self.expected_result in result - except AssertionError: + except AssertionError as ex: raise AssertionError( "Actual value '{}' doesn't contain Expected value {}".format(result, - self.expected_result)) + self.expected_result)) from ex diff --git a/knack/testsdk/exceptions.py b/knack/testsdk/exceptions.py index f490a6d..4cbea3d 100644 --- a/knack/testsdk/exceptions.py +++ b/knack/testsdk/exceptions.py @@ -7,19 +7,18 @@ class CliTestError(Exception): def __init__(self, error_message): message = 'An error caused by the CLI test harness failed the test: {}' - super(CliTestError, self).__init__(message.format(error_message)) + super().__init__(message.format(error_message)) class CliExecutionError(Exception): def __init__(self, exception): self.exception = exception message = 'The CLI throws exception {} during execution and fails the command.' - super(CliExecutionError, self).__init__(message.format(exception.__class__.__name__, - exception)) + super().__init__(message.format(exception.__class__.__name__, exception)) class JMESPathCheckAssertionError(AssertionError): def __init__(self, query, expected, actual, json_data): message = "Query '{}' doesn't yield expected value '{}', instead the actual value " \ "is '{}'. Data: \n{}\n".format(query, expected, actual, json_data) - super(JMESPathCheckAssertionError, self).__init__(message) + super().__init__(message) diff --git a/knack/util.py b/knack/util.py index ec24056..ca80a47 100644 --- a/knack/util.py +++ b/knack/util.py @@ -42,8 +42,7 @@ class CtxTypeError(TypeError): def __init__(self, obj): from .cli import CLI - super(CtxTypeError, self).__init__('expected instance of {} got {}'.format(CLI.__name__, - obj.__class__.__name__)) + super().__init__('expected instance of {} got {}'.format(CLI.__name__, obj.__class__.__name__)) class ColorizedString(object): diff --git a/requirements.txt b/requirements.txt index 209afc4..786e703 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,9 +1,9 @@ argcomplete==1.11.1 colorama==0.4.3 -flake8==3.7.9 +flake8==3.8.4 jmespath==0.9.5 mock==4.0.1 -pylint==2.5.3 +pylint==2.6.0 Pygments==2.5.2 PyYAML==5.3.1 six==1.14.0 diff --git a/tests/test_cli_scenarios.py b/tests/test_cli_scenarios.py index de23ea9..ed1e39c 100644 --- a/tests/test_cli_scenarios.py +++ b/tests/test_cli_scenarios.py @@ -158,7 +158,7 @@ def test_should_enable_color(self, stdout_isatty_mock, stderr_isatty_mock): def test_init_log(self): class MyCLI(CLI): def __init__(self, **kwargs): - super(MyCLI, self).__init__(**kwargs) + super().__init__(**kwargs) self.init_debug_log.append("init debug log: 6aa19a11") self.init_info_log.append("init info log: b0746f58") cli = MyCLI() diff --git a/tests/test_command_with_configured_defaults.py b/tests/test_command_with_configured_defaults.py index 92f02d9..b15fa5b 100644 --- a/tests/test_command_with_configured_defaults.py +++ b/tests/test_command_with_configured_defaults.py @@ -41,7 +41,7 @@ def _set_up_command_table(self, required): class TestCommandsLoader(CLICommandsLoader): def load_command_table(self, args): - super(TestCommandsLoader, self).load_command_table(args) + super().load_command_table(args) with CommandGroup(self, 'foo', '{}#{{}}'.format(__name__)) as g: g.command('list', 'list_foo') return self.command_table @@ -50,7 +50,7 @@ def load_arguments(self, command): with ArgumentsContext(self, 'foo') as c: c.argument('my_param', options_list='--my-param', configured_default='param', required=required) - super(TestCommandsLoader, self).load_arguments(command) + super().load_arguments(command) self.cli_ctx = DummyCLI(commands_loader_cls=TestCommandsLoader) @mock.patch.dict(os.environ, {'CLI_DEFAULTS_PARAM': 'myVal'}) diff --git a/tests/test_deprecation.py b/tests/test_deprecation.py index 5f61764..aceb87c 100644 --- a/tests/test_deprecation.py +++ b/tests/test_deprecation.py @@ -35,7 +35,7 @@ def setUp(self): class DeprecationTestCommandLoader(CLICommandsLoader): def load_command_table(self, args): - super(DeprecationTestCommandLoader, self).load_command_table(args) + super().load_command_table(args) with CommandGroup(self, '', '{}#{{}}'.format(__name__)) as g: g.command('cmd1', 'example_handler', deprecate_info=g.deprecate(redirect='alt-cmd1')) g.command('cmd2', 'example_handler', deprecate_info=g.deprecate(redirect='alt-cmd2', hide='1.0.0')) @@ -53,7 +53,7 @@ def load_arguments(self, command): c.argument('arg1', options_list=['--arg', '-a'], required=False, type=int, choices=[1, 2, 3]) c.argument('arg2', options_list=['-b'], required=True, choices=['a', 'b', 'c']) - super(DeprecationTestCommandLoader, self).load_arguments(command) + super().load_arguments(command) helps['grp1'] = """ type: group @@ -163,7 +163,7 @@ def setUp(self): class DeprecationTestCommandLoader(CLICommandsLoader): def load_command_table(self, args): - super(DeprecationTestCommandLoader, self).load_command_table(args) + super().load_command_table(args) with CommandGroup(self, 'group1', '{}#{{}}'.format(__name__), deprecate_info=self.deprecate(redirect='alt-group1')) as g: g.command('cmd1', 'example_handler') @@ -187,7 +187,7 @@ def load_arguments(self, command): c.argument('arg1', options_list=['--arg', '-a'], required=False, type=int, choices=[1, 2, 3]) c.argument('arg2', options_list=['-b'], required=True, choices=['a', 'b', 'c']) - super(DeprecationTestCommandLoader, self).load_arguments(command) + super().load_arguments(command) helps['group1'] = """ type: group @@ -293,7 +293,7 @@ def setUp(self): class DeprecationTestCommandLoader(CLICommandsLoader): def load_command_table(self, args): - super(DeprecationTestCommandLoader, self).load_command_table(args) + super().load_command_table(args) with CommandGroup(self, '', '{}#{{}}'.format(__name__)) as g: g.command('arg-test', 'example_arg_handler') return self.command_table @@ -311,7 +311,7 @@ def load_arguments(self, command): c.argument('arg5', deprecate_info=c.deprecate(expiration='0.1.0')) c.argument('opt5', options_list=['--opt5', c.deprecate(redirect='--opt5', target='--alt5', expiration='0.1.0')]) - super(DeprecationTestCommandLoader, self).load_arguments(command) + super().load_arguments(command) helps['grp1'] = """ type: group diff --git a/tests/test_help.py b/tests/test_help.py index 8d901fd..b5b32ae 100644 --- a/tests/test_help.py +++ b/tests/test_help.py @@ -62,7 +62,7 @@ def setUp(self): class HelpTestCommandLoader(CLICommandsLoader): def load_command_table(self, args): - super(HelpTestCommandLoader, self).load_command_table(args) + super().load_command_table(args) with CommandGroup(self, '', '{}#{{}}'.format(__name__)) as g: g.command('n1', 'example_handler') g.command('n2', 'example_handler') @@ -90,7 +90,7 @@ def load_arguments(self, command): c.argument('arg2', options_list=['--foobar2'], required=True) c.argument('arg3', options_list=['--foobar3'], help='the foobar3') - super(HelpTestCommandLoader, self).load_arguments(command) + super().load_arguments(command) helps['n2'] = """ type: command diff --git a/tests/test_preview.py b/tests/test_preview.py index 736e92a..14aedf9 100644 --- a/tests/test_preview.py +++ b/tests/test_preview.py @@ -36,7 +36,7 @@ def setUp(self): class PreviewTestCommandLoader(CLICommandsLoader): def load_command_table(self, args): - super(PreviewTestCommandLoader, self).load_command_table(args) + super().load_command_table(args) with CommandGroup(self, '', '{}#{{}}'.format(__name__)) as g: g.command('cmd1', 'example_handler', is_preview=True) @@ -50,7 +50,7 @@ def load_arguments(self, command): c.argument('arg1', options_list=['--arg', '-a'], required=False, type=int, choices=[1, 2, 3]) c.argument('arg2', options_list=['-b'], required=True, choices=['a', 'b', 'c']) - super(PreviewTestCommandLoader, self).load_arguments(command) + super().load_arguments(command) helps['grp1'] = """ type: group @@ -136,7 +136,7 @@ def setUp(self): class PreviewTestCommandLoader(CLICommandsLoader): def load_command_table(self, args): - super(PreviewTestCommandLoader, self).load_command_table(args) + super().load_command_table(args) with CommandGroup(self, 'group1', '{}#{{}}'.format(__name__), is_preview=True) as g: g.command('cmd1', 'example_handler') @@ -148,7 +148,7 @@ def load_arguments(self, command): c.argument('arg1', options_list=['--arg', '-a'], required=False, type=int, choices=[1, 2, 3]) c.argument('arg2', options_list=['-b'], required=True, choices=['a', 'b', 'c']) - super(PreviewTestCommandLoader, self).load_arguments(command) + super().load_arguments(command) helps['group1'] = """ type: group @@ -217,7 +217,7 @@ def __call__(self, parser, namespace, values, option_string=None): class PreviewTestCommandLoader(CLICommandsLoader): def load_command_table(self, args): - super(PreviewTestCommandLoader, self).load_command_table(args) + super().load_command_table(args) with CommandGroup(self, '', '{}#{{}}'.format(__name__)) as g: g.command('arg-test', 'example_arg_handler') return self.command_table @@ -226,7 +226,7 @@ def load_arguments(self, command): with ArgumentsContext(self, 'arg-test') as c: c.argument('arg1', help='Arg1', is_preview=True, action=LoggerAction) - super(PreviewTestCommandLoader, self).load_arguments(command) + super().load_arguments(command) helps['grp1'] = """ type: group diff --git a/tests/util.py b/tests/util.py index 71481a2..3eccda7 100644 --- a/tests/util.py +++ b/tests/util.py @@ -61,7 +61,7 @@ def remove_space(str): class MockContext(CLI): def __init__(self): - super(MockContext, self).__init__(config_dir=new_temp_folder()) + super().__init__(config_dir=new_temp_folder()) loader = CLICommandsLoader(cli_ctx=self) invocation = mock.MagicMock(spec=CommandInvoker) invocation.data = {} @@ -76,7 +76,7 @@ def get_cli_version(self): def __init__(self, **kwargs): kwargs['config_dir'] = new_temp_folder() - super(DummyCLI, self).__init__(**kwargs) + super().__init__(**kwargs) # Force colorama to initialize self.enable_color = True