Showing 160 of 222 total issues
File download_column_historical_lookups.py
has 3065 lines of code (exceeds 250 allowed). Consider refactoring. Open
"""
Sets up mappings from column names used in downloads to the query paths used to get the data from django.
Not in use while we pull CSV data from the non-historical tables. Until we switch to pulling CSV downloads from the
historical tables TransactionFPDS and TransactionFABS, import download_column_lookups.py instead.
Function matview_search_filter
has a Cognitive Complexity of 176 (exceeds 15 allowed). Consider refactoring. Open
def matview_search_filter(filters, model, for_downloads=False):
queryset = model.objects.all()
recipient_scope_q = Q(recipient_location_country_code="USA") | Q(recipient_location_country_name="UNITED STATES")
pop_scope_q = Q(pop_country_code="USA") | Q(pop_country_name="UNITED STATES")
- Read upRead up
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 load_transactions_in_delta.py
has 1286 lines of code (exceeds 250 allowed). Consider refactoring. Open
import copy
import logging
import re
from contextlib import contextmanager
File conftest_spark.py
has 1236 lines of code (exceeds 250 allowed). Consider refactoring. Open
import json
import logging
import uuid
from datetime import datetime
from pathlib import Path
Function subaward_filter
has a Cognitive Complexity of 144 (exceeds 15 allowed). Consider refactoring. Open
def subaward_filter(filters, for_downloads=False):
queryset = SubawardSearch.objects.all()
recipient_scope_q = Q(sub_legal_entity_country_code="USA") | Q(sub_legal_entity_country_name="UNITED STATES")
pop_scope_q = Q(sub_place_of_perform_country_co="USA") | Q(sub_place_of_perform_country_name="UNITED STATES")
- Read upRead up
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 download_annotation_functions.py
has 863 lines of code (exceeds 250 allowed). Consider refactoring. Open
import datetime
from typing import List, Optional
from django.db.models.functions import Cast, Coalesce
from django.db.models import (
Function post
has a Cognitive Complexity of 110 (exceeds 15 allowed). Consider refactoring. Open
def post(self, request, pk, format=None):
# create response
response = {"results": {}}
# get federal account id from url
- Read upRead up
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 sqs_work_dispatcher.py
has 825 lines of code (exceeds 250 allowed). Consider refactoring. Open
import logging
import inspect
import json
import os
Cyclomatic complexity is too high in function get_business_categories_fpds. (104) Open
def get_business_categories_fpds(row):
# This function is supposed to be invoked as a Spark UDF with a named_struct containing the necessary
# fields passed to it.
def row_get(row, col_name):
- Read upRead up
- Exclude checks
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. |
File query_with_filters.py
has 784 lines of code (exceeds 250 allowed). Consider refactoring. Open
import copy
import itertools
import logging
import re
from datetime import datetime
Function validate_post_request
has a Cognitive Complexity of 96 (exceeds 15 allowed). Consider refactoring. Open
def validate_post_request(self, request):
if "filters" in request:
for filt in request["filters"]:
if "combine_method" in filt:
try:
- Read upRead up
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 request_validations.py
has 723 lines of code (exceeds 250 allowed). Consider refactoring. Open
import json
from copy import deepcopy
from datetime import datetime, MINYEAR, MAXYEAR
from django.conf import settings
from typing import Optional
Cyclomatic complexity is too high in function matview_search_filter. (84) Open
def matview_search_filter(filters, model, for_downloads=False):
queryset = model.objects.all()
recipient_scope_q = Q(recipient_location_country_code="USA") | Q(recipient_location_country_name="UNITED STATES")
pop_scope_q = Q(pop_country_code="USA") | Q(pop_country_name="UNITED STATES")
- Read upRead up
- Exclude checks
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. |
File swap_in_new_table.py
has 672 lines of code (exceeds 250 allowed). Consider refactoring. Open
import json
import logging
import re
from datetime import datetime
File download_generation.py
has 668 lines of code (exceeds 250 allowed). Consider refactoring. Open
import json
import logging
import multiprocessing
import os
from copy import deepcopy
Function get_business_categories_fpds
has a Cognitive Complexity of 83 (exceeds 15 allowed). Consider refactoring. Open
def get_business_categories_fpds(row):
# This function is supposed to be invoked as a Spark UDF with a named_struct containing the necessary
# fields passed to it.
def row_get(row, col_name):
- Read upRead up
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 spark.py
has 639 lines of code (exceeds 250 allowed). Consider refactoring. Open
"""
Spark utility functions that could be used as stages or steps of an ETL job (aka "data pipeline")
NOTE: This is distinguished from the usaspending_api.common.helpers.spark_helpers module, which holds mostly boilerplate
functions for setup and configuration of the spark environment
File award_search.py
has 638 lines of code (exceeds 250 allowed). Consider refactoring. Open
from usaspending_api.awards.v2.lookups.lookups import award_type_mapping
AWARD_SEARCH_COLUMNS = {
"treasury_account_identifiers": {"delta": "ARRAY<INTEGER>", "postgres": "INTEGER[]", "gold": False},
"award_id": {"delta": "LONG NOT NULL", "postgres": "BIGINT NOT NULL", "gold": False},
Function build_elasticsearch_result
has a Cognitive Complexity of 81 (exceeds 15 allowed). Consider refactoring. Open
def build_elasticsearch_result(self, response: dict) -> Dict[str, dict]:
results = {}
geo_info_buckets = response.get("group_by_agg_key", {}).get("buckets", [])
for bucket in geo_info_buckets:
- Read upRead up
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 federal_accounts_v2.py
has 622 lines of code (exceeds 250 allowed). Consider refactoring. Open
import ast
from collections import OrderedDict
from typing import List
from django.db.models import DecimalField, Exists, F, Func, OuterRef, Q, Subquery, Sum