Sourcery Starbot ⭐ refactored dgk/django-business-logic#42
Sourcery Starbot ⭐ refactored dgk/django-business-logic#42SourceryAI wants to merge 1 commit intodgk:masterfrom
Conversation
| """ | ||
| init_py = open(os.path.join(package, '__init__.py')).read() | ||
| return re.search("__version__ = ['\"]([^'\"]+)['\"]", init_py).group(1) | ||
| return re.search("__version__ = ['\"]([^'\"]+)['\"]", init_py)[1] |
There was a problem hiding this comment.
Function get_version refactored with the following changes:
- Replace m.group(x) with m[x] for re.Match objects (
use-getitem-for-re-match-groups)
|
|
||
| def clean(): | ||
| for d in ('dist', 'build', '{}.egg-info'.format(NAME.replace('-', '_'))): | ||
| for d in ('dist', 'build', f"{NAME.replace('-', '_')}.egg-info"): |
There was a problem hiding this comment.
Function clean refactored with the following changes:
- Replace call to format with f-string. (
use-fstring-for-formatting)
| long_description=codecs.open(abs_path('README.rst'), encoding='utf-8').read(), | ||
| long_description=codecs.open( | ||
| abs_path('README.rst'), encoding='utf-8' | ||
| ).read(), | ||
| author=AUTHOR, | ||
| author_email=AUTHOR_EMAIL, | ||
| url=URL, | ||
| download_url='{}/archive/{}.tar.gz'.format(URL, version), | ||
| download_url=f'{URL}/archive/{version}.tar.gz', |
There was a problem hiding this comment.
Lines 83-128 refactored with the following changes:
- Replace call to format with f-string. [×2] (
use-fstring-for-formatting)
| for k in kwargs.keys(): | ||
| if k not in self.defaults: | ||
| raise TypeError('Incorrect kwarg {}'.format(k)) | ||
| raise TypeError(f'Incorrect kwarg {k}') |
There was a problem hiding this comment.
Function ContextConfig.__init__ refactored with the following changes:
- Replace call to format with f-string. (
use-fstring-for-formatting)
| break | ||
|
|
||
| method_name = 'visit_{}'.format(camel_case_to_snake_case(cls.__name__)) | ||
| method_name = f'visit_{camel_case_to_snake_case(cls.__name__)}' |
There was a problem hiding this comment.
Function BlocklyXmlBuilder.visit refactored with the following changes:
- Replace call to format with f-string. [×2] (
use-fstring-for-formatting)
| method_name = 'visit_block_{}'.format(node.get('type')) | ||
| method_name = f"visit_block_{node.get('type')}" |
There was a problem hiding this comment.
Function BlocklyXmlParser.visit_block refactored with the following changes:
- Replace call to format with f-string. (
use-fstring-for-formatting)
| method_name = 'visit_field_{}'.format(node.get('name').lower()) | ||
| method_name = f"visit_field_{node.get('name').lower()}" |
There was a problem hiding this comment.
Function BlocklyXmlParser.visit_field refactored with the following changes:
- Replace call to format with f-string. (
use-fstring-for-formatting)
|
|
||
|
|
There was a problem hiding this comment.
Lines 22-513 refactored with the following changes:
- Unwrap a constant iterable constructor. [×2] (
unwrap-iterable-construction)
| if not self.frames: | ||
| return None | ||
| return self.frames[-1] | ||
| return self.frames[-1] if self.frames else None |
There was a problem hiding this comment.
Function Context._frame refactored with the following changes:
- Swap if/else branches of if expression to remove negation (
swap-if-expression) - Lift code into else after jump in control flow (
reintroduce-else) - Replace if statement with if expression (
assign-if-exp)
| if not self.config.cache: | ||
| return node.get_children().all() | ||
|
|
||
| return super(Context, self).get_children(node) | ||
| return ( | ||
| super(Context, self).get_children(node) | ||
| if self.config.cache | ||
| else node.get_children().all() | ||
| ) |
There was a problem hiding this comment.
Function Context.get_children refactored with the following changes:
- Swap if/else branches of if expression to remove negation (
swap-if-expression) - Lift code into else after jump in control flow (
reintroduce-else) - Replace if statement with if expression (
assign-if-exp)
| value = six.text_type(value) | ||
| if len(value) > LOG_ENTRY_VALUE_LENGTH: | ||
| value = value[:LOG_ENTRY_VALUE_LENGTH - 3] + '...' | ||
| value = f'{value[:LOG_ENTRY_VALUE_LENGTH - 3]}...' |
There was a problem hiding this comment.
Function Logger.prepare_value refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
|
|
||
| def __str__(self): | ||
| return 'Node {}({}): {}'.format(self.id, self.content_type, self.content_object) | ||
| return f'Node {self.id}({self.content_type}): {self.content_object}' |
There was a problem hiding this comment.
Function Node.__str__ refactored with the following changes:
- Replace call to format with f-string. (
use-fstring-for-formatting)
| node_kwargs = dict() | ||
| node_kwargs = {} | ||
|
|
||
| if self.clone is None: | ||
| clone = self.clone = Node.add_root(**node_kwargs) | ||
| clone.rgt = node.rgt | ||
| clone.lft = node.lft | ||
| clone.save() | ||
| else: | ||
| node_kwargs.update( | ||
| dict([(field_name, getattr(node, field_name)) for field_name in ('rgt', 'lft', 'depth')])) | ||
| node_kwargs.update(dict(tree_id=self.clone.tree_id)) | ||
| node_kwargs |= dict( | ||
| [ | ||
| (field_name, getattr(node, field_name)) | ||
| for field_name in ('rgt', 'lft', 'depth') | ||
| ] | ||
| ) | ||
|
|
||
| node_kwargs |= dict(tree_id=self.clone.tree_id) | ||
| clone = Node.objects.create(**node_kwargs) | ||
| clone.save() | ||
|
|
||
| clone.save() |
There was a problem hiding this comment.
Function Node.clone.CloneVisitor.visit refactored with the following changes:
- Replace dict() with {} (
dict-literal) - Hoist repeated code outside conditional statement (
hoist-statement-from-if) - Merge dictionary updates via the union operator. [×2] (
dict-assign-update-to-union)
| self._child_by_parent_id = {} | ||
| for parent in tree: | ||
| self._child_by_parent_id[parent.id] = [ | ||
| node for node in tree | ||
| if node.lft >= parent.lft and node.lft <= parent.rgt - 1 and node.depth == parent.depth + 1 | ||
| self._child_by_parent_id = { | ||
| parent.id: [ | ||
| node | ||
| for node in tree | ||
| if node.lft >= parent.lft | ||
| and node.lft <= parent.rgt - 1 | ||
| and node.depth == parent.depth + 1 | ||
| ] | ||
| for parent in tree | ||
| } |
There was a problem hiding this comment.
Function NodeCache._initialize refactored with the following changes:
- Convert for loop into dictionary comprehension (
dict-comprehension)
|
|
||
| def get_variable_name(self): | ||
| return '{}.{}'.format(self.program_argument.name, self.name) | ||
| return f'{self.program_argument.name}.{self.name}' |
There was a problem hiding this comment.
Function ProgramArgumentField.get_variable_name refactored with the following changes:
- Replace call to format with f-string. (
use-fstring-for-formatting)
|
|
||
| def __str__(self): | ||
| return u'%s %s' % (self.first_name, self.last_name) | ||
| return f'{self.first_name} {self.last_name}' |
There was a problem hiding this comment.
Function Author.__str__ refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring)
| fields = {} | ||
| for field in self.field_list: | ||
| fields[field] = ProgramArgumentField.objects.create( | ||
| return { | ||
| field: ProgramArgumentField.objects.create( | ||
| name=field, | ||
| program_argument=argument, | ||
| ) | ||
|
|
||
| return fields | ||
| for field in self.field_list | ||
| } |
There was a problem hiding this comment.
Function ProgramTestBase.create_argument_fields refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable) - Convert for loop into dictionary comprehension (
dict-comprehension)
| self.assertEqual(1, len(context.frames)) | ||
| elif node == node1: | ||
| self.assertEqual(2, len(context.frames)) | ||
| elif node == node2: | ||
| self.assertEqual(2, len(context.frames)) | ||
| elif node == node1_1: | ||
| self.assertEqual(2, len(context.frames)) | ||
| elif node == node2_1: | ||
| elif node in [node1, node2, node1_1, node2_1]: | ||
| self.assertEqual(2, len(context.frames)) |
There was a problem hiding this comment.
Function FrameTest.test_switch_frames.on_interpret_enter refactored with the following changes:
- Merge duplicate blocks in conditional (
merge-duplicate-blocks) - Remove redundant conditional (
remove-redundant-if) - Replace multiple comparisons of same variable with
inoperator (merge-comparisons)
| self.assertEqual(1, len(context.frames)) | ||
| elif node == node1: | ||
| self.assertEqual(2, len(context.frames)) | ||
| elif node == node2: | ||
| self.assertEqual(2, len(context.frames)) | ||
| elif node == node1_1: | ||
| self.assertEqual(2, len(context.frames)) | ||
| elif node == node2_1: | ||
| elif node in [node1, node2, node1_1, node2_1]: | ||
| self.assertEqual(2, len(context.frames)) |
There was a problem hiding this comment.
Function FrameTest.test_switch_frames.on_interpret_leave refactored with the following changes:
- Merge duplicate blocks in conditional (
merge-duplicate-blocks) - Remove redundant conditional (
remove-redundant-if) - Replace multiple comparisons of same variable with
inoperator (merge-comparisons)
|
|
||
| try: | ||
| 0.0 // 0.0 | ||
| 1.0 |
There was a problem hiding this comment.
Function LogTest.test_log_exception refactored with the following changes:
- Simplify binary operation (
bin-op-identity)
| self.assertEqual('{}.{}'.format(self.argument.name, 'int_value'), int_value_field.variable_definition.name) | ||
| self.assertEqual( | ||
| f'{self.argument.name}.int_value', | ||
| int_value_field.variable_definition.name, | ||
| ) |
There was a problem hiding this comment.
Function ProgramTest.test_program_argument_field_variable_definition refactored with the following changes:
- Replace call to format with f-string. (
use-fstring-for-formatting) - Simplify unnecessary nesting, casting and constant values in f-strings (
simplify-fstring-formatting)
| self.assertEqual('{}.{}'.format(self.argument.name, 'int_value'), variable_definition.name) | ||
| self.assertEqual(f'{self.argument.name}.int_value', variable_definition.name) |
There was a problem hiding this comment.
Function ProgramTest.test_save_program_argument_change_field_variable_definition refactored with the following changes:
- Replace call to format with f-string. (
use-fstring-for-formatting) - Simplify unnecessary nesting, casting and constant values in f-strings (
simplify-fstring-formatting)
| variable_definitions = {} | ||
|
|
||
| for field in ( | ||
| variable_definitions = { | ||
| field: VariableDefinition.objects.create(name=field) | ||
| for field in ( | ||
| 'test_model.int_value', | ||
| 'test_model.not_exists', | ||
| 'test_model.foreign_value.string_value', | ||
| 'test_model.foreign_value.not_exists', | ||
| ): | ||
| variable_definitions[field] = VariableDefinition.objects.create(name=field) | ||
| ) | ||
| } | ||
|
|
There was a problem hiding this comment.
Function VariableTest.test_get_variable_returns_recursive_attribute_undefined refactored with the following changes:
- Convert for loop into dictionary comprehension (
dict-comprehension)
| level_objects = [ | ||
| dict(data=dict(object_id=obj_cls.objects.create(**obj_kwargs).id, content_type_id=content_type_id)) | ||
| for x in range(pow(2, level)) | ||
| dict( | ||
| data=dict( | ||
| object_id=obj_cls.objects.create(**obj_kwargs).id, | ||
| content_type_id=content_type_id, | ||
| ) | ||
| ) | ||
| for _ in range(pow(2, level)) | ||
| ] | ||
|
|
There was a problem hiding this comment.
Function symmetric_tree refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore)
| print('\n'.join([ | ||
| '%s %s %s %s %s %s' % (n.pk, getattr(n, '%s_id' % opts.parent_attr) or '-', getattr(n, opts.tree_id_attr), | ||
| getattr(n, opts.level_attr), getattr(n, opts.left_attr), getattr(n, opts.right_attr)) | ||
| for n in nodes | ||
| ])) | ||
| print( | ||
| '\n'.join( | ||
| [ | ||
| f"{n.pk} {getattr(n, f'{opts.parent_attr}_id') or '-'} {getattr(n, opts.tree_id_attr)} {getattr(n, opts.level_attr)} {getattr(n, opts.left_attr)} {getattr(n, opts.right_attr)}" | ||
| for n in nodes | ||
| ] | ||
| ) | ||
| ) |
There was a problem hiding this comment.
Function print_tree_details refactored with the following changes:
- Replace interpolated string formatting with f-string [×2] (
replace-interpolation-with-fstring)
| ret = super(Client, self).post( | ||
| path, data=data, content_type=content_type, follow=follow, HTTP_X_REQUESTED_WITH='XMLHttpRequest', **extra) | ||
| return ret | ||
| return super(Client, self).post( | ||
| path, | ||
| data=data, | ||
| content_type=content_type, | ||
| follow=follow, | ||
| HTTP_X_REQUESTED_WITH='XMLHttpRequest', | ||
| **extra | ||
| ) |
There was a problem hiding this comment.
Function JSONClient.post refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| ret = super(Client, self).put( | ||
| path, data=data, content_type=content_type, follow=follow, HTTP_X_REQUESTED_WITH='XMLHttpRequest', **extra) | ||
| return ret | ||
| return super(Client, self).put( | ||
| path, | ||
| data=data, | ||
| content_type=content_type, | ||
| follow=follow, | ||
| HTTP_X_REQUESTED_WITH='XMLHttpRequest', | ||
| **extra | ||
| ) |
There was a problem hiding this comment.
Function JSONClient.put refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| ret = super(Client, self).delete( | ||
| path, data=data, content_type=content_type, follow=follow, HTTP_X_REQUESTED_WITH='XMLHttpRequest', **extra) | ||
| return ret | ||
| return super(Client, self).delete( | ||
| path, | ||
| data=data, | ||
| content_type=content_type, | ||
| follow=follow, | ||
| HTTP_X_REQUESTED_WITH='XMLHttpRequest', | ||
| **extra | ||
| ) |
There was a problem hiding this comment.
Function JSONClient.delete refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| self.assertEqual(ContentType.objects.get_for_model(Model).id, content_type['id']) | ||
|
|
||
| fields = dict((x['name'], x) for x in argument['fields']) | ||
| fields = {x['name']: x for x in argument['fields']} |
There was a problem hiding this comment.
Function ProgramInterfaceTest.test_program_interface_view refactored with the following changes:
- Replace list(), dict() or set() with comprehension (
collection-builtin-to-comprehension)
| self.test_models = [] | ||
|
|
||
| for i in range(11): | ||
| self.test_models.append(Model.objects.create(string_value='str_{}'.format(str(i) * 3))) | ||
| self.test_models = [ | ||
| Model.objects.create(string_value=f'str_{str(i) * 3}') | ||
| for i in range(11) | ||
| ] |
There was a problem hiding this comment.
Function ReferenceListTest.setUp refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension) - Replace call to format with f-string. (
use-fstring-for-formatting)
Thanks for starring sourcery-ai/sourcery ✨ 🌟 ✨
Here's your pull request refactoring your most popular Python repo.
If you want Sourcery to refactor all your Python repos and incoming pull requests install our bot.
Review changes via command line
To manually merge these changes, make sure you're on the
masterbranch, then run: