Sourcery refactored master branch#1
Conversation
| @@ -20,6 +20,7 @@ | |||
|
|
|||
| """Utilities for writing code that runs on Python 2 and 3""" | |||
|
|
|||
There was a problem hiding this comment.
Lines 38-38 refactored with the following changes:
- Replace a[0:x] with a[:x] and a[x:len(a)] with a[x:] (
remove-redundant-slice-index)
| if old_attr is None: | ||
| new_attr = name | ||
| else: | ||
| new_attr = old_attr | ||
| new_attr = name if old_attr is None else old_attr |
There was a problem hiding this comment.
Function MovedAttribute.__init__ refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
| self.known_modules[self.name + "." + fullname] = mod | ||
| self.known_modules[f"{self.name}.{fullname}"] = mod |
There was a problem hiding this comment.
Function _SixMetaPathImporter._add_module refactored with the following changes:
- Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation)
| return self.known_modules[self.name + "." + fullname] | ||
| return self.known_modules[f"{self.name}.{fullname}"] |
There was a problem hiding this comment.
Function _SixMetaPathImporter._get_module refactored with the following changes:
- Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation)
| if fullname in self.known_modules: | ||
| return self | ||
| return None | ||
| return self if fullname in self.known_modules else None |
There was a problem hiding this comment.
Function _SixMetaPathImporter.find_module refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else) - Replace if statement with if expression (
assign-if-exp)
| """ | ||
| full_message = ("Error parsing parameter '%s': %s" % | ||
| (cli_name, message)) | ||
| full_message = f"Error parsing parameter '{cli_name}': {message}" |
There was a problem hiding this comment.
Function ParamError.__init__ refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring)
| 'load-cli-arg.%s.%s.%s' % (service_name, | ||
| operation_name, | ||
| param_name), | ||
| param=cli_argument, value=value, service_name=service_name, | ||
| operation_name=operation_name) | ||
| f'load-cli-arg.{service_name}.{operation_name}.{param_name}', | ||
| param=cli_argument, | ||
| value=value, | ||
| service_name=service_name, | ||
| operation_name=operation_name, | ||
| ) |
There was a problem hiding this comment.
Function unpack_argument refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring)
| return 'structure(%s)' % ', '.join(sorted(set(sub_types))) | ||
| return f"structure({', '.join(sorted(set(sub_types)))})" | ||
| elif param.type_name == 'list': | ||
| return 'list-%s' % _detect_shape_structure(param.member, stack) | ||
| return f'list-{_detect_shape_structure(param.member, stack)}' | ||
| elif param.type_name == 'map': | ||
| if param.value.type_name in SCALAR_TYPES: | ||
| return 'map-scalar' | ||
| else: | ||
| return 'map-%s' % _detect_shape_structure(param.value, stack) | ||
| return f'map-{_detect_shape_structure(param.value, stack)}' |
There was a problem hiding this comment.
Function _detect_shape_structure refactored with the following changes:
- Replace interpolated string formatting with f-string [×3] (
replace-interpolation-with-fstring)
| if model.serialization.get('jsonvalue') and \ | ||
| model.serialization.get('location') == 'header' and \ | ||
| model.type_name == 'string': | ||
| return True | ||
| return False | ||
| return bool( | ||
| model.serialization.get('jsonvalue') | ||
| and model.serialization.get('location') == 'header' | ||
| and model.type_name == 'string' | ||
| ) |
There was a problem hiding this comment.
Function _special_type refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else) - Replace if statement with if expression (
assign-if-exp) - Simplify boolean if expression (
boolean-if-exp-identity)
| def _unpack_complex_cli_arg(argument_model, value, cli_name): | ||
| type_name = argument_model.type_name | ||
| if type_name == 'structure' or type_name == 'map': | ||
| if type_name in ['structure', 'map']: |
There was a problem hiding this comment.
Function _unpack_complex_cli_arg refactored with the following changes:
- Replace multiple comparisons of same variable with
inoperator (merge-comparisons)
| if argument_model.type_name == 'integer' or argument_model.type_name == 'long': | ||
| if argument_model.type_name in ['integer', 'long']: | ||
| return int(value) | ||
| elif argument_model.type_name == 'float' or argument_model.type_name == 'double': | ||
| elif argument_model.type_name in ['float', 'double']: |
There was a problem hiding this comment.
Function unpack_scalar_cli_arg refactored with the following changes:
- Replace multiple comparisons of same variable with
inoperator [×2] (merge-comparisons)
| else: | ||
| service_id, operation_name = \ | ||
| service_id, operation_name = \ | ||
| find_service_and_method_in_event_name(event_name) | ||
| return self._parse_as_shorthand( | ||
| cli_argument, value, service_id, operation_name) | ||
| return self._parse_as_shorthand( | ||
| cli_argument, value, service_id, operation_name) |
There was a problem hiding this comment.
Function ParamShorthandParser.__call__ refactored with the following changes:
- Swap if/else branches [×2] (
swap-if-else-branches) - Remove unnecessary else after guard condition (
remove-unnecessary-else)
| self._visitor.visit(parsed, cli_argument.argument_model) | ||
| else: | ||
| # Otherwise value is just a string. | ||
| parsed = self._parser.parse(value) | ||
| self._visitor.visit(parsed, cli_argument.argument_model) | ||
| self._visitor.visit(parsed, cli_argument.argument_model) |
There was a problem hiding this comment.
Function ParamShorthandParser._parse_as_shorthand refactored with the following changes:
- Hoist repeated code outside conditional statement (
hoist-statement-from-if)
| self._uses_old_list_case(service_id, operation_name, cli_argument.name): | ||
| model.member.type_name == 'structure' and \ | ||
| len(model.member.members) == 1 and \ | ||
| self._uses_old_list_case(service_id, operation_name, cli_argument.name): |
There was a problem hiding this comment.
Function ParamShorthandParser._handle_special_cases refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| if value and isinstance(value, list): | ||
| check_val = value[0] | ||
| else: | ||
| check_val = value | ||
| check_val = value[0] if value and isinstance(value, list) else value |
There was a problem hiding this comment.
Function ParamShorthandParser._should_parse_as_shorthand refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
| if self._required is None: | ||
| return False | ||
| return self._required | ||
| return False if self._required is None else self._required |
There was a problem hiding this comment.
Function CustomArgument.required refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else) - Replace if statement with if expression (
assign-if-exp)
| cli_type = str | ||
| if self._action in ['store_true', 'store_false']: | ||
| cli_type = bool | ||
| return cli_type | ||
| return bool if self._action in ['store_true', 'store_false'] else str |
There was a problem hiding this comment.
Function CustomArgument.cli_type refactored with the following changes:
- Move setting of default value for variable into
elsebranch (introduce-default-else) - Replace if statement with if expression (
assign-if-exp) - Inline variable that is immediately returned (
inline-immediately-returned-variable)
| else: | ||
| # This is a two step process. First is the process of converting | ||
| # the command line value into a python value. Normally this is | ||
| # handled by argparse directly, but there are cases where extra | ||
| # processing is needed. For example, "--foo name=value" the value | ||
| # can be converted from "name=value" to {"name": "value"}. This is | ||
| # referred to as the "unpacking" process. Once we've unpacked the | ||
| # argument value, we have to decide how this is converted into | ||
| # something that can be consumed by botocore. Many times this is | ||
| # just associating the key and value in the params dict as down | ||
| # below. Sometimes this can be more complicated, and subclasses | ||
| # can customize as they need. | ||
| unpacked = self._unpack_argument(value) | ||
| LOG.debug('Unpacked value of %r for parameter "%s": %r', value, | ||
| self.py_name, unpacked) | ||
| parameters[self._serialized_name] = unpacked | ||
| # This is a two step process. First is the process of converting | ||
| # the command line value into a python value. Normally this is | ||
| # handled by argparse directly, but there are cases where extra | ||
| # processing is needed. For example, "--foo name=value" the value | ||
| # can be converted from "name=value" to {"name": "value"}. This is | ||
| # referred to as the "unpacking" process. Once we've unpacked the | ||
| # argument value, we have to decide how this is converted into | ||
| # something that can be consumed by botocore. Many times this is | ||
| # just associating the key and value in the params dict as down | ||
| # below. Sometimes this can be more complicated, and subclasses | ||
| # can customize as they need. | ||
| unpacked = self._unpack_argument(value) | ||
| LOG.debug('Unpacked value of %r for parameter "%s": %r', value, | ||
| self.py_name, unpacked) | ||
| parameters[self._serialized_name] = unpacked |
There was a problem hiding this comment.
Function CLIArgument.add_to_params refactored with the following changes:
- Remove unnecessary else after guard condition (
remove-unnecessary-else)
| override = self._emit_first_response('process-cli-arg.%s.%s' % ( | ||
| service_name, operation_name), param=self.argument_model, | ||
| cli_argument=self, value=value) | ||
| if override is not None: | ||
| # A plugin supplied an alternate conversion, | ||
| # use it instead. | ||
| return override | ||
| else: | ||
| # Fall back to the default arg processing. | ||
| return unpack_cli_arg(self, value) | ||
| override = self._emit_first_response( | ||
| f'process-cli-arg.{service_name}.{operation_name}', | ||
| param=self.argument_model, | ||
| cli_argument=self, | ||
| value=value, | ||
| ) | ||
| return override if override is not None else unpack_cli_arg(self, value) |
There was a problem hiding this comment.
Function CLIArgument._unpack_argument refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring) - Replace if statement with if expression (
assign-if-exp)
This removes the following comments ( why? ):
# use it instead.
# A plugin supplied an alternate conversion,
# Fall back to the default arg processing.
| if dest is None: | ||
| self._destination = self.py_name | ||
| else: | ||
| self._destination = dest | ||
| if group_name is None: | ||
| self._group_name = self.name | ||
| else: | ||
| self._group_name = group_name | ||
| self._destination = self.py_name if dest is None else dest | ||
| self._group_name = self.name if group_name is None else group_name |
There was a problem hiding this comment.
Function BooleanArgument.__init__ refactored with the following changes:
- Replace if statement with if expression [×2] (
assign-if-exp)
| negative_name = 'no-%s' % self.name | ||
| negative_name = f'no-{self.name}' |
There was a problem hiding this comment.
Function BooleanArgument.add_to_arg_table refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring)
| if is_json_value_header(shape): | ||
| return 'JSON' | ||
| return default | ||
| return 'JSON' if is_json_value_header(shape) else default |
There was a problem hiding this comment.
Function CLIDocumentEventHandler._get_argument_type_name refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else) - Replace if statement with if expression (
assign-if-exp)
| doc.write(':ref:`%s <cli:%s>`' % (cmd, full_cmd_name)) | ||
| doc.write(f':ref:`{cmd} <cli:{full_cmd_name}>`') |
There was a problem hiding this comment.
Function CLIDocumentEventHandler.doc_breadcrumbs refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring)
| reference = 'aws ' + reference | ||
| doc.writeln('.. _cli:%s:' % reference) | ||
| reference = f'aws {reference}' | ||
| doc.writeln(f'.. _cli:{reference}:') |
There was a problem hiding this comment.
Function CLIDocumentEventHandler.doc_title refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation) - Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring)
| doc.writeln('%s' % help_command.name) | ||
| doc.writeln(f'{help_command.name}') |
There was a problem hiding this comment.
Function CLIDocumentEventHandler.doc_synopsis_start refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring)
| doc.write('%s, ...' % self._json_example_value_name(argument_model.member)) | ||
| doc.write(f'{self._json_example_value_name(argument_model.member)}, ...') |
There was a problem hiding this comment.
Function OperationDocumentEventHandler._do_json_example refactored with the following changes:
- Replace interpolated string formatting with f-string [×2] (
replace-interpolation-with-fstring)
| doc.write('"%s": %s' % (member_name, | ||
| self._json_example_value_name(member_model))) | ||
| elif member_type_name == 'structure': | ||
| doc.write('"%s": ' % member_name) | ||
| self._json_example(doc, member_model, stack) | ||
| elif member_type_name == 'map': | ||
| doc.write('"%s": ' % member_name) | ||
| self._json_example(doc, member_model, stack) | ||
| elif member_type_name == 'list': | ||
| doc.write('"%s": ' % member_name) | ||
| doc.write(f'"{member_name}": {self._json_example_value_name(member_model)}') | ||
| elif member_type_name in ['structure', 'map', 'list']: | ||
| doc.write(f'"{member_name}": ') |
There was a problem hiding this comment.
Function OperationDocumentEventHandler._doc_input_structure_members refactored with the following changes:
- Merge duplicate blocks in conditional (
merge-duplicate-blocks) - Replace interpolated string formatting with f-string [×2] (
replace-interpolation-with-fstring) - Remove redundant conditional (
remove-redundant-if) - Replace multiple comparisons of same variable with
inoperator (merge-comparisons)
| find_service_and_method_in_event_name(event_name) | ||
| find_service_and_method_in_event_name(event_name) | ||
| doc = help_command.doc | ||
| cli_argument = help_command.arg_table[arg_name] | ||
| if cli_argument.group_name in self._arg_groups: | ||
| if cli_argument.group_name in self._documented_arg_groups: | ||
| # Args with group_names (boolean args) don't | ||
| # need to generate example syntax. | ||
| return | ||
| if ( | ||
| cli_argument.group_name in self._arg_groups | ||
| and cli_argument.group_name in self._documented_arg_groups | ||
| ): | ||
| # Args with group_names (boolean args) don't | ||
| # need to generate example syntax. | ||
| return |
There was a problem hiding this comment.
Function OperationDocumentEventHandler.doc_option_example refactored with the following changes:
- Merge nested if conditions (
merge-nested-ifs) - Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring)
| if member_shape.name in stack: | ||
| # Document the recursion once, otherwise just | ||
| # note the fact that it's recursive and return. | ||
| if stack.count(member_shape.name) > 1: | ||
| if member_shape.type_name == 'structure': | ||
| doc.write('( ... recursive ... )') | ||
| return | ||
| if member_shape.name in stack and stack.count(member_shape.name) > 1: | ||
| if member_shape.type_name == 'structure': | ||
| doc.write('( ... recursive ... )') | ||
| return |
There was a problem hiding this comment.
Function OperationDocumentEventHandler._doc_member_for_output refactored with the following changes:
- Merge nested if conditions (
merge-nested-ifs)
This removes the following comments ( why? ):
# Document the recursion once, otherwise just
# note the fact that it's recursive and return.
| doc.write('(%s)' % member_shape.type_name) | ||
| doc.write(f'({member_shape.type_name})') |
There was a problem hiding this comment.
Function OperationDocumentEventHandler._do_doc_member_for_output refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring)
Branch
masterrefactored by Sourcery.If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.
See our documentation here.
Run Sourcery locally
Reduce the feedback loop during development by using the Sourcery editor plugin:
Review changes via command line
To manually merge these changes, make sure you're on the
masterbranch, then run:Help us improve this pull request!