treyhunner/django-simple-history

View on GitHub

Showing 47 of 47 total issues

Cyclomatic complexity is too high in method _process_instance. (6)
Open

    def _process_instance(self, instance, model, stop_date=None, dry_run=True):
        entries_deleted = 0
        history = utils.get_history_manager_for_model(instance)
        o_qs = history.all()
        if stop_date:

Cyclomatic Complexity

Cyclomatic Complexity corresponds to the number of decisions a block of code contains plus 1. This number (also called McCabe number) is equal to the number of linearly independent paths through the code. This number can be used as a guide when testing conditional logic in blocks.

Radon analyzes the AST tree of a Python program to compute Cyclomatic Complexity. Statements have the following effects on Cyclomatic Complexity:

Construct Effect on CC Reasoning
if +1 An if statement is a single decision.
elif +1 The elif statement adds another decision.
else +0 The else statement does not cause a new decision. The decision is at the if.
for +1 There is a decision at the start of the loop.
while +1 There is a decision at the while statement.
except +1 Each except branch adds a new conditional path of execution.
finally +0 The finally block is unconditionally executed.
with +1 The with statement roughly corresponds to a try/except block (see PEP 343 for details).
assert +1 The assert statement internally roughly equals a conditional statement.
Comprehension +1 A list/set/dict comprehension of generator expression is equivalent to a for loop.
Boolean Operator +1 Every boolean operator (and, or) adds a decision point.

Source: http://radon.readthedocs.org/en/latest/intro.html

Cyclomatic complexity is too high in method _handle_model_list. (6)
Open

    def _handle_model_list(self, *args):
        failing = False
        for natural_key in args:
            try:
                model, history = self._model_from_natural_key(natural_key)

Cyclomatic Complexity

Cyclomatic Complexity corresponds to the number of decisions a block of code contains plus 1. This number (also called McCabe number) is equal to the number of linearly independent paths through the code. This number can be used as a guide when testing conditional logic in blocks.

Radon analyzes the AST tree of a Python program to compute Cyclomatic Complexity. Statements have the following effects on Cyclomatic Complexity:

Construct Effect on CC Reasoning
if +1 An if statement is a single decision.
elif +1 The elif statement adds another decision.
else +0 The else statement does not cause a new decision. The decision is at the if.
for +1 There is a decision at the start of the loop.
while +1 There is a decision at the while statement.
except +1 Each except branch adds a new conditional path of execution.
finally +0 The finally block is unconditionally executed.
with +1 The with statement roughly corresponds to a try/except block (see PEP 343 for details).
assert +1 The assert statement internally roughly equals a conditional statement.
Comprehension +1 A list/set/dict comprehension of generator expression is equivalent to a for loop.
Boolean Operator +1 Every boolean operator (and, or) adds a decision point.

Source: http://radon.readthedocs.org/en/latest/intro.html

Cyclomatic complexity is too high in method _bulk_history_create. (6)
Open

    def _bulk_history_create(self, model, batch_size):
        """Save a copy of all instances to the historical model.

        :param model: Model you want to bulk create
        :param batch_size: number of models to create at once.

Cyclomatic Complexity

Cyclomatic Complexity corresponds to the number of decisions a block of code contains plus 1. This number (also called McCabe number) is equal to the number of linearly independent paths through the code. This number can be used as a guide when testing conditional logic in blocks.

Radon analyzes the AST tree of a Python program to compute Cyclomatic Complexity. Statements have the following effects on Cyclomatic Complexity:

Construct Effect on CC Reasoning
if +1 An if statement is a single decision.
elif +1 The elif statement adds another decision.
else +0 The else statement does not cause a new decision. The decision is at the if.
for +1 There is a decision at the start of the loop.
while +1 There is a decision at the while statement.
except +1 Each except branch adds a new conditional path of execution.
finally +0 The finally block is unconditionally executed.
with +1 The with statement roughly corresponds to a try/except block (see PEP 343 for details).
assert +1 The assert statement internally roughly equals a conditional statement.
Comprehension +1 A list/set/dict comprehension of generator expression is equivalent to a for loop.
Boolean Operator +1 Every boolean operator (and, or) adds a decision point.

Source: http://radon.readthedocs.org/en/latest/intro.html

Cyclomatic complexity is too high in method _instanceize. (6)
Open

    def _instanceize(self):
        """
        Convert the result cache to instances if possible and it has not already been
        done.  If a query extracts `.values(...)` then the result cache will not contain
        historical objects to be converted.
Severity: Minor
Found in simple_history/manager.py by radon

Cyclomatic Complexity

Cyclomatic Complexity corresponds to the number of decisions a block of code contains plus 1. This number (also called McCabe number) is equal to the number of linearly independent paths through the code. This number can be used as a guide when testing conditional logic in blocks.

Radon analyzes the AST tree of a Python program to compute Cyclomatic Complexity. Statements have the following effects on Cyclomatic Complexity:

Construct Effect on CC Reasoning
if +1 An if statement is a single decision.
elif +1 The elif statement adds another decision.
else +0 The else statement does not cause a new decision. The decision is at the if.
for +1 There is a decision at the start of the loop.
while +1 There is a decision at the while statement.
except +1 Each except branch adds a new conditional path of execution.
finally +0 The finally block is unconditionally executed.
with +1 The with statement roughly corresponds to a try/except block (see PEP 343 for details).
assert +1 The assert statement internally roughly equals a conditional statement.
Comprehension +1 A list/set/dict comprehension of generator expression is equivalent to a for loop.
Boolean Operator +1 Every boolean operator (and, or) adds a decision point.

Source: http://radon.readthedocs.org/en/latest/intro.html

Function _process has a Cognitive Complexity of 16 (exceeds 7 allowed). Consider refactoring.
Open

    def _process(self, to_process, date_back=None, dry_run=True):
        if date_back:
            stop_date = timezone.now() - timezone.timedelta(minutes=date_back)
        else:
            stop_date = None
Severity: Minor
Found in simple_history/management/commands/clean_duplicate_history.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 diff_against has a Cognitive Complexity of 14 (exceeds 7 allowed). Consider refactoring.
Open

    def diff_against(self, old_history, excluded_fields=None, included_fields=None):
        if not isinstance(old_history, type(self)):
            raise TypeError(
                ("unsupported type(s) for diffing: " "'{}' and '{}'").format(
                    type(self), type(old_history)
Severity: Minor
Found in simple_history/models.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 history_view has a Cognitive Complexity of 13 (exceeds 7 allowed). Consider refactoring.
Open

    def history_view(self, request, object_id, extra_context=None):
        """The 'history' admin view for this model."""
        request.current_app = self.admin_site.name
        model = self.model
        opts = model._meta
Severity: Minor
Found in simple_history/admin.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 __init__ has 30 lines of code (exceeds 25 allowed). Consider refactoring.
Open

    def __init__(
        self,
        verbose_name=None,
        verbose_name_plural=None,
        bases=(models.Model,),
Severity: Minor
Found in simple_history/models.py - About 1 hr to fix

    Function bulk_create_with_history has a Cognitive Complexity of 10 (exceeds 7 allowed). Consider refactoring.
    Open

    def bulk_create_with_history(
        objs,
        model,
        batch_size=None,
        ignore_conflicts=False,
    Severity: Minor
    Found in simple_history/utils.py - About 45 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 bulk_update_with_history has 9 arguments (exceeds 7 allowed). Consider refactoring.
    Open

    def bulk_update_with_history(
    Severity: Major
    Found in simple_history/utils.py - About 45 mins to fix

      Function update_change_reason has a Cognitive Complexity of 10 (exceeds 7 allowed). Consider refactoring.
      Open

      def update_change_reason(instance, reason):
          attrs = {}
          model = type(instance)
          manager = instance if instance.pk is not None else model
          history = get_history_manager_for_model(manager)
      Severity: Minor
      Found in simple_history/utils.py - About 45 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 bulk_create_with_history has 8 arguments (exceeds 7 allowed). Consider refactoring.
      Open

      def bulk_create_with_history(
      Severity: Major
      Found in simple_history/utils.py - About 35 mins to fix

        Function transform_field has a Cognitive Complexity of 9 (exceeds 7 allowed). Consider refactoring.
        Open

        def transform_field(field):
            """Customize field appropriately for use in historical model"""
            field.name = field.attname
            if isinstance(field, models.BigAutoField):
                field.__class__ = models.BigIntegerField
        Severity: Minor
        Found in simple_history/models.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 history_form_view has a Cognitive Complexity of 9 (exceeds 7 allowed). Consider refactoring.
        Open

            def history_form_view(self, request, object_id, version_id, extra_context=None):
                request.current_app = self.admin_site.name
                original_opts = self.model._meta
                model = getattr(
                    self.model, self.model._meta.simple_history_manager_attribute
        Severity: Minor
        Found in simple_history/admin.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 finalize has a Cognitive Complexity of 9 (exceeds 7 allowed). Consider refactoring.
        Open

            def finalize(self, sender, **kwargs):
                inherited = False
                if self.cls is not sender:  # set in concrete
                    inherited = self.inherit and issubclass(sender, self.cls)
                    if not inherited:
        Severity: Minor
        Found in simple_history/models.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 handle has a Cognitive Complexity of 8 (exceeds 7 allowed). Consider refactoring.
        Open

            def handle(self, *args, **options):
                self.verbosity = options["verbosity"]
        
                to_process = set()
                model_strings = options.get("models", []) or args
        Severity: Minor
        Found in simple_history/management/commands/populate_history.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 latest_of_each has a Cognitive Complexity of 8 (exceeds 7 allowed). Consider refactoring.
        Open

            def latest_of_each(self):
                """
                Ensures results in the queryset are the latest historical record for each
                primary key.  Deletions are not removed.
        
        
        Severity: Minor
        Found in simple_history/manager.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 _bulk_history_create has a Cognitive Complexity of 8 (exceeds 7 allowed). Consider refactoring.
        Open

            def _bulk_history_create(self, model, batch_size):
                """Save a copy of all instances to the historical model.
        
                :param model: Model you want to bulk create
                :param batch_size: number of models to create at once.
        Severity: Minor
        Found in simple_history/management/commands/populate_history.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

        Either merge this branch with the identical one on line "238" or change one of the implementations.
        Open

                    return True
        Severity: Major
        Found in simple_history/admin.py by sonar-python

        Having two branches in the same if structure with the same implementation is at best duplicate code, and at worst a coding error. If the same logic is truly needed for both instances, then they should be combined.

        Noncompliant Code Example

        if 0 <= a < 10:
            do_the_thing()
        elif 10 <= a < 20:
            do_the_other_thing()
        elif 20 <= a < 50:
            do_the_thing()  # Noncompliant; duplicates first condition
        else:
            do_the_rest()
        
        b = 4 if a > 12 else 4
        

        Compliant Solution

        if (0 <= a < 10) or (20 <= a < 50):
            do_the_thing()
        elif 10 <= a < 20:
            do_the_other_thing()
        else:
            do_the_rest()
        
        b = 4
        

        or

        if 0 <= a < 10:
            do_the_thing()
        elif 10 <= a < 20:
            do_the_other_thing()
        elif 20 <= a < 50:
            do_the_third_thing()
        else:
            do_the_rest()
        
        b = 8 if a > 12 else 4
        

        Method "bulk_history_create" has 8 parameters, which is greater than the 7 authorized.
        Open

                self,
                objs,
                batch_size=None,
                update=False,
                default_user=None,
        Severity: Major
        Found in simple_history/manager.py by sonar-python

        A long parameter list can indicate that a new structure should be created to wrap the numerous parameters or that the function is doing too many things.

        Noncompliant Code Example

        With a maximum number of 4 parameters:

        def do_something(param1, param2, param3, param4, param5):
            ...
        

        Compliant Solution

        def do_something(param1, param2, param3, param4):
            ...
        
        Severity
        Category
        Status
        Source
        Language