File .deepsource.toml
not found in the default branch of the repository root. Please refer to the docs.
else
/ elif
used after return
PYL-R1705 604 )
605 def get(self, request, format=None):
606 result, success = fast_api_key_validation_and_cache(request)
607 if not success: 608 return result
609 else:
610 organization_pk = result
143 stream_events = {"events": events, "organization_id": organization_pk}
144 kafka_producer.produce(customer_id, stream_events)
145
146 if len(bad_events) == len(event_list):147 return Response(
148 {"success": "none", "failed_events": bad_events},
149 status=status.HTTP_400_BAD_REQUEST,
93@permission_classes([])
94def track_event(request):
95 result, success = fast_api_key_validation_and_cache(request)
96 if not success: 97 return result
98 else:
99 organization_pk = result
551 return response
552
553 def get_serializer_class(self):
554 if self.action == "retrieve": 555 return PlanDetailSerializer
556 elif self.action == "partial_update":
557 return PlanUpdateSerializer
655 )
656
657 def get_serializer_class(self):
658 if self.action == "partial_update": 659 return SubscriptionUpdateSerializer
660 elif self.action == "retrieve":
661 return SubscriptionDetailSerializer
The use of else
or elif
becomes redundant and can be dropped if the last statement under the leading if
/ elif
block is a return
statement.
In the case of an elif
after return
, it can be written as a separate if
block.
For else
blocks after return
, the statements can be shifted out of else
. Please refer to the examples below for reference.
Refactoring the code this way can improve code-readability and make it easier to maintain.
def classify_number(x):
if x % 2 == 0:
return 'Even'
else:
return 'Odd'
def what_is_this_number(x):
if x % 2 == 0 and x >= 0:
return 'Even'
elif x % 2 == 0 and x < 0:
return 'Even and Negative'
elif x % 2 != 0 and x < 0:
return 'Odd and Negative.'
else:
return 'Odd'
def classify_number(x):
if x % 2 == 0:
return 'Even'
return 'Odd'
def what_is_this_number(x):
if x % 2 == 0 and x >= 0:
return 'Even'
if x % 2 == 0 and x < 0:
return 'Even and Negative'
if x % 2 != 0 and x < 0:
return 'Odd and Negative'
return 'Odd'