GraphQLSchemaGenerator.filter_queryset
has a cyclomatic complexity of 19 with "high" risk 340 name = get_table_name(MappedBase.metadata, mapper.selectable)
341 return self.relationships_by_clean_name.get(name, {})
342
343 def filter_queryset(self, request, MappedBase, queryset, mapper, filters, parent_relations=None, exclude=False): 344 parent_relations = parent_relations or []
345
346 for filters_item in filters:
GraphQLSchemaGenerator.resolve_model_list
has a cyclomatic complexity of 17 with "high" risk 856
857 i += 1
858
859 def resolve_model_list(self, MappedBase, Model, mapper,info, filters=None, lookups=None, sort=None, pagination=None, search=None): 860 try:
861 filters = filters or []
862 lookups = lookups or []
GraphQLSchemaGenerator.get_relationships
has a cyclomatic complexity of 18 with "high" risk 206
207 return queryset
208
209 def get_relationships(self, request, MappedBase, draft): 210 result = {}
211 relationships_overrides = {}
212
SqlSerializer.execute
has a cyclomatic complexity of 34 with "very-high" risk257
258 return queryset
259
260 def execute(self, data):261 request = self.context.get('request')
262 session = request.session
263
map_column
has a cyclomatic complexity of 23 with "high" risk 77 }
78
79
80def map_column(metadata, column, editable, primary_key_auto): 81 params = {}
82 data_source_field = None
83 data_source_name = None
A function with high cyclomatic complexity can be hard to understand and maintain. Cyclomatic complexity is a software metric that measures the number of independent paths through a function. A higher cyclomatic complexity indicates that the function has more decision points and is more complex.
Functions with high cyclomatic complexity are more likely to have bugs and be harder to test. They may lead to reduced code maintainability and increased development time.
To reduce the cyclomatic complexity of a function, you can:
def number_to_name():
number = input()
if not number.isdigit():
print("Enter a valid number")
return
number = int(number)
if number >= 10:
print("Number is too big")
return
if number == 1:
print("one")
elif number == 2:
print("two")
elif number == 3:
print("three")
elif number == 4:
print("four")
elif number == 5:
print("five")
elif number == 6:
print("six")
elif number == 7:
print("seven")
elif number == 8:
print("eight")
elif number == 9:
print("nine")
def number_to_name():
number = input()
if not number.isdigit():
print("Enter a valid number")
return
number = int(number)
if number >= 10:
print("Number is too big")
return
names = {
1: "one",
2: "two",
3: "three",
4: "four",
5: "five",
6: "six",
7: "seven",
8: "eight",
9: "nine",
}
print(names[number])
Cyclomatic complexity threshold can be configured using the
cyclomatic_complexity_threshold
meta field in the
.deepsource.toml
config file.
Configuring this is optional. If you don't provide a value, the Analyzer will
raise issues for functions with complexity higher than the default threshold,
which is medium
for the Python Analyzer.
Here's the mapping of the risk category to the cyclomatic complexity score to help you configure this better:
Risk category | Cyclomatic complexity range | Recommended action |
---|---|---|
low | 1-5 | No action needed. |
medium | 6-15 | Review and monitor. |
high | 16-25 | Review and refactor. Recommended to add comments if the function is absolutely needed to be kept as it is. |
very-high | 26-50 | Refactor to reduce the complexity. |
critical | >50 | Must refactor this. This can make the code untestable and very difficult to understand. |