Skip to content
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

Consolidate package config to pyproject.toml (Sourcery refactored) #183

Open
wants to merge 2 commits into
base: pyproject
Choose a base branch
from

Conversation

sourcery-ai[bot]
Copy link

@sourcery-ai sourcery-ai bot commented Aug 16, 2022

Pull Request #182 refactored by Sourcery.

If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.

NOTE: As code is pushed to the original Pull Request, Sourcery will
re-run and update (force-push) this Pull Request with new refactorings as
necessary. If Sourcery finds no refactorings at any point, this Pull Request
will be closed automatically.

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 pyproject branch, then run:

git fetch origin sourcery/pyproject
git merge --ff-only FETCH_HEAD
git reset HEAD^

Help us improve this pull request!

@sourcery-ai sourcery-ai bot requested a review from shosca August 16, 2022 12:19
Copy link
Author

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to GitHub API limits, only the first 60 comments can be shown.

Comment on lines -194 to +202
base_formfield_callback = None
for base in bases:
if hasattr(base, "Meta") and hasattr(base.Meta, "formfield_callback"):
base_formfield_callback = base.Meta.formfield_callback
break
base_formfield_callback = next(
(
base.Meta.formfield_callback
for base in bases
if hasattr(base, "Meta")
and hasattr(base.Meta, "formfield_callback")
),
None,
)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ModelFormMetaclass.__new__ refactored with the following changes:

  • Use the built-in function next instead of a for-loop (use-next)

Comment on lines -334 to 340
"The {} could not be saved because the data didn't validate.".format(self.instance.__class__.__name__)
f"The {self.instance.__class__.__name__} could not be saved because the data didn't validate."
)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function BaseModelForm.save refactored with the following changes:

Comment on lines -375 to +380
field_setter = getattr(self, "set_" + name, None)
field_setter = getattr(self, f"set_{name}", None)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function BaseModelForm.update_attribute refactored with the following changes:

Comment on lines -411 to +416
class_name = model.__name__ + "Form"
class_name = f"{model.__name__}Form"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function modelform_factory refactored with the following changes:

raise Http404("No %s matches the given query." % query)
raise Http404(f"No {query} matches the given query.")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_object_or_404 refactored with the following changes:

getattr(instance, "clean_" + self.name, bool)()
getattr(instance, f"clean_{self.name}", bool)()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function column_info.validate refactored with the following changes:

if value is None:
return value
return str(value).strip()
return value if value is None else str(value).strip()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function string_column_info.to_python refactored with the following changes:

Comment on lines -327 to +325
if value in ("f", "F"):
return False
return self.coercer.to_python(value)
return False if value in ("f", "F") else self.coercer.to_python(value)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function boolean_column_info.to_python refactored with the following changes:

Comment on lines -79 to +80
self.label = "{}.{}".format(self.app_label, self.object_name)
self.label_lower = "{}.{}".format(self.app_label, self.model_name)
self.label = f"{self.app_label}.{self.object_name}"
self.label_lower = f"{self.app_label}.{self.model_name}"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function model_info.__init__ refactored with the following changes:

Comment on lines -151 to +160
reprs.extend(" " + repr(i) for i in self.primary_keys.values())
reprs.extend(" " + repr(i) for _, i in sorted(self.properties.items()))
reprs.extend(" " + i for i in chain(*[repr(c).split("\n") for _, c in sorted(self.composites.items())]))
reprs.extend(" " + repr(i) for _, i in sorted(self.relationships.items()))
reprs.extend(f" {repr(i)}" for i in self.primary_keys.values())
reprs.extend(f" {repr(i)}" for _, i in sorted(self.properties.items()))
reprs.extend(
f" {i}"
for i in chain(
*[repr(c).split("\n") for _, c in sorted(self.composites.items())]
)
)

reprs.extend(f" {repr(i)}" for _, i in sorted(self.relationships.items()))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function model_info.__repr__ refactored with the following changes:

Comment on lines -189 to +195
if len(pks) < 2:
return next(iter(pks), None)

return tuple(pks)
return next(iter(pks), None) if len(pks) < 2 else tuple(pks)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function model_info.primary_keys_from_dict refactored with the following changes:

Comment on lines -58 to +61
for i in target_pk:
self.local_remote_pairs_for_identity_key.append((pairs[i], i))
self.local_remote_pairs_for_identity_key.extend(
(pairs[i], i) for i in target_pk
)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function relation_info.__init__ refactored with the following changes:

Comment on lines 101 to 105
if self.uselist:
return ModelMultipleChoiceField

return ModelChoiceField
return ModelMultipleChoiceField if self.uselist else ModelChoiceField
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function relation_info.get_form_class refactored with the following changes:

Comment on lines -107 to +108
return "<relation_info({}.{})>".format(self.parent_model.__name__, self.name)
return f"<relation_info({self.parent_model.__name__}.{self.name})>"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function relation_info.__repr__ refactored with the following changes:

Comment on lines -33 to +37
if not (self.data or self.files):
return len(self.get_queryset())

return super().initial_form_count()
return (
super().initial_form_count()
if (self.data or self.files)
else len(self.get_queryset())
)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function BaseModelFormSet.initial_form_count refactored with the following changes:

return "%s_list" % model.__name__.lower()
return f"{model.__name__.lower()}_list"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function MultipleObjectMixin.get_context_object_name refactored with the following changes:

self.template_name_suffix = "_" + self.action
self.template_name_suffix = f"_{self.action}"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function GenericViewSet.get_template_names refactored with the following changes:

view.suffix = initkwargs.get("suffix", None)
view.suffix = initkwargs.get("suffix")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function GenericViewSet.as_view refactored with the following changes:

return "%s_list" % model.__name__.lower()
return f"{model.__name__.lower()}_list"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ListModelMixin.get_list_context_object_name refactored with the following changes:

if form.is_valid():
return self.form_valid(form)

return self.form_invalid(form)
return self.form_valid(form) if form.is_valid() else self.form_invalid(form)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ModelFormMixin.process_form refactored with the following changes:

Comment on lines -32 to +35
instance=instance, data=self.request.POST if self.request.POST else None
instance=instance, data=self.request.POST or None
)


Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function PollsViewSet.get_choice_formset refactored with the following changes:

Comment on lines -91 to +97
db.add_all([Owner(first_name="first_name {}".format(i), last_name="last_name {}".format(i)) for i in range(10)])
db.add_all(
[
Owner(first_name=f"first_name {i}", last_name=f"last_name {i}")
for i in range(10)
]
)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function TestModelChoiceField.setUp refactored with the following changes:

Comment on lines -204 to +216
db.add_all([Owner(first_name="first_name {}".format(i), last_name="last_name {}".format(i)) for i in range(10)])
db.add_all(
[
Owner(first_name=f"first_name {i}", last_name=f"last_name {i}")
for i in range(10)
]
)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function TestModelMultipleChoiceField.setUp refactored with the following changes:

' <option value="{}">{}</option>'.format(self.owner.id, self.owner),
f' <option value="{self.owner.id}">{self.owner}</option>',
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function TestModelForm.test_modelform_factory_new_render refactored with the following changes:

Comment on lines -407 to +408
' <option value="{}">{}</option>'.format(self.owner.id, self.owner),
f' <option value="{self.owner.id}">{self.owner}</option>',
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function TestModelForm.test_modelform_factory_instance_render refactored with the following changes:

Comment on lines -61 to +62
self.write_migration(M1, "{}_.py".format("000000000000"))
self.write_migration(M2, "{}_.py".format("000000000001"))
self.write_migration(M1, '000000000000_.py')
self.write_migration(M2, '000000000001_.py')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function TestHeads.setUp refactored with the following changes:

self.delete_migration("{}_.py".format(rev))
self.delete_migration(f"{rev}_.py")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function TestHeads.tearDown refactored with the following changes:

"Path: {}/000000000001_.py\n".format(MIGRATION_DIR),
f"Path: {MIGRATION_DIR}/000000000001_.py\n",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function TestHeads.test_verbose refactored with the following changes:

Comment on lines -26 to +27
self.delete_migration("{}_.py".format(rev))
self.delete_migration("{}_zero.py".format(rev))
self.delete_migration(f"{rev}_.py")
self.delete_migration(f"{rev}_zero.py")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function TestRevision.tearDown refactored with the following changes:

self.assertTrue(os.path.isfile(os.path.join(MIGRATION_DIR, "{}_zero.py".format(rev))))
self.assertTrue(os.path.isfile(os.path.join(MIGRATION_DIR, f"{rev}_zero.py")))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function TestRevision.test_with_name refactored with the following changes:

@sourcery-ai
Copy link
Author

sourcery-ai bot commented Aug 16, 2022

Sourcery Code Quality Report

❌  Merging this PR will decrease code quality in the affected files by 0.10%.

Quality metrics Before After Change
Complexity 4.78 ⭐ 4.70 ⭐ -0.08 👍
Method Length 61.12 🙂 60.97 ⭐ -0.15 👍
Working memory 6.61 🙂 6.68 🙂 0.07 👎
Quality 74.28% 🙂 74.18% 🙂 -0.10% 👎
Other metrics Before After Change
Lines 5813 5837 24
Changed files Quality Before Quality After Quality Change
django_sorcery/forms.py 54.25% 🙂 54.71% 🙂 0.46% 👍
django_sorcery/shortcuts.py 89.44% ⭐ 89.23% ⭐ -0.21% 👎
django_sorcery/db/fields.py 77.76% ⭐ 78.21% ⭐ 0.45% 👍
django_sorcery/db/models.py 58.36% 🙂 58.36% 🙂 0.00%
django_sorcery/db/profiler.py 82.66% ⭐ 82.98% ⭐ 0.32% 👍
django_sorcery/db/query.py 69.30% 🙂 69.06% 🙂 -0.24% 👎
django_sorcery/db/sqlalchemy.py 76.42% ⭐ 76.42% ⭐ 0.00%
django_sorcery/db/meta/column.py 74.55% 🙂 74.13% 🙂 -0.42% 👎
django_sorcery/db/meta/model.py 57.98% 🙂 57.71% 🙂 -0.27% 👎
django_sorcery/db/meta/relations.py 48.64% 😞 49.53% 😞 0.89% 👍
django_sorcery/formsets/base.py 62.27% 🙂 61.42% 🙂 -0.85% 👎
django_sorcery/formsets/inline.py 72.57% 🙂 70.88% 🙂 -1.69% 👎
django_sorcery/management/alembic.py 68.46% 🙂 68.44% 🙂 -0.02% 👎
django_sorcery/management/base.py 77.04% ⭐ 76.47% ⭐ -0.57% 👎
django_sorcery/validators/init.py 98.50% ⭐ % %
django_sorcery/validators/base.py 79.63% ⭐ 79.89% ⭐ 0.26% 👍
django_sorcery/views/base.py 72.32% 🙂 71.99% 🙂 -0.33% 👎
django_sorcery/views/edit.py 91.55% ⭐ 91.63% ⭐ 0.08% 👍
django_sorcery/views/list.py 80.17% ⭐ 80.14% ⭐ -0.03% 👎
django_sorcery/viewsets/base.py 58.07% 🙂 58.10% 🙂 0.03% 👍
django_sorcery/viewsets/mixins.py 89.07% ⭐ 88.92% ⭐ -0.15% 👎
test_site/polls/views.py 82.69% ⭐ 82.96% ⭐ 0.27% 👍
tests/test_fields.py 81.71% ⭐ 81.89% ⭐ 0.18% 👍
tests/test_forms.py 82.79% ⭐ 81.91% ⭐ -0.88% 👎
tests/formsets/test_base.py 74.99% 🙂 73.50% 🙂 -1.49% 👎
tests/formsets/test_inline.py 84.04% ⭐ 83.24% ⭐ -0.80% 👎
tests/management/commands/test_sorcery_current.py 88.29% ⭐ 88.71% ⭐ 0.42% 👍
tests/management/commands/test_sorcery_downgrade.py 89.04% ⭐ 89.22% ⭐ 0.18% 👍
tests/management/commands/test_sorcery_heads.py 92.73% ⭐ 92.29% ⭐ -0.44% 👎
tests/management/commands/test_sorcery_revision.py 87.90% ⭐ 87.96% ⭐ 0.06% 👍
tests/management/commands/test_sorcery_stamp.py 92.38% ⭐ 92.77% ⭐ 0.39% 👍
tests/management/commands/test_sorcery_upgrade.py 87.92% ⭐ 87.97% ⭐ 0.05% 👍
tests/views/test_detail.py 91.56% ⭐ 91.53% ⭐ -0.03% 👎
tests/views/test_list.py 86.92% ⭐ 87.57% ⭐ 0.65% 👍

Here are some functions in these files that still need a tune-up:

File Function Complexity Length Working Memory Quality Recommendation
django_sorcery/db/meta/model.py model_info.clean_relation_fields 52 ⛔ 211 ⛔ 13 😞 21.90% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
django_sorcery/db/models.py clone 25 😞 267 ⛔ 14 😞 26.69% 😞 Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
django_sorcery/db/meta/column.py column_info.__init__ 17 🙂 396 ⛔ 12 😞 31.81% 😞 Try splitting into smaller methods. Extract out complex expressions
django_sorcery/forms.py fields_for_model 18 🙂 170 😞 17 ⛔ 34.51% 😞 Try splitting into smaller methods. Extract out complex expressions
django_sorcery/db/meta/relations.py relation_info.__init__ 7 ⭐ 336 ⛔ 17 ⛔ 35.31% 😞 Try splitting into smaller methods. Extract out complex expressions

Legend and Explanation

The emojis denote the absolute quality of the code:

  • ⭐ excellent
  • 🙂 good
  • 😞 poor
  • ⛔ very poor

The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request.


Please see our documentation here for details on how these metrics are calculated.

We are actively working on this report - lots more documentation and extra metrics to come!

Help us improve this quality report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant