How to fix R1710 Either all return statements in a function should return an expression, or none of them should.?

Viewed 3028

I'm using pylint on my code and getting the error

R1710: Either all return statements in a function should return an expression, or none of them should. (inconsistent-return-statements)

There are only two possible return statements and they both return expression if I am not mistaken

@api_view(["GET", "POST"])
def user_info(request):
    if request.method == 'GET':
        username = request.GET.get("username")
        password = request.GET.get("password")

        return JsonResponse(error_handle(serialize(username, password)))

    if request.method == 'POST':
        username = request.data["username"]
        password = request.data["password"]

        return JsonResponse(error_handle(serialize(username, password)))
def error_handle(serializer):
   error = serializer["error"].value
   if error > 0:
       return {"success": "false", "internal_code": error}
   return {"success": "true",
           "account_token": serializer.data["account_token"],
           "user_id": serializer.data["id"],
           "account_name": serializer.data["account_name"],
           "account_permission": serializer.data["account_permission"],
           "pin": serializer.data["pin"]
           }


def serialize(user, password):
   data = Account.objects.get(username=user, password=password)
   return AccountSerializer(data)
1 Answers

What happens in user_info if request.method is neither GET nor POST?

@api_view(["GET", "POST"])
def user_info(request):
    if request.method == 'GET':
        # ...
        return JsonResponse(error_handle(serialize(username, password)))

    if request.method == 'POST':
        # ..
        return JsonResponse(error_handle(serialize(username, password)))

    # ???
    # Something should be returned here!

In principle an exception "405 - Method Not Allowed" could be raised. But because of the decorator that already restricts the possible methods for the request, it is unreachable code. Pylint probably can not know that it is unreachable code.

Related