bitcoin/bitcoin

View on GitHub
contrib/seeds/asmap.py

Summary

Maintainability
F
5 days
Test Coverage

File asmap.py has 661 lines of code (exceeds 250 allowed). Consider refactoring.
Open

# Copyright (c) 2022 Pieter Wuille
# Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.

"""
Severity: Major
Found in contrib/seeds/asmap.py - About 1 day to fix

    Function test_patching has a Cognitive Complexity of 43 (exceeds 5 allowed). Consider refactoring.
    Open

        def test_patching(self) -> None:
            """Test behavior of update, lookup, extends, and diff."""
            #pylint: disable=too-many-locals,too-many-nested-blocks
            # Iterate over the number of leaves the random test ASMap objects have.
            for leaves in range(1, 20):
    Severity: Minor
    Found in contrib/seeds/asmap.py - About 6 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 _to_entries_minimal has a Cognitive Complexity of 34 (exceeds 5 allowed). Consider refactoring.
    Open

        def _to_entries_minimal(self, fill: bool = False) -> list[ASNEntry]:
            """Convert a trie to a minimal list of ASNEntry objects, exploiting overlap."""
            prefix : list[bool] = []
    
            def recurse(node: list) -> (tuple[dict[Optional[int], list[ASNEntry]], bool]):
    Severity: Minor
    Found in contrib/seeds/asmap.py - About 5 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 _to_binnode has a Cognitive Complexity of 30 (exceeds 5 allowed). Consider refactoring.
    Open

        def _to_binnode(self, fill: bool = False) -> _BinNode:
            """Convert a trie to a _BinNode object."""
            def recurse(node: list) -> tuple[dict[Optional[int], _BinNode], bool]:
                if len(node) == 1 and node[0] == 0:
                    return {(None if fill else 0): _BinNode.make_end()}, True
    Severity: Minor
    Found in contrib/seeds/asmap.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

    Function diff has a Cognitive Complexity of 18 (exceeds 5 allowed). Consider refactoring.
    Open

        def diff(self, other: "ASMap") -> list[ASNDiff]:
            """Compute the diff from self to other."""
            prefix: list[bool] = []
            ret: list[ASNDiff] = []
    
    
    Severity: Minor
    Found in contrib/seeds/asmap.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 from_binary has a Cognitive Complexity of 18 (exceeds 5 allowed). Consider refactoring.
    Open

        def from_binary(bindata: bytes) -> Optional["ASMap"]:
            """Decode an ASMap object from the provided binary encoding."""
    
            bits: list[int] = []
            for byte in bindata:
    Severity: Minor
    Found in contrib/seeds/asmap.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 _from_binnode has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring.
    Open

        def _from_binnode(binnode: _BinNode) -> "ASMap":
            """Construct an ASMap object from a _BinNode. Internal use only."""
            def recurse(node: _BinNode, default: int) -> list:
                if node.ins == _Instruction.RETURN:
                    return [node.arg1]
    Severity: Minor
    Found in contrib/seeds/asmap.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 _to_entries_flat has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
    Open

        def _to_entries_flat(self, fill: bool = False) -> list[ASNEntry]:
            """Convert an ASMap object to a list of non-overlapping (prefix, asn) objects."""
            prefix : list[bool] = []
    
            def recurse(node: list) -> list[ASNEntry]:
    Severity: Minor
    Found in contrib/seeds/asmap.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 test_asmap_roundtrips has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
    Open

        def test_asmap_roundtrips(self) -> None:
            """Test case that verifies random ASMap objects roundtrip to/from entries/binary."""
            # Iterate over the number of leaves the random test ASMap objects have.
            for leaves in range(1, 20):
                # Iterate over the number of bits in the AS numbers used.
    Severity: Minor
    Found in contrib/seeds/asmap.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_trie has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
    Open

        def _set_trie(self, trie) -> None:
            """Set trie directly. Internal use only."""
            def recurse(node: list) -> None:
                if len(node) < 2:
                    return
    Severity: Minor
    Found in contrib/seeds/asmap.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 extends has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
    Open

        def extends(self, req: "ASMap") -> bool:
            """Determine whether this matches req for all subranges where req is assigned."""
            def recurse(actual: list, require: list) -> bool:
                if len(require) == 1 and require[0] == 0:
                    return True
    Severity: Minor
    Found in contrib/seeds/asmap.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 to_binary has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
    Open

        def to_binary(self, fill: bool = False) -> bytes:
            """
            Convert this ASMap object to binary.
    
            Argument:
    Severity: Minor
    Found in contrib/seeds/asmap.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 update has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
    Open

        def update(self, prefix: list[bool], asn: int) -> None:
            """Update this ASMap object to map prefix to the specified asn."""
            assert asn == 0 or _CODER_ASN.can_encode(asn)
    
            def recurse(node: list, offset: int) -> None:
    Severity: Minor
    Found in contrib/seeds/asmap.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

    Avoid deeply nested control flow statements.
    Open

                            for path, old_asn, new_asn in diff:
                                # Verify asmap and patched actually differ there.
                                self.assertTrue(old_asn != new_asn)
                                self.assertEqual(asmap.lookup(path), old_asn)
                                self.assertEqual(patched.lookup(path), new_asn)
    Severity: Major
    Found in contrib/seeds/asmap.py - About 45 mins to fix

      Function make_branch has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
      Open

          def make_branch(node0: "_BinNode", node1: "_BinNode") -> "_BinNode":
              """
              Construct a _BinNode corresponding to running either the node0 or node1 subprogram,
              based on the next input bit. It exploits shortcuts that are possible in the encoding,
              and uses either a JUMP, MATCH, or END instruction.
      Severity: Minor
      Found in contrib/seeds/asmap.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 from_random has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
      Open

          def from_random(num_leaves: int = 10, max_asn: int = 6,
                          unassigned_prob: float = 0.5) -> "ASMap":
              """
              Construct a random ASMap object, with specified:
               - Number of leaves in its trie (at least 1)
      Severity: Minor
      Found in contrib/seeds/asmap.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 encode has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
      Open

          def encode(self, val: int, ret: list[int]) -> None:
              """Append encoding of val onto integer list ret."""
      
              assert self._minval <= val <= self._maxval
              val -= self._minval
      Severity: Minor
      Found in contrib/seeds/asmap.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 _BinNode(_Instruction.JUMP, node0, node1)
      Severity: Major
      Found in contrib/seeds/asmap.py - About 30 mins to fix

        Avoid too many return statements within this function.
        Open

                    return recurse(actual, require[0]) and recurse(actual, require[1])
        Severity: Major
        Found in contrib/seeds/asmap.py - About 30 mins to fix

          Avoid too many return statements within this function.
          Open

                      return _BinNode(_Instruction.MATCH, 2, node0)
          Severity: Major
          Found in contrib/seeds/asmap.py - About 30 mins to fix

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

                def decode(self, stream, bitpos) -> tuple[int,int]:
                    """Decode a number starting at bitpos in stream, returning value and new bitpos."""
                    val = self._minval
                    bits = 0
                    for k, bits in enumerate(self._clsbits):
            Severity: Minor
            Found in contrib/seeds/asmap.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 __init__ has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
            Open

                def __init__(self, ins: _Instruction, arg1=None, arg2=None):
                    """
                    Construct a new asmap node. Possibilities are:
                    - _BinNode(_Instruction.RETURN, asn)
                    - _BinNode(_Instruction.JUMP, node_0, node_1)
            Severity: Minor
            Found in contrib/seeds/asmap.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