django/django

View on GitHub
django/db/models/deletion.py

Summary

Maintainability
D
3 days
Test Coverage

Function collect has a Cognitive Complexity of 60 (exceeds 5 allowed). Consider refactoring.
Open

    def collect(self, objs, source=None, nullable=False, collect_related=True,
                source_attr=None, reverse_dependency=False, keep_parents=False,
                fail_on_restricted=True):
        """
        Add 'objs' to the collection of objects to be deleted as well as all
Severity: Minor
Found in django/db/models/deletion.py - About 1 day to fix

Cognitive Complexity

Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

A method's cognitive complexity is based on a few simple rules:

  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
  • Code is considered more complex for each "break in the linear flow of the code"
  • Code is considered more complex when "flow breaking structures are nested"

Further reading

Function delete has a Cognitive Complexity of 31 (exceeds 5 allowed). Consider refactoring.
Open

    def delete(self):
        # sort instance collections
        for model, instances in self.data.items():
            self.data[model] = sorted(instances, key=attrgetter("pk"))

Severity: Minor
Found in django/db/models/deletion.py - About 4 hrs to fix

Cognitive Complexity

Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

A method's cognitive complexity is based on a few simple rules:

  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
  • Code is considered more complex for each "break in the linear flow of the code"
  • Code is considered more complex when "flow breaking structures are nested"

Further reading

File deletion.py has 347 lines of code (exceeds 250 allowed). Consider refactoring.
Open

import operator
from collections import Counter, defaultdict
from functools import partial, reduce
from itertools import chain
from operator import attrgetter
Severity: Minor
Found in django/db/models/deletion.py - About 4 hrs to fix

    Function sort has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
    Open

        def sort(self):
            sorted_models = []
            concrete_models = set()
            models = list(self.data)
            while len(sorted_models) < len(models):
    Severity: Minor
    Found in django/db/models/deletion.py - About 1 hr to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function collect has 8 arguments (exceeds 4 allowed). Consider refactoring.
    Open

        def collect(self, objs, source=None, nullable=False, collect_related=True,
    Severity: Major
    Found in django/db/models/deletion.py - About 1 hr to fix

      Avoid deeply nested control flow statements.
      Open

                              if objs:
                                  key = "'%s.%s'" % (related_model.__name__, field.name)
                                  restricted_objects[key] += objs
                      if restricted_objects:
      Severity: Major
      Found in django/db/models/deletion.py - About 45 mins to fix

        Function can_fast_delete has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
        Open

            def can_fast_delete(self, objs, from_field=None):
                """
                Determine if the objects in the given queryset-like or single object
                can be fast-deleted. This can be done if there are no cascades, no
                parents and no signal listeners for the object class.
        Severity: Minor
        Found in django/db/models/deletion.py - About 35 mins to fix

        Cognitive Complexity

        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

        A method's cognitive complexity is based on a few simple rules:

        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
        • Code is considered more complex for each "break in the linear flow of the code"
        • Code is considered more complex when "flow breaking structures are nested"

        Further reading

        Function add has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
        Open

            def add(self, objs, source=None, nullable=False, reverse_dependency=False):
                """
                Add 'objs' to the collection of objects to be deleted.  If the call is
                the result of a cascade, 'source' should be the model that caused it,
                and 'nullable' should be set to True if the relation can be null.
        Severity: Minor
        Found in django/db/models/deletion.py - About 25 mins to fix

        Cognitive Complexity

        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

        A method's cognitive complexity is based on a few simple rules:

        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
        • Code is considered more complex for each "break in the linear flow of the code"
        • Code is considered more complex when "flow breaking structures are nested"

        Further reading

        Similar blocks of code found in 2 locations. Consider refactoring.
        Open

                if protected_objects:
                    raise ProtectedError(
                        'Cannot delete some instances of model %r because they are '
                        'referenced through protected foreign keys: %s.' % (
                            model.__name__,
        Severity: Major
        Found in django/db/models/deletion.py and 1 other location - About 1 hr to fix
        django/db/models/deletion.py on lines 335..343

        Duplicated Code

        Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

        Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

        When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

        Tuning

        This issue has a mass of 39.

        We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

        The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

        If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

        See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

        Refactorings

        Further Reading

        Similar blocks of code found in 2 locations. Consider refactoring.
        Open

                        if restricted_objects:
                            raise RestrictedError(
                                'Cannot delete some instances of model %r because '
                                'they are referenced through restricted foreign keys: '
                                '%s.' % (
        Severity: Major
        Found in django/db/models/deletion.py and 1 other location - About 1 hr to fix
        django/db/models/deletion.py on lines 301..308

        Duplicated Code

        Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

        Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

        When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

        Tuning

        This issue has a mass of 39.

        We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

        The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

        If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

        See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

        Refactorings

        Further Reading

        There are no issues that match your filters.

        Category
        Status