django/django

View on GitHub
django/db/models/fields/related_descriptors.py

Summary

Maintainability
F
6 days
Test Coverage

File related_descriptors.py has 1232 lines of code (exceeds 250 allowed). Consider refactoring.
Open

"""
Accessors for related objects.

When a field defines a relation between two models, each model class provides
an attribute to access related instances of the other model class (unless the
Severity: Major
Found in django/db/models/fields/related_descriptors.py - About 3 days to fix

    Function create_reverse_many_to_one_manager has a Cognitive Complexity of 26 (exceeds 5 allowed). Consider refactoring.
    Open

    def create_reverse_many_to_one_manager(superclass, rel):
        """
        Create a manager for the reverse side of a many-to-one relation.
    
        This manager subclasses another manager, generally the default manager of
    Severity: Minor
    Found in django/db/models/fields/related_descriptors.py - About 3 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

    Function __set__ has a Cognitive Complexity of 17 (exceeds 5 allowed). Consider refactoring.
    Open

        def __set__(self, instance, value):
            """
            Set the related instance through the forward relation.
    
            With the example above, when setting ``child.parent = parent``:
    Severity: Minor
    Found in django/db/models/fields/related_descriptors.py - About 2 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

    Function __get__ has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring.
    Open

        def __get__(self, instance, cls=None):
            """
            Get the related instance through the forward relation.
    
            With the example above, when getting ``child.parent``:
    Severity: Minor
    Found in django/db/models/fields/related_descriptors.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 set has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring.
    Open

            def set(self, objs, *, bulk=True, clear=False):
                self._check_fk_val()
                # Force evaluation of `objs` in case it's a queryset whose value
                # could be affected by `manager.clear()`. Refs #19816.
                objs = tuple(objs)
    Severity: Minor
    Found in django/db/models/fields/related_descriptors.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 __set__ has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
    Open

        def __set__(self, instance, value):
            """
            Set the related instance through the reverse relation.
    
            With the example above, when setting ``place.restaurant = restaurant``:
    Severity: Minor
    Found in django/db/models/fields/related_descriptors.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 set has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
    Open

            def set(self, objs, *, clear=False, through_defaults=None):
                # Force evaluation of `objs` in case it's a queryset whose value
                # could be affected by `manager.clear()`. Refs #19816.
                objs = tuple(objs)
    
    
    Severity: Minor
    Found in django/db/models/fields/related_descriptors.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 remove has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
    Open

                def remove(self, *objs, bulk=True):
                    if not objs:
                        return
                    self._check_fk_val()
                    val = self.field.get_foreign_related_value(self.instance)
    Severity: Minor
    Found in django/db/models/fields/related_descriptors.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 __get__ has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
    Open

        def __get__(self, instance, cls=None):
            """
            Get the related instance through the reverse relation.
    
            With the example above, when getting ``place.restaurant``:
    Severity: Minor
    Found in django/db/models/fields/related_descriptors.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 add has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
    Open

            def add(self, *objs, bulk=True):
                self._check_fk_val()
                self._remove_prefetched_objects()
                db = router.db_for_write(self.model, instance=self.instance)
    
    
    Severity: Minor
    Found in django/db/models/fields/related_descriptors.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 _get_target_ids has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
    Open

            def _get_target_ids(self, target_field_name, objs):
                """
                Return the set of ids of `objs` that the target field references.
                """
                from django.db.models import Model
    Severity: Minor
    Found in django/db/models/fields/related_descriptors.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 get_prefetch_querysets has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
    Open

        def get_prefetch_querysets(self, instances, querysets=None):
            if querysets and len(querysets) != 1:
                raise ValueError(
                    "querysets argument of get_prefetch_querysets() should have a length "
                    "of 1."
    Severity: Minor
    Found in django/db/models/fields/related_descriptors.py - About 55 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

    Avoid deeply nested control flow statements.
    Open

                                if obj in old_objs:
                                    old_objs.remove(obj)
                                else:
                                    new_objs.append(obj)
    
    
    Severity: Major
    Found in django/db/models/fields/related_descriptors.py - About 45 mins to fix

      Function set has 5 arguments (exceeds 4 allowed). Consider refactoring.
      Open

              def set(self, objs, *, bulk=True, clear=False):
      Severity: Minor
      Found in django/db/models/fields/related_descriptors.py - About 35 mins to fix

        Function set has 5 arguments (exceeds 4 allowed). Consider refactoring.
        Open

                def set(self, objs, *, clear=False, through_defaults=None):
        Severity: Minor
        Found in django/db/models/fields/related_descriptors.py - About 35 mins to fix

          Function _add_items has 5 arguments (exceeds 4 allowed). Consider refactoring.
          Open

                  def _add_items(
          Severity: Minor
          Found in django/db/models/fields/related_descriptors.py - About 35 mins to fix

            Function _get_missing_target_ids has 5 arguments (exceeds 4 allowed). Consider refactoring.
            Open

                    def _get_missing_target_ids(
            Severity: Minor
            Found in django/db/models/fields/related_descriptors.py - About 35 mins to fix

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

                      def _remove_items(self, source_field_name, target_field_name, *objs):
                          # source_field_name: the PK colname in join table for the source object
                          # target_field_name: the PK colname in join table for the target object
                          # *objs - objects to remove. Either object instances, or primary
                          # keys of object instances.
              Severity: Minor
              Found in django/db/models/fields/related_descriptors.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 get_prefetch_querysets has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
              Open

                      def get_prefetch_querysets(self, instances, querysets=None):
                          if querysets and len(querysets) != 1:
                              raise ValueError(
                                  "querysets argument of get_prefetch_querysets() should have a "
                                  "length of 1."
              Severity: Minor
              Found in django/db/models/fields/related_descriptors.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

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

              def _filter_prefetch_queryset(queryset, field_name, instances):
                  predicate = Q(**{f"{field_name}__in": instances})
                  db = queryset._db or DEFAULT_DB_ALIAS
                  if queryset.query.is_sliced:
                      if not connections[db].features.supports_over_clause:
              Severity: Minor
              Found in django/db/models/fields/related_descriptors.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

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

                          def _clear(self, queryset, bulk):
                              self._remove_prefetched_objects()
                              db = router.db_for_write(self.model, instance=self.instance)
                              queryset = queryset.using(db)
                              if bulk:
              Severity: Minor
              Found in django/db/models/fields/related_descriptors.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

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

                  def __set__(self, instance, value):
                      super().__set__(instance, value)
                      # If the primary key is a link to a parent model and a parent instance
                      # is being set, update the value of the inherited pk(s).
                      if self.field.primary_key and self.field.remote_field.parent_link:
              Severity: Minor
              Found in django/db/models/fields/related_descriptors.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

              There are no issues that match your filters.

              Category
              Status