mike0sv/pyjackson

View on GitHub
src/pyjackson/deserialization.py

Summary

Maintainability
B
5 hrs
Test Coverage

Cyclomatic complexity is too high in function deserialize. (21)
Open

def deserialize(obj, as_class: SerializerType):
    """Convert python dict into given class

    :param obj: dict (or list or any primitive) to deserialize
    :param as_class: type or serializer
Severity: Minor
Found in src/pyjackson/deserialization.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 deserialize has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring.
Open

def deserialize(obj, as_class: SerializerType):
    """Convert python dict into given class

    :param obj: dict (or list or any primitive) to deserialize
    :param as_class: type or serializer
Severity: Minor
Found in src/pyjackson/deserialization.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

Cyclomatic complexity is too high in function _construct_object. (6)
Open

def _construct_object(obj, as_class: Type):
    if isinstance(as_class, Hashable) and as_class in SERIALIZER_MAPPING:
        as_class = SERIALIZER_MAPPING[as_class]

    if issubclass(as_class, StaticSerializer):
Severity: Minor
Found in src/pyjackson/deserialization.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 _construct_from_dict has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
Open

def _construct_from_dict(obj, as_class):
    kwargs = {}
    for f in get_class_fields(as_class):
        name = f.name
        field_type = _get_field_type(f, obj)
Severity: Minor
Found in src/pyjackson/deserialization.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 _construct_from_list has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
Open

def _construct_from_list(obj, as_class):
    args = []
    if type_field_position_is(as_class, Position.INSIDE):
        obj = obj[1:]
    for i, f in enumerate(get_class_fields(as_class)):
Severity: Minor
Found in src/pyjackson/deserialization.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

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

def _construct_object(obj, as_class: Type):
    if isinstance(as_class, Hashable) and as_class in SERIALIZER_MAPPING:
        as_class = SERIALIZER_MAPPING[as_class]

    if issubclass(as_class, StaticSerializer):
Severity: Minor
Found in src/pyjackson/deserialization.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

Avoid too many return statements within this function.
Open

            return seq_type([deserialize(o, seq_int_type) for o in obj])
Severity: Major
Found in src/pyjackson/deserialization.py - About 30 mins to fix

    Refactor this function to reduce its Cognitive Complexity from 32 to the 15 allowed.
    Open

    def deserialize(obj, as_class: SerializerType):
    Severity: Critical
    Found in src/pyjackson/deserialization.py by sonar-python

    Cognitive Complexity is a measure of how hard the control flow of a function is to understand. Functions with high Cognitive Complexity will be difficult to maintain.

    See

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

            return obj

    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
    

    Line too long (100 > 79 characters)
    Open

                if type_field_position_is(as_class, Position.INSIDE) and (is_hierarchy_root(as_class) or
    Severity: Minor
    Found in src/pyjackson/deserialization.py by pep8

    Limit all lines to a maximum of 79 characters.

    There are still many devices around that are limited to 80 character
    lines; plus, limiting windows to 80 characters makes it possible to
    have several windows side-by-side.  The default wrapping on such
    devices looks ugly.  Therefore, please limit all lines to a maximum
    of 79 characters. For flowing long blocks of text (docstrings or
    comments), limiting the length to 72 characters is recommended.
    
    Reports error E501.

    Line too long (120 > 79 characters)
    Open

                                 is_hierarchy_root, is_mapping, is_tuple, is_union, resolve_subtype, type_field_position_is,
    Severity: Minor
    Found in src/pyjackson/deserialization.py by pep8

    Limit all lines to a maximum of 79 characters.

    There are still many devices around that are limited to 80 character
    lines; plus, limiting windows to 80 characters makes it possible to
    have several windows side-by-side.  The default wrapping on such
    devices looks ugly.  Therefore, please limit all lines to a maximum
    of 79 characters. For flowing long blocks of text (docstrings or
    comments), limiting the length to 72 characters is recommended.
    
    Reports error E501.

    Line too long (114 > 79 characters)
    Open

                    raise DeserializationError("Cannot construct type {} from argument list {}".format(as_class, obj))
    Severity: Minor
    Found in src/pyjackson/deserialization.py by pep8

    Limit all lines to a maximum of 79 characters.

    There are still many devices around that are limited to 80 character
    lines; plus, limiting windows to 80 characters makes it possible to
    have several windows side-by-side.  The default wrapping on such
    devices looks ugly.  Therefore, please limit all lines to a maximum
    of 79 characters. For flowing long blocks of text (docstrings or
    comments), limiting the length to 72 characters is recommended.
    
    Reports error E501.

    Line too long (108 > 79 characters)
    Open

    from pyjackson.core import BUILTIN_TYPES, FIELD_MAPPING_NAME_FIELD, SERIALIZABLE_DICT_TYPES, Field, Position
    Severity: Minor
    Found in src/pyjackson/deserialization.py by pep8

    Limit all lines to a maximum of 79 characters.

    There are still many devices around that are limited to 80 character
    lines; plus, limiting windows to 80 characters makes it possible to
    have several windows side-by-side.  The default wrapping on such
    devices looks ugly.  Therefore, please limit all lines to a maximum
    of 79 characters. For flowing long blocks of text (docstrings or
    comments), limiting the length to 72 characters is recommended.
    
    Reports error E501.

    Line too long (116 > 79 characters)
    Open

    from pyjackson.utils import (get_class_fields, get_collection_internal_type, get_collection_type, get_mapping_types,
    Severity: Minor
    Found in src/pyjackson/deserialization.py by pep8

    Limit all lines to a maximum of 79 characters.

    There are still many devices around that are limited to 80 character
    lines; plus, limiting windows to 80 characters makes it possible to
    have several windows side-by-side.  The default wrapping on such
    devices looks ugly.  Therefore, please limit all lines to a maximum
    of 79 characters. For flowing long blocks of text (docstrings or
    comments), limiting the length to 72 characters is recommended.
    
    Reports error E501.

    Line too long (98 > 79 characters)
    Open

                        f'mapping key type must be one of {SERIALIZABLE_DICT_TYPES}, not {key_type}. '
    Severity: Minor
    Found in src/pyjackson/deserialization.py by pep8

    Limit all lines to a maximum of 79 characters.

    There are still many devices around that are limited to 80 character
    lines; plus, limiting windows to 80 characters makes it possible to
    have several windows side-by-side.  The default wrapping on such
    devices looks ugly.  Therefore, please limit all lines to a maximum
    of 79 characters. For flowing long blocks of text (docstrings or
    comments), limiting the length to 72 characters is recommended.
    
    Reports error E501.

    Line too long (84 > 79 characters)
    Open

                return {key_type(k): deserialize(v, value_type) for k, v in obj.items()}
    Severity: Minor
    Found in src/pyjackson/deserialization.py by pep8

    Limit all lines to a maximum of 79 characters.

    There are still many devices around that are limited to 80 character
    lines; plus, limiting windows to 80 characters makes it possible to
    have several windows side-by-side.  The default wrapping on such
    devices looks ugly.  Therefore, please limit all lines to a maximum
    of 79 characters. For flowing long blocks of text (docstrings or
    comments), limiting the length to 72 characters is recommended.
    
    Reports error E501.

    Line too long (95 > 79 characters)
    Open

    from pyjackson.generics import SERIALIZER_MAPPING, Serializer, SerializerType, StaticSerializer
    Severity: Minor
    Found in src/pyjackson/deserialization.py by pep8

    Limit all lines to a maximum of 79 characters.

    There are still many devices around that are limited to 80 character
    lines; plus, limiting windows to 80 characters makes it possible to
    have several windows side-by-side.  The default wrapping on such
    devices looks ugly.  Therefore, please limit all lines to a maximum
    of 79 characters. For flowing long blocks of text (docstrings or
    comments), limiting the length to 72 characters is recommended.
    
    Reports error E501.

    Line too long (91 > 79 characters)
    Open

                    raise ValueError("Type {} has required argument {}".format(as_class, name))
    Severity: Minor
    Found in src/pyjackson/deserialization.py by pep8

    Limit all lines to a maximum of 79 characters.

    There are still many devices around that are limited to 80 character
    lines; plus, limiting windows to 80 characters makes it possible to
    have several windows side-by-side.  The default wrapping on such
    devices looks ugly.  Therefore, please limit all lines to a maximum
    of 79 characters. For flowing long blocks of text (docstrings or
    comments), limiting the length to 72 characters is recommended.
    
    Reports error E501.

    Line too long (111 > 79 characters)
    Open

                                 get_tuple_internal_types, has_subtype_alias, is_aslist, is_collection, is_generic,
    Severity: Minor
    Found in src/pyjackson/deserialization.py by pep8

    Limit all lines to a maximum of 79 characters.

    There are still many devices around that are limited to 80 character
    lines; plus, limiting windows to 80 characters makes it possible to
    have several windows side-by-side.  The default wrapping on such
    devices looks ugly.  Therefore, please limit all lines to a maximum
    of 79 characters. For flowing long blocks of text (docstrings or
    comments), limiting the length to 72 characters is recommended.
    
    Reports error E501.

    Line too long (104 > 79 characters)
    Open

                                                                          has_subtype_alias(as_class, obj)):
    Severity: Minor
    Found in src/pyjackson/deserialization.py by pep8

    Limit all lines to a maximum of 79 characters.

    There are still many devices around that are limited to 80 character
    lines; plus, limiting windows to 80 characters makes it possible to
    have several windows side-by-side.  The default wrapping on such
    devices looks ugly.  Therefore, please limit all lines to a maximum
    of 79 characters. For flowing long blocks of text (docstrings or
    comments), limiting the length to 72 characters is recommended.
    
    Reports error E501.

    Line too long (84 > 79 characters)
    Open

                    raise ValueError("Too few arguments for type  {} ".format(as_class))
    Severity: Minor
    Found in src/pyjackson/deserialization.py by pep8

    Limit all lines to a maximum of 79 characters.

    There are still many devices around that are limited to 80 character
    lines; plus, limiting windows to 80 characters makes it possible to
    have several windows side-by-side.  The default wrapping on such
    devices looks ugly.  Therefore, please limit all lines to a maximum
    of 79 characters. For flowing long blocks of text (docstrings or
    comments), limiting the length to 72 characters is recommended.
    
    Reports error E501.

    There are no issues that match your filters.

    Category
    Status